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