inlib  1.2.0
Classes | Functions
inlib::dir Namespace Reference

Classes

class  visitor
class  tree

Functions

bool pwd (std::string &a_pwd)
bool cd (const std::string &a_path)
bool create (const std::string &a_name)
std::string home ()
bool cd_home ()
bool in_fs (const std::string &a_path)
bool is_a (const std::string &a_path, bool &a_value)
bool is_dot (const std::string &a_path)
bool mkcd (const std::string &a_name)
bool is_empty (const std::string &a_path, bool &a_is_empty)
bool entries (const std::string &a_path, std::vector< std::string > &a_list, bool a_full_path=true)
bool is_an_entry (const std::string &a_path, const std::string &a_name, bool &a_found)
std::string path_base_name (const std::string &a_path)
bool rmdir (const std::string &a_path)

Function Documentation

bool inlib::dir::cd ( const std::string &  a_path) [inline]

Definition at line 48 of file dir.

                                       {
  if(::chdir(a_path.c_str())!=0) return false;
  return true;
}
bool inlib::dir::cd_home ( ) [inline]

Definition at line 76 of file dir.

                      {
  std::string s = home();
  if(s.empty()) return false;
  return cd(s);
}
bool inlib::dir::create ( const std::string &  a_name) [inline]

Definition at line 53 of file dir.

                                           {
  // a_name should be a single directory name, and not a file system path.
  // Then it must not contain : ., .., /, \ etc...
#ifdef WIN32
  return (::mkdir(a_name.c_str())==0 ? true : false); 
#else
  return (::mkdir(a_name.c_str(), 0755)==0 ? true : false); 
#endif
}
bool inlib::dir::entries ( const std::string &  a_path,
std::vector< std::string > &  a_list,
bool  a_full_path = true 
) [inline]

