2 #if defined(LICORNEA_OS_LINUX) || defined(LICORNEA_OS_DARWIN) 13 #include <system_error> 18 constexpr std::size_t path_max_length = 1024;
22 std::string path(path_);
23 if(path.back() ==
'/') path = path.substr(0, path.length() - 1);
24 auto pos = path.find_last_of(
'/');
25 if(pos != std::string::npos)
return path.substr(0, pos + 1);
26 else if(path !=
"." && path !=
".." && path !=
"")
return ".";
27 else throw std::invalid_argument(
"cannot get parent path of " + path);
30 std::string
filename_append(
const std::string& a,
const std::string& b) {
31 if(a.back() ==
'/')
return a + b;
32 else return a +
"/" + b;
36 char buffer[path_max_length];
37 char* path = getcwd(buffer, path_max_length);
38 if(path !=
nullptr)
return std::string(path);
39 else throw std::system_error(errno, std::system_category(),
"getcwd() failed");
44 return (stat(filename.c_str(), &sb) == 0);
49 int result = stat(filename.c_str(), &sb);
50 if(result == 0)
return S_ISDIR(sb.st_mode);
51 else throw std::system_error(errno, std::system_category(),
"stat() failed");
54 bool is_file(
const std::string& filename) {
56 int result = stat(filename.c_str(), &sb);
57 if(result == 0)
return S_ISREG(sb.st_mode);
58 else throw std::system_error(errno, std::system_category(),
"stat() failed");
61 std::size_t
file_size(
const std::string& filename) {
63 int result = stat(filename.c_str(), &sb);
64 if(result == 0)
return sb.st_size;
65 else throw std::system_error(errno, std::system_category(),
"stat() failed");
71 int result = mkdir(dirname.c_str(), S_IRWXU | S_IRWXG);
72 if(result == 0 || (result == -1 && errno == EEXIST &&
is_directory(dirname)))
return;
73 else throw std::system_error(errno, std::system_category(),
"mkdir() failed");
77 std::deque<std::string> new_dirs;
81 new_dirs.push_front(dir);
84 for(
const std::string& dir : new_dirs) {
90 int result = unlink(filename.c_str());
91 if(result != 0)
throw std::system_error(errno, std::system_category(),
"unlink() failed");
std::string filename_append(const std::string &a, const std::string &b)
bool file_exists(const std::string &filename)
bool is_file(const std::string &filename)
bool is_directory(const std::string &filename)
std::size_t file_size(const std::string &filename)
std::string get_current_directory()
void delete_file(const std::string &filename)
std::string filename_parent(const std::string &filename)
void make_directory(const std::string &dirname)
void make_parent_directories(const std::string &filename)