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