Skip to content

Commit c591ef8

Browse files
committed
added the file/directory existence functions from Cppcheck
1 parent 27b4508 commit c591ef8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

simplecpp.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stack>
3232
#include <stdexcept>
3333
#include <string>
34+
#include <sys/stat.h>
3435
#if __cplusplus >= 201103L
3536
#ifdef SIMPLECPP_WINDOWS
3637
#include <mutex>
@@ -42,8 +43,10 @@
4243

4344
#ifdef _WIN32
4445
#include <direct.h>
46+
using mode_t = unsigned short;
4547
#else
4648
#include <unistd.h>
49+
#include <sys/types.h>
4750
#endif
4851

4952
#ifdef SIMPLECPP_WINDOWS
@@ -153,7 +156,7 @@ static unsigned long long stringToULL(const std::string &s)
153156
return ret;
154157
}
155158

156-
// TODO: added an undercore since this conflicts with a function of the same name in utils.h from Cppcheck source when building Cppcheck with MSBuild
159+
// TODO: added an undercore since this conflicts with a function of the same name in utils.h from Cppcheck source when building Cppcheck with MSBuild
157160
static bool startsWith_(const std::string &s, const std::string &p)
158161
{
159162
return (s.size() >= p.size()) && std::equal(p.begin(), p.end(), s.begin());
@@ -4103,6 +4106,24 @@ std::string simplecpp::getCppStdString(const std::string &std)
41034106
return getCppStdString(getCppStd(std));
41044107
}
41054108

4109+
static mode_t file_type(const std::string &path)
4110+
{
4111+
struct stat file_stat;
4112+
if (stat(path.c_str(), &file_stat) == -1)
4113+
return 0;
4114+
return file_stat.st_mode & S_IFMT;
4115+
}
4116+
4117+
bool simplecpp::isFile(const std::string &path)
4118+
{
4119+
return file_type(path) == S_IFREG;
4120+
}
4121+
4122+
bool simplecpp::isDirectory(const std::string &path)
4123+
{
4124+
return file_type(path) == S_IFDIR;
4125+
}
4126+
41064127
#if (__cplusplus < 201103L) && !defined(__APPLE__)
41074128
#undef nullptr
41084129
#endif

simplecpp.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ namespace simplecpp {
389389
/** Returns the __cplusplus value for a given standard */
390390
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
391391
SIMPLECPP_LIB std::string getCppStdString(cppstd_t std);
392+
393+
/**
394+
* @brief Checks if given path is a file
395+
* @param path Path to be checked
396+
* @return true if given path is a file
397+
*/
398+
SIMPLECPP_LIB bool isFile(const std::string &path);
399+
400+
/**
401+
* @brief Checks if a given path is a directory
402+
* @param path Path to be checked
403+
* @return true if given path is a directory
404+
*/
405+
SIMPLECPP_LIB bool isDirectory(const std::string &path);
392406
}
393407

394408
#if defined(_MSC_VER)

0 commit comments

Comments
 (0)