vdr 2.6.8
tools.h
Go to the documentation of this file.
1/*
2 * tools.h: Various tools
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * $Id: tools.h 5.8 2024/01/20 13:59:55 kls Exp $
8 */
9
10#ifndef __TOOLS_H
11#define __TOOLS_H
12
13#include <dirent.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <float.h>
17#include <iconv.h>
18#include <math.h>
19#include <poll.h>
20#include <stdarg.h>
21#include <stddef.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syslog.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include "thread.h"
30
31typedef unsigned char uchar;
32
33extern int SysLogLevel;
34
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() )
38
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)
41
42#define SECSINDAY 86400
43
44#define KILOBYTE(n) ((n) * 1024)
45#define MEGABYTE(n) ((n) * 1024LL * 1024LL)
46
47#define MALLOC(type, size) (type *)malloc(sizeof(type) * (size))
48
49template<class T> inline void DELETENULL(T *&p) { T *q = p; p = NULL; delete q; }
50
51#define CHECK(s) { if ((s) < 0) LOG_ERROR; } // used for 'ioctl()' calls
52#define FATALERRNO (errno && errno != EAGAIN && errno != EINTR)
53
54#if __cplusplus >= 201103L
55// any gcc >= 4.8.1; we have swap, min, max
56#include <algorithm> // std::min, std::max, (c++98: also swap)
57#include <utility> // std::swap (since c++11)
58using std::min;
59using std::max;
60using std::swap;
61#else
62// no c++11 and old compiler, let's include our own templates
63template<class T> inline T min(T a, T b) { return a <= b ? a : b; }
64template<class T> inline T max(T a, T b) { return a >= b ? a : b; }
65template<class T> inline void swap(T &a, T &b) { T t = a; a = b; b = t; }
66#endif
67
68template<class T> inline int sgn(T a) { return a < 0 ? -1 : a > 0 ? 1 : 0; }
69
70template<class T> inline T constrain(T v, T l, T h) { return v < l ? l : v > h ? h : v; }
71
72void syslog_with_tid(int priority, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
73
74#define BCDCHARTOINT(x) (10 * ((x & 0xF0) >> 4) + (x & 0xF))
75int BCD2INT(int x);
76
77#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
78
79// Unfortunately there are no platform independent macros for unaligned
80// access, so we do it this way:
81
82template<class T> inline T get_unaligned(T *p)
83{
84 struct s { T v; } __attribute__((packed));
85 return ((s *)p)->v;
86}
87
88template<class T> inline void put_unaligned(unsigned int v, T* p)
89{
90 struct s { T v; } __attribute__((packed));
91 ((s *)p)->v = v;
92}
93
94// Comparing doubles for equality is unsafe, but unfortunately we can't
95// overwrite operator==(double, double), so this will have to do:
96
97inline bool DoubleEqual(double a, double b)
98{
99 return fabs(a - b) <= DBL_EPSILON;
100}
101
102// When handling strings that might contain UTF-8 characters, it may be necessary
103// to process a "symbol" that consists of several actual character bytes. The
104// following functions allow transparently accessing a "char *" string without
105// having to worry about what character set is actually used.
106
107int Utf8CharLen(const char *s);
110uint Utf8CharGet(const char *s, int Length = 0);
114int Utf8CharSet(uint c, char *s = NULL);
118int Utf8SymChars(const char *s, int Symbols);
121int Utf8StrLen(const char *s);
124char *Utf8Strn0Cpy(char *Dest, const char *Src, int n);
129int Utf8ToArray(const char *s, uint *a, int Size);
133int Utf8FromArray(const uint *a, char *s, int Size, int Max = -1);
139
140// When allocating buffer space, make sure we reserve enough space to hold
141// a string in UTF-8 representation:
142
143#define Utf8BufSize(s) ((s) * 4)
144
145// The following macros automatically use the correct versions of the character
146// class functions:
147
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))
150
152private:
153 iconv_t cd;
154 char *result;
155 size_t length;
157public:
158 cCharSetConv(const char *FromCode = NULL, const char *ToCode = NULL);
164 const char *Convert(const char *From, char *To = NULL, size_t ToLength = 0);
174 static const char *SystemCharacterTable(void) { return systemCharacterTable; }
175 static void SetSystemCharacterTable(const char *CharacterTable);
176 };
177
178class cString {
179private:
180 char *s;
181public:
182 cString(const char *S = NULL, bool TakePointer = false);
183 cString(const char *S, const char *To);
184 cString(const cString &String);
185 cString(cString &&String): s(String.s) { String.s = NULL; }
186 virtual ~cString();
187 operator const void * () const { return s; } // to catch cases where operator*() should be used
188 operator const char * () const { return s; } // for use in (const char *) context
189 const char * operator*() const { return s; } // for use in (const void *) context (printf() etc.)
190 cString &operator=(const cString &String);
191 cString &operator=(cString &&String);
192 cString &operator=(const char *String);
193 cString &Append(const char *String);
194 cString &Append(char c);
195 cString &Truncate(int Index);
196 cString &CompactChars(char c);
197 static cString sprintf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
198 static cString vsprintf(const char *fmt, va_list &ap);
199 };
200
202private:
203 char *p;
204 char c;
205public:
207 p = NULL;
208 c = 0;
209 }
211 Set(s);
212 }
214 if (p)
215 *p = c;
216 }
217 void Set(char *s) {
218 if (s) {
219 p = s;
220 c = *s;
221 *s = 0;
222 }
223 else
224 p = NULL;
225 }
226 };
227
228ssize_t safe_read(int filedes, void *buffer, size_t size);
229ssize_t safe_write(int filedes, const void *buffer, size_t size);
230void writechar(int filedes, char c);
231int WriteAllOrNothing(int fd, const uchar *Data, int Length, int TimeoutMs = 0, int RetryMs = 0);
235char *strcpyrealloc(char *dest, const char *src);
236char *strn0cpy(char *dest, const char *src, size_t n);
237char *strreplace(char *s, char c1, char c2);
238char *strreplace(char *s, const char *s1, const char *s2);
239const char *strchrn(const char *s, char c, size_t n);
240int strcountchr(const char *s, char c);
241cString strgetbefore(const char *s, char c, int n = 1); // returns the part of 's' before (and excluding) the n'th occurrence of 'c' from the right, or an empty string if there is no such 'c'.
242const char *strgetlast(const char *s, char c); // returns the part of 's' after the last occurrence of 'c', or 's' if there is no 'c'.
243inline char *strgetlast(char *s, char c) { return const_cast<char *>(strgetlast(static_cast<const char *>(s), c)); } // returns the part of 's' after the last occurrence of 'c', or 's' if there is no 'c'.
244inline char *skipspace(const char *s)
245{
246 if ((uchar)*s > ' ') // most strings don't have any leading space, so handle this case as fast as possible
247 return (char *)s;
248 while (*s && (uchar)*s <= ' ') // avoiding isspace() here, because it is much slower
249 s++;
250 return (char *)s;
251}
252char *stripspace(char *s);
253char *compactspace(char *s);
254char *compactchars(char *s, char c);
255cString strescape(const char *s, const char *chars);
256cString strgetval(const char *s, const char *name, char d = '=');
264char *strshift(char *s, int n);
270bool startswith(const char *s, const char *p);
271bool endswith(const char *s, const char *p);
272bool isempty(const char *s);
273int numdigits(int n);
274bool isnumber(const char *s);
275int64_t StrToNum(const char *s);
281bool StrInArray(const char *a[], const char *s);
284double atod(const char *s);
288cString dtoa(double d, const char *Format = "%f");
292cString itoa(int n);
293inline uint16_t Peek13(const uchar *p)
294{
295 uint16_t v = uint16_t(*p++ & 0x1F) << 8;
296 return v + (*p & 0xFF);
297}
298inline void Poke13(uchar *p, uint16_t v)
299{
300 v |= uint16_t(*p & ~0x1F) << 8;
301 *p++ = v >> 8;
302 *p = v & 0xFF;
303}
304cString AddDirectory(const char *DirName, const char *FileName);
305bool EntriesOnSameFileSystem(const char *File1, const char *File2);
309int FreeDiskSpaceMB(const char *Directory, int *UsedMB = NULL);
310bool DirectoryOk(const char *DirName, bool LogErrors = false);
311bool MakeDirs(const char *FileName, bool IsDirectory = false);
312bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks = false);
313bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis = false, const char *IgnoreFiles[] = NULL);
319int DirSizeMB(const char *DirName);
320char *ReadLink(const char *FileName);
321bool SpinUpDisk(const char *FileName);
322void TouchFile(const char *FileName);
323time_t LastModifiedTime(const char *FileName);
324off_t FileSize(const char *FileName);
325cString WeekDayName(int WeekDay);
328cString WeekDayName(time_t t);
330cString WeekDayNameFull(int WeekDay);
333cString WeekDayNameFull(time_t t);
335cString DayDateTime(time_t t = 0);
338cString TimeToString(time_t t);
340cString DateString(time_t t);
342cString ShortDateString(time_t t);
344cString TimeString(time_t t);
346uchar *RgbToJpeg(uchar *Mem, int Width, int Height, int &Size, int Quality = 100);
355const char *GetHostName(void);
357
359private:
360 const uchar *data;
363 int i;
364 char *result;
365 static const char *b64;
366public:
367 cBase64Encoder(const uchar *Data, int Length, int MaxResult = 64);
374 const char *NextLine(void);
380 };
381
383private:
384 const uint8_t *data;
385 int length; // in bits
386 int index; // in bits
387public:
388 cBitStream(const uint8_t *Data, int Length) : data(Data), length(Length), index(0) {}
390 int GetBit(void);
391 uint32_t GetBits(int n);
392 void ByteAlign(void);
393 void WordAlign(void);
394 bool SetLength(int Length);
395 void SkipBits(int n) { index += n; }
396 void SkipBit(void) { SkipBits(1); }
397 bool IsEOF(void) const { return index >= length; }
398 void Reset(void) { index = 0; }
399 int Length(void) const { return length; }
400 int Index(void) const { return (IsEOF() ? length : index); }
401 const uint8_t *GetData(void) const { return (IsEOF() ? NULL : data + (index / 8)); }
402 };
403
404class cTimeMs {
405private:
406 uint64_t begin;
407public:
408 cTimeMs(int Ms = 0);
412 static uint64_t Now(void);
413 void Set(int Ms = 0);
420 bool TimedOut(void) const;
421 uint64_t Elapsed(void) const;
422 };
423
425private:
426 size_t size;
427 char *buffer;
428public:
429 cReadLine(void);
430 ~cReadLine();
431 char *Read(FILE *f);
432 };
433
434class cPoller {
435private:
436 enum { MaxPollFiles = 64 };
437 pollfd pfd[MaxPollFiles];
439public:
440 cPoller(int FileHandle = -1, bool Out = false);
441 bool Add(int FileHandle, bool Out);
442 void Del(int FileHandle, bool Out);
443 bool Poll(int TimeoutMs = 0);
444 };
445
446class cReadDir {
447private:
449 struct dirent *result;
450#if !__GLIBC_PREREQ(2, 24) // readdir_r() is deprecated as of GLIBC 2.24
451 union { // according to "The GNU C Library Reference Manual"
452 struct dirent d;
453 char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
454 } u;
455#endif
456public:
457 cReadDir(const char *Directory);
458 ~cReadDir();
459 bool Ok(void) { return directory != NULL; }
460 struct dirent *Next(void);
461 };
462
463#ifndef DEPRECATED_CFILE
464#define DEPRECATED_CFILE 0
465#endif
466class cFile {
467private:
468#if DEPRECATED_CFILE
469 static bool files[];
470 static int maxFiles;
471#endif
472 int f;
473public:
474 cFile(void);
475 ~cFile();
476 operator int () { return f; }
477 bool Open(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
478 bool Open(int FileDes);
479 void Close(void);
480 bool IsOpen(void) { return f >= 0; }
481 bool Ready(bool Wait = true);
482#if DEPRECATED_CFILE
483 static bool AnyFileReady(int FileDes = -1, int TimeoutMs = 1000);
484#endif
485 static bool FileReady(int FileDes, int TimeoutMs = 1000);
486#if DEPRECATED_CFILE
487 static bool FileReadyForWriting(int FileDes, int TimeoutMs = 1000);
488#endif
489 };
490
492private:
493 FILE *f;
494 char *fileName;
495 char *tempName;
496public:
497 cSafeFile(const char *FileName);
498 ~cSafeFile();
499 operator FILE* () { return f; }
500 bool Open(void);
501 bool Close(void);
502 };
503
506
508private:
509 int fd;
510 off_t curpos;
513 off_t begin;
514 off_t lastpos;
515 off_t ahead;
516 size_t readahead;
517 size_t written;
519 int FadviseDrop(off_t Offset, off_t Len);
520public:
521 cUnbufferedFile(void);
523 int Open(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
524 int Close(void);
525 void SetReadAhead(size_t ra);
526 off_t Seek(off_t Offset, int Whence);
527 ssize_t Read(void *Data, size_t Size);
528 ssize_t Write(const void *Data, size_t Size);
529 static cUnbufferedFile *Create(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
530 };
531
533private:
534 char *fileName;
535 int f;
536public:
537 cLockFile(const char *Directory);
538 ~cLockFile();
539 bool Lock(int WaitSeconds = 0);
540 void Unlock(void);
541 };
542
545private:
547 cListObject(const cListObject &ListObject) { abort(); } // no copy constructor!
548 cListObject& operator= (const cListObject &ListObject) { abort(); return *this; } // no assignment operator!
549public:
550 cListObject(void);
551 virtual ~cListObject();
552 virtual int Compare(const cListObject &ListObject) const { return 0; }
555 void Append(cListObject *Object);
556 void Insert(cListObject *Object);
557 void Unlink(void);
558 int Index(void) const;
559 cListObject *Prev(void) const { return prev; }
560 cListObject *Next(void) const { return next; }
561 };
562
564private:
567 time_t lastPut;
568public:
571 void Put(cListObject *Object);
572 void Purge(bool Force = false);
573 };
574
576
578protected:
580 int count;
582 const char *needsLocking;
584 cListBase(const char *NeedsLocking = NULL);
585public:
586 virtual ~cListBase();
587 bool Lock(cStateKey &StateKey, bool Write = false, int TimeoutMs = 0) const;
612 void SetSyncStateKey(cStateKey &StateKey) { stateLock.SetSyncStateKey(StateKey); }
617 void SetUseGarbageCollector(void) { useGarbageCollector = true; }
618 void SetExplicitModify(void);
623 void SetModified(void);
625 void Add(cListObject *Object, cListObject *After = NULL);
626 void Ins(cListObject *Object, cListObject *Before = NULL);
627 void Del(cListObject *Object, bool DeleteObject = true);
628 virtual void Move(int From, int To);
629 void Move(cListObject *From, cListObject *To);
630 virtual void Clear(void);
631 bool Contains(const cListObject *Object) const;
638 const cListObject *Get(int Index) const;
639 cListObject *Get(int Index) { return const_cast<cListObject *>(static_cast<const cListBase *>(this)->Get(Index)); }
640 int Count(void) const { return count; }
641 void Sort(void);
642 };
643
644template<class T> class cList : public cListBase {
645public:
646 cList(const char *NeedsLocking = NULL): cListBase(NeedsLocking) {}
653 const T *Get(int Index) const { return (T *)cListBase::Get(Index); }
656 const T *First(void) const { return (T *)objects; }
658 const T *Last(void) const { return (T *)lastObject; }
660 const T *Prev(const T *Object) const { return (T *)Object->cListObject::Prev(); } // need to call cListObject's members to
663 const T *Next(const T *Object) const { return (T *)Object->cListObject::Next(); } // avoid ambiguities in case of a "list of lists"
666 T *Get(int Index) { return const_cast<T *>(static_cast<const cList<T> *>(this)->Get(Index)); }
668 T *First(void) { return const_cast<T *>(static_cast<const cList<T> *>(this)->First()); }
670 T *Last(void) { return const_cast<T *>(static_cast<const cList<T> *>(this)->Last()); }
672 T *Prev(const T *Object) { return const_cast<T *>(static_cast<const cList<T> *>(this)->Prev(Object)); }
674 T *Next(const T *Object) { return const_cast<T *>(static_cast<const cList<T> *>(this)->Next(Object)); }
676 };
677
678// The DEF_LIST_LOCK macro defines a convenience class that can be used to obtain
679// a lock on a cList and make sure the lock is released when the current scope
680// is left:
681
682#define DEF_LIST_LOCK2(Class, Name) \
683class c##Name##_Lock { \
684private: \
685 cStateKey stateKey; \
686 const c##Class *list; \
687public: \
688 c##Name##_Lock(bool Write = false) \
689 { \
690 if (Write) \
691 list = c##Class::Get##Name##Write(stateKey); \
692 else \
693 list = c##Class::Get##Name##Read(stateKey); \
694 } \
695 ~c##Name##_Lock() { if (list) stateKey.Remove(); } \
696 const c##Class *Name(void) const { return list; } \
697 c##Class *Name(void) { return const_cast<c##Class *>(list); } \
698 }
699#define DEF_LIST_LOCK(Class) DEF_LIST_LOCK2(Class, Class)
700
701// The USE_LIST_LOCK macro sets up a local variable of a class defined by
702// a suitable DEF_LIST_LOCK, and also a pointer to the provided list:
703
704#define USE_LIST_LOCK_READ2(Class, Name) \
705c##Name##_Lock Name##_Lock(false); \
706const c##Class *Name __attribute__((unused)) = Name##_Lock.Name();
707#define USE_LIST_LOCK_READ(Class) USE_LIST_LOCK_READ2(Class, Class)
708
709#define USE_LIST_LOCK_WRITE2(Class, Name) \
710c##Name##_Lock Name##_Lock(true); \
711c##Class *Name __attribute__((unused)) = Name##_Lock.Name();
712#define USE_LIST_LOCK_WRITE(Class) USE_LIST_LOCK_WRITE2(Class, Class)
713
714template<class T> class cVector {
716private:
717 mutable int allocated;
718 mutable int size;
719 mutable T *data;
720 cVector(const cVector &Vector) {} // don't copy...
721 cVector &operator=(const cVector &Vector) { return *this; } // ...or assign this!
722 void Realloc(int Index) const
723 {
724 if (++Index > allocated) {
725 data = (T *)realloc(data, Index * sizeof(T));
726 if (!data) {
727 esyslog("ERROR: out of memory - abort!");
728 abort();
729 }
730 for (int i = allocated; i < Index; i++)
731 data[i] = T(0);
732 allocated = Index;
733 }
734 }
735public:
736 cVector(int Allocated = 10)
737 {
738 allocated = 0;
739 size = 0;
740 data = NULL;
741 Realloc(Allocated);
742 }
743 virtual ~cVector() { free(data); }
744 T& At(int Index) const
745 {
746 Realloc(Index);
747 if (Index >= size)
748 size = Index + 1;
749 return data[Index];
750 }
751 const T& operator[](int Index) const
752 {
753 return At(Index);
754 }
755 T& operator[](int Index)
756 {
757 return At(Index);
758 }
759 int IndexOf(const T &Data) // returns the index of Data, or -1 if not found
760 {
761 for (int i = 0; i < size; i++) {
762 if (data[i] == Data)
763 return i;
764 }
765 return -1;
766 }
767 int Size(void) const { return size; }
768 virtual void Insert(T Data, int Before = 0)
769 {
770 if (Before < size) {
771 Realloc(size);
772 memmove(&data[Before + 1], &data[Before], (size - Before) * sizeof(T));
773 size++;
774 data[Before] = Data;
775 }
776 else
777 Append(Data);
778 }
779 bool InsertUnique(T Data, int Before = 0)
780 {
781 if (IndexOf(Data) < 0) {
782 Insert(Data, Before);
783 return true;
784 }
785 return false;
786 }
787 virtual void Append(T Data)
788 {
789 if (size >= allocated)
790 Realloc(allocated * 3 / 2); // increase size by 50%
791 data[size++] = Data;
792 }
793 bool AppendUnique(T Data)
794 {
795 if (IndexOf(Data) < 0) {
796 Append(Data);
797 return true;
798 }
799 return false;
800 }
801 virtual void Remove(int Index)
802 {
803 if (Index < 0)
804 return; // prevents out-of-bounds access
805 if (Index < size - 1)
806 memmove(&data[Index], &data[Index + 1], (size - Index) * sizeof(T));
807 size--;
808 }
809 bool RemoveElement(const T &Data)
810 {
811 int i = IndexOf(Data);
812 if (i >= 0) {
813 Remove(i);
814 return true;
815 }
816 return false;
817 }
818 virtual void Clear(void)
819 {
820 for (int i = 0; i < size; i++)
821 data[i] = T(0);
822 size = 0;
823 }
824 void Sort(__compar_fn_t Compare)
825 {
826 qsort(data, size, sizeof(T), Compare);
827 }
828 };
829
830inline int CompareInts(const void *a, const void *b)
831{
832 return *(const int *)a - *(const int *)b;
833}
834
835inline int CompareStrings(const void *a, const void *b)
836{
837 return strcmp(*(const char **)a, *(const char **)b);
838}
839
840inline int CompareStringsIgnoreCase(const void *a, const void *b)
841{
842 return strcasecmp(*(const char **)a, *(const char **)b);
843}
844
845inline int CompareStringsNumerically(const void *a, const void *b)
846{
847 int d = atoi(*(const char **)a) - atoi(*(const char **)b);
848 return d ? d : CompareStrings(a, b);
849}
850
851class cStringList : public cVector<char *> {
852public:
853 cStringList(int Allocated = 10): cVector<char *>(Allocated) {}
854 virtual ~cStringList();
855 int Find(const char *s) const;
856 void Sort(bool IgnoreCase = false)
857 {
858 if (IgnoreCase)
860 else
862 }
867 virtual void Clear(void);
868 };
869
871public:
872 cFileNameList(const char *Directory = NULL, bool DirsOnly = false);
873 bool Load(const char *Directory, bool DirsOnly = false);
874 };
875
877private:
880 int size; // the total size of the buffer (bytes in memory)
881 int used; // the number of used bytes, starting at the beginning of the buffer
882 bool Realloc(int NewSize);
883 bool Assert(int NewSize) { return size < NewSize ? Realloc(NewSize) : true; } // inline for performance!
884public:
885 cDynamicBuffer(int InitialSize = 1024);
887 void Append(const uchar *Data, int Length);
888 void Append(uchar Data) { if (Assert(used + 1)) buffer[used++] = Data; }
889 void Set(int Index, uchar Data) { if (Assert(Index + 1)) buffer[Index] = Data; }
890 uchar Get(int Index) { return Index < used ? buffer[Index] : 0; }
891 void Clear(void) { used = 0; }
892 uchar *Data(void) { return buffer; }
893 int Length(void) { return used; }
894 };
895
896class cHashObject : public cListObject {
897 friend class cHashBase;
898private:
899 unsigned int id;
901public:
902 cHashObject(cListObject *Object, unsigned int Id) { object = Object; id = Id; }
903 cListObject *Object(void) { return object; }
904 };
905
907private:
909 int size;
911 unsigned int hashfn(unsigned int Id) const { return Id % size; }
912protected:
913 cHashBase(int Size, bool OwnObjects);
918public:
919 virtual ~cHashBase();
920 void Add(cListObject *Object, unsigned int Id);
921 void Del(cListObject *Object, unsigned int Id);
922 void Clear(void);
923 cListObject *Get(unsigned int Id) const;
924 cList<cHashObject> *GetList(unsigned int Id) const;
925 };
926
927#define HASHSIZE 512
928
929template<class T> class cHash : public cHashBase {
930public:
931 cHash(int Size = HASHSIZE, bool OwnObjects = false) : cHashBase(Size, OwnObjects) {}
932 T *Get(unsigned int Id) const { return (T *)cHashBase::Get(Id); }
933};
934
935#endif //__TOOLS_H
static uint8_t * SetLength(uint8_t *Data, int Length)
Definition ci.c:57
char * result
Definition tools.h:364
const uchar * data
Definition tools.h:360
int maxResult
Definition tools.h:362
static const char * b64
Definition tools.h:365
void SkipBit(void)
Definition tools.h:396
int Index(void) const
Definition tools.h:400
cBitStream(const uint8_t *Data, int Length)
Definition tools.h:388
int length
Definition tools.h:385
const uint8_t * data
Definition tools.h:384
int index
Definition tools.h:386
int Length(void) const
Definition tools.h:399
bool IsEOF(void) const
Definition tools.h:397
const uint8_t * GetData(void) const
Definition tools.h:401
void SkipBits(int n)
Definition tools.h:395
~cBitStream()
Definition tools.h:389
void Reset(void)
Definition tools.h:398
cCharSetConv(const char *FromCode=NULL, const char *ToCode=NULL)
Sets up a character set converter to convert from FromCode to ToCode.
Definition tools.c:973
static const char * SystemCharacterTable(void)
Definition tools.h:174
static void SetSystemCharacterTable(const char *CharacterTable)
Definition tools.c:991
char * result
Definition tools.h:154
size_t length
Definition tools.h:155
iconv_t cd
Definition tools.h:153
static char * systemCharacterTable
Definition tools.h:156
~cCharSetConv()
Definition tools.c:984
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).
Definition tools.c:1014
void Set(int Index, uchar Data)
Definition tools.h:889
uchar Get(int Index)
Definition tools.h:890
int Length(void)
Definition tools.h:893
uchar * Data(void)
Definition tools.h:892
uchar * buffer
Definition tools.h:878
void Clear(void)
Definition tools.h:891
bool Assert(int NewSize)
Definition tools.h:883
void Append(uchar Data)
Definition tools.h:888
int initialSize
Definition tools.h:879
Definition tools.h:466
bool IsOpen(void)
Definition tools.h:480
int f
Definition tools.h:472
cListObject * Get(unsigned int Id) const
Definition tools.c:2454
cList< cHashObject > ** hashTable
Definition tools.h:908
int size
Definition tools.h:909
bool ownObjects
Definition tools.h:910
unsigned int hashfn(unsigned int Id) const
Definition tools.h:911
unsigned int id
Definition tools.h:899
cListObject * object
Definition tools.h:900
cListObject * Object(void)
Definition tools.h:903
cHashObject(cListObject *Object, unsigned int Id)
Definition tools.h:902
Definition tools.h:929
cHash(int Size=HASHSIZE, bool OwnObjects=false)
Definition tools.h:931
T * Get(unsigned int Id) const
Definition tools.h:932
cListObject * lastObject
Definition tools.h:579
cStateLock stateLock
Definition tools.h:581
bool useGarbageCollector
Definition tools.h:583
cListObject * Get(int Index)
Definition tools.h:639
void SetSyncStateKey(cStateKey &StateKey)
When making changes to this list (while holding a write lock) that shall not affect some other code t...
Definition tools.h:612
void SetUseGarbageCollector(void)
Definition tools.h:617
int count
Definition tools.h:580
const char * needsLocking
Definition tools.h:582
const cListObject * Get(int Index) const
Definition tools.c:2326
int Count(void) const
Definition tools.h:640
cListObject * objects
Definition tools.h:566
cListObject(const cListObject &ListObject)
Definition tools.h:547
cListObject * next
Definition tools.h:546
cListObject * Prev(void) const
Definition tools.h:559
virtual int Compare(const cListObject &ListObject) const
Must return 0 if this object is equal to ListObject, a positive value if it is "greater",...
Definition tools.h:552
cListObject * Next(void) const
Definition tools.h:560
Definition tools.h:644
T * Last(void)
Non-const version of Last().
Definition tools.h:670
T * Prev(const T *Object)
Non-const version of Prev().
Definition tools.h:672
T * First(void)
Non-const version of First().
Definition tools.h:668
const T * Prev(const T *Object) const
Definition tools.h:660
T * Get(int Index)
< Returns the element immediately following Object in this list, or NULL if Object is the last elemen...
Definition tools.h:666
const T * First(void) const
Returns the first element in this list, or NULL if the list is empty.
Definition tools.h:656
T * Next(const T *Object)
Non-const version of Next().
Definition tools.h:674
cList(const char *NeedsLocking=NULL)
Sets up a new cList of the given type T.
Definition tools.h:646
const T * Next(const T *Object) const
< Returns the element immediately before Object in this list, or NULL if Object is the first element ...
Definition tools.h:663
const T * Last(void) const
Returns the last element in this list, or NULL if the list is empty.
Definition tools.h:658
const T * Get(int Index) const
Returns the list element at the given Index, or NULL if no such element exists.
Definition tools.h:653
char * fileName
Definition tools.h:534
int f
Definition tools.h:535
char * p
Definition tools.h:203
void Set(char *s)
Definition tools.h:217
cNullTerminate(char *s)
Definition tools.h:210
cNullTerminate(void)
Definition tools.h:206
~cNullTerminate()
Definition tools.h:213
int numFileHandles
Definition tools.h:438
struct dirent * result
Definition tools.h:449
DIR * directory
Definition tools.h:448
bool Ok(void)
Definition tools.h:459
char * buffer
Definition tools.h:427
size_t size
Definition tools.h:426
char * tempName
Definition tools.h:495
char * fileName
Definition tools.h:494
FILE * f
Definition tools.h:493
void SetSyncStateKey(cStateKey &StateKey)
Sets the given StateKey to be synchronized to the state of this lock.
Definition thread.c:797
cStringList(int Allocated=10)
Definition tools.h:853
void Sort(bool IgnoreCase=false)
Definition tools.h:856
void SortNumerically(void)
Definition tools.h:863
cString & CompactChars(char c)
Compact any sequence of characters 'c' to a single character, and strip all of them from the beginnin...
Definition tools.c:1174
static cString static cString vsprintf(const char *fmt, va_list &ap)
Definition tools.c:1193
virtual ~cString()
Definition tools.c:1100
const char * operator*() const
Definition tools.h:189
cString(const char *S=NULL, bool TakePointer=false)
Definition tools.c:1076
static cString sprintf(const char *fmt,...) __attribute__((format(printf
Definition tools.c:1180
cString & operator=(const cString &String)
Definition tools.c:1105
cString(cString &&String)
Definition tools.h:185
char * s
Definition tools.h:180
cString & Append(const char *String)
Definition tools.c:1133
cString & Truncate(int Index)
Truncate the string at the given Index (if Index is < 0 it is counted from the end of the string).
Definition tools.c:1164
uint64_t begin
Definition tools.h:406
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner,...
Definition tools.h:507
off_t ahead
Definition tools.h:515
off_t begin
Definition tools.h:513
size_t readahead
Definition tools.h:516
size_t totwritten
Definition tools.h:518
off_t lastpos
Definition tools.h:514
off_t cachedstart
Definition tools.h:511
off_t cachedend
Definition tools.h:512
size_t written
Definition tools.h:517
off_t curpos
Definition tools.h:510
int Size(void) const
Definition tools.h:767
cVector(int Allocated=10)
Definition tools.h:736
bool AppendUnique(T Data)
Definition tools.h:793
void Realloc(int Index) const
Definition tools.h:722
void Sort(__compar_fn_t Compare)
Definition tools.h:824
int IndexOf(const T &Data)
Definition tools.h:759
T & operator[](int Index)
Definition tools.h:755
cVector & operator=(const cVector &Vector)
Definition tools.h:721
int allocated
< cVector may only be used for simple types, like int or pointers - not for class objects that alloca...
Definition tools.h:717
virtual void Clear(void)
Definition tools.h:818
virtual ~cVector()
Definition tools.h:743
virtual void Insert(T Data, int Before=0)
Definition tools.h:768
T * data
Definition tools.h:719
virtual void Append(T Data)
Definition tools.h:787
bool InsertUnique(T Data, int Before=0)
Definition tools.h:779
int size
Definition tools.h:718
T & At(int Index) const
Definition tools.h:744
virtual void Remove(int Index)
Definition tools.h:801
const T & operator[](int Index) const
Definition tools.h:751
bool RemoveElement(const T &Data)
Definition tools.h:809
cVector(const cVector &Vector)
Definition tools.h:720
struct __attribute__((packed))
Definition recording.c:2692
char * ReadLink(const char *FileName)
returns a new string allocated on the heap, which the caller must delete (or NULL in case of an error...
Definition tools.c:676
char * strcpyrealloc(char *dest, const char *src)
Definition tools.c:114
const char * strgetlast(const char *s, char c)
Definition tools.c:218
cString TimeString(time_t t)
Converts the given time to a string of the form "hh:mm".
Definition tools.c:1286
void TouchFile(const char *FileName)
Definition tools.c:722
cString WeekDayNameFull(int WeekDay)
Converts the given WeekDay (0=Sunday, 1=Monday, ...) to a full day name.
Definition tools.c:1224
T get_unaligned(T *p)
Definition tools.h:82
char * compactchars(char *s, char c)
removes all occurrences of 'c' from the beginning an end of 's' and replaces sequences of multiple 'c...
Definition tools.c:253
T constrain(T v, T l, T h)
Definition tools.h:70
char * Utf8Strn0Cpy(char *Dest, const char *Src, int n)
Copies at most n character bytes from Src to Dest, making sure that the resulting copy ends with a co...
Definition tools.c:904
bool isempty(const char *s)
Definition tools.c:354
int Utf8ToArray(const char *s, uint *a, int Size)
Converts the given character bytes (including the terminating 0) into an array of UTF-8 symbols of th...
Definition tools.c:923
char * strreplace(char *s, char c1, char c2)
Definition tools.c:139
#define HASHSIZE
Definition tools.h:927
cString strescape(const char *s, const char *chars)
Definition tools.c:277
int strcountchr(const char *s, char c)
returns the number of occurrences of 'c' in 's'.
Definition tools.c:196
cString TimeToString(time_t t)
Converts the given time to a string of the form "www mmm dd hh:mm:ss yyyy".
Definition tools.c:1256
bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis=false, const char *IgnoreFiles[]=NULL)
Removes all empty directories under the given directory DirName.
Definition tools.c:590
bool SpinUpDisk(const char *FileName)
Definition tools.c:690
int Utf8StrLen(const char *s)
Returns the number of UTF-8 symbols formed by the given string of character bytes.
Definition tools.c:892
int Utf8CharSet(uint c, char *s=NULL)
Converts the given UTF-8 symbol to a sequence of character bytes and copies them to the given string.
Definition tools.c:845
cString strgetbefore(const char *s, char c, int n=1)
Definition tools.c:208
uint16_t Peek13(const uchar *p)
Definition tools.h:293
cString WeekDayName(int WeekDay)
Converts the given WeekDay (0=Sunday, 1=Monday, ...) to a three letter day name.
Definition tools.c:1203
bool MakeDirs(const char *FileName, bool IsDirectory=false)
Definition tools.c:504
bool startswith(const char *s, const char *p)
Definition tools.c:334
unsigned char uchar
Definition tools.h:31
char * strshift(char *s, int n)
Shifts the given string to the left by the given number of bytes, thus removing the first n bytes fro...
Definition tools.c:322
const char * GetHostName(void)
Gets the host name of this machine.
Definition tools.c:1394
cString strgetval(const char *s, const char *name, char d='=')
Returns the value part of a 'name=value' pair in s.
Definition tools.c:300
int CompareStrings(const void *a, const void *b)
Definition tools.h:835
int CompareInts(const void *a, const void *b)
Definition tools.h:830
uchar * RgbToJpeg(uchar *Mem, int Width, int Height, int &Size, int Quality=100)
Converts the given Memory to a JPEG image and returns a pointer to the resulting image.
Definition tools.c:1351
int WriteAllOrNothing(int fd, const uchar *Data, int Length, int TimeoutMs=0, int RetryMs=0)
Writes either all Data to the given file descriptor, or nothing at all.
Definition tools.c:90
time_t LastModifiedTime(const char *FileName)
Definition tools.c:728
uint Utf8CharGet(const char *s, int Length=0)
Returns the UTF-8 symbol at the beginning of the given string.
Definition tools.c:830
char * compactspace(char *s)
Definition tools.c:236
int sgn(T a)
Definition tools.h:68
double atod(const char *s)
Converts the given string, which is a floating point number using a '.
Definition tools.c:416
cString ShortDateString(time_t t)
Converts the given time to a string of the form "dd.mm.yy".
Definition tools.c:1277
ssize_t safe_read(int filedes, void *buffer, size_t size)
Definition tools.c:53
void Poke13(uchar *p, uint16_t v)
Definition tools.h:298
bool StrInArray(const char *a[], const char *s)
Returns true if the string s is equal to one of the strings pointed to by the (NULL terminated) array...
Definition tools.c:395
char * skipspace(const char *s)
Definition tools.h:244
char * stripspace(char *s)
Definition tools.c:224
void DELETENULL(T *&p)
Definition tools.h:49
int FreeDiskSpaceMB(const char *Directory, int *UsedMB=NULL)
Definition tools.c:469
bool DoubleEqual(double a, double b)
Definition tools.h:97
ssize_t safe_write(int filedes, const void *buffer, size_t size)
Definition tools.c:65
int numdigits(int n)
Definition tools.c:359
int Utf8SymChars(const char *s, int Symbols)
Returns the number of character bytes at the beginning of the given string that form at most the give...
Definition tools.c:879
cString DayDateTime(time_t t=0)
Converts the given time to a string of the form "www dd.mm. hh:mm".
Definition tools.c:1245
int CompareStringsIgnoreCase(const void *a, const void *b)
Definition tools.h:840
bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks=false)
Definition tools.c:532
int DirSizeMB(const char *DirName)
returns the total size of the files in the given directory, or -1 in case of an error
Definition tools.c:644
cString DateString(time_t t)
Converts the given time to a string of the form "www dd.mm.yyyy".
Definition tools.c:1266
int SysLogLevel
Definition tools.c:31
cString dtoa(double d, const char *Format="%f")
Converts the given double value to a string, making sure it uses a '.
Definition tools.c:437
bool DirectoryOk(const char *DirName, bool LogErrors=false)
Definition tools.c:486
T min(T a, T b)
Definition tools.h:63
void swap(T &a, T &b)
Definition tools.h:65
int Utf8CharLen(const char *s)
Returns the number of character bytes at the beginning of the given string that form a UTF-8 symbol.
Definition tools.c:816
T max(T a, T b)
Definition tools.h:64
void syslog_with_tid(int priority, const char *format,...) __attribute__((format(printf
#define esyslog(a...)
Definition tools.h:35
off_t FileSize(const char *FileName)
returns the size of the given file, or -1 in case of an error (e.g. if the file doesn't exist)
Definition tools.c:736
int CompareStringsNumerically(const void *a, const void *b)
Definition tools.h:845
bool EntriesOnSameFileSystem(const char *File1, const char *File2)
Checks whether the given files are on the same file system.
Definition tools.c:454
char * strn0cpy(char *dest, const char *src, size_t n)
Definition tools.c:131
void put_unaligned(unsigned int v, T *p)
Definition tools.h:88
int Utf8FromArray(const uint *a, char *s, int Size, int Max=-1)
Converts the given array of UTF-8 symbols (including the terminating 0) into a sequence of character ...
Definition tools.c:941
int BCD2INT(int x)
Definition tools.c:45
bool endswith(const char *s, const char *p)
Definition tools.c:343
cString itoa(int n)
Definition tools.c:447
const char * strchrn(const char *s, char c, size_t n)
returns a pointer to the n'th occurrence (counting from 1) of c in s, or NULL if no such character wa...
Definition tools.c:183
bool isnumber(const char *s)
Definition tools.c:369
cString AddDirectory(const char *DirName, const char *FileName)
Definition tools.c:407
void writechar(int filedes, char c)
Definition tools.c:85
cListGarbageCollector ListGarbageCollector
Definition tools.c:2155
int64_t StrToNum(const char *s)
Converts the given string to a number.
Definition tools.c:380