Warning, /include/Geant4/tools/path is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_path
0005 #define tools_path
0006
0007 #include <string>
0008 #include <utility>
0009
0010 namespace tools {
0011
0012 inline void suffix(const std::string& a_string,std::string& a_value,bool a_back = true) {
0013 // If a_string = dir0/dir1/dir2/dir3/name.xxx
0014 // a_value = xxx
0015 std::string::size_type pos = a_back?a_string.rfind('.'):a_string.find('.');
0016 if(pos==std::string::npos) {a_value.clear();return;}
0017 pos++;
0018 a_value = a_string.substr(pos,a_string.size()-pos);
0019 }
0020
0021 inline void nosuffix(const std::string& a_string,std::string& a_value,bool a_back = true){
0022 // If a_string = dir0/dir1/dir2/dir3/name.xxx
0023 // a_value = name
0024 // Start searching after the last / (or last \ for Windows).
0025 std::string::size_type pos = a_string.rfind('/');
0026 if(pos==std::string::npos) pos = a_string.rfind('\\');
0027 if(pos==std::string::npos) pos = 0;
0028 else pos++;
0029 std::string _s = a_string.substr(pos,a_string.size()-pos);
0030 std::string::size_type dot_pos = a_back?_s.rfind('.'):_s.find('.');
0031 if(dot_pos==std::string::npos) {
0032 a_value = std::move(_s);
0033 } else {
0034 a_value = _s.substr(0,dot_pos);
0035 }
0036 }
0037
0038 inline void path_no_suffix(const std::string& a_string,std::string& a_value){
0039 // If a_string = dir0/dir1/dir2/dir3/name.xxx
0040 // a_value = dir0/dir1/dir2/dir3/name
0041 std::string::size_type dot_pos = a_string.rfind('.');
0042 if(dot_pos==std::string::npos) {
0043 a_value = a_string;
0044 } else {
0045 a_value = a_string.substr(0,dot_pos);
0046 }
0047 }
0048
0049 inline bool has_dir(const std::string& a_string){
0050 if(a_string.rfind('/')!=std::string::npos) return true;
0051 if(a_string.rfind('\\')!=std::string::npos) return true;
0052 return false;
0053 }
0054
0055 inline void base_name(const std::string& a_path,std::string& a_value) {
0056 std::string::size_type pos_slash = a_path.rfind('/');
0057 std::string::size_type pos_bslash = a_path.rfind('\\');
0058 std::string::size_type pos = 0;
0059 if(pos_slash==std::string::npos) {
0060 if(pos_bslash==std::string::npos) {
0061 pos = std::string::npos;
0062 } else {
0063 pos = pos_bslash;
0064 }
0065 } else {
0066 if(pos_bslash==std::string::npos) {
0067 pos = pos_slash;
0068 } else {
0069 if(pos_slash<=pos_bslash) {
0070 pos = pos_bslash;
0071 } else {
0072 pos = pos_slash;
0073 }
0074 }
0075 }
0076 if(pos==std::string::npos) {a_value = a_path;return;}
0077 pos++;
0078 a_value = a_path.substr(pos,a_path.size()-pos);
0079 }
0080
0081 //inline bool first_word(const std::string& a_string,std::string& a_value) {
0082 // if(a_string.empty()) {a_value.clear();return false;}
0083 // std::string::size_type pos = a_string.find(' ');
0084 // if(pos==std::string::npos) {a_value = a_string;return true;}
0085 // a_value = a_string.substr(0,pos);
0086 // return true;
0087 //}
0088
0089 inline std::string suffix(const std::string& a_string,bool a_back = true) {
0090 std::string value;
0091 suffix(a_string,value,a_back);
0092 return value;
0093 }
0094
0095 inline std::string nosuffix(const std::string& a_string,bool a_back = true){
0096 std::string value;
0097 nosuffix(a_string,value,a_back);
0098 return value;
0099 }
0100
0101 inline std::string base_name(const std::string& a_path) { //deprecated.
0102 std::string value;
0103 base_name(a_path,value);
0104 return value;
0105 }
0106
0107 inline bool is_absolute_path(const std::string& a_path) {
0108 if(a_path.find('\\')!=std::string::npos) { //Windows path.
0109 if(a_path.find(':')!=std::string::npos) return true;
0110 return (a_path.size()&&(a_path[0]=='\\')?true:false);
0111 } else { //UNIX path
0112 return (a_path.size()&&(a_path[0]=='/')?true:false);
0113 }
0114 }
0115
0116 inline bool path_name_suffix(const std::string& a_string,std::string& a_path,std::string& a_name,std::string& a_suffix){
0117 // If a_string = dir0/dir1/dir2/dir3/name.xxx
0118 // a_path = dir0/dir1/dir2/dir3
0119 // a_name = name.xxx
0120 // a_suffix = xxx
0121 // If a_string = dir0/name.xxx
0122 // a_path = dir0
0123 // a_name = name.xxx
0124 // a_suffix = xxx
0125 // If a_string = name.xxx
0126 // a_path.clear()
0127 // a_name = name.xxx
0128 // a_suffix = xxx
0129 // If a_string = /name.xxx
0130 // a_path = "/"
0131 // a_name = name.xxx
0132 // a_suffix = xxx
0133 // If a_string = .
0134 // a_path = "."
0135 // a_name.clear()
0136 // a_suffix.clear()
0137 // If a_string = ..
0138 // a_path = ".."
0139 // a_name.clear()
0140 // a_suffix.clear()
0141 // If a_string = dir0/dir1/dir2/dir3/
0142 // a_path = dir0/dir1/dir2/dir3
0143 // a_name.clear()
0144 // a_suffix.clear()
0145 // If a_string = dir0/dir1/dir2/dir3/.
0146 // a_path = dir0/dir1/dir2/dir3
0147 // a_name = "."
0148 // a_suffix.clear()
0149 // If a_string = dir0/dir1/dir2/dir3/..
0150 // a_path = dir0/dir1/dir2/dir3
0151 // a_name = ".."
0152 // a_suffix.clear()
0153 if(a_string==".") {
0154 a_path = ".";
0155 a_name.clear();
0156 a_suffix.clear();
0157 return true;
0158 } else if(a_string=="..") {
0159 a_path = "..";
0160 a_name.clear();
0161 a_suffix.clear();
0162 return true;
0163 }
0164
0165 std::string::size_type pos_slash = a_string.rfind('/');
0166 std::string::size_type pos_bslash = a_string.rfind('\\');
0167 std::string::size_type pos = 0;
0168 if(pos_slash==std::string::npos) {
0169 if(pos_bslash==std::string::npos) {
0170 pos = std::string::npos;
0171 } else {
0172 pos = pos_bslash;
0173 }
0174 } else {
0175 if(pos_bslash==std::string::npos) {
0176 pos = pos_slash;
0177 } else {
0178 if(pos_slash<=pos_bslash) {
0179 pos = pos_bslash;
0180 } else {
0181 pos = pos_slash;
0182 }
0183 }
0184 }
0185
0186 if(pos==std::string::npos) {
0187 a_path.clear();
0188 pos = 0;
0189 } else if(pos==0) {
0190 a_path = "/";
0191 pos++;
0192 } else {
0193 a_path = a_string.substr(0,pos);
0194 pos++;
0195 }
0196 std::string _s = a_string.substr(pos,a_string.size()-pos);
0197 pos = _s.rfind('.');
0198 if(pos==std::string::npos) {
0199 a_name = _s;
0200 a_suffix.clear();
0201 } else {
0202 a_name = _s;
0203 pos++;
0204 a_suffix = _s.substr(pos,_s.size()-pos);
0205 }
0206 return true;
0207 }
0208
0209 inline std::string dir_name(const std::string& a_path,unsigned int a_num = 1){
0210 std::string path = a_path;
0211 for(unsigned int index=0;index<a_num;index++) {
0212 std::string p,n,_s;
0213 path_name_suffix(path,p,n,_s);
0214 path = p;
0215 }
0216 return path;
0217 }
0218
0219 inline void quote(std::string& a_path) {
0220 if(a_path.find(' ')==std::string::npos) return;
0221 // path with spaces :
0222 if(a_path[0]=='"') return; //Already in double quote.
0223 a_path = std::string("\"")+a_path+"\"";
0224 }
0225
0226 //used in OpenPAW, BatchLab.
0227 inline bool is_f77(const std::string& a_path){
0228 std::string sfx = suffix(a_path);
0229
0230 //tolowercase(sfx);
0231 for(std::string::iterator it=sfx.begin();it!=sfx.end();++it) {
0232 char c = *it;
0233 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' + 'a' : c);
0234 }
0235
0236 if(sfx=="f") return true; //the standard.
0237 if(sfx=="for") return true; //for opaw. Known by g77.
0238 if(sfx=="ftn") return true; //for opaw.
0239 if(sfx=="fortran") return true; //for opaw.
0240 if(sfx=="f77") return true;
0241 return false;
0242 }
0243
0244 //used in OpenPAW, BatchLab.
0245 inline bool is_cpp(const std::string& a_path){
0246 std::string sfx = suffix(a_path);
0247
0248 //tolowercase(sfx);
0249 for(std::string::iterator it=sfx.begin();it!=sfx.end();++it) {
0250 char c = *it;
0251 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' + 'a' : c);
0252 }
0253
0254 if(sfx=="c") return true;
0255 if(sfx=="cxx") return true;
0256 if(sfx=="cpp") return true;
0257 if(sfx=="C") return true;
0258 return false;
0259 }
0260
0261 //used in OpenPAW, BatchLab.
0262 inline bool is_python(const std::string& a_path){
0263 std::string sfx = suffix(a_path);
0264
0265 //tolowercase(sfx);
0266 for(std::string::iterator it=sfx.begin();it!=sfx.end();++it) {
0267 char c = *it;
0268 *it = ((c) >= 'A' && (c) <= 'Z' ? c - 'A' + 'a' : c);
0269 }
0270
0271 if(sfx=="py") return true;
0272 return false;
0273 }
0274
0275 }
0276
0277 #include <sstream>
0278
0279 namespace tools {
0280
0281 inline bool url_parse(const std::string& a_url,std::string& a_host,unsigned int& a_port,std::string& a_path) {
0282 std::string _s;
0283 if((a_url.size()>=7)&&(a_url.substr(0,7)=="http://")) {
0284 _s = a_url.substr(7,a_url.size()-7);
0285 } else if((a_url.size()>=8)&&(a_url.substr(0,8)=="https://")) {
0286 _s = a_url.substr(8,a_url.size()-8);
0287 } else {
0288 a_host.clear();
0289 a_port = 0;
0290 a_path.clear();
0291 return false;
0292 }
0293
0294 {std::string::size_type pos = _s.find('/');
0295 if(pos==std::string::npos) {
0296 a_host = _s;
0297 a_path = "/";
0298 } else {
0299 a_host = _s.substr(0,pos);
0300 a_path = _s.substr(pos,_s.size()-pos);
0301 }}
0302
0303 {std::string::size_type pos = a_host.find(':');
0304 if(pos==std::string::npos) {
0305 a_port = 0;
0306 } else {
0307 std::string sport = a_host.substr(pos+1,a_host.size()-(pos+1));
0308 std::istringstream strm(sport.c_str());
0309 strm >> a_port;
0310 if(strm.fail()) {
0311 a_host.clear();
0312 a_port = 0;
0313 a_path.clear();
0314 return false;
0315 }
0316 a_host = a_host.substr(0,pos);
0317 }}
0318
0319 return true;
0320 }
0321
0322 }
0323
0324 #endif