summaryrefslogtreecommitdiff
path: root/common.h
blob: c3907e76a5228b8ebb434f9179555e7a7546562d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
 *  GraphTFTng plugin for the Video Disk Recorder 
 * 
 *  common.h - A plugin for the Video Disk Recorder
 *
 *  (c) 2004 Lars Tegeler, Sascha Volkenandt
 *  (c) 2006-2014 Jörg Wendel
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 **/

#ifndef ___COMMON_H
#define ___COMMON_H

//***************************************************************************
// Includes
//***************************************************************************

#include <string>
#include <algorithm> 

#include <string.h>
#include <iconv.h>
#include <stdint.h>

class MemoryStruct;

//***************************************************************************
// 
//***************************************************************************

// since we compile with __STL_CONFIG_H

template<class T> inline T min(T a, T b) { return a <= b ? a : b; }
template<class T> inline T max(T a, T b) { return a >= b ? a : b; }

using std::string;

//***************************************************************************
// 
//***************************************************************************

enum TimeConst
{
   tmeSecondsPerMinute = 60,
   tmeSecondsPerHour = tmeSecondsPerMinute * 60,
   tmeSecondsPerDay = tmeSecondsPerHour *24
};

enum Misc
{
   success = 0,
   done    = success,
   fail    = -1,
   ignore  = -1,
   na      = -1,
   yes     = 1,
   on      = 1,
   off     = 0,
   no      = 0,
   TB      = 1
};

enum Sizes
{
   sizeStamp = 14,
   sizeTime = 6,
   sizeDate = 8,
   sizeHHMM = 4
};

enum LogDevice
{
   devNone,       // 0
   devStdOut,     // 1
   devSyslog,     // 2
   devFile        // 3
};

enum Eloquence
{
   eloOff,               // 0
   eloAlways,            // 1
   eloDetail,            // 2
   eloDebug,             // 3
   eloDebug1 = eloDebug, // 3
   eloDebug2,            // 4
   eloDebug3             // 5
};

extern int logLevel;
extern int logDevice;

void __attribute__ ((format(printf, 2, 3))) tell(int eloquence, const char* format, ...);

char* srealloc(void* ptr, size_t size);
const char* suffixOf(const char* path);
int fileExists(string filename);
int loadFromFile(const char* infile, MemoryStruct* data);
int jpegDimensions(const char* path, unsigned int& pWidth, unsigned int& pHeight);

#ifdef VDR_PLUGIN
int toUTF8(char* out, int outMax, const char* in);
#endif

uint64_t msNow();

//***************************************************************************
// MemoryStruct
//***************************************************************************

struct MemoryStruct
{
   public:

      MemoryStruct()   { expireAt = 0; memory = 0; zmemory = 0; clear(); }
      MemoryStruct(const MemoryStruct* o)
      {
         size = o->size;
         memory = (char*)malloc(size);
         memcpy(memory, o->memory, size);

         zsize = o->zsize;
         zmemory = (char*)malloc(zsize);
         memcpy(zmemory, o->zmemory, zsize);

         copyAttributes(o);
      }
      
      ~MemoryStruct()  { clear(); }

      int isEmpty()  { return memory == 0; }

      int append(const char* buf, int len)
      {
         memory = srealloc(memory, size+len);
         memcpy(memory+size, buf, len);
         size += len;

         return success;
      }

      void copyAttributes(const MemoryStruct* o)
      {
         strcpy(tag, o->tag);
         strcpy(name, o->name);
         strcpy(contentType, o->contentType);
         strcpy(contentEncoding, o->contentEncoding);
         strcpy(mimeType, o->mimeType);
         headerOnly = o->headerOnly;
         modTime = o->modTime;
         expireAt = o->expireAt;
      }

      void clear() 
      {
         free(memory);
         memory = 0;
         size = 0;
         free(zmemory);
         zmemory = 0;
         zsize = 0;
         *tag = 0;
         *name = 0;
         *contentType = 0;
         *contentEncoding = 0;
         *mimeType = 0;
         modTime = time(0);
         headerOnly = no;
         // expireAt = time(0); -> don't reset 'expireAt' here !!!!
      }

      // data
      
      char* memory;
      long unsigned int size;

      char* zmemory;
      long unsigned int zsize;
      
      // tag attribute
      
      char tag[100+TB];              // the tag to be compared 
      char name[100+TB];             // content name (filename)
      char contentType[100+TB];      // e.g. text/html
      char mimeType[100+TB];         // 
      char contentEncoding[100+TB];  // 
      int headerOnly;
      time_t modTime;
      time_t expireAt;
};

//***************************************************************************
// RGBA Stuff
//***************************************************************************

typedef unsigned char t_rgba[4];
typedef unsigned char* p_rgba;

enum RGB
{
   rgbR,
   rgbG,
   rgbB,
   rgbA
};

p_rgba str2rgba(const char* value, p_rgba rgba);
p_rgba int2rgba(int r, int g, int b, int a, p_rgba rgba);
void rgba2int(p_rgba rgba, int& r, int& g, int& b, int& a);

//***************************************************************************
// Wrapper Regual Expression Library
//***************************************************************************

enum Option
{
  repUseRegularExpression = 1,
  repIgnoreCase = 2
};

int rep(const char* string, const char* expression, Option options = repUseRegularExpression);

int rep(const char* string, const char* expression, 
        const char*& s_location, Option options = repUseRegularExpression);

int rep(const char* string, const char* expression, const char*& s_location, 
        const char*& e_location, Option options = repUseRegularExpression);


//***************************************************************************
// Log Duration
//***************************************************************************

class LogDuration
{
   public:

      LogDuration(const char* aMessage, int aLogLevel = 2);
      ~LogDuration();

      void show(const char* label = "");

   protected:

      char message[1000];
      uint64_t durationStart;
      int logLevel;
};

//***************************************************************************
// std::string trim stuff
//***************************************************************************

static inline std::string &ltrim(std::string &s) 
{
   s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
   return s;
}

static inline std::string &rtrim(std::string &s) 
{
   s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
   return s;
}

static inline std::string &trim(std::string &s) 
{
   return ltrim(rtrim(s));
}

//***************************************************************************
// Class Str
//***************************************************************************

class Str
{
   public:

      enum Case
      {
         cUpper,
         cLower
      };

      // Manipulation

      static char* rTrim(char* buf);
      static char* lTrim(char* buf);
      static char* allTrim(char* buf);
      static const char* toCase(Case cs, char* str);

      // converting

      static const char* toStr(const char* s);
      static const char* toStr(bool value);
      static const char* toStr(int value);
      static const char* toStr(double value, int precision = 2);

      // Checks

      static int isEmpty(const char* buf);
      static int isBlank(const char* buf);
      static const char* notNull(const char* s)   { return s ? s : ""; }
};

int clen(const char* s);
string replaceChar(string str, char ch1, char ch2);

//***************************************************************************
#endif //___COMMON_H