Skip to content

Commit 54848e8

Browse files
committed
added the file/directory existence functions from Cppcheck
1 parent ac223ba commit 54848e8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

simplecpp.cpp

+25
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
@@ -4006,6 +4013,24 @@ std::string simplecpp::getCppStdString(const std::string &std)
40064013
return getCppStdString(getCppStd(std));
40074014
}
40084015

4016+
static mode_t file_type(const std::string &path)
4017+
{
4018+
struct stat file_stat;
4019+
if (stat(path.c_str(), &file_stat) == -1)
4020+
return 0;
4021+
return file_stat.st_mode & S_IFMT;
4022+
}
4023+
4024+
bool simplecpp::isFile(const std::string &path)
4025+
{
4026+
return file_type(path) == S_IFREG;
4027+
}
4028+
4029+
bool simplecpp::isDirectory(const std::string &path)
4030+
{
4031+
return file_type(path) == S_IFDIR;
4032+
}
4033+
40094034
#if (__cplusplus < 201103L) && !defined(__APPLE__)
40104035
#undef nullptr
40114036
#endif

simplecpp.h

+14
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)