|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- /*
- * Carla common utils
- * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the GPL.txt file
- */
-
- #ifndef __CARLA_UTILS_HPP__
- #define __CARLA_UTILS_HPP__
-
- #include "carla_juce_utils.hpp"
-
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
-
- #ifdef CPP11_MUTEX
- # include <mutex>
- #else
- # include <pthread.h>
- #endif
-
- #if defined(Q_OS_HAIKU)
- # include <kernel/OS.h>
- #elif defined(Q_OS_LINUX)
- # include <sys/prctl.h>
- # include <linux/prctl.h>
- #endif
-
- // -------------------------------------------------
- // carla_assert*
-
- static inline
- void carla_assert(const char* const assertion, const char* const file, const int line)
- {
- qCritical("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
- }
-
- static inline
- void carla_assert_int(const char* const assertion, const char* const file, const int line, const int value)
- {
- qCritical("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
- }
-
- static inline
- void carla_assert_int2(const char* const assertion, const char* const file, const int line, const int v1, const int v2)
- {
- qCritical("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
- }
-
- // -------------------------------------------------
- // carla_*sleep
-
- static inline
- void carla_sleep(const unsigned int secs)
- {
- CARLA_ASSERT(secs > 0);
-
- #ifdef Q_OS_WIN
- Sleep(secs * 1000);
- #else
- sleep(secs);
- #endif
- }
-
- static inline
- void carla_msleep(const unsigned int msecs)
- {
- CARLA_ASSERT(msecs > 0);
-
- #ifdef Q_OS_WIN
- Sleep(msecs);
- #else
- usleep(msecs * 1000);
- #endif
- }
-
- static inline
- void carla_usleep(const unsigned int usecs)
- {
- CARLA_ASSERT(usecs > 0);
-
- #ifdef Q_OS_WIN
- Sleep(usecs / 1000);
- #else
- usleep(usecs);
- #endif
- }
-
- // -------------------------------------------------
- // carla_setenv
-
- static inline
- void carla_setenv(const char* const key, const char* const value)
- {
- CARLA_ASSERT(key != nullptr);
- CARLA_ASSERT(value != nullptr);
-
- #ifdef Q_OS_WIN
- SetEnvironmentVariableA(key, value);
- #else
- setenv(key, value, 1);
- #endif
- }
-
- // -------------------------------------------------
- // carla_setprocname (not available on all platforms)
-
- static inline
- void carla_setprocname(const char* const name)
- {
- CARLA_ASSERT(name != nullptr);
-
- #if defined(Q_OS_HAIKU)
- if ((thread_id this_thread = find_thread(nullptr)) != B_NAME_NOT_FOUND)
- rename_thread(this_thread, name);
- #elif defined(Q_OS_LINUX)
- prctl(PR_SET_NAME, name);
- #else
- qWarning("carla_setprocname(\"%s\") - unsupported on this platform", name);
- #endif
- }
-
- // -------------------------------------------------
- // math functions
-
- template<typename T>
- static inline
- const T& carla_min(const T& v1, const T& v2, const T& min)
- {
- return ((v1 < min || v2 < min) ? min : (v1 < v2 ? v1 : v2));
- }
-
- template<typename T>
- static inline
- const T& carla_fixValue(const T& min, const T& max, const T& value)
- {
- if (value < min)
- return min;
- if (value > max)
- return max;
- return value;
- }
-
- template<typename T>
- static inline
- void carla_fill(T* data, const unsigned int size, const T v)
- {
- CARLA_ASSERT(data != nullptr);
- CARLA_ASSERT(size > 0);
-
- if (data == nullptr)
- return;
-
- for (unsigned int i=0; i < size; i++)
- *data++ = v;
- }
-
- static inline
- void carla_zeroDouble(double* data, const unsigned size)
- {
- carla_fill<double>(data, size, 0.0);
- }
-
- static inline
- void carla_zeroFloat(float* data, const unsigned size)
- {
- carla_fill<float>(data, size, 0.0f);
- }
-
- // -------------------------------------------------
- // memory functions
-
- static inline
- void carla_zeroMem(void* const memory, const size_t numBytes)
- {
- CARLA_ASSERT(memory != nullptr);
-
- if (memory == nullptr)
- return;
-
- std::memset(memory, 0, numBytes);
- }
-
- template <typename T>
- static inline
- void carla_zeroStruct(T& structure)
- {
- std::memset(&structure, 0, sizeof(T));
- }
-
- // -------------------------------------------------
- // other misc functions
-
- static inline
- const char* bool2str(const bool yesNo)
- {
- return yesNo ? "true" : "false";
- }
-
- static inline
- void pass() {}
-
- // -------------------------------------------------
- // CarlaMutex class
-
- class CarlaMutex
- {
- public:
- CarlaMutex()
- {
- #ifndef CPP11_MUTEX
- pthread_mutex_init(&pmutex, nullptr);
- #endif
- }
-
- ~CarlaMutex()
- {
- #ifndef CPP11_MUTEX
- pthread_mutex_destroy(&pmutex);
- #endif
- }
-
- void lock()
- {
- #ifdef CPP11_MUTEX
- cmutex.lock();
- #else
- pthread_mutex_lock(&pmutex);
- #endif
- }
-
- bool tryLock()
- {
- #ifdef CPP11_MUTEX
- return cmutex.try_lock();
- #else
- return (pthread_mutex_trylock(&pmutex) == 0);
- #endif
- }
-
- void unlock()
- {
- #ifdef CPP11_MUTEX
- cmutex.unlock();
- #else
- pthread_mutex_unlock(&pmutex);
- #endif
- }
-
- class ScopedLocker
- {
- public:
- ScopedLocker(CarlaMutex* const mutex)
- : fMutex(mutex)
- {
- fMutex->lock();
- }
-
- ~ScopedLocker()
- {
- fMutex->unlock();
- }
-
- private:
- CarlaMutex* const fMutex;
-
- CARLA_PREVENT_HEAP_ALLOCATION
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ScopedLocker)
- };
-
- private:
- #ifdef CPP11_MUTEX
- std::mutex cmutex;
- #else
- pthread_mutex_t pmutex;
- #endif
-
- CARLA_PREVENT_HEAP_ALLOCATION
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaMutex)
- };
-
- // -------------------------------------------------
- // CarlaString class
-
- class CarlaString
- {
- public:
- // ---------------------------------------------
- // constructors (no explicit conversions allowed)
-
- explicit CarlaString()
- {
- _init();
- _dup(nullptr);
- }
-
- explicit CarlaString(char* const strBuf)
- {
- _init();
- _dup(strBuf);
- }
-
- explicit CarlaString(const char* const strBuf)
- {
- _init();
- _dup(strBuf);
- }
-
- explicit CarlaString(const int value)
- {
- const size_t strBufSize = std::abs(value/10) + 3;
- char strBuf[strBufSize];
- std::snprintf(strBuf, strBufSize, "%d", value);
-
- _init();
- _dup(strBuf, strBufSize);
- }
-
- explicit CarlaString(const unsigned int value, const bool hexadecimal = false)
- {
- const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
- char strBuf[strBufSize];
- std::snprintf(strBuf, strBufSize, hexadecimal ? "0x%x" : "%u", value);
-
- _init();
- _dup(strBuf, strBufSize);
- }
-
- explicit CarlaString(const long int value)
- {
- const size_t strBufSize = std::abs(value/10) + 3;
- char strBuf[strBufSize];
- std::snprintf(strBuf, strBufSize, "%ld", value);
-
- _init();
- _dup(strBuf, strBufSize);
- }
-
- explicit CarlaString(const unsigned long int value, const bool hexadecimal = false)
- {
- const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
- char strBuf[strBufSize];
- std::snprintf(strBuf, strBufSize, hexadecimal ? "0x%lx" : "%lu", value);
-
- _init();
- _dup(strBuf, strBufSize);
- }
-
- explicit CarlaString(const float value)
- {
- char strBuf[0xff];
- std::snprintf(strBuf, 0xff, "%f", value);
-
- _init();
- _dup(strBuf);
- }
-
- explicit CarlaString(const double value)
- {
- char strBuf[0xff];
- std::snprintf(strBuf, 0xff, "%g", value);
-
- _init();
- _dup(strBuf);
- }
-
- // ---------------------------------------------
- // non-explicit constructor
-
- CarlaString(const CarlaString& str)
- {
- _init();
- _dup(str.buffer);
- }
-
- // ---------------------------------------------
- // deconstructor
-
- ~CarlaString()
- {
- CARLA_ASSERT(buffer);
-
- delete[] buffer;
- }
-
- // ---------------------------------------------
- // public methods
-
- size_t length() const
- {
- return bufferLen;
- }
-
- bool isEmpty() const
- {
- return (bufferLen == 0);
- }
-
- bool isNotEmpty() const
- {
- return (bufferLen != 0);
- }
-
- #if __USE_GNU
- bool contains(const char* const strBuf, const bool ignoreCase = false) const
- {
- if (strBuf == nullptr)
- return false;
-
- if (ignoreCase)
- return (strcasestr(buffer, strBuf) != nullptr);
- else
- return (std::strstr(buffer, strBuf) != nullptr);
- }
-
- bool contains(const CarlaString& str, const bool ignoreCase = false) const
- {
- return contains(str.buffer, ignoreCase);
- }
- #else
- bool contains(const char* const strBuf) const
- {
- if (strBuf == nullptr)
- return false;
-
- return (std::strstr(buffer, strBuf) != nullptr);
- }
-
- bool contains(const CarlaString& str) const
- {
- return contains(str.buffer);
- }
- #endif
-
- bool isDigit(const size_t pos) const
- {
- if (pos >= bufferLen)
- return false;
-
- return (buffer[pos] >= '0' && buffer[pos] <= '9');
- }
-
- void clear()
- {
- truncate(0);
- }
-
- void replace(const char before, const char after)
- {
- if (after == '\0')
- return;
-
- for (size_t i=0; i < bufferLen; i++)
- {
- if (buffer[i] == before)
- buffer[i] = after;
- else if (buffer[i] == '\0')
- break;
- }
- }
-
- void truncate(const size_t n)
- {
- if (n >= bufferLen)
- return;
-
- for (size_t i=n; i < bufferLen; i++)
- buffer[i] = '\0';
-
- bufferLen = n;
- }
-
- void toBasic()
- {
- for (size_t i=0; i < bufferLen; i++)
- {
- if (buffer[i] >= '0' && buffer[i] <= '9')
- continue;
- if (buffer[i] >= 'A' && buffer[i] <= 'Z')
- continue;
- if (buffer[i] >= 'a' && buffer[i] <= 'z')
- continue;
- if (buffer[i] == '_')
- continue;
-
- buffer[i] = '_';
- }
- }
-
- void toLower()
- {
- static const char charDiff = 'a' - 'A';
-
- for (size_t i=0; i < bufferLen; i++)
- {
- if (buffer[i] >= 'A' && buffer[i] <= 'Z')
- buffer[i] += charDiff;
- }
- }
-
- void toUpper()
- {
- static const char charDiff = 'a' - 'A';
-
- for (size_t i=0; i < bufferLen; i++)
- {
- if (buffer[i] >= 'a' && buffer[i] <= 'z')
- buffer[i] -= charDiff;
- }
- }
-
- // ---------------------------------------------
- // public operators
-
- operator const char*() const
- {
- return buffer;
- }
-
- char& operator[](const size_t pos)
- {
- return buffer[pos];
- }
-
- bool operator==(const char* const strBuf) const
- {
- return (strBuf != nullptr && std::strcmp(buffer, strBuf) == 0);
- }
-
- bool operator==(const CarlaString& str) const
- {
- return operator==(str.buffer);
- }
-
- bool operator!=(const char* const strBuf) const
- {
- return !operator==(strBuf);
- }
-
- bool operator!=(const CarlaString& str) const
- {
- return !operator==(str.buffer);
- }
-
- CarlaString& operator=(const char* const strBuf)
- {
- _dup(strBuf);
-
- return *this;
- }
-
- CarlaString& operator=(const CarlaString& str)
- {
- return operator=(str.buffer);
- }
-
- CarlaString& operator+=(const char* const strBuf)
- {
- const size_t newBufSize = bufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
- char newBuf[newBufSize];
-
- std::strcpy(newBuf, buffer);
- std::strcat(newBuf, strBuf);
-
- _dup(newBuf, newBufSize-1);
-
- return *this;
- }
-
- CarlaString& operator+=(const CarlaString& str)
- {
- return operator+=(str.buffer);
- }
-
- CarlaString operator+(const char* const strBuf)
- {
- const size_t newBufSize = bufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
- char newBuf[newBufSize];
-
- std::strcpy(newBuf, buffer);
- std::strcat(newBuf, strBuf);
-
- return CarlaString(newBuf);
- }
-
- CarlaString operator+(const CarlaString& str)
- {
- return operator+(str.buffer);
- }
-
- // ---------------------------------------------
-
- private:
- char* buffer;
- size_t bufferLen;
- bool firstInit;
-
- void _init()
- {
- buffer = nullptr;
- bufferLen = 0;
- firstInit = true;
- }
-
- // allocate string strBuf if not null
- // size > 0 only if strBuf is valid
- void _dup(const char* const strBuf, const size_t size = 0)
- {
- if (strBuf != nullptr)
- {
- // don't recreate string if contents match
- if (firstInit || std::strcmp(buffer, strBuf) != 0)
- {
- if (! firstInit)
- {
- CARLA_ASSERT(buffer);
- delete[] buffer;
- }
-
- bufferLen = (size > 0) ? size : std::strlen(strBuf);
- buffer = new char[bufferLen+1];
-
- std::strcpy(buffer, strBuf);
-
- buffer[bufferLen] = '\0';
-
- firstInit = false;
- }
- }
- else
- {
- CARLA_ASSERT(size == 0);
-
- // don't recreate null string
- if (firstInit || bufferLen != 0)
- {
- if (! firstInit)
- {
- CARLA_ASSERT(buffer);
- delete[] buffer;
- }
-
- bufferLen = 0;
- buffer = new char[1];
- buffer[0] = '\0';
-
- firstInit = false;
- }
- }
- }
-
- CARLA_LEAK_DETECTOR(CarlaString)
- CARLA_PREVENT_HEAP_ALLOCATION
- };
-
- static inline
- CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
- {
- const char* const strBufAfter = (const char*)strAfter;
- const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + std::strlen(strBufAfter) + 1;
- char newBuf[newBufSize];
-
- std::strcpy(newBuf, strBufBefore);
- std::strcat(newBuf, strBufAfter);
-
- return CarlaString(newBuf);
- }
-
- // -------------------------------------------------
-
- #endif // __CARLA_UTILS_HPP__
|