29 # include <sys/stat.h> 32 # include <mach-o/dyld.h> 43 namespace basis {
namespace os {
51 char* buffer = _getcwd(NULL, 0);
75 newbuf =
static_cast<LPTSTR
>(realloc(buffer, buflen *
sizeof(TCHAR)));
78 retval = GetModuleFileName(NULL, buffer, buflen);
79 if (retval == 0 || retval < buflen)
break;
86 int n = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, NULL, 0, NULL, NULL);
87 char* mbpath =
static_cast<char*
>(malloc(n));
89 WideCharToMultiByte(CP_UTF8, 0, buffer, -1, mbpath, n, NULL, NULL);
102 uint32_t buflen = 256;
104 buffer =
reinterpret_cast<char*
>(malloc(buflen *
sizeof(
char)));
106 if (_NSGetExecutablePath(buffer, &buflen) == 0) {
109 newbuf =
reinterpret_cast<char*
>(realloc(buffer, buflen *
sizeof(
char)));
112 if (_NSGetExecutablePath(buffer, &buflen) == 0) {
130 if (exec_path.empty())
return "";
134 if (ext ==
".exe" || ext ==
".com") exec_path = head;
155 newbuf =
reinterpret_cast<char*
>(realloc(buffer, buflen *
sizeof(
char)));
158 int n =
::readlink(path.c_str(), buffer, buflen);
160 if (static_cast<size_t>(n) < buflen) {
174 static inline bool makedir(
const string& path,
bool parent)
176 if (path.empty())
return true;
188 for (vector<string>::reverse_iterator it = dirs.rbegin(); it != dirs.rend(); ++it) {
190 if (CreateDirectory(it->c_str(), NULL) == FALSE)
return false;
192 if (::
mkdir(it->c_str(), 0755) != 0)
return false;
201 return makedir(path,
false);
207 return makedir(path,
true);
212 static inline bool removedir(
const string& path,
bool recursive)
215 if (recursive && !
emptydir(path))
return false;
218 return (::SetFileAttributes(path.c_str(), FILE_ATTRIBUTE_NORMAL) == TRUE) &&
219 (::RemoveDirectory(path.c_str()) == TRUE);
228 return removedir(path,
false);
234 return removedir(path,
true);
244 WIN32_FIND_DATA info;
245 HANDLE hFile = ::FindFirstFile(
path::join(path,
"*.*").c_str(), &info);
246 if (hFile != INVALID_HANDLE_VALUE) {
249 if (strncmp(info.cFileName,
".", 2) == 0 || strncmp(info.cFileName,
"..", 3) == 0) {
254 if(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
255 if (!removedir(subpath,
true)) ok =
false;
257 if (::SetFileAttributes(subpath.c_str(), FILE_ATTRIBUTE_NORMAL) == FALSE ||
258 ::DeleteFile(subpath.c_str()) == FALSE) ok =
false;
260 }
while (::FindNextFile(hFile, &info) == TRUE);
264 struct dirent *p = NULL;
265 DIR *d = opendir(path.c_str());
267 while ((p = readdir(d)) != NULL) {
269 if (strncmp(p->d_name,
".", 2) == 0 || strncmp(p->d_name,
"..", 3) == 0) {
275 if (!
rmtree(subpath)) ok =
false;
277 if (unlink(subpath.c_str()) != 0) ok =
false;
bool mkdir(const std::string &path)
Make directory.
Basic exceptions and related helper macros.
std::string getcwd()
Get absolute path of the (current) working directory.
function normpath(in path)
Clean path, i.e., remove occurences of "./", duplicate slashes,...
function realpath(in path)
Get canonical file path.
function ok(in expression, in name)
Evaluate test expression and fail if it does not evaluate to 0 or check return value of function...
std::string join(const std::string &base, const std::string &path)
Join two paths, e.g., base path and relative path.
System related macro definitions.
bool rmdir(const std::string &path)
Remove empty directory.
bool emptydir(const std::string &path)
Remove files and directories from directory.
bool exists(const std::string path)
Test the existance of a file or directory.
bool isfile(const std::string path)
Test whether a given path is the path of an existent file.
std::string basename(const std::string &path)
Get file name.
bool makedirs(const std::string &path)
Make directory including parent directories if required.
bool rmtree(const std::string &path)
Remove whole directory tree.
function exepath(out path, in target)
Get absolute path of executable file.
Operating system dependent functions.
void splitext(const std::string &path, std::string &head, std::string &ext, const std::set< std::string > *exts=NULL, bool icase=false)
Get file name extension.
std::string dirname(const std::string &path)
Get file directory.
File/directory path related functions.
std::string readlink(const std::string &path)
Read value of symbolic link.
bool isdir(const std::string path)
Test whether a given path is the path of an existent directory.
function exedir(out dir, in name)
Get directory of executable file.
function exename(out file, in name)
Get name of executable file.