Skip to content

Commit

Permalink
added the file/directory existence functions from Cppcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jul 14, 2024
1 parent 2096a96 commit 9ed8f1a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <stack>
#include <stdexcept>
#include <string>
#include <sys/stat.h>
#if __cplusplus >= 201103L
#ifdef SIMPLECPP_WINDOWS
#include <mutex>
Expand All @@ -39,6 +40,12 @@
#include <utility>
#include <vector>

#ifdef _WIN32
using mode_t = unsigned short;
#else
#include <sys/types.h>
#endif

#ifdef SIMPLECPP_WINDOWS
#include <windows.h>
#undef ERROR
Expand Down Expand Up @@ -3838,6 +3845,24 @@ std::string simplecpp::getCppStdString(const std::string &std)
return "";
}

static mode_t file_type(const std::string &path)
{
struct stat file_stat;
if (stat(path.c_str(), &file_stat) == -1)
return 0;
return file_stat.st_mode & S_IFMT;
}

bool simplecpp::isFile(const std::string &path)
{
return file_type(path) == S_IFREG;
}

bool simplecpp::isDirectory(const std::string &path)
{
return file_type(path) == S_IFDIR;
}

#if (__cplusplus < 201103L) && !defined(__APPLE__)
#undef nullptr
#endif
14 changes: 14 additions & 0 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,20 @@ namespace simplecpp {

/** Returns the __cplusplus value for a given standard */
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);

/**
* @brief Checks if given path is a file
* @param path Path to be checked
* @return true if given path is a file
*/
SIMPLECPP_LIB bool isFile(const std::string &path);

/**
* @brief Checks if a given path is a directory
* @param path Path to be checked
* @return true if given path is a directory
*/
SIMPLECPP_LIB bool isDirectory(const std::string &path);
}

#if defined(_MSC_VER)
Expand Down

0 comments on commit 9ed8f1a

Please sign in to comment.