Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_fmanip 5 #define tools_fmanip 6 7 #include "file" 8 #include "sep" 9 #include "sout" 10 #include <ostream> 11 12 namespace tools { 13 14 inline bool find_with_dirs(std::ostream& a_out, 15 const std::vector<std::string>& a_dirs, 16 const std::string& a_file, 17 std::string& a_path, 18 bool a_verbose = false ) { 19 std::vector<std::string>::const_iterator it; 20 for(it=a_dirs.begin();it!=a_dirs.end();++it) { 21 if((*it).empty()) { 22 // with a "" in dirs, this case could solve : 23 // - a_file in the current directory. 24 // - a_file with an absolute path name. 25 a_path = a_file; //may be an absolute file name. 26 } else { 27 a_path = *it; 28 a_path += sep(); 29 a_path += a_file; 30 } 31 32 if(a_verbose) { 33 a_out << "find_with_dirs :" 34 << " look for " << sout(a_path) << " ..." 35 << std::endl; 36 } 37 38 if(file::exists(a_path)) { 39 if(a_verbose) { 40 a_out << "find_with_dirs :" 41 << " found " << sout(a_path) << "." 42 << std::endl; 43 } 44 return true; 45 } 46 } 47 a_path.clear(); 48 49 if(a_verbose) { 50 a_out << "find_with_dirs :" 51 << " " << sout(a_file) << " not found." 52 << std::endl; 53 } 54 55 return false; 56 } 57 58 } 59 60 #include "get_env" 61 62 namespace tools { 63 64 inline bool find_with_env(std::ostream& a_out, 65 const std::string& a_env, 66 const std::string& a_file, 67 std::string& a_path, 68 bool a_verbose = false ) { 69 std::string PATH; 70 if(!get_env(a_env,PATH)) { 71 //look for a a_file in current directory. 72 if(file::exists(a_file)) { 73 //a_out << "tools::find_with_env :" 74 // << " env variable " << sout(a_env) << " not defined, but file " << sout(a_file) << " exists." 75 // << std::endl; 76 a_path = a_file; 77 return true; 78 } 79 a_out << "tools::find_with_env : env variable " << sout(a_env) << " not defined." << std::endl; 80 a_path.clear(); 81 return false; 82 } 83 if(a_verbose) { 84 a_out << "find_with_env : env " << sout(a_env) << " is " << sout(PATH) << std::endl; 85 } 86 87 std::vector<std::string> dirs; 88 words(PATH,psep(),false,dirs); //or true ? 89 90 return find_with_dirs(a_out,dirs,a_file,a_path,a_verbose); 91 } 92 93 } 94 95 #endif