Home

Dokumentation

Impressum

Dokumentation VDR
 

Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

tools.h

Go to the documentation of this file.
00001 
00010 #ifndef __TOOLS_H
00011 #define __TOOLS_H
00012 
00013 #include <errno.h>
00014 #include <fcntl.h>
00015 #include <poll.h>
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include <syslog.h>
00019 #include <sys/stat.h>
00020 #include <sys/types.h>
00021 
00022 typedef unsigned char uchar;
00023 typedef unsigned long long int uint64;
00024 
00025 extern int SysLogLevel;
00026 
00027 #define esyslog(a...) void( (SysLogLevel > 0) ? syslog(LOG_ERR,   a) : void() )
00028 #define isyslog(a...) void( (SysLogLevel > 1) ? syslog(LOG_INFO,  a) : void() )
00029 #define dsyslog(a...) void( (SysLogLevel > 2) ? syslog(LOG_DEBUG, a) : void() )
00030 
00031 #define LOG_ERROR         esyslog("ERROR (%s,%d): %m", __FILE__, __LINE__)
00032 #define LOG_ERROR_STR(s)  esyslog("ERROR: %s: %m", s)
00033 
00034 #define SECSINDAY  86400
00035 
00036 #define KILOBYTE(n) ((n) * 1024)
00037 #define MEGABYTE(n) ((n) * 1024 * 1024)
00038 
00039 #define MAXPARSEBUFFER KILOBYTE(10)
00040 
00041 #define MALLOC(type, size)  (type *)malloc(sizeof(type) * (size))
00042 
00043 #define DELETENULL(p) (delete (p), p = NULL)
00044 
00045 #define CHECK(s) { if ((s) < 0) LOG_ERROR; } // used for 'ioctl()' calls
00046 #define FATALERRNO (errno != EAGAIN && errno != EINTR)
00047 
00048 #ifndef __STL_CONFIG_H // in case some plugin needs to use the STL
00049 template<class T> inline T min(T a, T b) { return a <= b ? a : b; }
00050 template<class T> inline T max(T a, T b) { return a >= b ? a : b; }
00051 template<class T> inline int sgn(T a) { return a < 0 ? -1 : a > 0 ? 1 : 0; }
00052 template<class T> inline void swap(T &a, T &b) { T t = a; a = b; b = t; }
00053 #endif
00054 
00055 ssize_t safe_read(int filedes, void *buffer, size_t size);
00056 ssize_t safe_write(int filedes, const void *buffer, size_t size);
00057 void writechar(int filedes, char c);
00058 char *readline(FILE *f);
00059 char *strcpyrealloc(char *dest, const char *src);
00060 char *strn0cpy(char *dest, const char *src, size_t n);
00061 char *strreplace(char *s, char c1, char c2);
00062 char *strreplace(char *s, const char *s1, const char *s2); 
00063 char *skipspace(const char *s);
00064 char *stripspace(char *s);
00065 char *compactspace(char *s);
00066 const char *strescape(const char *s, const char *chars); 
00067 bool startswith(const char *s, const char *p);
00068 bool endswith(const char *s, const char *p);
00069 bool isempty(const char *s);
00070 int numdigits(int n);
00071 int time_ms(void);
00072 void delay_ms(int ms);
00073 bool isnumber(const char *s);
00074 const char *itoa(int n); 
00075 const char *AddDirectory(const char *DirName, const char *FileName); 
00076 int FreeDiskSpaceMB(const char *Directory, int *UsedMB = NULL);
00077 bool DirectoryOk(const char *DirName, bool LogErrors = false);
00078 bool MakeDirs(const char *FileName, bool IsDirectory = false);
00079 bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks = false);
00080 bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis = false);
00081 char *ReadLink(const char *FileName);
00082 bool SpinUpDisk(const char *FileName);
00083 const char *WeekDayName(int WeekDay); 
00084 const char *DayDateTime(time_t t = 0); 
00085 
00087 class cPoller {
00088 private:
00089   enum { MaxPollFiles = 16 };
00090   pollfd pfd[MaxPollFiles];
00091   int numFileHandles;
00092 public:
00093   cPoller(int FileHandle = -1, bool Out = false);
00094   bool Add(int FileHandle, bool Out);
00095   bool Poll(int TimeoutMs = 0);
00096   };
00097 
00099 class cFile {
00100 private:
00101   static bool files[];
00102   static int maxFiles;
00103   int f;
00104 public:
00105   cFile(void);
00106   ~cFile();
00107   operator int () { return f; }
00108   bool Open(const char *FileName, int Flags, mode_t Mode = S_IRUSR | S_IWUSR | S_IRGRP);
00109   bool Open(int FileDes);
00110   void Close(void);
00111   bool IsOpen(void) { return f >= 0; }
00112   bool Ready(bool Wait = true);
00113   static bool AnyFileReady(int FileDes = -1, int TimeoutMs = 1000);
00114   static bool FileReady(int FileDes, int TimeoutMs = 1000);
00115   static bool FileReadyForWriting(int FileDes, int TimeoutMs = 1000);
00116   };
00117 
00119 class cSafeFile {
00120 private:
00121   FILE *f;
00122   char *fileName;
00123   char *tempName;
00124 public:
00125   cSafeFile(const char *FileName);
00126   ~cSafeFile();
00127   operator FILE* () { return f; }
00128   bool Open(void);
00129   bool Close(void);
00130   };
00131 
00133 class cLockFile {
00134 private:
00135   char *fileName;
00136   int f;
00137 public:
00138   cLockFile(const char *Directory);
00139   ~cLockFile();
00140   bool Lock(int WaitSeconds = 0);
00141   void Unlock(void);
00142   };
00143 
00145 class cListObject {
00146 private:
00147   cListObject *prev, *next;
00148 public:
00149   cListObject(void);
00150   virtual ~cListObject();
00151   virtual bool operator< (const cListObject &ListObject) { return false; }
00152   void Append(cListObject *Object);
00153   void Insert(cListObject *Object);
00154   void Unlink(void);
00155   int Index(void);
00156   cListObject *Prev(void) const { return prev; }
00157   cListObject *Next(void) const { return next; }
00158   };
00159 
00161 class cListBase {
00162 protected:
00163   cListObject *objects, *lastObject;
00164   cListBase(void);
00165 public:
00166   virtual ~cListBase();
00167   void Add(cListObject *Object, cListObject *After = NULL);
00168   void Ins(cListObject *Object, cListObject *Before = NULL);
00169   void Del(cListObject *Object, bool DeleteObject = true);
00170   virtual void Move(int From, int To);
00171   void Move(cListObject *From, cListObject *To);
00172   virtual void Clear(void);
00173   cListObject *Get(int Index) const;
00174   int Count(void) const;
00175   void Sort(void);
00176   };
00177 
00179 template<class T> class cList : public cListBase {
00180 public:
00181   T *Get(int Index) const { return (T *)cListBase::Get(Index); }
00182   T *First(void) const { return (T *)objects; }
00183   T *Last(void) const { return (T *)lastObject; }
00184   T *Prev(const T *object) const { return (T *)object->cListObject::Prev(); } // need to call cListObject's members to
00185   T *Next(const T *object) const { return (T *)object->cListObject::Next(); } // avoid ambiguities in case of a "list of lists"
00186   };
00187 
00188 #endif //__TOOLS_H
00189 

Generated on Wed Feb 5 23:30:12 2003 for VDR by doxygen1.3-rc2