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
|
#ifndef __HELPERS_DVDSWITCH_H
#define __HELPERS_DVDSWITCH_H
#include <sys/types.h>
#include <sys/stat.h>
#include <regex.h>
#include <ctype.h>
#include <cmath>
#include <stdio.h>
#include <unistd.h>
#include <vdr/tools.h>
#include <vdr/interface.h>
#include <vdr/thread.h>
#define FREENULL(p) (free(p), p = NULL)
void OsdMsg(eMessageType Type, const char *Msg);
void ChangeChars(char *name, char *chars);
void StrRepeat(const char *input, int count, char *dest);
bool RegIMatch(const char *string,const char *pattern);
// --- cStringValue -------------------------------------
class cStringValue
{
private:
char *String;
void Init(const char *string)
{
FREENULL(String);
String = string ? strdup(string) : NULL;
}
public:
cStringValue(void) { String = NULL; }
~cStringValue(void) { free(String); }
cStringValue &operator= (const char *string)
{
Init(string);
return *this;
}
cStringValue &operator+= (const char *string)
{
if(String && string)
{
String = (char*) realloc(String, strlen(String) + strlen(string) + 1);
strcat(String, string);
}
if(!String)
Init(string);
return *this;
}
char *operator& (void) { return String; }
int len(void) const { return String ? strlen(String) : 0; }
};
// --- cStringList --------------------------------------
class cStringListItem : public cListObject
{
private:
char *String;
public:
cStringListItem(const char *string)
{
String = string ? strdup(string) : NULL;
}
~cStringListItem(void) { free(String); }
char *Value(void) { return String; }
};
//class cStringList : public cList<cStringListItem> {};
// --- cTokenizer ---------------------------------------
class cToken : public cListObject
{
private:
char *String;
public:
cToken(char *string) {
String = string ? strdup(string) : NULL;
}
~cToken(void) { free(String); }
char *Value(void) { return String; }
};
class cTokenizer : public cList<cToken>
{
private:
char *String;
char *Delim;
void Tokenize(bool trim);
public:
cTokenizer(const char *string,const char *delim, bool trim = false)
{
String = string ? strdup(string) : NULL;
Delim = delim ? strdup(delim) : NULL;
if(String && Delim)
Tokenize(trim);
}
~cTokenizer(void)
{
free(String);
free(Delim);
}
const char *GetToken(int index) const { return index > 0 && index <= Count() ? Get(index - 1)->Value() : NULL; }
};
// --- cFileInfo ----------------------------------------
enum eFileInfo
{
tNone = 0,
tDir,
tFile,
tUnknown,
};
class cFileInfo
{
private:
char *File;
char *buffer;
struct stat64 Info;
unsigned long long int size;
public:
cFileInfo(const char *file);
~cFileInfo(void);
char *Path(void);
char *FileName(void);
char *FileNameWithoutExt(void);
char *Extension(bool withPoint = true);
eFileInfo Type(void);
unsigned long long int SizeByte(void);
double SizeKByte(int decimals = 2);
double SizeMByte(int decimals = 2);
double SizeGByte(int decimals = 1);
bool isReadable(void);
bool isWriteable(void);
bool isExecutable(void);
bool isExists(void);
};
// --- cFileCMD ------------------------------------------
class cFileCMD
{
public:
static bool Del(const char *file);
static bool Mkdir(const char *dir) { return MakeDirs(dir, true); }
static bool Rn(const char *oldfile, const char *newfile) { return !rename(oldfile, newfile); }
static bool DirIsEmpty(const char *file);
};
// --- cFileList -------------------------------
enum eFileList
{
sNone,
sAsc,
sDesc,
};
class cFileOptListItem : public cListObject
{
private:
char *RegEx;
eFileInfo Type;
public:
cFileOptListItem(const char *regex, eFileInfo type = tNone)
{
RegEx = (regex && !isempty(regex)) ? strdup(regex) : NULL;
Type = type;
}
~cFileOptListItem(void) { free(RegEx); }
char *Regex(void) { return RegEx; }
eFileInfo FileType(void) { return Type; }
};
class cFileOptList : public cList<cFileOptListItem> {};
class cFileList;
class cFileListItem : public cListObject
{
friend class cFileList;
private:
char *File;
public:
cFileListItem(const char *dir, const char *file);
cFileListItem(const char *file);
~cFileListItem(void) { free(File); }
char *Value(void) { return File; }
};
class cFileList : public cList<cFileListItem>
{
private:
char *Dir;
bool Sub;
cFileOptList *Includes;
cFileOptList *Excludes;
bool Protect;
eFileList SortMode;
bool SortToFilename;
eFileInfo Type;
bool Read(const char *dir, bool withsub);
void SortIn(const char *dir, const char *file);
bool CheckIncludes(const char *dir, const char *file);
bool CheckExcludes(const char *dir, const char *file);
bool CheckType(const char *dir, const char *file, eFileInfo type);
public:
cFileList(void);
~cFileList(void);
bool Load(const char *dir = NULL, bool withsub = false);
void OptInclude(const char *include, eFileInfo type = tNone)
{
if(!Includes)
Includes = new cFileOptList();
if(!include)
Includes->Clear();
else
Includes->Add(new cFileOptListItem(include, type));
}
void OptExclude(const char *exclude, eFileInfo type = tNone)
{
if(!Excludes)
Excludes = new cFileOptList();
if(!exclude)
Excludes->Clear();
else
Excludes->Add(new cFileOptListItem(exclude, type));
}
void OptSort(eFileList mode, bool tofilename = false)
{
SortMode = mode;
SortToFilename = tofilename;
}
void OptFilterType(eFileInfo type) { Type = type; }
bool DirIsEmpty(cFileListItem *item);
static bool DirIsIn(const char *file, const char *strings);
static bool DirIsIn(cFileListItem *item, const char *strings) { return DirIsIn(item->Value(), strings); }
};
#endif // __HELPERS_DVDSWITCH_H
|