21 #if defined(__linux__) || \
22 defined(__FreeBSD_kernel__) || \
24 defined(__unix__) || \
25 defined(__CYGWIN__) || \
35 #include <util/pragma_push.def>
37 #pragma warning(disable:4668)
39 #pragma warning(disable : 5039)
47 #include <util/pragma_pop.def>
55 char *wd=realpath(
".",
nullptr);
59 std::string(
"realpath failed: ") + std::strerror(errno));
61 std::string working_directory=wd;
65 DWORD retval=GetCurrentDirectory(4096, buffer);
70 std::string working_directory(
narrow(buffer));
72 std::string working_directory(buffer);
77 return working_directory;
84 if(chdir(path.c_str()) != 0)
86 std::string(
"chdir failed: ") + std::strerror(errno));
92 void delete_directory_utf16(
const std::wstring &path)
94 std::wstring pattern=path + L
"\\*";
96 struct _wfinddata_t info;
97 intptr_t hFile=_wfindfirst(pattern.c_str(), &info);
102 if(wcscmp(info.name, L
".")==0 || wcscmp(info.name, L
"..")==0)
104 std::wstring sub_path=path+L
"\\"+info.name;
105 if(info.attrib & _A_SUBDIR)
106 delete_directory_utf16(sub_path);
108 DeleteFileW(sub_path.c_str());
110 while(_wfindnext(hFile, &info)==0);
112 RemoveDirectoryW(path.c_str());
123 DIR *dir=opendir(path.c_str());
127 while((ent=readdir(dir))!=
nullptr)
130 if(strcmp(ent->d_name,
".")==0 || strcmp(ent->d_name,
"..")==0)
133 std::string sub_path=path+
"/"+ent->d_name;
136 int result=stat(sub_path.c_str(), &stbuf);
139 std::string(
"Stat failed: ") + std::strerror(errno));
141 if(S_ISDIR(stbuf.st_mode))
145 result=remove(sub_path.c_str());
148 std::string(
"Remove failed: ") + std::strerror(errno));
160 const std::string &directory,
161 const std::string &file_name)
165 file_name.size() > 1 && file_name[0] !=
'/' && file_name[0] !=
'\\' &&
171 !directory.empty() && (directory.back() ==
'/' || directory.back() ==
'\\'))
173 return directory + file_name;
176 return directory +
'\\' + file_name;
178 if(!file_name.empty() && file_name[0] ==
'/')
180 else if(!directory.empty() && directory.back() ==
'/')
181 return directory + file_name;
183 return directory +
'/' + file_name;
194 auto attributes = ::GetFileAttributesW(
widen(path).c_str());
195 if (attributes == INVALID_FILE_ATTRIBUTES)
198 return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
204 if(stat(path.c_str(), &buf)!=0)
207 return (buf.st_mode & S_IFDIR) != 0;
215 return _mkdir(path.c_str()) == 0;
218 return mkdir(path.c_str(), 0777) == 0;
227 return access(path.c_str(), F_OK) == 0;
236 return unlink(path.c_str()) == 0;
240 void file_rename(
const std::string &old_path,
const std::string &new_path)
247 auto MoveFile_result =
248 MoveFileW(
widen(old_path).c_str(),
widen(new_path).c_str());
250 if(MoveFile_result == 0)
259 auto MoveFileEx_result = MoveFileExW(
260 widen(old_path).c_str(),
261 widen(new_path).c_str(),
262 MOVEFILE_REPLACE_EXISTING);
264 if(MoveFileEx_result == 0)
268 int rename_result = rename(old_path.c_str(), new_path.c_str());
270 if(rename_result != 0)
272 std::string(
"rename failed: ") + std::strerror(errno));
Thrown when some external system fails unexpectedly.
void set_current_path(const std::string &path)
Set working directory.
std::string get_current_working_directory()
bool file_exists(const std::string &path)
Check whether file with given path exists.
bool is_directory(const std::string &path)
void delete_directory(const std::string &path)
deletes all files in 'path' and then the directory itself
bool file_remove(const std::string &path)
C++17 will allow us to use std::filesystem::remove.
bool create_directory(const std::string &path)
Create a directory with given path C++17 will allow us to use std::filesystem::create_directory.
void file_rename(const std::string &old_path, const std::string &new_path)
Rename a file.
std::string concat_dir_file(const std::string &directory, const std::string &file_name)
output_type narrow(input_type input)
Run-time checked narrowing cast.
std::wstring widen(const char *s)
std::wstring utf8_to_utf16_native_endian(const std::string &in)
Convert UTF8-encoded string to UTF-16 with architecture-native endianness.