Skip to content

Commit 5cb2dff

Browse files
committed
added the file/directory existence functions from Cppcheck
1 parent 30926aa commit 5cb2dff

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

simplecpp.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stack>
3131
#include <stdexcept>
3232
#include <string>
33+
#include <sys/stat.h>
3334
#if __cplusplus >= 201103L
3435
#ifdef SIMPLECPP_WINDOWS
3536
#include <mutex>
@@ -39,6 +40,12 @@
3940
#include <utility>
4041
#include <vector>
4142

43+
#ifdef _WIN32
44+
using mode_t = unsigned short;
45+
#else
46+
#include <sys/types.h>
47+
#endif
48+
4249
#ifdef SIMPLECPP_WINDOWS
4350
#include <windows.h>
4451
#undef ERROR
@@ -3779,6 +3786,24 @@ std::string simplecpp::getCppStdString(const std::string &std)
37793786
return "";
37803787
}
37813788

3789+
static mode_t file_type(const std::string &path)
3790+
{
3791+
struct stat file_stat;
3792+
if (stat(path.c_str(), &file_stat) == -1)
3793+
return 0;
3794+
return file_stat.st_mode & S_IFMT;
3795+
}
3796+
3797+
bool simplecpp::isFile(const std::string &path)
3798+
{
3799+
return file_type(path) == S_IFREG;
3800+
}
3801+
3802+
bool simplecpp::isDirectory(const std::string &path)
3803+
{
3804+
return file_type(path) == S_IFDIR;
3805+
}
3806+
37823807
#if (__cplusplus < 201103L) && !defined(__APPLE__)
37833808
#undef nullptr
37843809
#endif

simplecpp.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,20 @@ namespace simplecpp {
368368

369369
/** Returns the __cplusplus value for a given standard */
370370
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
371+
372+
/**
373+
* @brief Checks if given path is a file
374+
* @param path Path to be checked
375+
* @return true if given path is a file
376+
*/
377+
SIMPLECPP_LIB bool isFile(const std::string &path);
378+
379+
/**
380+
* @brief Checks if a given path is a directory
381+
* @param path Path to be checked
382+
* @return true if given path is a directory
383+
*/
384+
SIMPLECPP_LIB bool isDirectory(const std::string &path);
371385
}
372386

373387
#if defined(_MSC_VER)

0 commit comments

Comments
 (0)