Files
geant4/source/externals/g4tools/include/tools/sys/dir
T
2021-06-25 16:12:29 +02:00

141 lines
3.4 KiB
Plaintext

// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_sys_dir
#define tools_sys_dir
// on Windows, avoid windows.h.
// See files to get directory entries (that needs windows.h on Windows).
#include <string>
#ifdef _MSC_VER
#include <direct.h>
//extern "C" {char* _getdcwd(int,char*,int);}
#else
#include <unistd.h>
#include <cstdlib> //getenv.
#endif
#include <sys/stat.h>
namespace tools {
namespace dir {
inline bool pwd(std::string& a_pwd) {
// Return current directory.
unsigned int mx_path_len = 2048;
char* cwd = new char[mx_path_len];
#ifdef _MSC_VER
// driveletter = 0 means return the working directory for the default drive.
if(::_getdcwd(0,cwd,mx_path_len)==NULL) {
delete [] cwd;
a_pwd.clear();
return false;
}
#else
if(::getcwd(cwd,mx_path_len)==NULL) {
delete [] cwd;
a_pwd.clear();
return false;
}
#endif
a_pwd = cwd;
delete [] cwd;
return true;
}
inline bool cd(const std::string& a_path){
if(::chdir(a_path.c_str())!=0) return false;
return true;
}
inline bool create(const std::string& a_name){
// a_name should be a single directory name, and not a file system path.
// Then it must not contain : ., .., /, \ etc...
#ifdef _MSC_VER
return (::mkdir(a_name.c_str())==0 ? true : false);
#else
return (::mkdir(a_name.c_str(), 0755)==0 ? true : false);
#endif
}
inline bool home(std::string& a_s) {
#ifdef _WIN32
const char* env = ::getenv("USERPROFILE");
#else
const char* env = ::getenv("HOME");
#endif
if(!env) {a_s.clear();return false;}
a_s = env;
return true;
}
inline bool cd_home() {
std::string s;
home(s);
if(s.empty()) return false;
return cd(s);
}
//NOTE : SWIG : exists is also a method in file
// (can be fix with a %rename(directory_exists))
inline bool in_fs(const std::string& a_path){
struct stat finfo;
if (::stat(a_path.c_str(),&finfo) < 0) return false;
return true;
}
//NOTE : SWIG : "is" is a Python keyword.
inline bool is_a(const std::string& a_path,bool& a_value) {
a_value = false;
struct stat finfo;
if (::stat(a_path.c_str(),&finfo) < 0) return false;
a_value = ( ((finfo.st_mode & S_IFMT) == S_IFDIR) ? true : false);
return true;
}
inline bool is_a(const std::string& a_path) {
bool _is;
if(!is_a(a_path,_is)) return false;
return _is;
}
inline bool is_dot(const std::string& a_path) {
#ifdef _WIN32
char sep = '\\';
#else
char sep = '/';
#endif
size_t l = a_path.size();
if((l==1) && (a_path[0]=='.') ) return true;
if((l==2) && (a_path[0]=='.') && (a_path[l]=='.') ) return true;
if((l>=2) && (a_path[l-1]=='.') && (a_path[l-2]==sep) ) return true;
if((l>=3) && (a_path[l-1]=='.') && (a_path[l-2]=='.') && (a_path[l-3]==sep) )
return true;
return false;
}
inline bool mkdir(const std::string& a_name) {
// a_name should be a single directory name, and not a file system path.
// Then it must not contain : ., .., /, \ etc...
bool is;
if(!is_a(a_name,is)) { //a_name does not exist as a file or dir.
if(!create(a_name)) return false;
} else {
if(!is) return false; //a_name exists but is not a directory.
}
return true;
}
inline bool mkcd(const std::string& a_name) {
// a_name should be a single directory name, and not a file system path.
// Then it must not contain : ., .., /, \ etc...
if(!mkdir(a_name)) return false;
return cd(a_name);
}
}}
#endif