vdr-1.4.7/tools.h

Go to the documentation of this file.
00001 /*
00002  * tools.h: Various tools
00003  *
00004  * See the main source file 'vdr.c' for copyright information and
00005  * how to reach the author.
00006  *
00007  * $Id: tools.h 1.96 2006/12/03 17:38:38 kls Exp $
00008  */
00009 
00010 #ifndef __TOOLS_H
00011 #define __TOOLS_H
00012 
00013 #include <dirent.h>
00014 #include <errno.h>
00015 #include <fcntl.h>
00016 #include <poll.h>
00017 #include <stddef.h>
00018 #include <stdint.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <syslog.h>
00022 #include <sys/stat.h>
00023 #include <sys/types.h>
00024 
00025 typedef unsigned char uchar;
00026 #define uint64 uint64_t // for compatibility - TODO remove in version 1.5
00027 
00028 extern int SysLogLevel;
00029 
00030 #define esyslog(a...) void( (SysLogLevel > 0) ? syslog_with_tid(LOG_ERR,   a) : void() )
00031 #define isyslog(a...) void( (SysLogLevel > 1) ? syslog_with_tid(LOG_INFO,  a) : void() )
00032 #define dsyslog(a...) void( (SysLogLevel > 2) ? syslog_with_tid(LOG_DEBUG, a) : void() )
00033 
00034 #define LOG_ERROR         esyslog("ERROR (%s,%d): %m", __FILE__, __LINE__)
00035 #define LOG_ERROR_STR(s)  esyslog("ERROR: %s: %m", s)
00036 
00037 #define SECSINDAY  86400
00038 
00039 #define KILOBYTE(n) ((n) * 1024)
00040 #define MEGABYTE(n) ((n) * 1024 * 1024)
00041 
00042 #define MALLOC(type, size)  (type *)malloc(sizeof(type) * (size))
00043 
00044 #define DELETENULL(p) (delete (p), p = NULL)
00045 
00046 #define CHECK(s) { if ((s) < 0) LOG_ERROR; } // used for 'ioctl()' calls
00047 #define FATALERRNO (errno && errno != EAGAIN && errno != EINTR)
00048 
00049 #ifndef __STL_CONFIG_H // in case some plugin needs to use the STL
00050 template<class T> inline T min(T a, T b) { return a <= b ? a : b; }
00051 template<class T> inline T max(T a, T b) { return a >= b ? a : b; }
00052 template<class T> inline int sgn(T a) { return a < 0 ? -1 : a > 0 ? 1 : 0; }
00053 template<class T> inline void swap(T &a, T &b) { T t = a; a = b; b = t; }
00054 #endif
00055 
00056 void syslog_with_tid(int priority, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
00057 
00058 #define BCDCHARTOINT(x) (10 * ((x & 0xF0) >> 4) + (x & 0xF))
00059 int BCD2INT(int x);
00060 
00061 // Unfortunately there are no platform independent macros for unaligned
00062 // access. so we do it this way:
00063 
00064 template<class T> inline T get_unaligned(T *p)
00065 {
00066   struct s { T v; } __attribute__((packed));
00067   return ((s *)p)->v;
00068 }
00069 
00070 template<class T> inline void put_unaligned(unsigned int v, T* p)
00071 {
00072   struct s { T v; } __attribute__((packed));
00073   ((s *)p)->v = v;
00074 }
00075 
00076 class cString {
00077 private:
00078   char *s;
00079 public:
00080   cString(const char *S = NULL, bool TakePointer = false);
00081   cString(const cString &String);
00082   virtual ~cString();
00083   operator const char * () const { return s; } // for use in (const char *) context
00084   const char * operator*() const { return s; } // for use in (const void *) context (printf() etc.)
00085   cString &operator=(const cString &String);
00086   static cString sprintf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
00087   };
00088 
00089 ssize_t safe_read(int filedes, void *buffer, size_t size);
00090 ssize_t safe_write(int filedes, const void *buffer, size_t size);
00091 void writechar(int filedes, char c);
00092 int WriteAllOrNothing(int fd, const uchar *Data, int Length, int TimeoutMs = 0, int RetryMs = 0);
00096 char *strcpyrealloc(char *dest, const char *src);
00097 char *strn0cpy(char *dest, const char *src, size_t n);
00098 char *strreplace(char *s, char c1, char c2);
00099 char *strreplace(char *s, const char *s1, const char *s2); 
00100 char *skipspace(const char *s);
00101 char *stripspace(char *s);
00102 char *compactspace(char *s);
00103 cString strescape(const char *s, const char *chars);
00104 bool startswith(const char *s, const char *p);
00105 bool endswith(const char *s, const char *p);
00106 bool isempty(const char *s);
00107 int numdigits(int n);
00108 bool isnumber(const char *s);
00109 cString itoa(int n);
00110 cString AddDirectory(const char *DirName, const char *FileName);
00111 int FreeDiskSpaceMB(const char *Directory, int *UsedMB = NULL);
00112 bool DirectoryOk(const char *DirName, bool LogErrors = false);
00113 bool MakeDirs(const char *FileName, bool IsDirectory = false);
00114 bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks = false);
00115 bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis = false);
00116 int DirSizeMB(const char *DirName); 
00117 char *ReadLink(const char *FileName); 
00118 bool SpinUpDisk(const char *FileName);
00119 void TouchFile(const char *FileName);
00120 time_t LastModifiedTime(const char *FileName);
00121 cString WeekDayName(int WeekDay);
00122 cString WeekDayName(time_t t);
00123 cString DayDateTime(time_t t = 0);
00124 cString TimeToString(time_t t);
00125 cString DateString(time_t t);
00126 cString TimeString(time_t t);
00127 uchar *RgbToJpeg(uchar *Mem, int Width, int Height, int &Size, int Quality = 100);
00136 
00137 class cBase64Encoder {
00138 private:
00139   const uchar *data;
00140   int length;
00141   int maxResult;
00142   int i;
00143   char *result;
00144   static const char *b64;
00145 public:
00146   cBase64Encoder(const uchar *Data, int Length, int MaxResult = 64);
00152   ~cBase64Encoder();
00153   const char *NextLine(void);
00159   };
00160 
00161 class cTimeMs {
00162 private:
00163   uint64_t begin;
00164 public:
00165   cTimeMs(void);
00166   static uint64_t Now(void);
00167   void Set(int Ms = 0);
00168   bool TimedOut(void);
00169   uint64_t Elapsed(void);
00170   };
00171 
00172 class cReadLine {
00173 private:
00174   size_t size;
00175   char *buffer;
00176 public:
00177   cReadLine(void);
00178   ~cReadLine();
00179   char *Read(FILE *f);
00180   };
00181 
00182 class cPoller {
00183 private:
00184   enum { MaxPollFiles = 16 };
00185   pollfd pfd[MaxPollFiles];
00186   int numFileHandles;
00187 public:
00188   cPoller(int FileHandle = -1, bool Out = false);
00189   bool Add(int FileHandle, bool Out);
00190   bool Poll(int TimeoutMs = 0);
00191   };
00192 
00193 class cReadDir {
00194 private:
00195   DIR *directory;
00196   struct dirent *result;
00197   union { // according to "The GNU C Library Reference Manual"
00198     struct dirent d;
00199     char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
00200     } u;
00201 public:
00202   cReadDir(const char *Directory);
00203   ~cReadDir();
00204   bool Ok(void) { return directory != NULL; }
00205   struct dirent *Next(void);
00206   };
00207 
00208 class cFile {
00209 private:
00210   static bool files[];
00211   static int maxFiles;
00212   int f;
00213 public:
00214   cFile(void);
00215   ~cFile();
00216   operator int () { return f; }
00217   bool Open(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
00218   bool Open(int FileDes);
00219   void Close(void);
00220   bool IsOpen(void) { return f >= 0; }
00221   bool Ready(bool Wait = true);
00222   static bool AnyFileReady(int FileDes = -1, int TimeoutMs = 1000);
00223   static bool FileReady(int FileDes, int TimeoutMs = 1000);
00224   static bool FileReadyForWriting(int FileDes, int TimeoutMs = 1000);
00225   };
00226 
00227 class cSafeFile {
00228 private:
00229   FILE *f;
00230   char *fileName;
00231   char *tempName;
00232 public:
00233   cSafeFile(const char *FileName);
00234   ~cSafeFile();
00235   operator FILE* () { return f; }
00236   bool Open(void);
00237   bool Close(void);
00238   };
00239 
00242 
00243 class cUnbufferedFile {
00244 private:
00245   int fd;
00246   off_t curpos;
00247   off_t cachedstart;
00248   off_t cachedend;
00249   off_t begin;
00250   off_t lastpos;
00251   off_t ahead;
00252   size_t readahead;
00253   size_t written;
00254   size_t totwritten;
00255   int FadviseDrop(off_t Offset, off_t Len);
00256 public:
00257   cUnbufferedFile(void);
00258   ~cUnbufferedFile();
00259   int Open(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
00260   int Close(void);
00261   void SetReadAhead(size_t ra);
00262   off_t Seek(off_t Offset, int Whence);
00263   ssize_t Read(void *Data, size_t Size);
00264   ssize_t Write(const void *Data, size_t Size);
00265   static cUnbufferedFile *Create(const char *FileName, int Flags, mode_t Mode = DEFFILEMODE);
00266   };
00267 
00268 class cLockFile {
00269 private:
00270   char *fileName;
00271   int f;
00272 public:
00273   cLockFile(const char *Directory);
00274   ~cLockFile();
00275   bool Lock(int WaitSeconds = 0);
00276   void Unlock(void);
00277   };
00278 
00279 class cListObject {
00280 private:
00281   cListObject *prev, *next;
00282 public:
00283   cListObject(void);
00284   virtual ~cListObject();
00285   virtual int Compare(const cListObject &ListObject) const { return 0; }
00288   void Append(cListObject *Object);
00289   void Insert(cListObject *Object);
00290   void Unlink(void);
00291   int Index(void) const;
00292   cListObject *Prev(void) const { return prev; }
00293   cListObject *Next(void) const { return next; }
00294   };
00295 
00296 class cListBase {
00297 protected:
00298   cListObject *objects, *lastObject;
00299   cListBase(void);
00300   int count;
00301 public:
00302   virtual ~cListBase();
00303   void Add(cListObject *Object, cListObject *After = NULL);
00304   void Ins(cListObject *Object, cListObject *Before = NULL);
00305   void Del(cListObject *Object, bool DeleteObject = true);
00306   virtual void Move(int From, int To);
00307   void Move(cListObject *From, cListObject *To);
00308   virtual void Clear(void);
00309   cListObject *Get(int Index) const;
00310   int Count(void) const { return count; }
00311   void Sort(void);
00312   };
00313 
00314 template<class T> class cList : public cListBase {
00315 public:
00316   T *Get(int Index) const { return (T *)cListBase::Get(Index); }
00317   T *First(void) const { return (T *)objects; }
00318   T *Last(void) const { return (T *)lastObject; }
00319   T *Prev(const T *object) const { return (T *)object->cListObject::Prev(); } // need to call cListObject's members to
00320   T *Next(const T *object) const { return (T *)object->cListObject::Next(); } // avoid ambiguities in case of a "list of lists"
00321   };
00322 
00323 class cHashObject : public cListObject {
00324   friend class cHashBase;
00325 private:
00326   unsigned int id;
00327   cListObject *object;
00328 public:
00329   cHashObject(cListObject *Object, unsigned int Id) { object = Object; id = Id; }
00330   cListObject *Object(void) { return object; }
00331   };
00332 
00333 class cHashBase {
00334 private:
00335   cList<cHashObject> **hashTable;
00336   int size;
00337   unsigned int hashfn(unsigned int Id) const { return Id % size; }
00338 protected:
00339   cHashBase(int Size);
00340 public:
00341   virtual ~cHashBase();
00342   void Add(cListObject *Object, unsigned int Id);
00343   void Del(cListObject *Object, unsigned int Id);
00344   void Clear(void);
00345   cListObject *Get(unsigned int Id) const;
00346   cList<cHashObject> *GetList(unsigned int Id) const;
00347   };
00348 
00349 #define HASHSIZE 512
00350 
00351 template<class T> class cHash : public cHashBase {
00352 public:
00353   cHash(int Size = HASHSIZE) : cHashBase(Size) {}
00354   T *Get(unsigned int Id) const { return (T *)cHashBase::Get(Id); }
00355 };
00356 
00357 #endif //__TOOLS_H

Generated on Tue Nov 6 19:57:56 2007 for VDR by  doxygen 1.5.3-20071008