/[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 3348 by schoenebeck, Tue Oct 3 15:05:45 2017 UTC revision 3484 by schoenebeck, Sat Feb 23 16:12:08 2019 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2017 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2019 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 29  Line 29 
29  #include <sstream>  #include <sstream>
30  #include <algorithm>  #include <algorithm>
31    
32  #if defined(WIN32) && !HAVE_CONFIG_H  #if defined(WIN32) && !HAVE_CONFIG_H && !defined(_MSC_VER)
33  # include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++  # include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++
34  # define PACKAGE "libgig"  # define PACKAGE "libgig"
35  # define VERSION VER_STRING // VER_STRING defined in libgig_private.h  # define VERSION VER_STRING // VER_STRING defined in libgig_private.h
36  #endif // WIN32  #endif // WIN32
37    
38  #if HAVE_CONFIG_H /*&& !HAVE_VASPRINTF*/ && defined(WIN32)  #if (HAVE_CONFIG_H /*&& !HAVE_VASPRINTF*/ && defined(WIN32)) || defined(_MSC_VER)
39  # include <stdarg.h>  # include <stdarg.h>
40  int vasprintf(char** ret, const char* format, va_list arg);  int vasprintf(char** ret, const char* format, va_list arg);
41  #endif  #endif
42    
43    #if defined(_MSC_VER)
44    # if _MSC_VER < 1900
45    #  error versions prior to msvc 2015 have not been tested
46    # else
47    #  include <BaseTsd.h>
48    typedef SSIZE_T ssize_t;
49    # endif
50    #endif
51    
52  #include "RIFF.h"  #include "RIFF.h"
53    
54  // *************** Helper Functions **************  // *************** Helper Functions **************
# Line 51  template<class T> inline std::string ToS Line 60  template<class T> inline std::string ToS
60      return ss.str();      return ss.str();
61  }  }
62    
63    // Behaves as printf() just that it returns it as string instead of writing to stdout.
64    inline std::string strPrint(const char* fmt, ...) {
65        va_list args;
66        va_start(args, fmt);
67        char* buf = NULL;
68        vasprintf(&buf, fmt, args);
69        std::string res = buf;
70        if (buf) free(buf);
71        va_end(args);
72        return res;
73    }
74    
75  inline std::string toLowerCase(std::string s) {  inline std::string toLowerCase(std::string s) {
76      std::transform(s.begin(), s.end(), s.begin(), ::tolower);      std::transform(s.begin(), s.end(), s.begin(), ::tolower);
77      return s;      return s;
# Line 129  inline void store32(uint8_t* pData, uint Line 150  inline void store32(uint8_t* pData, uint
150  }  }
151    
152  /**  /**
153     * Loads a 16 bit integer in memory using little-endian format.
154     *
155     * @param pData - memory pointer
156     * @returns 16 bit data word
157     */
158    inline uint16_t load16(uint8_t* pData) {
159        return uint16_t(pData[0]) |
160               uint16_t(pData[1]) << 8;
161    }
162    
163    /**
164   * Loads a 32 bit integer in memory using little-endian format.   * Loads a 32 bit integer in memory using little-endian format.
165   *   *
166   * @param pData - memory pointer   * @param pData - memory pointer
# Line 274  inline void __divide_progress(RIFF::prog Line 306  inline void __divide_progress(RIFF::prog
306      }      }
307  }  }
308    
309    // private helper function to divide a progress into subprogresses
310    inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float total, float lo, float hi) {
311        if (pParentProgress && pParentProgress->callback) {
312            const float totalrange    = pParentProgress->__range_max - pParentProgress->__range_min;
313            pSubProgress->callback    = pParentProgress->callback;
314            pSubProgress->custom      = pParentProgress->custom;
315            pSubProgress->__range_min = pParentProgress->__range_min + totalrange * (lo / total);
316            pSubProgress->__range_max = pSubProgress->__range_min + totalrange * ((hi-lo) / total);
317        }
318    }
319    
320    
321    /*****************************************************************************
322     * Any problems with any of the following helper functions?                  *
323     *                                                                           *
324     * Then please first have a look at their current TEST CASES at              *
325     * src/testcases/HelperTest.cpp as basis for your modifications!             *
326     *****************************************************************************/
327    
328    
329    /// Removes one or more consecutive occurences of @a needle from the end of @a haystack.
330    inline std::string strip2ndFromEndOf1st(const std::string haystack, char needle) {
331        if (haystack.empty()) return haystack;
332        if (*haystack.rbegin() != needle) return haystack;
333        for (int i = haystack.length() - 1; i >= 0; --i)
334            if (haystack[i] != needle)
335                return haystack.substr(0, i+1);
336        return "";
337    }
338    
339    #ifndef NATIVE_PATH_SEPARATOR
340    # ifdef _WIN32
341    #  define NATIVE_PATH_SEPARATOR '\\'
342    # else
343    #  define NATIVE_PATH_SEPARATOR '/'
344    # endif
345    #endif
346    
347    /**
348     * Returns the owning path of the given path (its parent path). So for example
349     * passing "/some/path" would return "/some".
350     */
351    inline std::string parentPath(const std::string path) {
352        if (path.empty()) return path;
353        std::string s = strip2ndFromEndOf1st(path, NATIVE_PATH_SEPARATOR);
354        if (s.empty()) {
355            s.push_back(NATIVE_PATH_SEPARATOR); // i.e. return "/"
356            return s;
357        }
358        #if defined(_WIN32)
359        if (s.length() == 2 && s[1] == ':')
360            return s;
361        #endif
362        std::size_t pos = s.find_last_of(NATIVE_PATH_SEPARATOR);
363        if (pos == std::string::npos) return "";
364        if (pos == 0) {
365            s = "";
366            s.push_back(NATIVE_PATH_SEPARATOR); // i.e. return "/"
367            return s;
368        }
369        return s.substr(0, pos);
370    }
371    
372    /**
373     * Returns the last (lowest) portion of the given path. So for example passing
374     * "/some/path" would return "path".
375     */
376    inline std::string lastPathComponent(const std::string path) {
377        #if defined(_WIN32)
378        if (path.length() == 2 && path[1] == ':')
379            return "";
380        #endif
381        std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR);
382        return (pos == std::string::npos) ? path : path.substr(pos+1);
383    }
384    
385    /**
386     * Returns the given path with the type extension being stripped from its end.
387     * So for example passing "/some/path.foo" would return "/some/path".
388     */
389    inline std::string pathWithoutExtension(const std::string path) {
390        std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR);
391        std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1;
392        std::size_t posDot = path.find_last_of(".");
393        return (posDot != std::string::npos && posDot > posBase)
394               ? path.substr(0, posDot) : path;
395    }
396    
397    /**
398     * Returns the type extension of the given path. So for example passing
399     * "/some/path.foo" would return "foo".
400     */
401    inline std::string extensionOfPath(const std::string path) {
402        std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR);
403        std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1;
404        std::size_t posDot = path.find_last_of(".");
405        return (posDot != std::string::npos && posDot > posBase)
406               ? path.substr(posDot+1) : "";
407    }
408    
409    /**
410     * Combines the two given paths with each other. So for example passing
411     * "/some/path" and "/another/one" would return "/some/path/another/one".
412     */
413    inline std::string concatPath(const std::string path1, const std::string path2) {
414        return (!path1.empty() && *(path1.rbegin()) != NATIVE_PATH_SEPARATOR &&
415                !path2.empty() && *(path2.begin())  != NATIVE_PATH_SEPARATOR)
416            ? path1 + NATIVE_PATH_SEPARATOR + path2
417            : path1 + path2;
418    }
419    
420  #endif // __LIBGIG_HELPER_H__  #endif // __LIBGIG_HELPER_H__

Legend:
Removed from v.3348  
changed lines
  Added in v.3484

  ViewVC Help
Powered by ViewVC