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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/*
* common.h: EPG2VDR plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#ifndef __COMMON_H
#define __COMMON_H
#include <stdint.h> // uint_64_t
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <openssl/md5.h> // MD5_*
//#ifdef VDR_PLUGIN
//# include <vdr/tools.h>
//# include <vdr/thread.h>
//#endif
#ifdef USELIBXML
# include <libxslt/transform.h>
# include <libxslt/xsltutils.h>
# include <libexslt/exslt.h>
#endif
//***************************************************************************
// Misc
//***************************************************************************
// #ifndef VDR_PLUGIN
inline long min(long a, long b) { return a < b ? a : b; }
inline long max(long a, long b) { return a > b ? a : b; }
// #endif
enum Misc
{
success = 0,
done = success,
fail = -1,
na = -1,
ignore = -2,
all = -3,
abrt = -4,
yes = 1,
on = 1,
off = 0,
no = 0,
TB = 1,
sizeMd5 = 2 * MD5_DIGEST_LENGTH,
sizeUuid = 36,
tmeSecondsPerMinute = 60,
tmeSecondsPerHour = tmeSecondsPerMinute * 60,
tmeSecondsPerDay = 24 * tmeSecondsPerHour
};
enum Case
{
cUpper,
cLower
};
const char* toCase(Case cs, char* str);
//***************************************************************************
// Tell
//***************************************************************************
extern const char* logPrefix;
const char* getLogPrefix();
void __attribute__ ((format(printf, 2, 3))) tell(int eloquence, const char* format, ...);
//***************************************************************************
//
//***************************************************************************
char* srealloc(void* ptr, size_t size);
//***************************************************************************
// MemoryStruct
//***************************************************************************
struct MemoryStruct
{
public:
MemoryStruct() { expireAt = time(0); memory = 0; clear(); }
MemoryStruct(const MemoryStruct* o)
{
size = o->size;
memory = (char*)malloc(size);
memcpy(memory, o->memory, size);
copyAttributes(o);
}
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;
}
~MemoryStruct() { clear(); }
int append(const char* buf, int len)
{
memory = srealloc(memory, size+len);
memcpy(memory+size, buf, len);
size += len;
return success;
}
// data
char* memory;
size_t size;
// tag attribute
char tag[100]; // the tag to be compared
char name[100]; // content name (filename)
char contentType[100]; // e.g. text/html
char mimeType[100]; //
char contentEncoding[100]; //
int headerOnly;
time_t modTime;
time_t expireAt;
int isEmpty() { return memory == 0; }
void clear()
{
free(memory);
memory = 0;
size = 0;
*tag = 0;
*name = 0;
*contentType = 0;
*contentEncoding = 0;
*mimeType = 0;
modTime = time(0);
// !!!! expireAt = time(0);
headerOnly = no;
}
};
//***************************************************************************
// Tools
//***************************************************************************
double usNow();
unsigned int getHostId();
const char* getHostName();
const char* getFirstIp();
#ifdef USEUUID
const char* getUniqueId();
#endif
void removeChars(std::string& str, const char* ignore);
void removeCharsExcept(std::string& str, const char* except);
void removeWord(std::string& pattern, std::string word);
void prepareCompressed(std::string& pattern);
char* rTrim(char* buf);
char* lTrim(char* buf);
char* allTrim(char* buf);
char* sstrcpy(char* dest, const char* src, int max);
std::string num2Str(int num);
std::string l2pTime(time_t t);
std::string ms2Dur(uint64_t t);
const char* c2s(char c, char* buf);
int urlUnescape(char* dst, const char* src, int normalize = yes);
int storeToFile(const char* filename, const char* data, int size);
int loadFromFile(const char* infile, MemoryStruct* data);
int fileExists(const char* path);
int fileSize(const char* path);
time_t fileModTime(const char* path);
int createLink(const char* link, const char* dest, int force);
int isLink(const char* path);
const char* suffixOf(const char* path);
int isEmpty(const char* str);
const char* notNull(const char* str);
int isZero(const char* str);
int removeFile(const char* filename);
int chkDir(const char* path);
int downloadFile(const char* url, int& size, MemoryStruct* data, int timeout = 30, const char* userAgent = "libcurl-agent/1.0");
#ifdef USELIBXML
xsltStylesheetPtr loadXSLT(const char* name, const char* path, int utf8);
#endif
#ifdef USEMD5
typedef char md5Buf[sizeMd5+TB];
typedef char md5;
int createMd5(const char* buf, md5* md5);
int createMd5OfFile(const char* path, const char* name, md5* md5);
#endif
//***************************************************************************
// Zip
//***************************************************************************
int gunzip(MemoryStruct* zippedData, MemoryStruct* unzippedData);
int gzip(MemoryStruct* data, MemoryStruct* zippedData);
void tellZipError(int errorCode, const char* op, const char* msg);
#ifdef USELIBARCHIVE
int unzip(const char* file, const char* filter, char*& buffer,
int& size, char* entryName);
#endif
//***************************************************************************
// cMyMutex
//***************************************************************************
class cMyMutex
{
friend class cCondVar;
private:
pthread_mutex_t mutex;
int locked;
public:
cMyMutex(void);
~cMyMutex();
void Lock(void);
void Unlock(void);
};
//***************************************************************************
// cMyTimeMs
//***************************************************************************
class cMyTimeMs
{
private:
uint64_t begin;
public:
cMyTimeMs(int Ms = 0);
static uint64_t Now(void);
void Set(int Ms = 0);
bool TimedOut(void);
uint64_t Elapsed(void);
};
//***************************************************************************
// 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;
};
//***************************************************************************
// Semaphore
//***************************************************************************
#include <sys/sem.h>
class Sem
{
public:
Sem(key_t aKey)
{
locked = no;
key = aKey;
if ((id = semget(key, 1, 0666 | IPC_CREAT)) == -1)
tell(0, "Error: Can't get semaphore, errno (%d) '%s'",
errno, strerror(errno));
}
~Sem()
{
if (locked)
v();
}
// ----------------------
// get lock
int p()
{
sembuf sops[2];
sops[0].sem_num = 0;
sops[0].sem_op = 0; // wait for lock
sops[0].sem_flg = SEM_UNDO;
sops[1].sem_num = 0;
sops[1].sem_op = 1; // increment
sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
if (semop(id, sops, 2) == -1)
{
tell(0, "Error: Can't lock semaphore, errno (%d) '%s'",
errno, strerror(errno));
return fail;
}
locked = yes;
return success;
}
// ----------------------
// increment
int inc()
{
sembuf sops[1];
sops[0].sem_num = 0;
sops[0].sem_op = 1; // increment
sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
if (semop(id, sops, 1) == -1)
{
if (errno != EAGAIN)
tell(0, "Error: Can't lock semaphore, errno was (%d) '%s'",
errno, strerror(errno));
return fail;
}
locked = yes;
return success;
}
// ----------------------
// decrement
int dec()
{
return v();
}
// ----------------------
// check
int check()
{
sembuf sops[1];
sops[0].sem_num = 0;
sops[0].sem_op = 0;
sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
if (semop(id, sops, 1) == -1)
{
if (errno != EAGAIN)
tell(0, "Error: Can't lock semaphore, errno was (%d) '%s'",
errno, strerror(errno));
return fail;
}
return success;
}
// ----------------------
// release lock
int v()
{
sembuf sops;
sops.sem_num = 0;
sops.sem_op = -1; // release control
sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
if (semop(id, &sops, 1) == -1)
{
if (errno != EAGAIN)
tell(0, "Error: Can't unlock semaphore, errno (%d) '%s'",
errno, strerror(errno));
return fail;
}
locked = no;
return success;
}
private:
key_t key;
int id;
int locked;
};
//***************************************************************************
#endif //___COMMON_H
|