Skip to content

Commit 535fcd2

Browse files
Add is_directory and path_exits functions (using stat)
1 parent 0ad7de6 commit 535fcd2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/lpython/utils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,25 @@ std::string get_runtime_library_header_dir()
6969
return get_runtime_library_dir() + "/impure";
7070
}
7171

72+
bool is_directory(std::string path) {
73+
struct stat buffer;
74+
if (stat(path.c_str(), &buffer) == 0) {
75+
if (S_ISDIR(buffer.st_mode)) {
76+
return true;
77+
} else {
78+
return false;
79+
}
80+
}
81+
return false;
82+
}
83+
84+
bool path_exits(std::string path) {
85+
struct stat buffer;
86+
if (stat(path.c_str(), &buffer) == 0) {
87+
return true;
88+
} else {
89+
return false;
90+
}
91+
}
7292

7393
}

src/lpython/utils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,26 @@
44
#include <string>
55
#include <libasr/utils.h>
66

7+
#include <sys/types.h>
8+
#include <sys/stat.h>
9+
#ifndef _WIN32
10+
#include <unistd.h>
11+
#endif
12+
13+
#ifdef _WIN32
14+
#define stat _stat
15+
#if !defined S_ISDIR
16+
#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
17+
#endif
18+
#endif
19+
720
namespace LFortran {
821

922
void get_executable_path(std::string &executable_path, int &dirname_length);
1023
std::string get_runtime_library_dir();
1124
std::string get_runtime_library_header_dir();
25+
bool is_directory(std::string path);
26+
bool path_exits(std::string path);
1227

1328
} // LFortran
1429

0 commit comments

Comments
 (0)