inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/fmanip
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_fmanip
00005 #define inlib_fmanip
00006 
00007 #include "file"
00008 #include "system"
00009 #include "smanip"
00010 #include "vmanip"
00011 #include "sep"
00012 
00013 namespace inlib {
00014 
00015 inline bool find_with_dirs(std::ostream& a_out,
00016                            const std::string& a_dirs,
00017                            const std::string& a_file,
00018                            std::string& a_path,
00019                            bool a_verbose = false ) {
00020   std::vector<std::string> words;
00021   inlib::words(a_dirs,psep(),false,words);
00022   std::vector<std::string>::const_iterator it;
00023   for(it=words.begin();it!=words.end();++it) {
00024     a_path = *it;
00025     a_path += sep();
00026     a_path += a_file;
00027 
00028     if(a_verbose) {
00029       a_out << "find_with_dirs :"
00030             << " look for " << inlib::sout(a_path) << " ..."
00031             << std::endl;
00032     }
00033 
00034     if(inlib::file::exists(a_path)) {
00035       if(a_verbose) {
00036         a_out << "find_with_dirs :"
00037               << " found " << inlib::sout(a_path) << "."
00038               << std::endl;
00039       }
00040       return true;
00041     }
00042   }
00043   a_path.clear();
00044 
00045   if(a_verbose) {
00046     a_out << "find_with_dirs :"
00047           << " " << inlib::sout(a_file) << " not found."
00048           << std::endl;
00049   }
00050 
00051   return false;   
00052 }
00053 
00054 inline bool find_with_path(std::ostream& a_out,
00055                            const std::string& a_env,
00056                            const std::string& a_file,
00057                            std::string& a_path,
00058                            bool a_verbose = false ) {
00059   std::string PATH;
00060   if(!inlib::getenv(a_env,PATH)) {
00061     //look for a a_file in current directory.
00062     a_path = a_file;
00063     if(inlib::file::exists(a_path)) return true;
00064     a_out << "inlib::find_with_path :"
00065           << " env variable " << sout(a_env) << " not defined."
00066           << std::endl;
00067     a_path.clear();
00068     return false;
00069   }
00070   if(a_verbose) {
00071     a_out << "find_with_path :"
00072           << " PATH is " << inlib::sout(PATH)
00073           << std::endl;
00074   }
00075   return find_with_dirs(a_out,PATH,a_file,a_path,a_verbose);
00076 }
00077 
00078 }
00079 
00080 #include "sys/dir"
00081 
00082 namespace inlib {
00083 
00084 inline bool get_files(std::ostream& a_out,const std::string& a_dirs,const std::string& a_exts,std::vector<std::string>& a_paths) {
00085 
00086   a_paths.clear();
00087 
00088   std::vector<std::string> exts;
00089   inlib::words(a_exts,"\n",false,exts);
00090 
00091   bool status = true;
00092 
00093   std::vector<std::string> words;
00094   inlib::words(a_dirs,inlib::psep(),false,words);
00095 
00096   std::vector<std::string>::const_iterator pit;
00097   for(pit=words.begin();pit!=words.end();++pit) {
00098     const std::string& dir = *pit;
00099     bool is_dir;
00100     if(!inlib::dir::is_a(dir,is_dir)) continue;
00101     if(!is_dir) continue;
00102         
00103     std::vector<std::string> ls;
00104     if(!inlib::dir::entries(dir,ls,false)){
00105       a_out << "inlib::get_files :"
00106             << " can't get entries"
00107             << " of directory " << inlib::sout(dir) << "."
00108             << std::endl;
00109       status = false;
00110       continue; //continue anyway.
00111   //} else {
00112       //a_out << "inlib::get_files :"
00113       //a_out << "number of files : " << ls.size() << std::endl;
00114       //std::vector<std::string>::const_iterator it;
00115       //for(it=ls.begin();it!=ls.end();++it) {
00116       //  a_out << *it << std::endl;
00117       //}
00118     }
00119     
00120     std::vector<std::string> files;
00121    {std::vector<std::string>::const_iterator it;
00122     for(it=ls.begin();it!=ls.end();++it) {
00123       if((*it)==".") continue;
00124       if((*it)=="..") continue;
00125 
00126       std::vector<std::string>::const_iterator eit;
00127       for(eit=exts.begin();eit!=exts.end();++eit) {
00128         const std::string& ext = *eit;
00129         if((*it).rfind(ext)!=std::string::npos) {
00130           files.push_back(*it);
00131         }
00132       }
00133     }}
00134       
00135     std::vector<std::string>::const_iterator it;
00136     for(it=files.begin();it!=files.end();++it) {
00137       std::string path = dir+"/"+(*it);
00138       a_paths.push_back(path);
00139     }
00140   }
00141 
00142   inlib::unique(a_paths);
00143   
00144   return status;
00145 }
00146 
00147 inline bool mkdirs(std::ostream& a_out,const std::string& a_path,bool a_cd = false) {
00148   //By default it does not "cd" to the leaf directory.
00149   //In the below, we should treat "absolute" and the case of win_path.
00150   bool absolute;
00151   bool win_path;
00152   std::string drive;
00153   std::vector<std::string> dirs = path_words(a_path,absolute,win_path,drive);
00154   if(drive.size()) {
00155     a_out << "inlib::mkdirs :"
00156           << " path with a Windows drive not treated yet."
00157           << std::endl;
00158     return false;
00159   }
00160   if(absolute) {
00161     a_out << "inlib::mkdirs :"
00162           << " absolute path not treated yet."
00163           << std::endl;
00164     return false;
00165   }
00166   std::string spwd;
00167   if(!a_cd) {
00168     if(!dir::pwd(spwd)) {
00169       a_out << "inlib::mkdirs :"
00170             << " can't get current directory."
00171             << std::endl;
00172       return false;
00173     }
00174   }  
00175   bool status = true;
00176   std::vector<std::string>::const_iterator it;
00177   for(it=dirs.begin();it!=dirs.end();++it) {
00178     if(!dir::mkcd(*it)) {
00179       a_out << "inlib::mkdirs :"
00180             << " can't mkcd " << sout(*it)
00181             << std::endl;
00182       status = false;
00183       break;
00184     }
00185   }
00186   if(!a_cd) {
00187     //return to spwd :
00188     if(!dir::cd(spwd)) {
00189       a_out << "inlib::mkdirs :"
00190             << " can't cd to " << spwd << "."
00191             << std::endl;
00192       return false;
00193     }
00194   }
00195   return status;
00196 }
00197 
00198 }
00199 
00200 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines