/[svn]/libgig/trunk/src/helper.h
ViewVC logotype

Diff of /libgig/trunk/src/helper.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3482 by schoenebeck, Fri Feb 22 13:19:55 2019 UTC revision 3483 by schoenebeck, Sat Feb 23 15:40:22 2019 UTC
# Line 317  inline void __divide_progress(RIFF::prog Line 317  inline void __divide_progress(RIFF::prog
317      }      }
318  }  }
319    
320  #ifdef _WIN32  /// Removes one or more consecutive occurences of @a needle from the end of @a haystack.
321  # define NATIVE_PATH_SEPARATOR '\\'  inline std::string strip2ndFromEndOf1st(const std::string haystack, char needle) {
322  #else      if (haystack.empty()) return haystack;
323  # define NATIVE_PATH_SEPARATOR '/'      if (*haystack.rbegin() != needle) return haystack;
324        for (int i = haystack.length() - 1; i >= 0; --i)
325            if (haystack[i] != needle)
326                return haystack.substr(0, i+1);
327        return "";
328    }
329    
330    #ifndef NATIVE_PATH_SEPARATOR
331    # ifdef _WIN32
332    #  define NATIVE_PATH_SEPARATOR '\\'
333    # else
334    #  define NATIVE_PATH_SEPARATOR '/'
335    # endif
336  #endif  #endif
337    
338  /**  /**
# Line 328  inline void __divide_progress(RIFF::prog Line 340  inline void __divide_progress(RIFF::prog
340   * passing "/some/path" would return "/some".   * passing "/some/path" would return "/some".
341   */   */
342  inline std::string parentPath(const std::string path) {  inline std::string parentPath(const std::string path) {
343      std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR);      if (path.empty()) return path;
344      return (pos == std::string::npos) ? path : path.substr(0, pos);      std::string s = strip2ndFromEndOf1st(path, NATIVE_PATH_SEPARATOR);
345        printf("\tstrip('%s')  =>  '%s'\n", path.c_str(), s.c_str());
346        if (s.empty()) {
347            s.push_back(NATIVE_PATH_SEPARATOR); // i.e. return "/"
348            return s;
349        }
350        #if defined(_WIN32)
351        if (s.length() == 2 && s[1] == ':')
352            return s;
353        #endif
354        std::size_t pos = s.find_last_of(NATIVE_PATH_SEPARATOR);
355        if (pos == std::string::npos) return "";
356        if (pos == 0) {
357            s = "";
358            s.push_back(NATIVE_PATH_SEPARATOR); // i.e. return "/"
359            return s;
360        }
361        return s.substr(0, pos);
362  }  }
363    
364  /**  /**
# Line 337  inline std::string parentPath(const std: Line 366  inline std::string parentPath(const std:
366   * "/some/path" would return "path".   * "/some/path" would return "path".
367   */   */
368  inline std::string lastPathComponent(const std::string path) {  inline std::string lastPathComponent(const std::string path) {
369        #if defined(_WIN32)
370        if (path.length() == 2 && path[1] == ':')
371            return "";
372        #endif
373      std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR);      std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR);
374      return (pos == std::string::npos) ? path : path.substr(pos+1);      return (pos == std::string::npos) ? path : path.substr(pos+1);
375  }  }
# Line 348  inline std::string lastPathComponent(con Line 381  inline std::string lastPathComponent(con
381  inline std::string pathWithoutExtension(const std::string path) {  inline std::string pathWithoutExtension(const std::string path) {
382      std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR);      std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR);
383      std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1;      std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1;
384      std::size_t posDot = path.find_last_of(".", posBase);      std::size_t posDot = path.find_last_of(".");
385      return (posDot != std::string::npos && posDot > posBase)      return (posDot != std::string::npos && posDot > posBase)
386             ? path.substr(0, posDot) : path;             ? path.substr(0, posDot) : path;
387  }  }
# Line 360  inline std::string pathWithoutExtension( Line 393  inline std::string pathWithoutExtension(
393  inline std::string extensionOfPath(const std::string path) {  inline std::string extensionOfPath(const std::string path) {
394      std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR);      std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR);
395      std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1;      std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1;
396      std::size_t posDot = path.find_last_of(".", posBase);      std::size_t posDot = path.find_last_of(".");
397      return (posDot != std::string::npos && posDot > posBase)      return (posDot != std::string::npos && posDot > posBase)
398             ? path.substr(posDot+1) : "";             ? path.substr(posDot+1) : "";
399  }  }

Legend:
Removed from v.3482  
changed lines
  Added in v.3483

  ViewVC Help
Powered by ViewVC