23 #ifndef __CLRX_UTILITIES_H__
24 #define __CLRX_UTILITIES_H__
26 #include <CLRX/Config.h>
71 const
char*
what() const noexcept;
91 ParseException(LineNo lineNo, ColNo charNo,
const std::string& message);
100 FLAGS_ALL = 0xffffffffU
107 DYNLIB_MODE1_MASK = 7,
116 static std::mutex mutex;
130 void load(
const char* filename,
Flags flags = 0);
135 void* getSymbol(
const char* symbolName);
141 inline bool isSpace(
unsigned char c);
144 {
return (c == 32 || (c < 32 && (0x3e00U & (1U<<c)))); }
147 inline bool isODigit(
unsigned char c);
150 {
return c>=
'0' && c<=
'7'; }
153 inline bool isDigit(
unsigned char c);
156 {
return c>=
'0' && c<=
'9'; }
159 inline bool isXDigit(
unsigned char c);
162 {
return (c>=
'0' && c<=
'9') || (c>=
'a' && c<='f') || (c>=
'A' && c<=
'F'); }
165 inline bool isAlpha(
unsigned char c);
168 {
return (c>=
'a' && c<=
'z') || (c>=
'A' && c<=
'Z'); }
171 inline bool isAlnum(
unsigned char c);
174 {
return (c>=
'0' && c<=
'9') || (c>=
'a' && c<='z') || (c>=
'A' && c<=
'Z'); }
190 const char* t = s+length;
191 if (t == s)
return s;
192 for (t--; t != s-1 &&
isSpace(*t); t--);
212 extern T
cstrtovCStyle(
const char* str,
const char* inend,
const char*& outend);
223 const char* var = getenv(envVar);
231 {
return cstrtovCStyle<T>(var,
nullptr, outend); }
233 {
return defaultValue; }
284 #ifndef __UTILITIES_MODULE__
288 std::string parseEnvVariable<std::string>(
const char* envVar,
289 const std::string& defaultValue);
302 { return ::strcmp(c1, c2)<0; }
310 { return ::strcasecmp(c1, c2)<0; }
318 { return ::strcmp(c1, c2)==0; }
331 for (
const char* p = c; *p != 0; p++)
332 hash = ((hash<<8)^(cxbyte)*p)*size_t(0xbf146a3dU);
338 inline cxuint
CLZ32(uint32_t v);
340 inline cxuint
CLZ64(uint64_t v);
345 return __builtin_clz(v);
348 for (uint32_t t = 1U<<31; t > v; t>>=1, count++);
356 return __builtin_clzll(v);
359 for (uint64_t t = 1ULL<<63; t > v; t>>=1, count++);
365 template<
typename T,
typename T2>
367 {
return ((a+b)>c) || ((a+b)<a); }
370 template<
typename T,
typename T2>
372 {
return ((a+b)>=c) || ((a+b)<a); }
396 size_t outMaxSize,
char* outStr,
size_t& outSize);
407 extern cxuint
cstrtoui(
const char* str,
const char* inend,
const char*& outend);
411 const char*& outend, cxuint bits);
414 extern uint64_t
cstrtouXCStyle(
const char* str,
const char* inend,
415 const char*& outend, cxuint bits);
425 extern uint64_t
cstrtofXCStyle(
const char* str,
const char* inend,
426 const char*& outend, cxuint expBits, cxuint mantisaBits);
436 {
return cstrtouXCStyle(str, inend, outend,
sizeof(cxuchar)<<3); }
456 {
return cstrtouXCStyle(str, inend, outend,
sizeof(cxushort)<<3); }
461 {
return cstrtoiXCStyle(str, inend, outend,
sizeof(cxshort)<<3); }
466 {
return cstrtouXCStyle(str, inend, outend,
sizeof(cxulong)<<3); }
476 {
return cstrtouXCStyle(str, inend, outend,
sizeof(cxullong)<<3); }
481 {
return cstrtoiXCStyle(str, inend, outend,
sizeof(cxllong)<<3); }
524 cxushort
cstrtohCStyle(
const char* str,
const char* inend,
const char*& outend);
527 inline cxushort
cstrtohCStyle(
const char* str,
const char* inend,
const char*& outend)
531 extern size_t uXtocstrCStyle(uint64_t value,
char* str,
size_t maxSize, cxuint radix,
532 cxuint width,
bool prefix);
535 extern size_t iXtocstrCStyle(int64_t value,
char* str,
size_t maxSize, cxuint radix,
536 cxuint width,
bool prefix);
549 extern size_t itocstrCStyle(T value,
char* str,
size_t maxSize, cxuint radix = 10,
550 cxuint width = 0,
bool prefix =
true);
555 cxuint width,
bool prefix)
556 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
561 cxuint width,
bool prefix)
562 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
567 cxuint width,
bool prefix)
568 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
573 cxuint width,
bool prefix)
574 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
579 cxuint width,
bool prefix)
580 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
585 cxuint width,
bool prefix)
586 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
591 cxuint width,
bool prefix)
592 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
597 cxuint width,
bool prefix)
598 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
603 cxuint width ,
bool prefix)
604 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
609 cxuint width,
bool prefix)
610 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
613 extern size_t fXtocstrCStyle(uint64_t value,
char* str,
size_t maxSize,
614 bool scientific, cxuint expBits, cxuint mantisaBits);
627 size_t htocstrCStyle(cxushort value,
char* str,
size_t maxSize,
628 bool scientific =
false);
630 inline size_t htocstrCStyle(cxushort value,
char* str,
size_t maxSize,
bool scientific)
631 {
return fXtocstrCStyle(value, str, maxSize, scientific, 5, 10); }
645 bool scientific =
false);
647 inline size_t ftocstrCStyle(
float value,
char* str,
size_t maxSize,
bool scientific)
668 size_t dtocstrCStyle(
double value,
char* str,
size_t maxSize,
669 bool scientific =
false);
671 inline size_t dtocstrCStyle(
double value,
char* str,
size_t maxSize,
bool scientific)
699 extern std::string
joinPaths(
const std::string& path1,
const std::string& path2);
707 extern void makeDir(
const char* dirname);
717 mutable std::atomic<size_t> refCount;
726 refCount.fetch_add(1);
732 return (refCount.fetch_sub(1) == 1);
740 mutable size_t refCount;
755 return (--refCount == 0);
771 explicit RefPtr(T* inputPtr) : ptr(inputPtr)
783 { refPtr.ptr =
nullptr; }
789 if (ptr->unreference())
797 if (ptr->unreference())
799 if (refPtr.ptr !=
nullptr)
800 refPtr.ptr->reference();
808 if (ptr->unreference())
811 refPtr.ptr =
nullptr;
817 {
return ptr == refPtr.ptr; }
820 {
return ptr != refPtr.ptr; }
823 operator bool()
const
824 {
return ptr!=
nullptr; }
828 {
return ptr==
nullptr; }
838 if (ptr->unreference())
852 template<
typename DestType>
855 DestType* p =
const_cast<DestType*
>(ptr);
861 template<
typename DestType>
864 DestType* p =
static_cast<DestType*
>(ptr);
870 template<
typename DestType>
873 DestType* p =
dynamic_cast<DestType*
>(ptr);
884 {
return (c >=
'A' && c <=
'Z') ? c -
'A' +
'a' : c; }
890 { std::transform(
string.begin(),
string.end(),
string.begin(),
toLower); }
897 for (; *cstr!=0; cstr++)
905 {
if (!
string.empty())
912 {
return (c >=
'a' && c <=
'z') ? c -
'a' +
'A' : c; }
918 { std::transform(
string.begin(),
string.end(),
string.begin(),
toUpper); }
925 for (; *cstr!=0; cstr++)
933 {
if (!
string.empty())
reference countable object
Definition: Utilities.h:714
template bool parseEnvVariable< bool >(const char *envVar, const bool &defaultValue)
parse environment variable of boolean type
bool operator!=(const RefPtr< T > &refPtr) const
unequality operator
Definition: Utilities.h:819
function class that returns true if C strings are equal
Definition: Utilities.h:314
function class that returns true if first C string is less than second (ignore case) ...
Definition: Utilities.h:306
int64_t cstrtoiXCStyle(const char *str, const char *inend, const char *&outend, cxuint bits)
parse 64-bit signed integer
cxlong cstrtovCStyle< cxlong >(const char *str, const char *inend, const char *&outend)
parse cxlong value from string
Definition: Utilities.h:470
std::string escapeStringCStyle(size_t strSize, const char *str)
escapes string into C-style string
non copyable and non movable base structure (class)
Definition: Utilities.h:43
bool operator!() const
return true if null
Definition: Utilities.h:827
uint64_t lo
low part
Definition: Utilities.h:420
bool unreference() const
unreference object (returns true if no reference count)
Definition: Utilities.h:730
template cxint parseEnvVariable< cxint >(const char *envVar, const cxint &defaultValue)
parse environment variable of cxint type
bool operator()(const char *c1, const char *c2) const
operator of call
Definition: Utilities.h:317
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
cxuint cstrtovCStyle< cxuint >(const char *str, const char *inend, const char *&outend)
parse cxuint value from string
Definition: Utilities.h:445
size_t itocstrCStyle< cxullong >(cxullong value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxullong value to string
Definition: Utilities.h:602
template cxushort parseEnvVariable< cxushort >(const char *envVar, const cxushort &defaultValue)
parse environment variable of cxushort type
void makeDir(const char *dirname)
create directory
template cxuchar parseEnvVariable< cxuchar >(const char *envVar, const cxuchar &defaultValue)
parse environment variable of cxuchar type
template double parseEnvVariable< double >(const char *envVar, const double &defaultValue)
parse environment variable of double type
const char * c_str() const
return C-style string pointer
Definition: CString.h:245
size_t iXtocstrCStyle(int64_t value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
formast signed value to string
bool isDirectory(const char *path)
returns true if path refers to directory
reference pointer based on Glibmm refptr
Definition: Utilities.h:761
RefPtr< DestType > staticCast() const
static cast
Definition: Utilities.h:862
bool unreference() const
unreference object (returns true if no reference count)
Definition: Utilities.h:753
template cxullong parseEnvVariable< cxullong >(const char *envVar, const cxullong &defaultValue)
parse environment variable of cxullong type
template cxchar parseEnvVariable< cxchar >(const char *envVar, const cxchar &defaultValue)
parse environment variable of cxchar type
cxullong cstrtovCStyle< cxullong >(const char *str, const char *inend, const char *&outend)
parse cxullong value from string
Definition: Utilities.h:475
size_t fXtocstrCStyle(uint64_t value, char *str, size_t maxSize, bool scientific, cxuint expBits, cxuint mantisaBits)
format float value to string
Unsigned 128-bit integer.
Definition: Utilities.h:418
size_t itocstrCStyle< cxushort >(cxushort value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxushort value to string
Definition: Utilities.h:566
RefPtr< DestType > constCast() const
const cast
Definition: Utilities.h:853
bool operator()(const char *c1, const char *c2) const
operator of call
Definition: Utilities.h:301
const char * what() const noexcept
get exception message
RefCountable()
constructor
Definition: Utilities.h:720
RefPtr(T *inputPtr)
constructor from pointer
Definition: Utilities.h:771
RefPtr< DestType > dynamicCast() const
dynamic cast
Definition: Utilities.h:871
cxuint CLZ32(uint32_t v)
counts leading zeroes for 32-bit unsigned integer. For zero behavior is undefined ...
Definition: Utilities.h:342
template cxuint parseEnvVariable< cxuint >(const char *envVar, const cxuint &defaultValue)
parse environment variable of cxuint type
bool operator==(const RefPtr< T > &refPtr) const
equality operator
Definition: Utilities.h:816
bool isDigit(unsigned char c)
check whether character is digit
Definition: Utilities.h:155
size_t dtocstrCStyle(double value, char *str, size_t maxSize, bool scientific=false)
formats double float in C-style
Definition: Utilities.h:671
uint64_t getFileTimestamp(const char *filename)
get file timestamp in nanosecond since Unix epoch
~RefPtr()
destructor
Definition: Utilities.h:786
const char * skipSpaces(const char *s)
skip spaces from cString
Definition: Utilities.h:179
size_t itocstrCStyle< cxuchar >(cxuchar value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxuchar value to string
Definition: Utilities.h:554
RefPtr< T > & operator=(const RefPtr< T > &refPtr)
copy constructor
Definition: Utilities.h:794
template cxshort parseEnvVariable< cxshort >(const char *envVar, const cxshort &defaultValue)
parse environment variable of cxshort type
bool usumGt(T a, T b, T2 c)
safely compares sum of two unsigned integers with other unsigned integer
Definition: Utilities.h:366
UInt128 cstrtou128CStyle(const char *str, const char *inend, const char *&outend)
parse 128-bit unsigned integer
uint64_t cstrtouXCStyle(const char *str, const char *inend, const char *&outend, cxuint bits)
parse 64-bit unsigned integer
size_t ColNo
column number type
Definition: Utilities.h:78
virtual ~Exception() noexcept=default
destructor
template float parseEnvVariable< float >(const char *envVar, const float &defaultValue)
parse environment variable of float type
UInt128 cstrtovCStyle< UInt128 >(const char *str, const char *inend, const char *&outend)
parse UInt128 value from string
Definition: Utilities.h:485
size_t itocstrCStyle< cxulong >(cxulong value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxulong value to string
Definition: Utilities.h:590
cxint cstrtovCStyle< cxint >(const char *str, const char *inend, const char *&outend)
parse cxint value from string
Definition: Utilities.h:450
size_t ftocstrCStyle(float value, char *str, size_t maxSize, bool scientific=false)
formats single float in C-style
Definition: Utilities.h:647
const char * skipSpacesAtEnd(const char *s, size_t length)
skip spaces from cString
Definition: Utilities.h:188
reference countable object (only for single threading usage)
Definition: Utilities.h:737
Array< cxbyte > loadDataFromFile(const char *filename)
load data from file (any regular or pipe or device)
bool isODigit(unsigned char c)
check whether character is digit
Definition: Utilities.h:149
template cxlong parseEnvVariable< cxlong >(const char *envVar, const cxlong &defaultValue)
parse environment variable of cxlong type
bool isSpace(unsigned char c)
check whether character is space
Definition: Utilities.h:143
void toUpperString(std::string &string)
convert string to uppercase
Definition: Utilities.h:917
bool usumGe(T a, T b, T2 c)
safely compares sum of two unsigned integers with other unsigned integer
Definition: Utilities.h:371
T cstrtovCStyle(const char *str, const char *inend, const char *&outend)
parses integer or float point formatted looks like C-style
void reset()
reset refpointer
Definition: Utilities.h:835
cxuint cstrtoui(const char *str, const char *inend, const char *&outend)
parses unsigned integer regardless locales
float cstrtovCStyle< float >(const char *str, const char *inend, const char *&outend)
parse float value from string
Definition: Utilities.h:490
std::string getHomeDir()
get user's home directory
bool isAlnum(unsigned char c)
check whether character is digit
Definition: Utilities.h:173
size_t uXtocstrCStyle(uint64_t value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format unsigned value to string
bool isAlpha(unsigned char c)
check whether character is digit
Definition: Utilities.h:167
size_t itocstrCStyle< cxchar >(cxchar value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxchar value to string
Definition: Utilities.h:560
size_t itocstrCStyle< cxshort >(cxshort value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxshort value to string
Definition: Utilities.h:572
void toLowerString(std::string &string)
convert string to lowercase
Definition: Utilities.h:889
treat symbols locally
Definition: Utilities.h:104
T parseEnvVariable(const char *envVar, const T &defaultValue=T())
parse environment variable
Definition: Utilities.h:221
size_t operator()(const char *c) const
operator of call
Definition: Utilities.h:325
size_t itocstrCStyle< cxint >(cxint value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxint value to string
Definition: Utilities.h:584
uint64_t cstrtofXCStyle(const char *str, const char *inend, const char *&outend, cxuint expBits, cxuint mantisaBits)
parse 64-bit float value
generate hash function for C string
Definition: Utilities.h:322
void swap(RefPtr< T > &refPtr)
swap between refpointers
Definition: Utilities.h:844
cxllong cstrtovCStyle< cxllong >(const char *str, const char *inend, const char *&outend)
parse cxllong value from string
Definition: Utilities.h:480
bool isXDigit(unsigned char c)
check whether character is hexadecimal digit
Definition: Utilities.h:161
cxshort cstrtovCStyle< cxshort >(const char *str, const char *inend, const char *&outend)
parse cxshort value from string
Definition: Utilities.h:460
cxuchar cstrtovCStyle< cxuchar >(const char *str, const char *inend, const char *&outend)
parse cxuchar value from string
Definition: Utilities.h:435
Exception()=default
empty constructor
char toLower(char c)
convert character to lowercase
Definition: Utilities.h:883
size_t itocstrCStyle< cxlong >(cxlong value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxlong value to string
Definition: Utilities.h:596
resolve symbols now
Definition: Utilities.h:106
NonCopyableAndNonMovable & operator=(const NonCopyableAndNonMovable &)=delete
copy-assignment
std::string message
message
Definition: Utilities.h:61
FastRefCountable()
constructor
Definition: Utilities.h:743
uint64_t hi
high part
Definition: Utilities.h:421
size_t htocstrCStyle(cxushort value, char *str, size_t maxSize, bool scientific=false)
formats half float in C-style
Definition: Utilities.h:630
exception class
Definition: Utilities.h:58
cxchar cstrtovCStyle< cxchar >(const char *str, const char *inend, const char *&outend)
parse cxchar value from string
Definition: Utilities.h:440
resolve symbols when is needed
Definition: Utilities.h:105
void reference() const
reference object
Definition: Utilities.h:747
parse exception class
Definition: Utilities.h:81
bool operator()(const char *c1, const char *c2) const
operator of call
Definition: Utilities.h:309
cxushort cstrtovCStyle< cxushort >(const char *str, const char *inend, const char *&outend)
parse cxushort value from string
Definition: Utilities.h:455
char toUpper(char c)
convert character to uppercase
Definition: Utilities.h:911
std::string joinPaths(const std::string &path1, const std::string &path2)
join two paths
uint64_t LineNo
line number type
Definition: Utilities.h:75
treats symbols globally
Definition: Utilities.h:108
template cxllong parseEnvVariable< cxllong >(const char *envVar, const cxllong &defaultValue)
parse environment variable of cxllong type
RefPtr(const RefPtr< T > &refPtr)
copy constructor
Definition: Utilities.h:775
function class that returns true if first C string is less than second
Definition: Utilities.h:298
RefPtr(RefPtr< T > &&refPtr)
move constructor
Definition: Utilities.h:782
cxushort cstrtohCStyle(const char *str, const char *inend, const char *&outend)
parses half float formatted looks like C-style
Definition: Utilities.h:527
cxuint CLZ64(uint64_t v)
counts leading zeroes for 64-bit unsigned integer. For zero behavior is undefined ...
Definition: Utilities.h:353
template cxulong parseEnvVariable< cxulong >(const char *envVar, const cxulong &defaultValue)
parse environment variable of cxulong type
NonCopyableAndNonMovable()
constructor
Definition: Utilities.h:46
double cstrtovCStyle< double >(const char *str, const char *inend, const char *&outend)
parse double value from string
Definition: Utilities.h:502
RefPtr()
empty constructor
Definition: Utilities.h:767
size_t itocstrCStyle(T value, char *str, size_t maxSize, cxuint radix=10, cxuint width=0, bool prefix=true)
formats an integer
size_t size() const
compute size
Definition: CString.h:265
simple C-string container
Definition: CString.h:38
size_t itocstrCStyle< cxuint >(cxuint value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxuint value to string
Definition: Utilities.h:578
dynamic library class
Definition: Utilities.h:112
containers and other utils for other libraries and programs
cxulong cstrtovCStyle< cxulong >(const char *str, const char *inend, const char *&outend)
parse cxulong value from string
Definition: Utilities.h:465
void filesystemPath(char *path)
convert to filesystem from unified path (with slashes)
size_t itocstrCStyle< cxllong >(cxllong value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxllong value to string
Definition: Utilities.h:608
RefPtr< T > & operator=(RefPtr< T > &&refPtr)
move constructor
Definition: Utilities.h:805
void reference() const
reference object
Definition: Utilities.h:724
T * operator->() const
get elem from pointer
Definition: Utilities.h:831