vdr  2.4.7
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 4.18 2020/09/16 13:48:33 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 
31 typedef unsigned char uchar;
32 
33 extern 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 
49 template<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 // In case some plugin needs to use the STL and gets an error message regarding one
55 // of these functions, you can #define DISABLE_TEMPLATES_COLLIDING_WITH_STL before
56 // including any VDR header files.
57 #if !defined(DISABLE_TEMPLATES_COLLIDING_WITH_STL)
58 template<class T> inline T min(T a, T b) { return a <= b ? a : b; }
59 template<class T> inline T max(T a, T b) { return a >= b ? a : b; }
60 #endif
61 template<class T> inline int sgn(T a) { return a < 0 ? -1 : a > 0 ? 1 : 0; }
62 #if !defined(DISABLE_TEMPLATES_COLLIDING_WITH_STL)
63 template<class T> inline void swap(T &a, T &b) { T t = a; a = b; b = t; }
64 #endif
65 
66 template<class T> inline T constrain(T v, T l, T h) { return v < l ? l : v > h ? h : v; }
67 
68 void syslog_with_tid(int priority, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
69 
70 #define BCDCHARTOINT(x) (10 * ((x & 0xF0) >> 4) + (x & 0xF))
71 int BCD2INT(int x);
72 
73 #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 
75 // Unfortunately there are no platform independent macros for unaligned
76 // access, so we do it this way:
77 
78 template<class T> inline T get_unaligned(T *p)
79 {
80  struct s { T v; } __attribute__((packed));
81  return ((s *)p)->v;
82 }
83 
84 template<class T> inline void put_unaligned(unsigned int v, T* p)
85 {
86  struct s { T v; } __attribute__((packed));
87  ((s *)p)->v = v;
88 }
89 
90 // Comparing doubles for equality is unsafe, but unfortunately we can't
91 // overwrite operator==(double, double), so this will have to do:
92 
93 inline bool DoubleEqual(double a, double b)
94 {
95  return fabs(a - b) <= DBL_EPSILON;
96 }
97 
98 // When handling strings that might contain UTF-8 characters, it may be necessary
99 // to process a "symbol" that consists of several actual character bytes. The
100 // following functions allow transparently accessing a "char *" string without
101 // having to worry about what character set is actually used.
102 
103 int Utf8CharLen(const char *s);
106 uint Utf8CharGet(const char *s, int Length = 0);
110 int Utf8CharSet(uint c, char *s = NULL);
114 int Utf8SymChars(const char *s, int Symbols);
117 int Utf8StrLen(const char *s);
120 char *Utf8Strn0Cpy(char *Dest, const char *Src, int n);
125 int Utf8ToArray(const char *s, uint *a, int Size);
129 int Utf8FromArray(const uint *a, char *s, int Size, int Max = -1);
135 
136 // When allocating buffer space, make sure we reserve enough space to hold
137 // a string in UTF-8 representation:
138 
139 #define Utf8BufSize(s) ((s) * 4)
140 
141 // The following macros automatically use the correct versions of the character
142 // class functions:
143 
144 #define Utf8to(conv, c) (cCharSetConv::SystemCharacterTable() ? to##conv(c) : tow##conv(c))
145 #define Utf8is(ccls, c) (cCharSetConv::SystemCharacterTable() ? is##ccls(c) : isw##ccls(c))
146 
148 private:
149  iconv_t cd;
150  char *result;
151  size_t length;
152  static char *systemCharacterTable;
153 public:
154  cCharSetConv(const char *FromCode = NULL, const char *ToCode = NULL);
159  ~cCharSetConv();
160  const char *Convert(const char *From, char *To = NULL, size_t ToLength = 0);
170  static const char *SystemCharacterTable(void) { return systemCharacterTable; }
171  static void SetSystemCharacterTable(const char *CharacterTable);
172  };
173 
174 class cString {
175 private:
176  char *s;
177 public:
178  cString(const char *S = NULL, bool TakePointer = false);
179  cString(const char *S, const char *To);
180  cString(const cString &String);
181  virtual ~cString();
182  operator const void * () const { return s; } // to catch cases where operator*() should be used
183  operator const char * () const { return s; } // for use in (const char *) context
184  const char * operator*() const { return s; } // for use in (const void *) context (printf() etc.)
185  cString &operator=(const cString &String);
186  cString &operator=(const char *String);
187  cString &Append(const char *String);
188  cString &Truncate(int Index);
189  cString &CompactChars(char c);
190  static cString sprintf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
191  static cString vsprintf(const char *fmt, va_list &ap);
192  };
193 
194 ssize_t safe_read(int filedes, void *buffer, size_t size);
195 ssize_t safe_write(int filedes, const void *buffer, size_t size);
196 void writechar(int filedes, char c);
197 int WriteAllOrNothing(int fd, const uchar *Data, int Length, int TimeoutMs = 0, int RetryMs = 0);
201 char *strcpyrealloc(char *dest, const char *src);
202 char *strn0cpy(char *dest, const char *src, size_t n);
203 char *strreplace(char *s, char c1, char c2);
204 char *strreplace(char *s, const char *s1, const char *s2);
205 const char *strchrn(const char *s, char c, size_t n);
206 int strcountchr(const char *s, char c);
207 inline char *skipspace(const char *s)
208 {
209  if ((uchar)*s > ' ') // most strings don't have any leading space, so handle this case as fast as possible
210  return (char *)s;
211  while (*s && (uchar)*s <= ' ') // avoiding isspace() here, because it is much slower
212  s++;
213  return (char *)s;
214 }
215 char *stripspace(char *s);
216 char *compactspace(char *s);
217 char *compactchars(char *s, char c);
218 cString strescape(const char *s, const char *chars);
219 cString strgetval(const char *s, const char *name, char d = '=');
227 char *strshift(char *s, int n);
233 bool startswith(const char *s, const char *p);
234 bool endswith(const char *s, const char *p);
235 bool isempty(const char *s);
236 int numdigits(int n);
237 bool isnumber(const char *s);
238 int64_t StrToNum(const char *s);
244 bool StrInArray(const char *a[], const char *s);
247 double atod(const char *s);
251 cString dtoa(double d, const char *Format = "%f");
255 cString itoa(int n);
256 inline uint16_t Peek13(const uchar *p)
257 {
258  uint16_t v = uint16_t(*p++ & 0x1F) << 8;
259  return v + (*p & 0xFF);
260 }
261 inline void Poke13(uchar *p, uint16_t v)
262 {
263  v |= uint16_t(*p & ~0x1F) << 8;
264  *p++ = v >> 8;
265  *p = v & 0xFF;
266 }
267 cString AddDirectory(const char *DirName, const char *FileName);
268 bool EntriesOnSameFileSystem(const char *File1, const char *File2);
272 int FreeDiskSpaceMB(const char *Directory, int *UsedMB = NULL);
273 bool DirectoryOk(const char *DirName, bool LogErrors = false);
274 bool MakeDirs(const char *FileName, bool IsDirectory = false);
275 bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks = false);
276 bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis = false, const char *IgnoreFiles[] = NULL);
282 int DirSizeMB(const char *DirName);
283 char *ReadLink(const char *FileName);
284 bool SpinUpDisk(const char *FileName);
285 void TouchFile(const char *FileName);
286 time_t LastModifiedTime(const char *FileName);
287 off_t FileSize(const char *FileName);
288 cString WeekDayName(int WeekDay);
291 cString WeekDayName(time_t t);
293 cString WeekDayNameFull(int WeekDay);
296 cString WeekDayNameFull(time_t t);
298 cString DayDateTime(time_t t = 0);
301 cString TimeToString(time_t t);
303 cString DateString(time_t t);
305 cString ShortDateString(time_t t);
307 cString TimeString(time_t t);
309 uchar *RgbToJpeg(uchar *Mem, int Width, int Height, int &Size, int Quality = 100);
318 const char *GetHostName(void);
320 
322 private:
323  const uchar *data;
324  int length;
326  int i;
327  char *result;
328  static const char *b64;
329 public:
330  cBase64Encoder(const uchar *Data, int Length, int MaxResult = 64);
336  ~cBase64Encoder();
337  const char *NextLine(void);
343  };
344 
345 class cBitStream {
346 private:
347  const uint8_t *data;
348  int length; // in bits
349  int index; // in bits
350 public:
351  cBitStream(const uint8_t *Data, int Length) : data(Data), length(Length), index(0) {}
353  int GetBit(void);
354  uint32_t GetBits(int n);
355  void ByteAlign(void);
356  void WordAlign(void);
357  bool SetLength(int Length);
358  void SkipBits(int n) { index += n; }
359  void SkipBit(void) { SkipBits(1); }
360  bool IsEOF(void) const { return index >= length; }
361  void Reset(void) { index = 0; }
362  int Length(void) const { return length; }
363  int Index(void) const { return (IsEOF() ? length : index); }
364  const uint8_t *GetData(void) const { return (IsEOF() ? NULL : data + (index / 8)); }
365  };
366 
367 class cTimeMs {
368 private:
369  uint64_t begin;
370 public:
371  cTimeMs(int Ms = 0);
375  static uint64_t Now(void);
376  void Set(int Ms = 0);
383  bool TimedOut(void) const;
384  uint64_t Elapsed(void) const;
385  };
386 
387 class cReadLine {
388 private:
389  size_t size;
390  char *buffer;
391 public:
392  cReadLine(void);
393  ~cReadLine();
394  char *Read(FILE *f);
395  };
396 
397 class cPoller {
398 private:
399  enum { MaxPollFiles = 64 };
400  pollfd pfd[MaxPollFiles];
402 public:
403  cPoller(int FileHandle = -1, bool Out = false);
404  bool Add(int FileHandle, bool Out);
405  void Del(int FileHandle, bool Out);
406  bool Poll(int TimeoutMs = 0);
407  };
408 
409 class cReadDir {
410 private:
411  DIR *directory;
412  struct dirent *result;
413 #if !__GLIBC_PREREQ(2, 24) // readdir_r() is deprecated as of GLIBC 2.24
414  union { // according to "The GNU C Library Reference Manual"
415  struct dirent d;
416  char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
417  } u;
418 #endif
419 public:
420  cReadDir(const char *Directory);
421  ~cReadDir();
422  bool Ok(void) { return directory != NULL; }
423  struct dirent *Next(void);
424  };
425 
426 class cFile {
427 private:
428  static bool files[];
429  static int maxFiles;
430  int f;
431 public:
432  cFile(void);
433  ~cFile();
434  operator int () { return f; }
435  bool Open(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
436  bool Open(int FileDes);
437  void Close(void);
438  bool IsOpen(void) { return f >= 0; }
439  bool Ready(bool Wait = true);
440  static bool AnyFileReady(int FileDes = -1, int TimeoutMs = 1000);
441  static bool FileReady(int FileDes, int TimeoutMs = 1000);
442  static bool FileReadyForWriting(int FileDes, int TimeoutMs = 1000);
443  };
444 
445 class cSafeFile {
446 private:
447  FILE *f;
448  char *fileName;
449  char *tempName;
450 public:
451  cSafeFile(const char *FileName);
452  ~cSafeFile();
453  operator FILE* () { return f; }
454  bool Open(void);
455  bool Close(void);
456  };
457 
460 
462 private:
463  int fd;
464  off_t curpos;
465  off_t cachedstart;
466  off_t cachedend;
467  off_t begin;
468  off_t lastpos;
469  off_t ahead;
470  size_t readahead;
471  size_t written;
472  size_t totwritten;
473  int FadviseDrop(off_t Offset, off_t Len);
474 public:
475  cUnbufferedFile(void);
476  ~cUnbufferedFile();
477  int Open(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
478  int Close(void);
479  void SetReadAhead(size_t ra);
480  off_t Seek(off_t Offset, int Whence);
481  ssize_t Read(void *Data, size_t Size);
482  ssize_t Write(const void *Data, size_t Size);
483  static cUnbufferedFile *Create(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
484  };
485 
486 class cLockFile {
487 private:
488  char *fileName;
489  int f;
490 public:
491  cLockFile(const char *Directory);
492  ~cLockFile();
493  bool Lock(int WaitSeconds = 0);
494  void Unlock(void);
495  };
496 
497 class cListObject {
498  friend class cListGarbageCollector;
499 private:
500  cListObject *prev, *next;
501  cListObject(const cListObject &ListObject) { abort(); } // no copy constructor!
502  cListObject& operator= (const cListObject &ListObject) { abort(); return *this; } // no assignment operator!
503 public:
504  cListObject(void);
505  virtual ~cListObject();
506  virtual int Compare(const cListObject &ListObject) const { return 0; }
509  void Append(cListObject *Object);
510  void Insert(cListObject *Object);
511  void Unlink(void);
512  int Index(void) const;
513  cListObject *Prev(void) const { return prev; }
514  cListObject *Next(void) const { return next; }
515  };
516 
518 private:
521  time_t lastPut;
522 public:
523  cListGarbageCollector(void);
525  void Put(cListObject *Object);
526  void Purge(bool Force = false);
527  };
528 
530 
531 class cListBase {
532 protected:
534  int count;
536  const char *needsLocking;
538  cListBase(const char *NeedsLocking = NULL);
539 public:
540  virtual ~cListBase();
541  bool Lock(cStateKey &StateKey, bool Write = false, int TimeoutMs = 0) const;
566  void SetSyncStateKey(cStateKey &StateKey) { stateLock.SetSyncStateKey(StateKey); }
571  void SetUseGarbageCollector(void) { useGarbageCollector = true; }
572  void SetExplicitModify(void);
577  void SetModified(void);
579  void Add(cListObject *Object, cListObject *After = NULL);
580  void Ins(cListObject *Object, cListObject *Before = NULL);
581  void Del(cListObject *Object, bool DeleteObject = true);
582  virtual void Move(int From, int To);
583  void Move(cListObject *From, cListObject *To);
584  virtual void Clear(void);
585  bool Contains(const cListObject *Object) const;
592  const cListObject *Get(int Index) const;
593  cListObject *Get(int Index) { return const_cast<cListObject *>(static_cast<const cListBase *>(this)->Get(Index)); }
594  int Count(void) const { return count; }
595  void Sort(void);
596  };
597 
598 template<class T> class cList : public cListBase {
599 public:
600  cList(const char *NeedsLocking = NULL): cListBase(NeedsLocking) {}
607  const T *Get(int Index) const { return (T *)cListBase::Get(Index); }
610  const T *First(void) const { return (T *)objects; }
612  const T *Last(void) const { return (T *)lastObject; }
614  const T *Prev(const T *Object) const { return (T *)Object->cListObject::Prev(); } // need to call cListObject's members to
617  const T *Next(const T *Object) const { return (T *)Object->cListObject::Next(); } // avoid ambiguities in case of a "list of lists"
620  T *Get(int Index) { return const_cast<T *>(static_cast<const cList<T> *>(this)->Get(Index)); }
622  T *First(void) { return const_cast<T *>(static_cast<const cList<T> *>(this)->First()); }
624  T *Last(void) { return const_cast<T *>(static_cast<const cList<T> *>(this)->Last()); }
626  T *Prev(const T *Object) { return const_cast<T *>(static_cast<const cList<T> *>(this)->Prev(Object)); }
628  T *Next(const T *Object) { return const_cast<T *>(static_cast<const cList<T> *>(this)->Next(Object)); }
630  };
631 
632 // The DEF_LIST_LOCK macro defines a convenience class that can be used to obtain
633 // a lock on a cList and make sure the lock is released when the current scope
634 // is left:
635 
636 #define DEF_LIST_LOCK2(Class, Name) \
637 class c##Name##_Lock { \
638 private: \
639  cStateKey stateKey; \
640  const c##Class *list; \
641 public: \
642  c##Name##_Lock(bool Write = false) \
643  { \
644  if (Write) \
645  list = c##Class::Get##Name##Write(stateKey); \
646  else \
647  list = c##Class::Get##Name##Read(stateKey); \
648  } \
649  ~c##Name##_Lock() { if (list) stateKey.Remove(); } \
650  const c##Class *Name(void) const { return list; } \
651  c##Class *Name(void) { return const_cast<c##Class *>(list); } \
652  }
653 #define DEF_LIST_LOCK(Class) DEF_LIST_LOCK2(Class, Class)
654 
655 // The USE_LIST_LOCK macro sets up a local variable of a class defined by
656 // a suitable DEF_LIST_LOCK, and also a pointer to the provided list:
657 
658 #define USE_LIST_LOCK_READ2(Class, Name) \
659 c##Name##_Lock Name##_Lock(false); \
660 const c##Class *Name __attribute__((unused)) = Name##_Lock.Name();
661 #define USE_LIST_LOCK_READ(Class) USE_LIST_LOCK_READ2(Class, Class)
662 
663 #define USE_LIST_LOCK_WRITE2(Class, Name) \
664 c##Name##_Lock Name##_Lock(true); \
665 c##Class *Name __attribute__((unused)) = Name##_Lock.Name();
666 #define USE_LIST_LOCK_WRITE(Class) USE_LIST_LOCK_WRITE2(Class, Class)
667 
668 template<class T> class cVector {
670 private:
671  mutable int allocated;
672  mutable int size;
673  mutable T *data;
674  cVector(const cVector &Vector) {} // don't copy...
675  cVector &operator=(const cVector &Vector) { return *this; } // ...or assign this!
676  void Realloc(int Index) const
677  {
678  if (++Index > allocated) {
679  data = (T *)realloc(data, Index * sizeof(T));
680  if (!data) {
681  esyslog("ERROR: out of memory - abort!");
682  abort();
683  }
684  for (int i = allocated; i < Index; i++)
685  data[i] = T(0);
686  allocated = Index;
687  }
688  }
689 public:
690  cVector(int Allocated = 10)
691  {
692  allocated = 0;
693  size = 0;
694  data = NULL;
695  Realloc(Allocated);
696  }
697  virtual ~cVector() { free(data); }
698  T& At(int Index) const
699  {
700  Realloc(Index);
701  if (Index >= size)
702  size = Index + 1;
703  return data[Index];
704  }
705  const T& operator[](int Index) const
706  {
707  return At(Index);
708  }
709  T& operator[](int Index)
710  {
711  return At(Index);
712  }
713  int IndexOf(const T &Data) // returns the index of Data, or -1 if not found
714  {
715  for (int i = 0; i < size; i++) {
716  if (data[i] == Data)
717  return i;
718  }
719  return -1;
720  }
721  int Size(void) const { return size; }
722  virtual void Insert(T Data, int Before = 0)
723  {
724  if (Before < size) {
725  Realloc(size);
726  memmove(&data[Before + 1], &data[Before], (size - Before) * sizeof(T));
727  size++;
728  data[Before] = Data;
729  }
730  else
731  Append(Data);
732  }
733  bool InsertUnique(T Data, int Before = 0)
734  {
735  if (IndexOf(Data) < 0) {
736  Insert(Data, Before);
737  return true;
738  }
739  return false;
740  }
741  virtual void Append(T Data)
742  {
743  if (size >= allocated)
744  Realloc(allocated * 3 / 2); // increase size by 50%
745  data[size++] = Data;
746  }
747  bool AppendUnique(T Data)
748  {
749  if (IndexOf(Data) < 0) {
750  Append(Data);
751  return true;
752  }
753  return false;
754  }
755  virtual void Remove(int Index)
756  {
757  if (Index < 0)
758  return; // prevents out-of-bounds access
759  if (Index < size - 1)
760  memmove(&data[Index], &data[Index + 1], (size - Index) * sizeof(T));
761  size--;
762  }
763  bool RemoveElement(const T &Data)
764  {
765  int i = IndexOf(Data);
766  if (i >= 0) {
767  Remove(i);
768  return true;
769  }
770  return false;
771  }
772  virtual void Clear(void)
773  {
774  for (int i = 0; i < size; i++)
775  data[i] = T(0);
776  size = 0;
777  }
778  void Sort(__compar_fn_t Compare)
779  {
780  qsort(data, size, sizeof(T), Compare);
781  }
782  };
783 
784 inline int CompareInts(const void *a, const void *b)
785 {
786  return *(const int *)a - *(const int *)b;
787 }
788 
789 inline int CompareStrings(const void *a, const void *b)
790 {
791  return strcmp(*(const char **)a, *(const char **)b);
792 }
793 
794 inline int CompareStringsIgnoreCase(const void *a, const void *b)
795 {
796  return strcasecmp(*(const char **)a, *(const char **)b);
797 }
798 
799 inline int CompareStringsNumerically(const void *a, const void *b)
800 {
801  int d = atoi(*(const char **)a) - atoi(*(const char **)b);
802  return d ? d : CompareStrings(a, b);
803 }
804 
805 class cStringList : public cVector<char *> {
806 public:
807  cStringList(int Allocated = 10): cVector<char *>(Allocated) {}
808  virtual ~cStringList();
809  int Find(const char *s) const;
810  void Sort(bool IgnoreCase = false)
811  {
812  if (IgnoreCase)
814  else
816  }
817  void SortNumerically(void)
818  {
820  }
821  virtual void Clear(void);
822  };
823 
824 class cFileNameList : public cStringList {
825 public:
826  cFileNameList(const char *Directory = NULL, bool DirsOnly = false);
827  bool Load(const char *Directory, bool DirsOnly = false);
828  };
829 
831 private:
834  int size; // the total size of the buffer (bytes in memory)
835  int used; // the number of used bytes, starting at the beginning of the buffer
836  bool Realloc(int NewSize);
837  bool Assert(int NewSize) { return size < NewSize ? Realloc(NewSize) : true; } // inline for performance!
838 public:
839  cDynamicBuffer(int InitialSize = 1024);
840  ~cDynamicBuffer();
841  void Append(const uchar *Data, int Length);
842  void Append(uchar Data) { if (Assert(used + 1)) buffer[used++] = Data; }
843  void Set(int Index, uchar Data) { if (Assert(Index + 1)) buffer[Index] = Data; }
844  uchar Get(int Index) { return Index < used ? buffer[Index] : 0; }
845  void Clear(void) { used = 0; }
846  uchar *Data(void) { return buffer; }
847  int Length(void) { return used; }
848  };
849 
850 class cHashObject : public cListObject {
851  friend class cHashBase;
852 private:
853  unsigned int id;
855 public:
856  cHashObject(cListObject *Object, unsigned int Id) { object = Object; id = Id; }
857  cListObject *Object(void) { return object; }
858  };
859 
860 class cHashBase {
861 private:
863  int size;
865  unsigned int hashfn(unsigned int Id) const { return Id % size; }
866 protected:
867  cHashBase(int Size, bool OwnObjects);
872 public:
873  virtual ~cHashBase();
874  void Add(cListObject *Object, unsigned int Id);
875  void Del(cListObject *Object, unsigned int Id);
876  void Clear(void);
877  cListObject *Get(unsigned int Id) const;
878  cList<cHashObject> *GetList(unsigned int Id) const;
879  };
880 
881 #define HASHSIZE 512
882 
883 template<class T> class cHash : public cHashBase {
884 public:
885  cHash(int Size = HASHSIZE, bool OwnObjects = false) : cHashBase(Size, OwnObjects) {}
886  T *Get(unsigned int Id) const { return (T *)cHashBase::Get(Id); }
887 };
888 
889 #endif //__TOOLS_H
static uint8_t * SetLength(uint8_t *Data, int Length)
Definition: ci.c:57
char * result
Definition: tools.h:327
int length
Definition: tools.h:324
const uchar * data
Definition: tools.h:323
int maxResult
Definition: tools.h:325
static const char * b64
Definition: tools.h:328
void SkipBit(void)
Definition: tools.h:359
const uint8_t * GetData(void) const
Definition: tools.h:364
int Index(void) const
Definition: tools.h:363
cBitStream(const uint8_t *Data, int Length)
Definition: tools.h:351
int length
Definition: tools.h:348
const uint8_t * data
Definition: tools.h:347
int index
Definition: tools.h:349
int Length(void) const
Definition: tools.h:362
bool IsEOF(void) const
Definition: tools.h:360
void SkipBits(int n)
Definition: tools.h:358
~cBitStream()
Definition: tools.h:352
void Reset(void)
Definition: tools.h:361
cCharSetConv(const char *FromCode=NULL, const char *ToCode=NULL)
Sets up a character set converter to convert from FromCode to ToCode.
Definition: tools.c:952
static void SetSystemCharacterTable(const char *CharacterTable)
Definition: tools.c:970
char * result
Definition: tools.h:150
size_t length
Definition: tools.h:151
iconv_t cd
Definition: tools.h:149
static char * systemCharacterTable
Definition: tools.h:152
~cCharSetConv()
Definition: tools.c:963
static const char * SystemCharacterTable(void)
Definition: tools.h:170
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:993
void Set(int Index, uchar Data)
Definition: tools.h:843
uchar Get(int Index)
Definition: tools.h:844
int Length(void)
Definition: tools.h:847
uchar * Data(void)
Definition: tools.h:846
uchar * buffer
Definition: tools.h:832
void Clear(void)
Definition: tools.h:845
bool Assert(int NewSize)
Definition: tools.h:837
void Append(uchar Data)
Definition: tools.h:842
int initialSize
Definition: tools.h:833
Definition: tools.h:426
static int maxFiles
Definition: tools.h:429
bool IsOpen(void)
Definition: tools.h:438
int f
Definition: tools.h:430
cListObject * Get(unsigned int Id) const
Definition: tools.c:2393
cList< cHashObject > ** hashTable
Definition: tools.h:862
int size
Definition: tools.h:863
bool ownObjects
Definition: tools.h:864
unsigned int hashfn(unsigned int Id) const
Definition: tools.h:865
cListObject * Object(void)
Definition: tools.h:857
unsigned int id
Definition: tools.h:853
cListObject * object
Definition: tools.h:854
cHashObject(cListObject *Object, unsigned int Id)
Definition: tools.h:856
Definition: tools.h:883
T * Get(unsigned int Id) const
Definition: tools.h:886
cHash(int Size=HASHSIZE, bool OwnObjects=false)
Definition: tools.h:885
cListObject * Get(int Index)
Definition: tools.h:593
cListObject * lastObject
Definition: tools.h:533
cStateLock stateLock
Definition: tools.h:535
bool useGarbageCollector
Definition: tools.h:537
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:566
void SetUseGarbageCollector(void)
Definition: tools.h:571
int count
Definition: tools.h:534
const char * needsLocking
Definition: tools.h:536
const cListObject * Get(int Index) const
Definition: tools.c:2265
int Count(void) const
Definition: tools.h:594
cListObject * objects
Definition: tools.h:520
cListObject(const cListObject &ListObject)
Definition: tools.h:501
cListObject * next
Definition: tools.h:500
cListObject * Prev(void) const
Definition: tools.h:513
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:506
cListObject * Next(void) const
Definition: tools.h:514
Definition: tools.h:598
T * Prev(const T *Object)
Non-const version of Prev().
Definition: tools.h:626
T * Next(const T *Object)
Non-const version of Next().
Definition: tools.h:628
T * Last(void)
Non-const version of Last().
Definition: tools.h:624
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:617
const T * Last(void) const
Returns the last element in this list, or NULL if the list is empty.
Definition: tools.h:612
T * First(void)
Non-const version of First().
Definition: tools.h:622
T * Get(int Index)
< Returns the element immediately following Object in this list, or NULL if Object is the last elemen...
Definition: tools.h:620
const T * First(void) const
Returns the first element in this list, or NULL if the list is empty.
Definition: tools.h:610
cList(const char *NeedsLocking=NULL)
Sets up a new cList of the given type T.
Definition: tools.h:600
const T * Get(int Index) const
Returns the list element at the given Index, or NULL if no such element exists.
Definition: tools.h:607
const T * Prev(const T *Object) const
Definition: tools.h:614
char * fileName
Definition: tools.h:488
int f
Definition: tools.h:489
Definition: thread.h:67
Definition: tools.h:397
int numFileHandles
Definition: tools.h:401
struct dirent * result
Definition: tools.h:412
DIR * directory
Definition: tools.h:411
bool Ok(void)
Definition: tools.h:422
char * buffer
Definition: tools.h:390
size_t size
Definition: tools.h:389
char * tempName
Definition: tools.h:449
char * fileName
Definition: tools.h:448
FILE * f
Definition: tools.h:447
void SetSyncStateKey(cStateKey &StateKey)
Sets the given StateKey to be synchronized to the state of this lock.
Definition: thread.c:789
cStringList(int Allocated=10)
Definition: tools.h:807
void Sort(bool IgnoreCase=false)
Definition: tools.h:810
void SortNumerically(void)
Definition: tools.h:817
Definition: tools.h:174
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:1127
static cString static cString vsprintf(const char *fmt, va_list &ap)
Definition: tools.c:1146
virtual ~cString()
Definition: tools.c:1079
cString(const char *S=NULL, bool TakePointer=false)
Definition: tools.c:1055
static cString sprintf(const char *fmt,...) __attribute__((format(printf
Definition: tools.c:1133
cString & operator=(const cString &String)
Definition: tools.c:1084
const char * operator*() const
Definition: tools.h:184
char * s
Definition: tools.h:176
cString & Append(const char *String)
Definition: tools.c:1102
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:1117
Definition: tools.h:367
uint64_t begin
Definition: tools.h:369
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner,...
Definition: tools.h:461
off_t ahead
Definition: tools.h:469
off_t begin
Definition: tools.h:467
size_t readahead
Definition: tools.h:470
size_t totwritten
Definition: tools.h:472
off_t lastpos
Definition: tools.h:468
off_t cachedstart
Definition: tools.h:465
off_t cachedend
Definition: tools.h:466
size_t written
Definition: tools.h:471
off_t curpos
Definition: tools.h:464
Definition: tools.h:668
int Size(void) const
Definition: tools.h:721
cVector(int Allocated=10)
Definition: tools.h:690
bool AppendUnique(T Data)
Definition: tools.h:747
void Realloc(int Index) const
Definition: tools.h:676
void Sort(__compar_fn_t Compare)
Definition: tools.h:778
int IndexOf(const T &Data)
Definition: tools.h:713
int allocated
< cVector may only be used for simple types, like int or pointers - not for class objects that alloca...
Definition: tools.h:671
virtual void Clear(void)
Definition: tools.h:772
cVector & operator=(const cVector &Vector)
Definition: tools.h:675
virtual ~cVector()
Definition: tools.h:697
virtual void Insert(T Data, int Before=0)
Definition: tools.h:722
T * data
Definition: tools.h:673
virtual void Append(T Data)
Definition: tools.h:741
bool InsertUnique(T Data, int Before=0)
Definition: tools.h:733
T & operator[](int Index)
Definition: tools.h:709
int size
Definition: tools.h:672
const T & operator[](int Index) const
Definition: tools.h:705
virtual void Remove(int Index)
Definition: tools.h:755
T & At(int Index) const
Definition: tools.h:698
bool RemoveElement(const T &Data)
Definition: tools.h:763
cVector(const cVector &Vector)
Definition: tools.h:674
struct __attribute__((packed))
Definition: recording.c:2509
cString TimeString(time_t t)
Converts the given time to a string of the form "hh:mm".
Definition: tools.c:1239
void TouchFile(const char *FileName)
Definition: tools.c:701
cString WeekDayNameFull(int WeekDay)
Converts the given WeekDay (0=Sunday, 1=Monday, ...) to a full day name.
Definition: tools.c:1177
char * strcpyrealloc(char *dest, const char *src)
Definition: tools.c:114
T get_unaligned(T *p)
Definition: tools.h:78
T constrain(T v, T l, T h)
Definition: tools.h:66
char * skipspace(const char *s)
Definition: tools.h:207
bool isempty(const char *s)
Definition: tools.c:333
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:902
#define HASHSIZE
Definition: tools.h:881
cString strescape(const char *s, const char *chars)
Definition: tools.c:256
int strcountchr(const char *s, char c)
returns the number of occurrences of 'c' in 's'.
Definition: tools.c:191
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:1209
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:883
bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis=false, const char *IgnoreFiles[]=NULL)
Removes all empty directories under the given directory DirName.
Definition: tools.c:569
bool SpinUpDisk(const char *FileName)
Definition: tools.c:669
int Utf8StrLen(const char *s)
Returns the number of UTF-8 symbols formed by the given string of character bytes.
Definition: tools.c:871
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:824
uint16_t Peek13(const uchar *p)
Definition: tools.h:256
cString WeekDayName(int WeekDay)
Converts the given WeekDay (0=Sunday, 1=Monday, ...) to a three letter day name.
Definition: tools.c:1156
bool MakeDirs(const char *FileName, bool IsDirectory=false)
Definition: tools.c:483
bool startswith(const char *s, const char *p)
Definition: tools.c:313
unsigned char uchar
Definition: tools.h:31
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:232
cString strgetval(const char *s, const char *name, char d='=')
Returns the value part of a 'name=value' pair in s.
Definition: tools.c:279
int CompareStrings(const void *a, const void *b)
Definition: tools.h:789
int CompareInts(const void *a, const void *b)
Definition: tools.h:784
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:707
uint Utf8CharGet(const char *s, int Length=0)
Returns the UTF-8 symbol at the beginning of the given string.
Definition: tools.c:809
int sgn(T a)
Definition: tools.h:61
double atod(const char *s)
Converts the given string, which is a floating point number using a '.
Definition: tools.c:395
cString ShortDateString(time_t t)
Converts the given time to a string of the form "dd.mm.yy".
Definition: tools.c:1230
char * strreplace(char *s, char c1, char c2)
Definition: tools.c:139
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:261
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:374
const char * GetHostName(void)
Gets the host name of this machine.
Definition: tools.c:1347
void DELETENULL(T *&p)
Definition: tools.h:49
int FreeDiskSpaceMB(const char *Directory, int *UsedMB=NULL)
Definition: tools.c:448
bool DoubleEqual(double a, double b)
Definition: tools.h:93
char * stripspace(char *s)
Definition: tools.c:203
ssize_t safe_write(int filedes, const void *buffer, size_t size)
Definition: tools.c:65
int numdigits(int n)
Definition: tools.c:338
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:858
cString DayDateTime(time_t t=0)
Converts the given time to a string of the form "www dd.mm. hh:mm".
Definition: tools.c:1198
int CompareStringsIgnoreCase(const void *a, const void *b)
Definition: tools.h:794
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:655
bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks=false)
Definition: tools.c:511
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:623
cString DateString(time_t t)
Converts the given time to a string of the form "www dd.mm.yyyy".
Definition: tools.c:1219
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:416
bool DirectoryOk(const char *DirName, bool LogErrors=false)
Definition: tools.c:465
T min(T a, T b)
Definition: tools.h:58
char * strn0cpy(char *dest, const char *src, size_t n)
Definition: tools.c:131
void swap(T &a, T &b)
Definition: tools.h:63
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:795
T max(T a, T b)
Definition: tools.h:59
void syslog_with_tid(int priority, const char *format,...) __attribute__((format(printf
#define esyslog(a...)
Definition: tools.h:35
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:301
char * compactspace(char *s)
Definition: tools.c:215
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:715
int CompareStringsNumerically(const void *a, const void *b)
Definition: tools.h:799
bool EntriesOnSameFileSystem(const char *File1, const char *File2)
Checks whether the given files are on the same file system.
Definition: tools.c:433
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:178
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:1304
void put_unaligned(unsigned int v, T *p)
Definition: tools.h:84
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:920
int BCD2INT(int x)
Definition: tools.c:45
bool endswith(const char *s, const char *p)
Definition: tools.c:322
cString itoa(int n)
Definition: tools.c:426
bool isnumber(const char *s)
Definition: tools.c:348
cString AddDirectory(const char *DirName, const char *FileName)
Definition: tools.c:386
void writechar(int filedes, char c)
Definition: tools.c:85
cListGarbageCollector ListGarbageCollector
Definition: tools.c:2094
int64_t StrToNum(const char *s)
Converts the given string to a number.
Definition: tools.c:359