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_path 4 #ifndef tools_path 5 #define tools_path 5 #define tools_path 6 6 7 #include <string> 7 #include <string> 8 #include <utility> << 9 8 10 namespace tools { 9 namespace tools { 11 10 12 inline void suffix(const std::string& a_string 11 inline void suffix(const std::string& a_string,std::string& a_value,bool a_back = true) { 13 // If a_string = dir0/dir1/dir2/dir3/name.xx 12 // If a_string = dir0/dir1/dir2/dir3/name.xxx 14 // a_value = xxx 13 // a_value = xxx 15 std::string::size_type pos = a_back?a_string 14 std::string::size_type pos = a_back?a_string.rfind('.'):a_string.find('.'); 16 if(pos==std::string::npos) {a_value.clear(); 15 if(pos==std::string::npos) {a_value.clear();return;} 17 pos++; 16 pos++; 18 a_value = a_string.substr(pos,a_string.size( 17 a_value = a_string.substr(pos,a_string.size()-pos); 19 } 18 } 20 19 21 inline void nosuffix(const std::string& a_stri 20 inline void nosuffix(const std::string& a_string,std::string& a_value,bool a_back = true){ 22 // If a_string = dir0/dir1/dir2/dir3/name.xx 21 // If a_string = dir0/dir1/dir2/dir3/name.xxx 23 // a_value = name 22 // a_value = name 24 // Start searching after the last / (or last 23 // Start searching after the last / (or last \ for Windows). 25 std::string::size_type pos = a_string.rfind( 24 std::string::size_type pos = a_string.rfind('/'); 26 if(pos==std::string::npos) pos = a_string.rf 25 if(pos==std::string::npos) pos = a_string.rfind('\\'); 27 if(pos==std::string::npos) pos = 0; 26 if(pos==std::string::npos) pos = 0; 28 else pos++; 27 else pos++; 29 std::string _s = a_string.substr(pos,a_strin 28 std::string _s = a_string.substr(pos,a_string.size()-pos); 30 std::string::size_type dot_pos = a_back?_s.r 29 std::string::size_type dot_pos = a_back?_s.rfind('.'):_s.find('.'); 31 if(dot_pos==std::string::npos) { 30 if(dot_pos==std::string::npos) { 32 a_value = std::move(_s); << 31 a_value = _s; 33 } else { 32 } else { 34 a_value = _s.substr(0,dot_pos); 33 a_value = _s.substr(0,dot_pos); 35 } 34 } 36 } 35 } 37 36 38 inline void path_no_suffix(const std::string& 37 inline void path_no_suffix(const std::string& a_string,std::string& a_value){ 39 // If a_string = dir0/dir1/dir2/dir3/name.xx 38 // If a_string = dir0/dir1/dir2/dir3/name.xxx 40 // a_value = dir0/dir1/dir2/dir3/name 39 // a_value = dir0/dir1/dir2/dir3/name 41 std::string::size_type dot_pos = a_string.rf 40 std::string::size_type dot_pos = a_string.rfind('.'); 42 if(dot_pos==std::string::npos) { 41 if(dot_pos==std::string::npos) { 43 a_value = a_string; 42 a_value = a_string; 44 } else { 43 } else { 45 a_value = a_string.substr(0,dot_pos); 44 a_value = a_string.substr(0,dot_pos); 46 } 45 } 47 } 46 } 48 47 49 inline bool has_dir(const std::string& a_strin 48 inline bool has_dir(const std::string& a_string){ 50 if(a_string.rfind('/')!=std::string::npos) r 49 if(a_string.rfind('/')!=std::string::npos) return true; 51 if(a_string.rfind('\\')!=std::string::npos) 50 if(a_string.rfind('\\')!=std::string::npos) return true; 52 return false; 51 return false; 53 } 52 } 54 53 55 inline void base_name(const std::string& a_pat 54 inline void base_name(const std::string& a_path,std::string& a_value) { 56 std::string::size_type pos_slash = a_path.rf 55 std::string::size_type pos_slash = a_path.rfind('/'); 57 std::string::size_type pos_bslash = a_path.r 56 std::string::size_type pos_bslash = a_path.rfind('\\'); 58 std::string::size_type pos = 0; 57 std::string::size_type pos = 0; 59 if(pos_slash==std::string::npos) { 58 if(pos_slash==std::string::npos) { 60 if(pos_bslash==std::string::npos) { 59 if(pos_bslash==std::string::npos) { 61 pos = std::string::npos; 60 pos = std::string::npos; 62 } else { 61 } else { 63 pos = pos_bslash; 62 pos = pos_bslash; 64 } 63 } 65 } else { 64 } else { 66 if(pos_bslash==std::string::npos) { 65 if(pos_bslash==std::string::npos) { 67 pos = pos_slash; 66 pos = pos_slash; 68 } else { 67 } else { 69 if(pos_slash<=pos_bslash) { 68 if(pos_slash<=pos_bslash) { 70 pos = pos_bslash; 69 pos = pos_bslash; 71 } else { 70 } else { 72 pos = pos_slash; 71 pos = pos_slash; 73 } 72 } 74 } 73 } 75 } 74 } 76 if(pos==std::string::npos) {a_value = a_path 75 if(pos==std::string::npos) {a_value = a_path;return;} 77 pos++; 76 pos++; 78 a_value = a_path.substr(pos,a_path.size()-po 77 a_value = a_path.substr(pos,a_path.size()-pos); 79 } 78 } 80 79 81 //inline bool first_word(const std::string& a_ 80 //inline bool first_word(const std::string& a_string,std::string& a_value) { 82 // if(a_string.empty()) {a_value.clear();retu 81 // if(a_string.empty()) {a_value.clear();return false;} 83 // std::string::size_type pos = a_string.find 82 // std::string::size_type pos = a_string.find(' '); 84 // if(pos==std::string::npos) {a_value = a_st 83 // if(pos==std::string::npos) {a_value = a_string;return true;} 85 // a_value = a_string.substr(0,pos); 84 // a_value = a_string.substr(0,pos); 86 // return true; 85 // return true; 87 //} 86 //} 88 87 89 inline std::string suffix(const std::string& a 88 inline std::string suffix(const std::string& a_string,bool a_back = true) { 90 std::string value; 89 std::string value; 91 suffix(a_string,value,a_back); 90 suffix(a_string,value,a_back); 92 return value; 91 return value; 93 } 92 } 94 93 95 inline std::string nosuffix(const std::string& 94 inline std::string nosuffix(const std::string& a_string,bool a_back = true){ 96 std::string value; 95 std::string value; 97 nosuffix(a_string,value,a_back); 96 nosuffix(a_string,value,a_back); 98 return value; 97 return value; 99 } 98 } 100 99 101 inline std::string base_name(const std::string 100 inline std::string base_name(const std::string& a_path) { //deprecated. 102 std::string value; 101 std::string value; 103 base_name(a_path,value); 102 base_name(a_path,value); 104 return value; 103 return value; 105 } 104 } 106 105 107 inline bool is_absolute_path(const std::string 106 inline bool is_absolute_path(const std::string& a_path) { 108 if(a_path.find('\\')!=std::string::npos) { / 107 if(a_path.find('\\')!=std::string::npos) { //Windows path. 109 if(a_path.find(':')!=std::string::npos) re 108 if(a_path.find(':')!=std::string::npos) return true; 110 return (a_path.size()&&(a_path[0]=='\\')?t 109 return (a_path.size()&&(a_path[0]=='\\')?true:false); 111 } else { //UNIX path 110 } else { //UNIX path 112 return (a_path.size()&&(a_path[0]=='/')?tr 111 return (a_path.size()&&(a_path[0]=='/')?true:false); 113 } 112 } 114 } 113 } 115 114 116 inline bool path_name_suffix(const std::string 115 inline bool path_name_suffix(const std::string& a_string,std::string& a_path,std::string& a_name,std::string& a_suffix){ 117 // If a_string = dir0/dir1/dir2/dir3/name.xx 116 // If a_string = dir0/dir1/dir2/dir3/name.xxx 118 // a_path = dir0/dir1/dir2/dir3 117 // a_path = dir0/dir1/dir2/dir3 119 // a_name = name.xxx 118 // a_name = name.xxx 120 // a_suffix = xxx 119 // a_suffix = xxx 121 // If a_string = dir0/name.xxx 120 // If a_string = dir0/name.xxx 122 // a_path = dir0 121 // a_path = dir0 123 // a_name = name.xxx 122 // a_name = name.xxx 124 // a_suffix = xxx 123 // a_suffix = xxx 125 // If a_string = name.xxx 124 // If a_string = name.xxx 126 // a_path.clear() 125 // a_path.clear() 127 // a_name = name.xxx 126 // a_name = name.xxx 128 // a_suffix = xxx 127 // a_suffix = xxx 129 // If a_string = /name.xxx 128 // If a_string = /name.xxx 130 // a_path = "/" 129 // a_path = "/" 131 // a_name = name.xxx 130 // a_name = name.xxx 132 // a_suffix = xxx 131 // a_suffix = xxx 133 // If a_string = . 132 // If a_string = . 134 // a_path = "." 133 // a_path = "." 135 // a_name.clear() 134 // a_name.clear() 136 // a_suffix.clear() 135 // a_suffix.clear() 137 // If a_string = .. 136 // If a_string = .. 138 // a_path = ".." 137 // a_path = ".." 139 // a_name.clear() 138 // a_name.clear() 140 // a_suffix.clear() 139 // a_suffix.clear() 141 // If a_string = dir0/dir1/dir2/dir3/ 140 // If a_string = dir0/dir1/dir2/dir3/ 142 // a_path = dir0/dir1/dir2/dir3 141 // a_path = dir0/dir1/dir2/dir3 143 // a_name.clear() 142 // a_name.clear() 144 // a_suffix.clear() 143 // a_suffix.clear() 145 // If a_string = dir0/dir1/dir2/dir3/. 144 // If a_string = dir0/dir1/dir2/dir3/. 146 // a_path = dir0/dir1/dir2/dir3 145 // a_path = dir0/dir1/dir2/dir3 147 // a_name = "." 146 // a_name = "." 148 // a_suffix.clear() 147 // a_suffix.clear() 149 // If a_string = dir0/dir1/dir2/dir3/.. 148 // If a_string = dir0/dir1/dir2/dir3/.. 150 // a_path = dir0/dir1/dir2/dir3 149 // a_path = dir0/dir1/dir2/dir3 151 // a_name = ".." 150 // a_name = ".." 152 // a_suffix.clear() 151 // a_suffix.clear() 153 if(a_string==".") { 152 if(a_string==".") { 154 a_path = "."; 153 a_path = "."; 155 a_name.clear(); 154 a_name.clear(); 156 a_suffix.clear(); 155 a_suffix.clear(); 157 return true; 156 return true; 158 } else if(a_string=="..") { 157 } else if(a_string=="..") { 159 a_path = ".."; 158 a_path = ".."; 160 a_name.clear(); 159 a_name.clear(); 161 a_suffix.clear(); 160 a_suffix.clear(); 162 return true; 161 return true; 163 } 162 } 164 163 165 std::string::size_type pos_slash = a_string. 164 std::string::size_type pos_slash = a_string.rfind('/'); 166 std::string::size_type pos_bslash = a_string 165 std::string::size_type pos_bslash = a_string.rfind('\\'); 167 std::string::size_type pos = 0; 166 std::string::size_type pos = 0; 168 if(pos_slash==std::string::npos) { 167 if(pos_slash==std::string::npos) { 169 if(pos_bslash==std::string::npos) { 168 if(pos_bslash==std::string::npos) { 170 pos = std::string::npos; 169 pos = std::string::npos; 171 } else { 170 } else { 172 pos = pos_bslash; 171 pos = pos_bslash; 173 } 172 } 174 } else { 173 } else { 175 if(pos_bslash==std::string::npos) { 174 if(pos_bslash==std::string::npos) { 176 pos = pos_slash; 175 pos = pos_slash; 177 } else { 176 } else { 178 if(pos_slash<=pos_bslash) { 177 if(pos_slash<=pos_bslash) { 179 pos = pos_bslash; 178 pos = pos_bslash; 180 } else { 179 } else { 181 pos = pos_slash; 180 pos = pos_slash; 182 } 181 } 183 } 182 } 184 } 183 } 185 184 186 if(pos==std::string::npos) { 185 if(pos==std::string::npos) { 187 a_path.clear(); 186 a_path.clear(); 188 pos = 0; 187 pos = 0; 189 } else if(pos==0) { 188 } else if(pos==0) { 190 a_path = "/"; 189 a_path = "/"; 191 pos++; 190 pos++; 192 } else { 191 } else { 193 a_path = a_string.substr(0,pos); 192 a_path = a_string.substr(0,pos); 194 pos++; 193 pos++; 195 } 194 } 196 std::string _s = a_string.substr(pos,a_strin 195 std::string _s = a_string.substr(pos,a_string.size()-pos); 197 pos = _s.rfind('.'); 196 pos = _s.rfind('.'); 198 if(pos==std::string::npos) { 197 if(pos==std::string::npos) { 199 a_name = _s; 198 a_name = _s; 200 a_suffix.clear(); 199 a_suffix.clear(); 201 } else { 200 } else { 202 a_name = _s; 201 a_name = _s; 203 pos++; 202 pos++; 204 a_suffix = _s.substr(pos,_s.size()-pos); 203 a_suffix = _s.substr(pos,_s.size()-pos); 205 } 204 } 206 return true; 205 return true; 207 } 206 } 208 207 209 inline std::string dir_name(const std::string& 208 inline std::string dir_name(const std::string& a_path,unsigned int a_num = 1){ 210 std::string path = a_path; 209 std::string path = a_path; 211 for(unsigned int index=0;index<a_num;index++ 210 for(unsigned int index=0;index<a_num;index++) { 212 std::string p,n,_s; 211 std::string p,n,_s; 213 path_name_suffix(path,p,n,_s); 212 path_name_suffix(path,p,n,_s); 214 path = p; 213 path = p; 215 } 214 } 216 return path; 215 return path; 217 } 216 } 218 217 219 inline void quote(std::string& a_path) { 218 inline void quote(std::string& a_path) { 220 if(a_path.find(' ')==std::string::npos) retu 219 if(a_path.find(' ')==std::string::npos) return; 221 // path with spaces : 220 // path with spaces : 222 if(a_path[0]=='"') return; //Already in doub 221 if(a_path[0]=='"') return; //Already in double quote. 223 a_path = std::string("\"")+a_path+"\""; 222 a_path = std::string("\"")+a_path+"\""; 224 } 223 } 225 224 226 //used in OpenPAW, BatchLab. 225 //used in OpenPAW, BatchLab. 227 inline bool is_f77(const std::string& a_path){ 226 inline bool is_f77(const std::string& a_path){ 228 std::string sfx = suffix(a_path); 227 std::string sfx = suffix(a_path); 229 228 230 //tolowercase(sfx); 229 //tolowercase(sfx); 231 for(std::string::iterator it=sfx.begin();it! 230 for(std::string::iterator it=sfx.begin();it!=sfx.end();++it) { 232 char c = *it; 231 char c = *it; 233 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' 232 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' + 'a' : c); 234 } 233 } 235 234 236 if(sfx=="f") return true; //the standa 235 if(sfx=="f") return true; //the standard. 237 if(sfx=="for") return true; //for opaw. 236 if(sfx=="for") return true; //for opaw. Known by g77. 238 if(sfx=="ftn") return true; //for opaw. 237 if(sfx=="ftn") return true; //for opaw. 239 if(sfx=="fortran") return true; //for opaw. 238 if(sfx=="fortran") return true; //for opaw. 240 if(sfx=="f77") return true; 239 if(sfx=="f77") return true; 241 return false; 240 return false; 242 } 241 } 243 242 244 //used in OpenPAW, BatchLab. 243 //used in OpenPAW, BatchLab. 245 inline bool is_cpp(const std::string& a_path){ 244 inline bool is_cpp(const std::string& a_path){ 246 std::string sfx = suffix(a_path); 245 std::string sfx = suffix(a_path); 247 246 248 //tolowercase(sfx); 247 //tolowercase(sfx); 249 for(std::string::iterator it=sfx.begin();it! 248 for(std::string::iterator it=sfx.begin();it!=sfx.end();++it) { 250 char c = *it; 249 char c = *it; 251 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' 250 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' + 'a' : c); 252 } 251 } 253 252 254 if(sfx=="c") return true; 253 if(sfx=="c") return true; 255 if(sfx=="cxx") return true; 254 if(sfx=="cxx") return true; 256 if(sfx=="cpp") return true; 255 if(sfx=="cpp") return true; 257 if(sfx=="C") return true; 256 if(sfx=="C") return true; 258 return false; 257 return false; 259 } 258 } 260 259 261 //used in OpenPAW, BatchLab. 260 //used in OpenPAW, BatchLab. 262 inline bool is_python(const std::string& a_pat 261 inline bool is_python(const std::string& a_path){ 263 std::string sfx = suffix(a_path); 262 std::string sfx = suffix(a_path); 264 263 265 //tolowercase(sfx); 264 //tolowercase(sfx); 266 for(std::string::iterator it=sfx.begin();it! 265 for(std::string::iterator it=sfx.begin();it!=sfx.end();++it) { 267 char c = *it; 266 char c = *it; 268 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' 267 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' + 'a' : c); 269 } 268 } 270 269 271 if(sfx=="py") return true; 270 if(sfx=="py") return true; 272 return false; 271 return false; 273 } 272 } 274 273 275 } 274 } 276 275 277 #include <sstream> 276 #include <sstream> 278 277 279 namespace tools { 278 namespace tools { 280 279 281 inline bool url_parse(const std::string& a_url 280 inline bool url_parse(const std::string& a_url,std::string& a_host,unsigned int& a_port,std::string& a_path) { 282 std::string _s; 281 std::string _s; 283 if((a_url.size()>=7)&&(a_url.substr(0,7)=="h 282 if((a_url.size()>=7)&&(a_url.substr(0,7)=="http://")) { 284 _s = a_url.substr(7,a_url.size()-7); 283 _s = a_url.substr(7,a_url.size()-7); 285 } else if((a_url.size()>=8)&&(a_url.substr(0 284 } else if((a_url.size()>=8)&&(a_url.substr(0,8)=="https://")) { 286 _s = a_url.substr(8,a_url.size()-8); 285 _s = a_url.substr(8,a_url.size()-8); 287 } else { 286 } else { 288 a_host.clear(); 287 a_host.clear(); 289 a_port = 0; 288 a_port = 0; 290 a_path.clear(); 289 a_path.clear(); 291 return false; 290 return false; 292 } 291 } 293 292 294 {std::string::size_type pos = _s.find('/'); 293 {std::string::size_type pos = _s.find('/'); 295 if(pos==std::string::npos) { 294 if(pos==std::string::npos) { 296 a_host = _s; 295 a_host = _s; 297 a_path = "/"; 296 a_path = "/"; 298 } else { 297 } else { 299 a_host = _s.substr(0,pos); 298 a_host = _s.substr(0,pos); 300 a_path = _s.substr(pos,_s.size()-pos); 299 a_path = _s.substr(pos,_s.size()-pos); 301 }} 300 }} 302 301 303 {std::string::size_type pos = a_host.find(':' 302 {std::string::size_type pos = a_host.find(':'); 304 if(pos==std::string::npos) { 303 if(pos==std::string::npos) { 305 a_port = 0; 304 a_port = 0; 306 } else { 305 } else { 307 std::string sport = a_host.substr(pos+1,a_ 306 std::string sport = a_host.substr(pos+1,a_host.size()-(pos+1)); 308 std::istringstream strm(sport.c_str()); 307 std::istringstream strm(sport.c_str()); 309 strm >> a_port; 308 strm >> a_port; 310 if(strm.fail()) { 309 if(strm.fail()) { 311 a_host.clear(); 310 a_host.clear(); 312 a_port = 0; 311 a_port = 0; 313 a_path.clear(); 312 a_path.clear(); 314 return false; 313 return false; 315 } 314 } 316 a_host = a_host.substr(0,pos); 315 a_host = a_host.substr(0,pos); 317 }} 316 }} 318 317 319 return true; 318 return true; 320 } 319 } 321 320 322 } 321 } 323 322 324 #endif 323 #endif