28 #include <sys/types.h> 34 #define esyslog(a...) void( (SysLogLevel > 0) ? syslog_with_tid(LOG_ERR, a) : void() ) 35 #define isyslog(a...) void( (SysLogLevel > 1) ? syslog_with_tid(LOG_INFO, a) : void() ) 36 #define dsyslog(a...) void( (SysLogLevel > 2) ? syslog_with_tid(LOG_DEBUG, a) : void() ) 38 #define LOG_ERROR esyslog("ERROR (%s,%d): %m", __FILE__, __LINE__) 39 #define LOG_ERROR_STR(s) esyslog("ERROR (%s,%d): %s: %m", __FILE__, __LINE__, s) 41 #define SECSINDAY 86400 43 #define KILOBYTE(n) ((n) * 1024) 44 #define MEGABYTE(n) ((n) * 1024LL * 1024LL) 46 #define MALLOC(type, size) (type *)malloc(sizeof(type) * (size)) 48 template<
class T>
inline void DELETENULL(T *&p) { T *q = p; p = NULL;
delete q; }
50 #define CHECK(s) { if ((s) < 0) LOG_ERROR; } // used for 'ioctl()' calls 51 #define FATALERRNO (errno && errno != EAGAIN && errno != EINTR) 53 #ifndef __STL_CONFIG_H // in case some plugin needs to use the STL 54 template<
class T>
inline T
min(T a, T b) {
return a <= b ? a : b; }
55 template<
class T>
inline T
max(T a, T b) {
return a >= b ? a : b; }
56 template<
class T>
inline int sgn(T a) {
return a < 0 ? -1 : a > 0 ? 1 : 0; }
57 template<
class T>
inline void swap(T &a, T &b) { T t = a; a = b; b = t; }
60 template<
class T>
inline T
constrain(T v, T l, T h) {
return v < l ? l : v > h ? h : v; }
62 void syslog_with_tid(
int priority,
const char *format, ...) __attribute__ ((format (printf, 2, 3)));
64 #define BCDCHARTOINT(x) (10 * ((x & 0xF0) >> 4) + (x & 0xF)) 67 #define IsBitSet(v, b) ((v) & (1 << (b))) // checks if the bit at index b is set in v, where the least significant bit has index 0 74 struct s { T v; } __attribute__((packed));
80 struct s { T v; } __attribute__((packed));
89 return fabs(a - b) <= DBL_EPSILON;
123 int Utf8FromArray(
const uint *a,
char *s,
int Size,
int Max = -1);
133 #define Utf8BufSize(s) ((s) * 4) 138 #define Utf8to(conv, c) (cCharSetConv::SystemCharacterTable() ? to##conv(c) : tow##conv(c)) 139 #define Utf8is(ccls, c) (cCharSetConv::SystemCharacterTable() ? is##ccls(c) : isw##ccls(c)) 148 cCharSetConv(
const char *FromCode = NULL,
const char *ToCode = NULL);
154 const char *Convert(
const char *From,
char *To = NULL,
size_t ToLength = 0);
172 cString(
const char *S = NULL,
bool TakePointer =
false);
173 cString(
const char *S,
const char *To);
176 operator const void * ()
const {
return s; }
177 operator const char * ()
const {
return s; }
180 cString &operator=(
const char *String);
183 static cString sprintf(
const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
184 static cString vsprintf(
const char *fmt, va_list &ap);
187 ssize_t
safe_read(
int filedes,
void *buffer,
size_t size);
188 ssize_t
safe_write(
int filedes,
const void *buffer,
size_t size);
195 char *
strn0cpy(
char *dest,
const char *src,
size_t n);
197 char *
strreplace(
char *s,
const char *s1,
const char *s2);
198 const char *
strchrn(
const char *s,
char c,
size_t n);
204 while (*s && (
uchar)*s <=
' ')
212 bool startswith(
const char *s,
const char *p);
213 bool endswith(
const char *s,
const char *p);
223 bool StrInArray(
const char *a[],
const char *s);
226 double atod(
const char *s);
241 bool DirectoryOk(
const char *DirName,
bool LogErrors =
false);
242 bool MakeDirs(
const char *FileName,
bool IsDirectory =
false);
243 bool RemoveFileOrDir(
const char *FileName,
bool FollowSymlinks =
false);
244 bool RemoveEmptyDirectories(
const char *DirName,
bool RemoveThis =
false,
const char *IgnoreFiles[] = NULL);
251 char *
ReadLink(
const char *FileName);
255 off_t
FileSize(
const char *FileName);
303 const char *NextLine(
void);
317 cBitStream(
const uint8_t *Data,
int Length) : data(Data), length(Length), index(0) {}
320 uint32_t GetBits(
int n);
321 void ByteAlign(
void);
322 void WordAlign(
void);
326 bool IsEOF(
void)
const {
return index >= length; }
328 int Length(
void)
const {
return length; }
329 int Index(
void)
const {
return (IsEOF() ? length : index); }
330 const uint8_t *
GetData(
void)
const {
return (IsEOF() ? NULL : data + (index / 8)); }
341 static uint64_t Now(
void);
342 void Set(
int Ms = 0);
343 bool TimedOut(
void)
const;
344 uint64_t Elapsed(
void)
const;
359 enum { MaxPollFiles = 16 };
360 pollfd pfd[MaxPollFiles];
363 cPoller(
int FileHandle = -1,
bool Out =
false);
364 bool Add(
int FileHandle,
bool Out);
365 bool Poll(
int TimeoutMs = 0);
374 char b[offsetof(
struct dirent, d_name) + NAME_MAX + 1];
379 bool Ok(
void) {
return directory != NULL; }
380 struct dirent *Next(
void);
391 operator int () {
return f; }
392 bool Open(
const char *FileName,
int Flags, mode_t Mode = DEFFILEMODE);
393 bool Open(
int FileDes);
396 bool Ready(
bool Wait =
true);
397 static bool AnyFileReady(
int FileDes = -1,
int TimeoutMs = 1000);
398 static bool FileReady(
int FileDes,
int TimeoutMs = 1000);
399 static bool FileReadyForWriting(
int FileDes,
int TimeoutMs = 1000);
410 operator FILE* () {
return f; }
430 int FadviseDrop(off_t Offset, off_t Len);
434 int Open(
const char *FileName,
int Flags, mode_t Mode = DEFFILEMODE);
436 void SetReadAhead(
size_t ra);
437 off_t Seek(off_t Offset,
int Whence);
438 ssize_t Read(
void *Data,
size_t Size);
439 ssize_t Write(
const void *Data,
size_t Size);
440 static cUnbufferedFile *Create(
const char *FileName,
int Flags, mode_t Mode = DEFFILEMODE);
450 bool Lock(
int WaitSeconds = 0);
466 int Index(
void)
const;
480 void Del(
cListObject *Object,
bool DeleteObject =
true);
481 virtual void Move(
int From,
int To);
483 virtual void Clear(
void);
485 int Count(
void)
const {
return count; }
492 T *
First(
void)
const {
return (T *)objects; }
493 T *
Last(
void)
const {
return (T *)lastObject; }
494 T *
Prev(
const T *
object)
const {
return (T *)
object->cListObject::Prev(); }
495 T *
Next(
const T *
object)
const {
return (T *)
object->cListObject::Next(); }
508 if (++Index > allocated) {
509 data = (T *)realloc(data, Index *
sizeof(T));
511 esyslog(
"ERROR: out of memory - abort!");
514 for (
int i = allocated; i < Index; i++)
528 T&
At(
int Index)
const 545 for (
int i = 0; i < size; i++) {
551 int Size(
void)
const {
return size; }
552 virtual void Insert(T Data,
int Before = 0)
556 memmove(&data[Before + 1], &data[Before], (size - Before) *
sizeof(T));
565 if (IndexOf(Data) < 0) {
566 Insert(Data, Before);
573 if (size >= allocated)
574 Realloc(allocated * 3 / 2);
579 if (IndexOf(Data) < 0) {
589 if (Index < size - 1)
590 memmove(&data[Index], &data[Index + 1], (size - Index) *
sizeof(T));
595 int i = IndexOf(Data);
604 for (
int i = 0; i < size; i++)
608 void Sort(__compar_fn_t Compare)
610 qsort(data, size,
sizeof(T), Compare);
616 return strcmp(*(
const char **)a, *(
const char **)b);
621 return strcasecmp(*(
const char **)a, *(
const char **)b);
628 int Find(
const char *s)
const;
629 void Sort(
bool IgnoreCase =
false)
636 virtual void Clear(
void);
641 cFileNameList(
const char *Directory = NULL,
bool DirsOnly =
false);
642 bool Load(
const char *Directory,
bool DirsOnly =
false);
659 unsigned int hashfn(
unsigned int Id)
const {
return Id % size; }
const char * operator*() const
cListObject * Get(unsigned int Id) const
bool RemoveElement(const T &Data)
static char * systemCharacterTable
const uint8_t * GetData(void) const
static const char * SystemCharacterTable(void)
virtual void Append(T Data)
int allocated
< cVector may only be used for simple types, like int or pointers - not for class objects that alloca...
cHashObject(cListObject *Object, unsigned int Id)
T & operator[](int Index)
cBitStream(const uint8_t *Data, int Length)
T * Get(unsigned int Id) const
int IndexOf(const T &Data)
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner...
cVector & operator=(const cVector &Vector)
cList< cHashObject > ** hashTable
virtual void Remove(int Index)
virtual int Compare(const cListObject &ListObject) const
Must return 0 if this object is equal to ListObject, a positive value if it is "greater", and a negative value if it is "smaller".
T * Next(const T *object) const
bool AppendUnique(T Data)
virtual void Insert(T Data, int Before=0)
cListObject * Next(void) const
cListObject * Object(void)
cVector(const cVector &Vector)
void Realloc(int Index) const
void Sort(__compar_fn_t Compare)
bool InsertUnique(T Data, int Before=0)
unsigned int hashfn(unsigned int Id) const
bool SetSystemCharacterTable(const char *CharacterTable)
static uint8_t * SetLength(uint8_t *Data, int Length)
const T & operator[](int Index) const
cListObject * Prev(void) const
cListObject * Get(int Index) const
void Sort(bool IgnoreCase=false)
T * Prev(const T *object) const
cStringList(int Allocated=10)
cVector(int Allocated=10)