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.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  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,std::string& a_value,bool a_back = true) {
 13   // If a_string = dir0/dir1/dir2/dir3/name.xxx
 14   //   a_value = xxx
 15   std::string::size_type pos = a_back?a_string.rfind('.'):a_string.find('.');
 16   if(pos==std::string::npos) {a_value.clear();return;}
 17   pos++;
 18   a_value = a_string.substr(pos,a_string.size()-pos);
 19 }
 20 
 21 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.xxx
 23   //   a_value = name
 24   // Start searching after the last / (or last \ for Windows).
 25   std::string::size_type pos = a_string.rfind('/');
 26   if(pos==std::string::npos) pos = a_string.rfind('\\');
 27   if(pos==std::string::npos) pos = 0;
 28   else pos++;
 29   std::string _s = a_string.substr(pos,a_string.size()-pos);
 30   std::string::size_type dot_pos = a_back?_s.rfind('.'):_s.find('.');
 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& a_string,std::string& a_value){
 39   // If a_string = dir0/dir1/dir2/dir3/name.xxx
 40   //   a_value = dir0/dir1/dir2/dir3/name
 41   std::string::size_type dot_pos = a_string.rfind('.');
 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_string){
 50   if(a_string.rfind('/')!=std::string::npos) return true;
 51   if(a_string.rfind('\\')!=std::string::npos) return true;
 52   return false;
 53 }
 54 
 55 inline void base_name(const std::string& a_path,std::string& a_value) {
 56   std::string::size_type pos_slash = a_path.rfind('/');
 57   std::string::size_type pos_bslash = a_path.rfind('\\');
 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;return;}
 77   pos++;
 78   a_value = a_path.substr(pos,a_path.size()-pos);
 79 }
 80 
 81 //inline bool first_word(const std::string& a_string,std::string& a_value) {
 82 //  if(a_string.empty()) {a_value.clear();return false;}
 83 //  std::string::size_type pos = a_string.find(' ');
 84 //  if(pos==std::string::npos) {a_value = a_string;return true;}
 85 //  a_value = a_string.substr(0,pos);
 86 //  return true;
 87 //}
 88 
 89 inline std::string suffix(const std::string& a_string,bool a_back = true) {
 90   std::string value;
 91   suffix(a_string,value,a_back);
 92   return value;
 93 }
 94 
 95 inline std::string nosuffix(const std::string& a_string,bool a_back = true){
 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& a_path) { //deprecated.
102   std::string value;
103   base_name(a_path,value);
104   return value;
105 }
106 
107 inline bool is_absolute_path(const std::string& a_path) {
108   if(a_path.find('\\')!=std::string::npos) { //Windows path.
109     if(a_path.find(':')!=std::string::npos) return true;
110     return (a_path.size()&&(a_path[0]=='\\')?true:false);
111   } else { //UNIX path
112     return (a_path.size()&&(a_path[0]=='/')?true:false);
113   }
114 }
115 
116 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.xxx
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.rfind('/');
166   std::string::size_type pos_bslash = a_string.rfind('\\');
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_string.size()-pos);
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& a_path,unsigned int a_num = 1){
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) return;
221   // path with spaces :
222   if(a_path[0]=='"') return; //Already in double quote.
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!=sfx.end();++it) {
232     char c = *it;
233     *it = ((c) >= 'A' && (c) <= 'Z' ?  c - 'A' + 'a' : c);
234   }
235 
236   if(sfx=="f") return true;       //the standard.
237   if(sfx=="for") return true;     //for opaw. Known by g77.
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!=sfx.end();++it) {
250     char c = *it;
251     *it = ((c) >= 'A' && (c) <= 'Z' ?  c - 'A' + 'a' : c);
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_path){
263   std::string sfx = suffix(a_path);
264 
265   //tolowercase(sfx);
266   for(std::string::iterator it=sfx.begin();it!=sfx.end();++it) {
267     char c = *it;
268     *it = ((c) >= 'A' && (c) <= 'Z' ?  c - 'A' + 'a' : c);
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,std::string& a_host,unsigned int& a_port,std::string& a_path) {
282   std::string _s;
283   if((a_url.size()>=7)&&(a_url.substr(0,7)=="http://")) {
284     _s = a_url.substr(7,a_url.size()-7);
285   } else if((a_url.size()>=8)&&(a_url.substr(0,8)=="https://")) {
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_host.size()-(pos+1));
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