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 Feb 10, 2025
1 parent 3dea134 commit 2efc1cc
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 @@ -31,6 +31,7 @@
#include <stack>
#include <stdexcept>
#include <string>
#include <sys/stat.h>
#if __cplusplus >= 201103L
#ifdef SIMPLECPP_WINDOWS
#include <mutex>
Expand All @@ -40,6 +41,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 @@ -3880,6 +3887,24 @@ std::string simplecpp::getCppStdString(const std::string &std)
return getCppStdString(getCppStd(std));
}

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 @@ -389,6 +389,20 @@ namespace simplecpp {
/** Returns the __cplusplus value for a given standard */
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
SIMPLECPP_LIB std::string getCppStdString(cppstd_t 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 2efc1cc

Please sign in to comment.