7 #ifndef TREELITE_COMMON_FILESYSTEM_H_ 8 #define TREELITE_COMMON_FILESYSTEM_H_ 10 #include <dmlc/logging.h> 20 #include <sys/types.h> 27 namespace filesystem {
42 std::string::size_type tmp = path.find_last_of(
"/\\");
43 if (tmp == path.length() - 1) {
45 while ((path[i] ==
'/' || path[i] ==
'\\') && i >= 0) {
48 path_ = path.substr(0, i + 1);
52 std::vector<char> fname(path_.length() + 1);
53 std::vector<char> ext(path_.length() + 1);
54 _splitpath_s(path_.c_str(), NULL, 0, NULL, 0,
55 &fname[0], path_.length() + 1, &ext[0], path_.length() + 1);
56 return std::string(&fname[0]) + std::string(&ext[0]);
58 char* path_ = strdup(path.c_str());
59 char* base = basename(path_);
60 std::string ret(base);
66 inline void HandleSystemError(
const std::string& msg) {
69 DWORD dw = GetLastError();
71 FORMAT_MESSAGE_ALLOCATE_BUFFER |
72 FORMAT_MESSAGE_FROM_SYSTEM |
73 FORMAT_MESSAGE_IGNORE_INSERTS,
76 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
79 const std::string msg_err(static_cast<const char*>(msg_buf));
82 const std::string msg_err(strerror(errno));
84 LOG(FATAL) << msg <<
"\nReason: " << msg_err;
87 inline void CreateDirectoryIfNotExist(
const char* dirpath) {
89 DWORD ftyp = GetFileAttributesA(dirpath);
90 if (ftyp == INVALID_FILE_ATTRIBUTES) {
92 if (CreateDirectoryA(dirpath, NULL) == 0) {
94 HandleSystemError(std::string(
"CreateDirectoryIfNotExist: " 95 "failed to create new directory ") + dirpath);
98 if (!(ftyp & FILE_ATTRIBUTE_DIRECTORY)) {
99 LOG(FATAL) <<
"CreateDirectoryIfNotExist: " 100 << dirpath <<
" is a file, not a directory";
105 if (stat(dirpath, &sb) != 0) {
107 if (mkdir(dirpath, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
109 HandleSystemError(std::string(
"CreateDirectoryIfNotExist: " 110 "failed to create new directory ") + dirpath);
113 if (!S_ISDIR(sb.st_mode)) {
114 LOG(FATAL) <<
"CreateDirectoryIfNotExist: " 115 << dirpath <<
" is a file, not a directory";
125 #endif // TREELITE_COMMON_FILESYSTEM_H_ std::string GetBasename(const std::string &path)
extract the base name from a full path. The base name is defined as the component that follows the la...