inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_system 00005 #define inlib_system 00006 00007 #include <string> 00008 #include <cstdlib> 00009 #include "cstr" 00010 #include "smanip" 00011 00012 namespace inlib { 00013 00014 inline bool isenv(const std::string& a_string){ 00015 const char* env = ::getenv(a_string.c_str()); 00016 return (env?true:false); 00017 } 00018 00019 inline bool getenv(const std::string& a_string,std::string& a_value){ 00020 const char* env = ::getenv(a_string.c_str()); 00021 if(env) { 00022 a_value = std::string(env?env:""); 00023 return true; 00024 } else { 00025 a_value.clear(); 00026 return false; 00027 } 00028 } 00029 00030 template <class T> 00031 inline bool get_env(const std::string& a_string,T& a_v){ 00032 std::string s; 00033 if(!getenv(a_string,s)) return false; 00034 return to<T>(s,a_v); 00035 } 00036 00037 inline bool get_env_bool(const std::string& a_string,bool& a_v){ 00038 std::string s; 00039 if(!getenv(a_string,s)) return false; 00040 return to(s,a_v); 00041 } 00042 00043 inline bool putenv(const std::string& a_env,const std::string& a_value){ 00044 std::string value = a_env+"="+a_value; 00045 if(::putenv(inlib::str_dup(value.c_str()))) return false; 00046 //check: 00047 std::string s; 00048 if(!getenv(a_env,s)) return false; 00049 if(s!=a_value) return false; 00050 return true; 00051 } 00052 00053 inline bool rmenv(const std::string& a_env){ 00054 #ifdef WIN32 00055 std::string value = a_env+"="; 00056 if(::putenv(inlib::str_dup(value.c_str()))) return false; 00057 #else 00058 ::unsetenv(a_env.c_str()); 00059 #endif 00060 return true; 00061 } 00062 00063 inline bool check_getenv(std::ostream& a_out,const std::string& a_new,const std::string& a_old,std::string& a_env){ 00064 if(inlib::getenv(a_new,a_env)) return true; 00065 if(inlib::getenv(a_old,a_env)) { 00066 a_out << "Environment variable " << sout(a_old) << " is deprecated." 00067 << " Use " << sout(a_new) << " instead." 00068 << std::endl; 00069 return true; 00070 } 00071 return false; 00072 } 00073 00074 inline int execute(const std::string& a_string) { 00075 return ::system(a_string.c_str()); 00076 } 00077 00078 inline bool env_append(const std::string& a_list,const std::string& a_value,const std::string& a_sep){ 00079 std::string old_value; 00080 if(inlib::isenv(a_list)) { 00081 if(!inlib::getenv(a_list,old_value)) return false; 00082 } 00083 00084 if(old_value.size()) { 00085 std::vector<std::string> ws; 00086 inlib::words(old_value,a_sep,false,ws); 00087 // Remove existing one, so that a_value be put at end. 00088 inlib::remove(ws,a_value); 00089 old_value = inlib::to_string(ws,a_sep); 00090 } 00091 00092 std::string new_value = old_value; 00093 if(new_value.size()) new_value += a_sep; 00094 new_value += a_value; 00095 if(!putenv(a_list,new_value)) return false; 00096 00097 return true; 00098 } 00099 00100 inline bool env_path_append(const std::string& a_paths,const std::string& a_path){ 00101 return env_append(a_paths,a_path,psep()); 00102 } 00103 00104 inline bool expand_vars(std::string& a_string){ 00105 std::string::size_type dollar; 00106 while((dollar=a_string.find('$'))!=std::string::npos){ 00107 std::string::size_type slash = a_string.find('/',dollar+1); 00108 std::string::size_type back_slash = a_string.find('\\',dollar+1); 00109 std::string::size_type pos = std::string::npos; 00110 if(slash!=std::string::npos) { 00111 if(back_slash!=std::string::npos) { 00112 pos = slash<back_slash?slash:back_slash; 00113 } else { 00114 pos = slash; 00115 } 00116 } else { 00117 if(back_slash!=std::string::npos) { 00118 pos = back_slash; 00119 } else { 00120 pos = std::string::npos; 00121 } 00122 } 00123 std::string env; 00124 if(pos==std::string::npos) { 00125 env = a_string.substr(dollar+1,a_string.length()-(dollar+1)); 00126 } else { 00127 // abc$xxx/ef 00128 // 0 3 7 9 00129 env = a_string.substr(dollar+1,pos-(dollar+1)); 00130 } 00131 char* val = ::getenv(env.c_str()); 00132 if(!val) return false; // Stop looping if env variable not found. 00133 std::string value = a_string.substr(0,dollar); 00134 value += val; 00135 if(pos!=std::string::npos) 00136 value += a_string.substr(pos,a_string.length()-pos); 00137 a_string = value; 00138 } 00139 return true; 00140 } 00141 00142 inline bool file_name(const std::string& a_path,std::string& a_name) { 00143 a_name = a_path; 00144 if(!expand_vars(a_name)) {a_name.clear();return false;} 00145 #ifdef WIN32 00146 replace(a_name,"/","\\"); 00147 replace(a_name,"\"",""); 00148 #endif 00149 return true; 00150 } 00151 00152 inline std::string plugin_prefix(){ 00153 #if defined(WIN32) 00154 return ""; 00155 #elif defined(__APPLE__) 00156 return ""; 00157 #elif defined(__CYGWIN__) && defined(__GNUC__) 00158 return ""; 00159 #else //UNIX 00160 return "lib"; 00161 #endif 00162 } 00163 00164 inline std::string plugin_suffix(){ 00165 #if defined(WIN32) 00166 return "dll"; 00167 #elif defined(__APPLE__) 00168 return "bundle"; 00169 #elif defined(__CYGWIN__) && defined(__GNUC__) 00170 return "dll"; 00171 #else //UNIX 00172 return "so"; 00173 #endif 00174 } 00175 00176 inline std::string plugin_name(const std::string& a_name){ 00177 #if defined(WIN32) 00178 return a_name + ".dll"; 00179 #elif defined(__CYGWIN__) && defined(__GNUC__) 00180 return a_name + ".dll"; 00181 #elif defined(__APPLE__) 00182 return a_name + ".bundle"; 00183 #else 00184 return std::string("lib") + a_name + ".so"; 00185 #endif 00186 } 00187 00188 inline std::string so_name(const std::string& a_name){ 00189 #if defined(WIN32) 00190 return a_name + ".dll"; 00191 #elif defined(__CYGWIN__) && defined(__GNUC__) 00192 return a_name + ".dll"; 00193 #elif defined(__APPLE__) 00194 return "lib" + a_name + ".dylib"; 00195 #else 00196 return std::string("lib") + a_name + ".so"; 00197 #endif 00198 } 00199 00200 } 00201 00202 #endif