g4tools  5.4.0
dir
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sys_dir
5 #define tools_sys_dir
6 
7 // on Windows, avoid windows.h.
8 // See files to get directory entries (that needs windows.h on Windows).
9 
10 #include <string>
11 
12 #ifdef _MSC_VER
13 #include <direct.h>
14 //extern "C" {char* _getdcwd(int,char*,int);}
15 #else
16 #include <unistd.h>
17 #include <cstdlib> //getenv.
18 #endif
19 
20 #include <sys/stat.h>
21 
22 namespace tools {
23 namespace dir {
24 
25 inline bool pwd(std::string& a_pwd) {
26  // Return current directory.
27  unsigned int mx_path_len = 2048;
28  char* cwd = new char[mx_path_len];
29 #ifdef _MSC_VER
30  // driveletter = 0 means return the working directory for the default drive.
31  if(::_getdcwd(0,cwd,mx_path_len)==NULL) {
32  delete [] cwd;
33  a_pwd.clear();
34  return false;
35  }
36 #else
37  if(::getcwd(cwd,mx_path_len)==NULL) {
38  delete [] cwd;
39  a_pwd.clear();
40  return false;
41  }
42 #endif
43  a_pwd = cwd;
44  delete [] cwd;
45  return true;
46 }
47 
48 inline bool cd(const std::string& a_path){
49  if(::chdir(a_path.c_str())!=0) return false;
50  return true;
51 }
52 
53 inline bool create(const std::string& a_name){
54  // a_name should be a single directory name, and not a file system path.
55  // Then it must not contain : ., .., /, \ etc...
56 #ifdef _MSC_VER
57  return (::mkdir(a_name.c_str())==0 ? true : false);
58 #else
59  return (::mkdir(a_name.c_str(), 0755)==0 ? true : false);
60 #endif
61 }
62 
63 inline bool home(std::string& a_s) {
64 #ifdef _WIN32
65  const char* env = ::getenv("USERPROFILE");
66 #else
67  const char* env = ::getenv("HOME");
68 #endif
69  if(!env) {a_s.clear();return false;}
70  a_s = env;
71  return true;
72 }
73 
74 inline bool cd_home() {
75  std::string s;
76  home(s);
77  if(s.empty()) return false;
78  return cd(s);
79 }
80 
81 //NOTE : SWIG : exists is also a method in file
82 // (can be fix with a %rename(directory_exists))
83 inline bool in_fs(const std::string& a_path){
84  struct stat finfo;
85  if (::stat(a_path.c_str(),&finfo) < 0) return false;
86  return true;
87 }
88 
89 //NOTE : SWIG : "is" is a Python keyword.
90 inline bool is_a(const std::string& a_path,bool& a_value) {
91  a_value = false;
92  struct stat finfo;
93  if (::stat(a_path.c_str(),&finfo) < 0) return false;
94  a_value = ( ((finfo.st_mode & S_IFMT) == S_IFDIR) ? true : false);
95  return true;
96 }
97 
98 inline bool is_a(const std::string& a_path) {
99  bool _is;
100  if(!is_a(a_path,_is)) return false;
101  return _is;
102 }
103 
104 inline bool is_dot(const std::string& a_path) {
105 #ifdef _WIN32
106  char sep = '\\';
107 #else
108  char sep = '/';
109 #endif
110  size_t l = a_path.size();
111  if((l==1) && (a_path[0]=='.') ) return true;
112  if((l==2) && (a_path[0]=='.') && (a_path[l]=='.') ) return true;
113  if((l>=2) && (a_path[l-1]=='.') && (a_path[l-2]==sep) ) return true;
114  if((l>=3) && (a_path[l-1]=='.') && (a_path[l-2]=='.') && (a_path[l-3]==sep) )
115  return true;
116  return false;
117 }
118 
119 inline bool mkdir(const std::string& a_name) {
120  // a_name should be a single directory name, and not a file system path.
121  // Then it must not contain : ., .., /, \ etc...
122  bool is;
123  if(!is_a(a_name,is)) { //a_name does not exist as a file or dir.
124  if(!create(a_name)) return false;
125  } else {
126  if(!is) return false; //a_name exists but is not a directory.
127  }
128  return true;
129 }
130 
131 inline bool mkcd(const std::string& a_name) {
132  // a_name should be a single directory name, and not a file system path.
133  // Then it must not contain : ., .., /, \ etc...
134  if(!mkdir(a_name)) return false;
135  return cd(a_name);
136 }
137 
138 }}
139 
140 #endif
tools::jpeg::is
bool is(const std::string &a_file)
Definition: jpeg:216
tools::dir::create
bool create(const std::string &a_name)
Definition: dir:53
tools::dir::mkcd
bool mkcd(const std::string &a_name)
Definition: dir:131
tools::dir::in_fs
bool in_fs(const std::string &a_path)
Definition: dir:83
tools::dir::home
bool home(std::string &a_s)
Definition: dir:63
tools::dir::is_dot
bool is_dot(const std::string &a_path)
Definition: dir:104
tools::dir::mkdir
bool mkdir(const std::string &a_name)
Definition: dir:119
tools::dir::is_a
bool is_a(const std::string &a_path, bool &a_value)
Definition: dir:90
tools::dir::pwd
bool pwd(std::string &a_pwd)
Definition: dir:25
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::dir::cd_home
bool cd_home()
Definition: dir:74
tools::sep
const std::string & sep()
Definition: sep:11
tools::dir::cd
bool cd(const std::string &a_path)
Definition: dir:48