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_get_env 4 #ifndef tools_get_env 5 #define tools_get_env 5 #define tools_get_env 6 6 7 #include <cstdlib> 7 #include <cstdlib> 8 8 9 #include "sto" 9 #include "sto" 10 10 11 namespace tools { 11 namespace tools { 12 12 13 inline bool is_env(const std::string& a_string 13 inline bool is_env(const std::string& a_string){ 14 const char* env = ::getenv(a_string.c_str()) 14 const char* env = ::getenv(a_string.c_str()); 15 return (env?true:false); 15 return (env?true:false); 16 } 16 } 17 17 18 inline bool get_env(const std::string& a_strin 18 inline bool get_env(const std::string& a_string,std::string& a_value){ 19 const char* env = ::getenv(a_string.c_str()) 19 const char* env = ::getenv(a_string.c_str()); 20 if(env) { 20 if(env) { 21 a_value = std::string(env?env:""); 21 a_value = std::string(env?env:""); 22 return true; 22 return true; 23 } else { 23 } else { 24 a_value.clear(); 24 a_value.clear(); 25 return false; 25 return false; 26 } 26 } 27 } 27 } 28 28 29 template <class T> 29 template <class T> 30 inline bool get_env(const std::string& a_strin 30 inline bool get_env(const std::string& a_string,T& a_v,const T& a_def = T()){ 31 std::string _s; << 31 std::string s; 32 if(!get_env(a_string,_s)) {a_v = a_def;retur << 32 if(!get_env(a_string,s)) {a_v = a_def;return false;} 33 return to<T>(_s,a_v,a_def); << 33 return to<T>(s,a_v,a_def); 34 } 34 } 35 35 36 inline bool get_env_bool(const std::string& a_ 36 inline bool get_env_bool(const std::string& a_string,bool& a_v){ 37 std::string _s; << 37 std::string s; 38 if(!get_env(a_string,_s)) return false; << 38 if(!get_env(a_string,s)) return false; 39 return to(_s,a_v); << 39 return to(s,a_v); 40 } 40 } 41 41 42 } 42 } 43 43 44 #endif 44 #endif