Skip to content

Commit 95d8b17

Browse files
committed
added the file/directory existence functions from Cppcheck
1 parent 538c5c4 commit 95d8b17

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

simplecpp.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242

4343
#ifdef _WIN32
4444
# include <direct.h>
45+
using mode_t = unsigned short;
4546
#else
4647
# include <sys/stat.h>
48+
# include <sys/types.h>
4749
#endif
4850

4951
static bool isHex(const std::string &s)
@@ -3838,3 +3840,21 @@ std::string simplecpp::getCppStdString(const std::string &std)
38383840
{
38393841
return getCppStdString(getCppStd(std));
38403842
}
3843+
3844+
static mode_t file_type(const std::string &path)
3845+
{
3846+
struct stat file_stat;
3847+
if (stat(path.c_str(), &file_stat) == -1)
3848+
return 0;
3849+
return file_stat.st_mode & S_IFMT;
3850+
}
3851+
3852+
bool simplecpp::isFile(const std::string &path)
3853+
{
3854+
return file_type(path) == S_IFREG;
3855+
}
3856+
3857+
bool simplecpp::isDirectory(const std::string &path)
3858+
{
3859+
return file_type(path) == S_IFDIR;
3860+
}

simplecpp.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,20 @@ namespace simplecpp {
353353
bool removeComments; /** remove comment tokens from included files */
354354
};
355355

356+
/**
357+
* @brief Checks if given path is a file
358+
* @param path Path to be checked
359+
* @return true if given path is a file
360+
*/
361+
SIMPLECPP_LIB bool isFile(const std::string &path);
362+
363+
/**
364+
* @brief Checks if a given path is a directory
365+
* @param path Path to be checked
366+
* @return true if given path is a directory
367+
*/
368+
SIMPLECPP_LIB bool isDirectory(const std::string &path);
369+
356370
struct SIMPLECPP_LIB FileData {
357371
/** The canonical filename associated with this data */
358372
std::string filename;

0 commit comments

Comments
 (0)