g4tools  5.4.0
Functions
tools::dir Namespace Reference

Functions

bool pwd (std::string &a_pwd)
 
bool cd (const std::string &a_path)
 
bool create (const std::string &a_name)
 
bool home (std::string &a_s)
 
bool cd_home ()
 
bool in_fs (const std::string &a_path)
 
bool is_a (const std::string &a_path, bool &a_value)
 
bool is_a (const std::string &a_path)
 
bool is_dot (const std::string &a_path)
 
bool mkdir (const std::string &a_name)
 
bool mkcd (const std::string &a_name)
 

Function Documentation

◆ cd()

bool tools::dir::cd ( const std::string &  a_path)
inline

Definition at line 48 of file dir.

48  {
49  if(::chdir(a_path.c_str())!=0) return false;
50  return true;
51 }

◆ cd_home()

bool tools::dir::cd_home ( )
inline

Definition at line 74 of file dir.

74  {
75  std::string s;
76  home(s);
77  if(s.empty()) return false;
78  return cd(s);
79 }

◆ create()

bool tools::dir::create ( const std::string &  a_name)
inline

Definition at line 53 of file dir.

53  {
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 }

◆ home()

bool tools::dir::home ( std::string &  a_s)
inline

Definition at line 63 of file dir.

63  {
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 }

◆ in_fs()

bool tools::dir::in_fs ( const std::string &  a_path)
inline

Definition at line 83 of file dir.

83  {
84  struct stat finfo;
85  if (::stat(a_path.c_str(),&finfo) < 0) return false;
86  return true;
87 }

◆ is_a() [1/2]

bool tools::dir::is_a ( const std::string &  a_path)
inline

Definition at line 98 of file dir.

98  {
99  bool _is;
100  if(!is_a(a_path,_is)) return false;
101  return _is;
102 }

◆ is_a() [2/2]

bool tools::dir::is_a ( const std::string &  a_path,
bool &  a_value 
)
inline

Definition at line 90 of file dir.

90  {
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 }

◆ is_dot()

bool tools::dir::is_dot ( const std::string &  a_path)
inline

Definition at line 104 of file dir.

104  {
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 }

◆ mkcd()

bool tools::dir::mkcd ( const std::string &  a_name)
inline

Definition at line 131 of file dir.

131  {
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 }

◆ mkdir()

bool tools::dir::mkdir ( const std::string &  a_name)
inline

Definition at line 119 of file dir.

119  {
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 }

◆ pwd()

bool tools::dir::pwd ( std::string &  a_pwd)
inline

Definition at line 25 of file dir.

25  {
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 }
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::home
bool home(std::string &a_s)
Definition: dir:63
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)
Definition: dir:98
tools::sep
const std::string & sep()
Definition: sep:11
tools::dir::cd
bool cd(const std::string &a_path)
Definition: dir:48