Skip to content

Commit 8e684a0

Browse files
committed
added the file/directory existence functions from Cppcheck
1 parent 1dfd8ca commit 8e684a0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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
@@ -3767,6 +3774,24 @@ std::string simplecpp::getCppStdString(const std::string &std)
37673774
return "";
37683775
}
37693776

3777+
static mode_t file_type(const std::string &path)
3778+
{
3779+
struct stat file_stat;
3780+
if (stat(path.c_str(), &file_stat) == -1)
3781+
return 0;
3782+
return file_stat.st_mode & S_IFMT;
3783+
}
3784+
3785+
bool simplecpp::isFile(const std::string &path)
3786+
{
3787+
return file_type(path) == S_IFREG;
3788+
}
3789+
3790+
bool simplecpp::isDirectory(const std::string &path)
3791+
{
3792+
return file_type(path) == S_IFDIR;
3793+
}
3794+
37703795
#if (__cplusplus < 201103L) && !defined(__APPLE__)
37713796
#undef nullptr
37723797
#endif

simplecpp.h

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

365365
/** Returns the __cplusplus value for a given standard */
366366
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
367+
368+
/**
369+
* @brief Checks if given path is a file
370+
* @param path Path to be checked
371+
* @return true if given path is a file
372+
*/
373+
SIMPLECPP_LIB bool isFile(const std::string &path);
374+
375+
/**
376+
* @brief Checks if a given path is a directory
377+
* @param path Path to be checked
378+
* @return true if given path is a directory
379+
*/
380+
SIMPLECPP_LIB bool isDirectory(const std::string &path);
367381
}
368382

369383
#if defined(_MSC_VER)

0 commit comments

Comments
 (0)