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 * |
51 |
return ss.str(); |
return ss.str(); |
52 |
} |
} |
53 |
|
|
54 |
|
// Behaves as printf() just that it returns it as string instead of writing to stdout. |
55 |
|
inline std::string strPrint(const char* fmt, ...) { |
56 |
|
va_list args; |
57 |
|
va_start(args, fmt); |
58 |
|
char* buf = NULL; |
59 |
|
vasprintf(&buf, fmt, args); |
60 |
|
std::string res = buf; |
61 |
|
if (buf) free(buf); |
62 |
|
va_end(args); |
63 |
|
return res; |
64 |
|
} |
65 |
|
|
66 |
inline std::string toLowerCase(std::string s) { |
inline std::string toLowerCase(std::string s) { |
67 |
std::transform(s.begin(), s.end(), s.begin(), ::tolower); |
std::transform(s.begin(), s.end(), s.begin(), ::tolower); |
68 |
return s; |
return s; |
141 |
} |
} |
142 |
|
|
143 |
/** |
/** |
144 |
|
* Loads a 16 bit integer in memory using little-endian format. |
145 |
|
* |
146 |
|
* @param pData - memory pointer |
147 |
|
* @returns 16 bit data word |
148 |
|
*/ |
149 |
|
inline uint16_t load16(uint8_t* pData) { |
150 |
|
return uint16_t(pData[0]) | |
151 |
|
uint16_t(pData[1]) << 8; |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
* Loads a 32 bit integer in memory using little-endian format. |
* Loads a 32 bit integer in memory using little-endian format. |
156 |
* |
* |
157 |
* @param pData - memory pointer |
* @param pData - memory pointer |
297 |
} |
} |
298 |
} |
} |
299 |
|
|
300 |
|
#ifdef _WIN32 |
301 |
|
# define NATIVE_PATH_SEPARATOR '\\' |
302 |
|
#else |
303 |
|
# define NATIVE_PATH_SEPARATOR '/' |
304 |
|
#endif |
305 |
|
|
306 |
|
/** |
307 |
|
* Returns the owning path of the given path (its parent path). So for example |
308 |
|
* passing "/some/path" would return "/some". |
309 |
|
*/ |
310 |
|
inline std::string parentPath(const std::string path) { |
311 |
|
std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR); |
312 |
|
return (pos == std::string::npos) ? path : path.substr(0, pos); |
313 |
|
} |
314 |
|
|
315 |
|
/** |
316 |
|
* Returns the last (lowest) portion of the given path. So for example passing |
317 |
|
* "/some/path" would return "path". |
318 |
|
*/ |
319 |
|
inline std::string lastPathComponent(const std::string path) { |
320 |
|
std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR); |
321 |
|
return (pos == std::string::npos) ? path : path.substr(pos+1); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
325 |
|
* Returns the given path with the type extension being stripped from its end. |
326 |
|
* So for example passing "/some/path.foo" would return "/some/path". |
327 |
|
*/ |
328 |
|
inline std::string pathWithoutExtension(const std::string path) { |
329 |
|
std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR); |
330 |
|
std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1; |
331 |
|
std::size_t posDot = path.find_last_of(".", posBase); |
332 |
|
return (posDot != std::string::npos && posDot > posBase) |
333 |
|
? path.substr(0, posDot) : path; |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
|
* Returns the type extension of the given path. So for example passing |
338 |
|
* "/some/path.foo" would return "foo". |
339 |
|
*/ |
340 |
|
inline std::string extensionOfPath(const std::string path) { |
341 |
|
std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR); |
342 |
|
std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1; |
343 |
|
std::size_t posDot = path.find_last_of(".", posBase); |
344 |
|
return (posDot != std::string::npos && posDot > posBase) |
345 |
|
? path.substr(posDot+1) : ""; |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
|
* Combines the two given paths with each other. So for example passing |
350 |
|
* "/some/path" and "/another/one" would return "/some/path/another/one". |
351 |
|
*/ |
352 |
|
inline std::string concatPath(const std::string path1, const std::string path2) { |
353 |
|
return (!path1.empty() && *(path1.rbegin()) != NATIVE_PATH_SEPARATOR && |
354 |
|
!path2.empty() && *(path2.begin()) != NATIVE_PATH_SEPARATOR) |
355 |
|
? path1 + NATIVE_PATH_SEPARATOR + path2 |
356 |
|
: path1 + path2; |
357 |
|
} |
358 |
|
|
359 |
#endif // __LIBGIG_HELPER_H__ |
#endif // __LIBGIG_HELPER_H__ |