Definition at line 175 of file dir.

                                                                                                 {
  a_list.clear();
  struct stat finfo;
  if (::stat(a_path.c_str(),&finfo) < 0)  return false;
#ifdef WIN32
  if (!(finfo.st_mode & S_IFDIR)) return false;
  std::string entry = a_path;
  if (!(entry[entry.size()] == '/' || entry[entry.size()] == '\\' ))
    entry += "\\";
  entry += "*";
  WIN32_FIND_DATA findFileData;
  HANDLE dir = ::FindFirstFile(entry.c_str(),&findFileData);
  if(dir == INVALID_HANDLE_VALUE) return false;
  // Get file names :
  for (;;) {
    if(!::FindNextFile(dir,&findFileData)) break;
    std::string name = (const char*)findFileData.cFileName;
    // Be sure we can work on the file :
    std::string fname = a_path+"\\"+name;
    if (::stat(fname.c_str(),&finfo) < 0)  continue;
    if(a_full_path)
      a_list.push_back(fname);
    else
      a_list.push_back(name);
  }
  ::FindClose(dir);
#else
  if (!S_ISDIR(finfo.st_mode)) return false;
  DIR* dir = ::opendir(a_path.c_str());
  if(!dir) return false;
  // Get file names :
  for (;;) {
    struct dirent* dp = ::readdir(dir);
    //struct direct* dp;
    if (dp==NULL) break;
#if defined(_POSIX_SOURCE)
    if(true) {
#else
    if(dp->d_ino!=0) {
#endif
      std::string name = dp->d_name;
      // Be sure we can work on the file :
      std::string fname = a_path+"/"+name;
      if (::stat(fname.c_str(),&finfo) < 0)  continue;
      if(a_full_path)
        a_list.push_back(fname);
      else
        a_list.push_back(name);
    }
  }
  ::closedir(dir);
#endif
  return true;
}
std::string inlib::dir::home ( ) [inline]

Definition at line 63 of file dir.

                        {
#ifdef WIN32
  const char* env = ::getenv("HOMEDRIVE");
  std::string drive = env ? env : std::string("C:");
 {const char* env = ::getenv("HOMEPATH");
  if(!env) return drive+"\\";
  return drive+std::string(env);}
#else
  const char* env = ::getenv("HOME");
  return (env?env:"");
#endif
}
bool inlib::dir::in_fs ( const std::string &  a_path) [inline]

Definition at line 84 of file dir.

                                          {
  struct stat finfo;
  if (::stat(a_path.c_str(),&finfo) < 0) return false;
  return true;
}
bool inlib::dir::is_a ( const std::string &  a_path,
bool &  a_value 
) [inline]

Definition at line 91 of file dir.

                                                        {
  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;
}
bool inlib::dir::is_an_entry ( const std::string &  a_path,
const std::string &  a_name,
bool &  a_found 
) [inline]

Definition at line 230 of file dir.

                                                                                      {
  a_found = false;
  struct stat finfo;
  if (::stat(a_path.c_str(),&finfo) < 0)  return false;
#ifdef WIN32
  if (!(finfo.st_mode & S_IFDIR)) return false;
  std::string entry = a_path;
  if (!(entry[entry.size()] == '/' || entry[entry.size()] == '\\' ))
    entry += "\\";
  entry += "*";
  WIN32_FIND_DATA findFileData;
  HANDLE dir = ::FindFirstFile(entry.c_str(),&findFileData);
  if(dir == INVALID_HANDLE_VALUE) return false;
  // Get file names :
  for (;;) {
    if(!::FindNextFile(dir,&findFileData)) break;
    std::string name = (const char*)findFileData.cFileName;
    if(name==a_name) {
      a_found = true;
      ::FindClose(dir);
      return true;
    }
  }
  ::FindClose(dir);
#else
  if (!S_ISDIR(finfo.st_mode)) return false;
  DIR* dir = ::opendir(a_path.c_str());
  if(!dir) return false;
  // Get file names :
  for (;;) {
    struct dirent* dp = ::readdir(dir);
    //struct direct* dp;
    if (dp==NULL) break;
#if defined(_POSIX_SOURCE)
    if(true) {
#else
    if(dp->d_ino!=0) {
#endif
      std::string name = dp->d_name;
      if(name==a_name) {
        a_found = true;
        ::closedir(dir);
        return true;
      }
    }
  }
  ::closedir(dir);
#endif
  return true;
}
bool inlib::dir::is_dot ( const std::string &  a_path) [inline]

Definition at line 99 of file dir.

                                            {
#ifdef WIN32
  char sep = '\\';
#else
  char sep = '/';
#endif
  unsigned int 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;
}
bool inlib::dir::is_empty ( const std::string &  a_path,
bool &  a_is_empty 
) [inline]

Definition at line 126 of file dir.

                                                              {
  a_is_empty = true;
  struct stat finfo;
  if (::stat(a_path.c_str(),&finfo) < 0)  return false;
#ifdef WIN32
  if (!(finfo.st_mode & S_IFDIR)) return false;
  std::string entry = a_path;
  if (!(entry[entry.size()] == '/' || entry[entry.size()] == '\\' ))
    entry += "\\";
  entry += "*";
  WIN32_FIND_DATA findFileData;
  HANDLE dir = ::FindFirstFile(entry.c_str(),&findFileData);
  if(dir == INVALID_HANDLE_VALUE) return false;
  for (;;) {
    if(!::FindNextFile(dir,&findFileData)) break;
    std::string name = (const char*)findFileData.cFileName;
    if(name==".") continue;
    if(name=="..") continue;
    a_is_empty = false;
    ::FindClose(dir);
    return true;  
  }
  ::FindClose(dir);
#else
  if (!S_ISDIR(finfo.st_mode)) return false;
  DIR* dir = ::opendir(a_path.c_str());
  if(!dir) return false;
  for (;;) {
    struct dirent* dp = ::readdir(dir);
    //struct direct* dp;
    if (dp==NULL) break;
#if defined(_POSIX_SOURCE)
    if(true) {
#else
    if(dp->d_ino!=0) {
#endif
      std::string name = dp->d_name;
      if(name==".") continue;
      if(name=="..") continue;
      a_is_empty = false;
      ::closedir(dir);
      return true;  
    }
  }
  ::closedir(dir);
#endif
  return true;
}
bool inlib::dir::mkcd ( const std::string &  a_name) [inline]

Definition at line 114 of file dir.

                                          {
  // 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 cd(a_name);
}
std::string inlib::dir::path_base_name ( const std::string &  a_path) [inline]

Definition at line 283 of file dir.

                                                         {
  std::string::size_type pos_slash = a_path.rfind('/');
  std::string::size_type pos_bslash = a_path.rfind('\\');
  std::string::size_type pos = 0;
  if(pos_slash==std::string::npos) {
    if(pos_bslash==std::string::npos) {
      pos = std::string::npos;
    } else {
      pos = pos_bslash;
    }
  } else {
    if(pos_bslash==std::string::npos) {
      pos = pos_slash;
    } else {
      if(pos_slash<=pos_bslash) {
        pos = pos_bslash;
      } else {
        pos = pos_slash;
      }
    }
  }
  if(pos==std::string::npos) return a_path;
  pos++;
  return a_path.substr(pos,a_path.size()-pos);
}
bool inlib::dir::pwd ( std::string &  a_pwd) [inline]

Definition at line 25 of file dir.

                                  {
  // Return current directory.
  unsigned int mx_path_len = 1024;
  char* cwd = new char[mx_path_len];
#ifdef WIN32
  //  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;
}
bool inlib::dir::rmdir ( const std::string &  a_path) [inline]

Definition at line 310 of file dir.

                                          {
  struct stat finfo;
  if(::stat(a_path.c_str(),&finfo) < 0) return true;
#ifdef WIN32
  if(!(finfo.st_mode & S_IFDIR)) return false;
#else
  if(!S_ISDIR(finfo.st_mode)) return false;
#endif
  // Empty the directory :
  std::vector<std::string> files;
  entries(a_path,files);
  unsigned int filen = files.size();
  for(unsigned int count=0;count<filen;count++) {
    const std::string& entry = files[count];     

   {std::string name = path_base_name(entry);
    if((name==".")||(name=="..")) continue;}

    bool is_dir;
    if(!is_a(entry,is_dir)) return false;
    if(is_dir) {
      if(!dir::rmdir(entry)) {
        //::printf("debug : rm dir %s failed.\n",entry.c_str());
        return false;
      }
      //::printf("debug : rm dir %s.\n",entry.c_str());
    } else {
      if(::remove(entry.c_str())!=0) {
        //::printf("debug : rm file %s failed.\n",entry.c_str());
        return false;
      }
      //::printf("debug : rm file %s.\n",entry.c_str());
    }
  }
  // Remove the directory :
  return (::rmdir(a_path.c_str())==0?true:false);
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines