Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/fmanip is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_fmanip
0005 #define tools_fmanip
0006 
0007 #include "file"
0008 #include "sep"
0009 #include "sout"
0010 #include <ostream>
0011 
0012 namespace tools {
0013 
0014 inline bool find_with_dirs(std::ostream& a_out,
0015                            const std::vector<std::string>& a_dirs,
0016                            const std::string& a_file,
0017                            std::string& a_path,
0018                            bool a_verbose = false ) {
0019   std::vector<std::string>::const_iterator it;
0020   for(it=a_dirs.begin();it!=a_dirs.end();++it) {
0021     if((*it).empty()) {
0022       // with a "" in dirs, this case could solve :
0023       // - a_file in the current directory.
0024       // - a_file with an absolute path name.
0025       a_path = a_file; //may be an absolute file name.
0026     } else {
0027       a_path = *it;
0028       a_path += sep();
0029       a_path += a_file;
0030     }
0031 
0032     if(a_verbose) {
0033       a_out << "find_with_dirs :"
0034             << " look for " << sout(a_path) << " ..."
0035             << std::endl;
0036     }
0037 
0038     if(file::exists(a_path)) {
0039       if(a_verbose) {
0040         a_out << "find_with_dirs :"
0041               << " found " << sout(a_path) << "."
0042               << std::endl;
0043       }
0044       return true;
0045     }
0046   }
0047   a_path.clear();
0048 
0049   if(a_verbose) {
0050     a_out << "find_with_dirs :"
0051           << " " << sout(a_file) << " not found."
0052           << std::endl;
0053   }
0054 
0055   return false;
0056 }
0057 
0058 }
0059 
0060 #include "get_env"
0061 
0062 namespace tools {
0063 
0064 inline bool find_with_env(std::ostream& a_out,
0065                           const std::string& a_env,
0066                           const std::string& a_file,
0067                           std::string& a_path,
0068                           bool a_verbose = false ) {
0069   std::string PATH;
0070   if(!get_env(a_env,PATH)) {
0071     //look for a a_file in current directory.
0072     if(file::exists(a_file)) {
0073       //a_out << "tools::find_with_env :"
0074       //      << " env variable " << sout(a_env) << " not defined, but file " << sout(a_file) << " exists."
0075       //      << std::endl;
0076       a_path = a_file;
0077       return true;
0078     }
0079     a_out << "tools::find_with_env : env variable " << sout(a_env) << " not defined." << std::endl;
0080     a_path.clear();
0081     return false;
0082   }
0083   if(a_verbose) {
0084     a_out << "find_with_env : env " << sout(a_env) << " is " << sout(PATH) << std::endl;
0085   }
0086 
0087   std::vector<std::string> dirs;
0088   words(PATH,psep(),false,dirs); //or true ?
0089 
0090   return find_with_dirs(a_out,dirs,a_file,a_path,a_verbose);
0091 }
0092 
0093 }
0094 
0095 #endif