inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/path
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_path
00005 #define inlib_path
00006 
00007 #include <string>
00008 
00009 namespace inlib {
00010 
00011 inline std::string suffix(const std::string& a_string,bool a_back = true) {
00012   // If a_string = dir0/dir1/dir2/dir3/name.xxx
00013   //   return xxx
00014   std::string::size_type pos = a_back?a_string.rfind('.'):a_string.find('.');
00015   if(pos==std::string::npos) return "";
00016   pos++;
00017   return a_string.substr(pos,a_string.size()-pos);
00018 }
00019 
00020 inline std::string nosuffix(const std::string& a_string,bool a_back = true){
00021   // If a_string = dir0/dir1/dir2/dir3/name.xxx
00022   //   return name
00023   // Start searching after the last / (or last \ for Windows).
00024   std::string::size_type pos = a_string.rfind('/');
00025   if(pos==std::string::npos) pos = a_string.rfind('\\');
00026   if(pos==std::string::npos) pos = 0;
00027   else pos++;
00028   std::string s = a_string.substr(pos,a_string.size()-pos);
00029   std::string::size_type dot_pos = a_back?s.rfind('.'):s.find('.');
00030   if(dot_pos==std::string::npos) return s;
00031   return s.substr(0,dot_pos);
00032 }
00033 
00034 inline std::string base_name(const std::string& a_path) {
00035   std::string::size_type pos_slash = a_path.rfind('/');
00036   std::string::size_type pos_bslash = a_path.rfind('\\');
00037   std::string::size_type pos = 0;
00038   if(pos_slash==std::string::npos) {
00039     if(pos_bslash==std::string::npos) {
00040       pos = std::string::npos;
00041     } else {
00042       pos = pos_bslash;
00043     }
00044   } else {
00045     if(pos_bslash==std::string::npos) {
00046       pos = pos_slash;
00047     } else {
00048       if(pos_slash<=pos_bslash) {
00049         pos = pos_bslash;
00050       } else {
00051         pos = pos_slash;
00052       }
00053     }
00054   }
00055   if(pos==std::string::npos) return a_path;
00056   pos++;
00057   return a_path.substr(pos,a_path.size()-pos);
00058 }
00059 
00060 inline bool is_absolute_path(const std::string& a_path) {
00061   if(a_path.find('\\')!=std::string::npos) { //Windows path.
00062     if(a_path.find(':')!=std::string::npos) return true;
00063     return (a_path.size()&&(a_path[0]=='\\')?true:false);
00064   } else { //UNIX path
00065     return (a_path.size()&&(a_path[0]=='/')?true:false);
00066   }
00067 }
00068 
00069 inline bool path_name_suffix(
00070  const std::string& a_string
00071 ,std::string& a_path
00072 ,std::string& a_name
00073 ,std::string& a_suffix
00074 ){
00075   // If a_string = dir0/dir1/dir2/dir3/name.xxx
00076   //   a_path = dir0/dir1/dir2/dir3
00077   //   a_name = name.xxx
00078   //   a_suffix = xxx
00079   // If a_string = dir0/name.xxx
00080   //   a_path = dir0
00081   //   a_name = name.xxx
00082   //   a_suffix = xxx
00083   // If a_string = name.xxx
00084   //   a_path.clear()
00085   //   a_name = name.xxx
00086   //   a_suffix = xxx
00087   // If a_string = /name.xxx
00088   //   a_path = "/"
00089   //   a_name = name.xxx
00090   //   a_suffix = xxx
00091   // If a_string = .
00092   //   a_path = "."
00093   //   a_name.clear()
00094   //   a_suffix.clear()
00095   // If a_string = ..
00096   //   a_path = ".."
00097   //   a_name.clear()
00098   //   a_suffix.clear()
00099   if(a_string==".") {
00100     a_path = ".";
00101     a_name.clear();
00102     a_suffix.clear();
00103     return true;
00104   } else if(a_string=="..") {
00105     a_path = "..";
00106     a_name.clear();
00107     a_suffix.clear();
00108     return true;
00109   }
00110 
00111   std::string::size_type pos_slash = a_string.rfind('/');
00112   std::string::size_type pos_bslash = a_string.rfind('\\');
00113   std::string::size_type pos = 0;
00114   if(pos_slash==std::string::npos) {
00115     if(pos_bslash==std::string::npos) {
00116       pos = std::string::npos;
00117     } else {
00118       pos = pos_bslash;
00119     }
00120   } else {
00121     if(pos_bslash==std::string::npos) {
00122       pos = pos_slash;
00123     } else {
00124       if(pos_slash<=pos_bslash) {
00125         pos = pos_bslash;
00126       } else {
00127         pos = pos_slash;
00128       }
00129     }
00130   }
00131 
00132   if(pos==std::string::npos) {
00133     a_path.clear();
00134     pos = 0;
00135   } else if(pos==0) {
00136     a_path = "/";
00137     pos++;
00138   } else {
00139     a_path = a_string.substr(0,pos);
00140     pos++;
00141   }
00142   std::string s = a_string.substr(pos,a_string.size()-pos);
00143   pos = s.rfind('.');
00144   if(pos==std::string::npos) {
00145     a_name = s;
00146     a_suffix.clear();
00147   } else {
00148     a_name = s;
00149     pos++;
00150     a_suffix = s.substr(pos,s.size()-pos);
00151   }
00152   return true;
00153 }
00154 
00155 inline std::string dir_name(const std::string& a_path,unsigned int a_num = 1){
00156   std::string path = a_path;
00157   for(unsigned int index=0;index<a_num;index++) {
00158     std::string p,n,s;
00159     path_name_suffix(path,p,n,s);
00160     path = p;
00161   }
00162   return path;
00163 }
00164 
00165 }
00166 
00167 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines