Skip to content

Commit c19bf09

Browse files
committed
added the file/directory existence functions from Cppcheck
1 parent 5aee295 commit c19bf09

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
@@ -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>
@@ -40,6 +41,12 @@
4041
#include <utility>
4142
#include <vector>
4243

44+
#ifdef _WIN32
45+
using mode_t = unsigned short;
46+
#else
47+
#include <sys/types.h>
48+
#endif
49+
4350
#ifdef SIMPLECPP_WINDOWS
4451
#include <windows.h>
4552
#undef ERROR
@@ -4001,6 +4008,24 @@ std::string simplecpp::getCppStdString(const std::string &std)
40014008
return getCppStdString(getCppStd(std));
40024009
}
40034010

4011+
static mode_t file_type(const std::string &path)
4012+
{
4013+
struct stat file_stat;
4014+
if (stat(path.c_str(), &file_stat) == -1)
4015+
return 0;
4016+
return file_stat.st_mode & S_IFMT;
4017+
}
4018+
4019+
bool simplecpp::isFile(const std::string &path)
4020+
{
4021+
return file_type(path) == S_IFREG;
4022+
}
4023+
4024+
bool simplecpp::isDirectory(const std::string &path)
4025+
{
4026+
return file_type(path) == S_IFDIR;
4027+
}
4028+
40044029
#if (__cplusplus < 201103L) && !defined(__APPLE__)
40054030
#undef nullptr
40064031
#endif

simplecpp.h

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

396410
#if defined(_MSC_VER)

0 commit comments

Comments
 (0)