Skip to content

Commit 0174019

Browse files
committed
added the file/directory existence functions from Cppcheck
1 parent 435a74c commit 0174019

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

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)
@@ -3834,3 +3836,21 @@ std::string simplecpp::getCppStdString(const std::string &std)
38343836
{
38353837
return getCppStdString(getCppStd(std));
38363838
}
3839+
3840+
static mode_t file_type(const std::string &path)
3841+
{
3842+
struct stat file_stat;
3843+
if (stat(path.c_str(), &file_stat) == -1)
3844+
return 0;
3845+
return file_stat.st_mode & S_IFMT;
3846+
}
3847+
3848+
bool simplecpp::isFile(const std::string &path)
3849+
{
3850+
return file_type(path) == S_IFREG;
3851+
}
3852+
3853+
bool simplecpp::isDirectory(const std::string &path)
3854+
{
3855+
return file_type(path) == S_IFDIR;
3856+
}

simplecpp.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,20 @@ namespace simplecpp {
396396
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
397397
SIMPLECPP_LIB std::string getCppStdString(cppstd_t std);
398398

399+
/**
400+
* @brief Checks if given path is a file
401+
* @param path Path to be checked
402+
* @return true if given path is a file
403+
*/
404+
SIMPLECPP_LIB bool isFile(const std::string &path);
405+
406+
/**
407+
* @brief Checks if a given path is a directory
408+
* @param path Path to be checked
409+
* @return true if given path is a directory
410+
*/
411+
SIMPLECPP_LIB bool isDirectory(const std::string &path);
412+
399413
struct SIMPLECPP_LIB FileData {
400414
/** The canonical filename associated with this data */
401415
std::string filename;

0 commit comments

Comments
 (0)