28 #include <sys/types.h>
35 #define esyslog(a...) void( (SysLogLevel > 0) ? syslog_with_tid(LOG_ERR, a) : void() )
36 #define isyslog(a...) void( (SysLogLevel > 1) ? syslog_with_tid(LOG_INFO, a) : void() )
37 #define dsyslog(a...) void( (SysLogLevel > 2) ? syslog_with_tid(LOG_DEBUG, a) : void() )
39 #define LOG_ERROR esyslog("ERROR (%s,%d): %m", __FILE__, __LINE__)
40 #define LOG_ERROR_STR(s) esyslog("ERROR (%s,%d): %s: %m", __FILE__, __LINE__, s)
42 #define SECSINDAY 86400
44 #define KILOBYTE(n) ((n) * 1024)
45 #define MEGABYTE(n) ((n) * 1024LL * 1024LL)
47 #define MALLOC(type, size) (type *)malloc(sizeof(type) * (size))
49 template<
class T>
inline void DELETENULL(T *&p) { T *q = p; p = NULL;
delete q; }
51 #define CHECK(s) { if ((s) < 0) LOG_ERROR; }
52 #define FATALERRNO (errno && errno != EAGAIN && errno != EINTR)
54 #if __cplusplus >= 201103L
63 template<
class T>
inline T
min(T a, T b) {
return a <= b ? a : b; }
64 template<
class T>
inline T
max(T a, T b) {
return a >= b ? a : b; }
65 template<
class T>
inline void swap(T &a, T &b) { T t = a; a = b; b = t; }
68 template<
class T>
inline int sgn(T a) {
return a < 0 ? -1 : a > 0 ? 1 : 0; }
70 template<
class T>
inline T
constrain(T v, T l, T h) {
return v < l ? l : v > h ? h : v; }
74 #define BCDCHARTOINT(x) (10 * ((x & 0xF0) >> 4) + (x & 0xF))
77 #define IsBitSet(v, b) ((v) & (1 << (b)))
99 return fabs(a - b) <= DBL_EPSILON;
133 int Utf8FromArray(
const uint *a,
char *s,
int Size,
int Max = -1);
143 #define Utf8BufSize(s) ((s) * 4)
148 #define Utf8to(conv, c) (cCharSetConv::SystemCharacterTable() ? to##conv(c) : tow##conv(c))
149 #define Utf8is(ccls, c) (cCharSetConv::SystemCharacterTable() ? is##ccls(c) : isw##ccls(c))
158 cCharSetConv(
const char *FromCode = NULL,
const char *ToCode = NULL);
164 const char *
Convert(
const char *From,
char *To = NULL,
size_t ToLength = 0);
182 cString(
const char *S = NULL,
bool TakePointer =
false);
183 cString(
const char *S,
const char *To);
186 operator const void * ()
const {
return s; }
187 operator const char * ()
const {
return s; }
225 ssize_t
safe_read(
int filedes,
void *buffer,
size_t size);
226 ssize_t
safe_write(
int filedes,
const void *buffer,
size_t size);
233 char *
strn0cpy(
char *dest,
const char *src,
size_t n);
235 char *
strreplace(
char *
s,
const char *s1,
const char *s2);
236 const char *
strchrn(
const char *
s,
char c,
size_t n);
268 bool endswith(
const char *
s,
const char *p);
281 double atod(
const char *
s);
292 uint16_t v = uint16_t(*p++ & 0x1F) << 8;
293 return v + (*p & 0xFF);
297 v |= uint16_t(*p & ~0x1F) << 8;
307 bool DirectoryOk(
const char *DirName,
bool LogErrors =
false);
308 bool MakeDirs(
const char *FileName,
bool IsDirectory =
false);
309 bool RemoveFileOrDir(
const char *FileName,
bool FollowSymlinks =
false);
310 bool RemoveEmptyDirectories(
const char *DirName,
bool RemoveThis =
false,
const char *IgnoreFiles[] = NULL);
317 char *
ReadLink(
const char *FileName);
321 off_t
FileSize(
const char *FileName);
371 const char *NextLine(
void);
385 cBitStream(
const uint8_t *Data,
int Length) : data(Data), length(Length), index(0) {}
388 uint32_t GetBits(
int n);
389 void ByteAlign(
void);
390 void WordAlign(
void);
394 bool IsEOF(
void)
const {
return index >= length; }
396 int Length(
void)
const {
return length; }
397 int Index(
void)
const {
return (IsEOF() ? length : index); }
398 const uint8_t *
GetData(
void)
const {
return (IsEOF() ? NULL : data + (index / 8)); }
409 static uint64_t Now(
void);
410 void Set(
int Ms = 0);
417 bool TimedOut(
void)
const;
418 uint64_t Elapsed(
void)
const;
433 enum { MaxPollFiles = 64 };
434 pollfd pfd[MaxPollFiles];
437 cPoller(
int FileHandle = -1,
bool Out =
false);
438 bool Add(
int FileHandle,
bool Out);
439 void Del(
int FileHandle,
bool Out);
440 bool Poll(
int TimeoutMs = 0);
447 #if !__GLIBC_PREREQ(2, 24)
450 char b[offsetof(
struct dirent, d_name) + NAME_MAX + 1];
456 bool Ok(
void) {
return directory != NULL; }
457 struct dirent *Next(
void);
460 #ifndef DEPRECATED_CFILE
461 #define DEPRECATED_CFILE 0
473 operator int () {
return f; }
474 bool Open(
const char *FileName,
int Flags, mode_t Mode = DEFFILEMODE);
475 bool Open(
int FileDes);
478 bool Ready(
bool Wait =
true);
480 static bool AnyFileReady(
int FileDes = -1,
int TimeoutMs = 1000);
482 static bool FileReady(
int FileDes,
int TimeoutMs = 1000);
484 static bool FileReadyForWriting(
int FileDes,
int TimeoutMs = 1000);
496 operator FILE* () {
return f; }
516 int FadviseDrop(off_t Offset, off_t Len);
520 int Open(
const char *FileName,
int Flags, mode_t Mode = DEFFILEMODE);
522 void SetReadAhead(
size_t ra);
523 off_t Seek(off_t Offset,
int Whence);
524 ssize_t Read(
void *Data,
size_t Size);
525 ssize_t Write(
const void *Data,
size_t Size);
526 static cUnbufferedFile *Create(
const char *FileName,
int Flags, mode_t Mode = DEFFILEMODE);
536 bool Lock(
int WaitSeconds = 0);
555 int Index(
void)
const;
569 void Purge(
bool Force =
false);
581 cListBase(
const char *NeedsLocking = NULL);
584 bool Lock(
cStateKey &StateKey,
bool Write =
false,
int TimeoutMs = 0)
const;
615 void SetExplicitModify(
void);
620 void SetModified(
void);
624 void Del(
cListObject *Object,
bool DeleteObject =
true);
625 virtual void Move(
int From,
int To);
627 virtual void Clear(
void);
637 int Count(
void)
const {
return count; }
653 const T *
First(
void)
const {
return (T *)objects; }
655 const T *
Last(
void)
const {
return (T *)lastObject; }
657 const T *
Prev(
const T *Object)
const {
return (T *)Object->cListObject::Prev(); }
660 const T *
Next(
const T *Object)
const {
return (T *)Object->cListObject::Next(); }
663 T *
Get(
int Index) {
return const_cast<T *
>(
static_cast<const cList<T> *
>(
this)->
Get(Index)); }
669 T *
Prev(
const T *Object) {
return const_cast<T *
>(
static_cast<const cList<T> *
>(
this)->
Prev(Object)); }
671 T *
Next(
const T *Object) {
return const_cast<T *
>(
static_cast<const cList<T> *
>(
this)->
Next(Object)); }
679 #define DEF_LIST_LOCK2(Class, Name) \
680 class c##Name##_Lock { \
682 cStateKey stateKey; \
683 const c##Class *list; \
685 c##Name##_Lock(bool Write = false) \
688 list = c##Class::Get##Name##Write(stateKey); \
690 list = c##Class::Get##Name##Read(stateKey); \
692 ~c##Name##_Lock() { if (list) stateKey.Remove(); } \
693 const c##Class *Name(void) const { return list; } \
694 c##Class *Name(void) { return const_cast<c##Class *>(list); } \
696 #define DEF_LIST_LOCK(Class) DEF_LIST_LOCK2(Class, Class)
701 #define USE_LIST_LOCK_READ2(Class, Name) \
702 c##Name##_Lock Name##_Lock(false); \
703 const c##Class *Name __attribute__((unused)) = Name##_Lock.Name();
704 #define USE_LIST_LOCK_READ(Class) USE_LIST_LOCK_READ2(Class, Class)
706 #define USE_LIST_LOCK_WRITE2(Class, Name) \
707 c##Name##_Lock Name##_Lock(true); \
708 c##Class *Name __attribute__((unused)) = Name##_Lock.Name();
709 #define USE_LIST_LOCK_WRITE(Class) USE_LIST_LOCK_WRITE2(Class, Class)
721 if (++Index > allocated) {
722 data = (T *)realloc(data, Index *
sizeof(T));
724 esyslog(
"ERROR: out of memory - abort!");
727 for (
int i = allocated; i < Index; i++)
741 T&
At(
int Index)
const
758 for (
int i = 0; i < size; i++) {
764 int Size(
void)
const {
return size; }
765 virtual void Insert(T Data,
int Before = 0)
769 memmove(&data[Before + 1], &data[Before], (size - Before) *
sizeof(T));
778 if (IndexOf(Data) < 0) {
779 Insert(Data, Before);
786 if (size >= allocated)
787 Realloc(allocated * 3 / 2);
792 if (IndexOf(Data) < 0) {
802 if (Index < size - 1)
803 memmove(&data[Index], &data[Index + 1], (size - Index) *
sizeof(T));
808 int i = IndexOf(Data);
817 for (
int i = 0; i < size; i++)
821 void Sort(__compar_fn_t Compare)
823 qsort(data, size,
sizeof(T), Compare);
829 return *(
const int *)a - *(
const int *)b;
834 return strcmp(*(
const char **)a, *(
const char **)b);
839 return strcasecmp(*(
const char **)a, *(
const char **)b);
844 int d = atoi(*(
const char **)a) - atoi(*(
const char **)b);
852 int Find(
const char *
s)
const;
853 void Sort(
bool IgnoreCase =
false)
864 virtual void Clear(
void);
869 cFileNameList(
const char *Directory = NULL,
bool DirsOnly =
false);
870 bool Load(
const char *Directory,
bool DirsOnly =
false);
879 bool Realloc(
int NewSize);
880 bool Assert(
int NewSize) {
return size < NewSize ? Realloc(NewSize) :
true; }
885 void Append(
uchar Data) {
if (Assert(used + 1)) buffer[used++] = Data; }
886 void Set(
int Index,
uchar Data) {
if (Assert(Index + 1)) buffer[Index] = Data; }
887 uchar Get(
int Index) {
return Index < used ? buffer[Index] : 0; }
908 unsigned int hashfn(
unsigned int Id)
const {
return Id % size; }
static uint8_t * SetLength(uint8_t *Data, int Length)
const uint8_t * GetData(void) const
cBitStream(const uint8_t *Data, int Length)
cCharSetConv(const char *FromCode=NULL, const char *ToCode=NULL)
Sets up a character set converter to convert from FromCode to ToCode.
static void SetSystemCharacterTable(const char *CharacterTable)
static char * systemCharacterTable
static const char * SystemCharacterTable(void)
const char * Convert(const char *From, char *To=NULL, size_t ToLength=0)
Converts the given Text from FromCode to ToCode (as set in the constructor).
void Set(int Index, uchar Data)
cListObject * Get(unsigned int Id) const
cList< cHashObject > ** hashTable
unsigned int hashfn(unsigned int Id) const
cListObject * Object(void)
cHashObject(cListObject *Object, unsigned int Id)
T * Get(unsigned int Id) const
cHash(int Size=HASHSIZE, bool OwnObjects=false)
cListObject * Get(int Index)
void SetSyncStateKey(cStateKey &StateKey)
When making changes to this list (while holding a write lock) that shall not affect some other code t...
void SetUseGarbageCollector(void)
const char * needsLocking
const cListObject * Get(int Index) const
cListObject(const cListObject &ListObject)
cListObject * Prev(void) const
virtual int Compare(const cListObject &ListObject) const
Must return 0 if this object is equal to ListObject, a positive value if it is "greater",...
cListObject * Next(void) const
T * Prev(const T *Object)
Non-const version of Prev().
T * Next(const T *Object)
Non-const version of Next().
T * Last(void)
Non-const version of Last().
const T * Next(const T *Object) const
< Returns the element immediately before Object in this list, or NULL if Object is the first element ...
const T * Last(void) const
Returns the last element in this list, or NULL if the list is empty.
T * First(void)
Non-const version of First().
T * Get(int Index)
< Returns the element immediately following Object in this list, or NULL if Object is the last elemen...
const T * First(void) const
Returns the first element in this list, or NULL if the list is empty.
cList(const char *NeedsLocking=NULL)
Sets up a new cList of the given type T.
const T * Get(int Index) const
Returns the list element at the given Index, or NULL if no such element exists.
const T * Prev(const T *Object) const
void SetSyncStateKey(cStateKey &StateKey)
Sets the given StateKey to be synchronized to the state of this lock.
cStringList(int Allocated=10)
void Sort(bool IgnoreCase=false)
void SortNumerically(void)
cString & CompactChars(char c)
Compact any sequence of characters 'c' to a single character, and strip all of them from the beginnin...
static cString static cString vsprintf(const char *fmt, va_list &ap)
cString(const char *S=NULL, bool TakePointer=false)
static cString sprintf(const char *fmt,...) __attribute__((format(printf
cString & operator=(const cString &String)
const char * operator*() const
cString & Append(const char *String)
cString & Truncate(int Index)
Truncate the string at the given Index (if Index is < 0 it is counted from the end of the string).
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner,...
cVector(int Allocated=10)
bool AppendUnique(T Data)
void Realloc(int Index) const
void Sort(__compar_fn_t Compare)
int IndexOf(const T &Data)
int allocated
< cVector may only be used for simple types, like int or pointers - not for class objects that alloca...
cVector & operator=(const cVector &Vector)
virtual void Insert(T Data, int Before=0)
virtual void Append(T Data)
bool InsertUnique(T Data, int Before=0)
T & operator[](int Index)
const T & operator[](int Index) const
virtual void Remove(int Index)
bool RemoveElement(const T &Data)
cVector(const cVector &Vector)
struct __attribute__((packed))