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
|
/*! -*- c++ -*-
* \file mg_tools.c
* \brief A few util functions for standalone and plugin messaging for the vdr muggle plugindatabase
*
* \version $Revision: 1.4 $
* \date $Date$
* \author Ralf Klueber, Lars von Wedel, Andreas Kellner
* \author file owner: $Author$
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/time.h>
#include <time.h>
#include "mg_tools.h"
#define __STL_CONFIG_H
#include <vdr/tools.h>
//! \brief buffer for messages
#define MAX_BUFLEN 2048
static char buffer[MAX_BUFLEN];
static int DEBUG_LEVEL = 3;
void
mgSetDebugLevel (int new_level) {
DEBUG_LEVEL = new_level;
}
int
msprintf(char **strp, const char *fmt, ...) {
va_list ap;
int res;
va_start (ap, fmt);
res=vmsprintf(strp,fmt,ap);
va_end (ap);
return res;
}
int
vmsprintf(char **strp, const char *fmt, va_list &ap) {
*strp=0;
int res=vasprintf (strp, fmt, ap);
if (res<0) {
*strp=strdup("???,see logfile");
mgError("vasprintf() returns %d. This probably means illformed UTF-8 characters."
" Please convert your file names to UTF-8",fmt,res);
}
return res;
}
void
mgDebug (int level, const char *fmt, ...) {
if (level <= DEBUG_LEVEL) {
va_list ap;
va_start (ap, fmt);
#ifdef HAVE_PG
buffer[0]='P';
#elif HAVE_SQLITE
buffer[0]='S';
#else
buffer[0]='M';
#endif
buffer[1]=' ';
vsnprintf (buffer+2, sizeof(buffer)-2, fmt, ap);
dsyslog("%s",buffer);
va_end (ap);
}
}
void
mgDebug (const char *fmt, ...) {
va_list ap;
va_start (ap, fmt);
mgDebug (1, fmt, ap);
va_end (ap);
}
extern void showmessage(int duration,const char*,...);
void
mgWarning (const char *fmt, ...) {
va_list ap;
va_start (ap, fmt);
vsnprintf (buffer, sizeof(buffer), fmt, ap);
isyslog("%s",buffer);
showmessage(0,buffer);
va_end (ap);
}
void
mgError (const char *fmt, ...) {
va_list ap;
va_start (ap, fmt);
vsnprintf (buffer, sizeof(buffer), fmt, ap);
esyslog("%s",buffer);
showmessage(0,buffer);
va_end (ap);
}
std::string trim(std::string const& source, char const* delims ) {
std::string result(source);
std::string::size_type index = result.find_last_not_of(delims);
if(index != std::string::npos)
result.erase(++index);
index = result.find_first_not_of(delims);
if(index != std::string::npos)
result.erase(0, index);
else
result.erase();
return result;
}
char *
SeparateFolders(const char *filename, char * folders[], unsigned int fcount) {
static char empty[1];
empty[0]=0;
for (unsigned int i=0;i<fcount;i++)
folders[i]=empty;
char *fbuf=strdup(filename);
char *slash=fbuf-1;
for (unsigned int i=0;i<fcount;i++) {
char *p=slash+1;
slash=strchr(p,'/');
if (!slash)
break;
folders[i]=p;
*slash=0;
}
return fbuf;
}
string&
addsep (string & s, string sep, string n) {
if (!n.empty ()) {
if (!s.empty ())
s.append (sep);
s.append (n);
}
return s;
}
string
comma (string & s, string n) {
return addsep (s, ",", n);
}
//! \brief converts long to string
string
itos (int i) {
std::stringstream s;
s << i;
return s.str ();
}
//! \brief convert long to string
string
ltos (long l) {
std::stringstream s;
s << l;
return s.str ();
}
char *
extension(const char *filename) {
char *dot = strrchr((char *)filename,'.');
if (!dot)
dot = strrchr((char *)filename,0)-1;
return dot+1;
}
bool
notempty(const char *s) {
if (!s)
return false;
else
return strlen(s);
}
bool samedir( const char *d1, const char *d2 ) {
int path_max;
#ifdef PATH_MAX
path_max = PATH_MAX;
#else
path_max = pathconf ( "/", _PC_PATH_MAX );
if (path_max <= 0) {
path_max = 4096;
}
#endif
char rp1[path_max], rp2[path_max];
if (!realpath(d1, rp1))
mgWarning("Error canonicalizing pathname %s: %s",
d1, strerror(errno));
if (!realpath(d2, rp2))
mgWarning("Error canonicalizing pathname %s: %s",
d2, strerror(errno));
return (!strcmp( rp1, rp2 ) );
}
bool mkdir_p(const char *s) {
char *slash=strrchr((char *)s,'/');
if (!slash) return false;
char *sc = strdup(s);
*strrchr(sc,'/')=0; // cut off the filename
char *p = sc;
int mode;
while (*p) {
slash=strchr(p+1,'/');
if (slash) *slash=0;
mode=R_OK|X_OK|W_OK;
if (slash) if (strchr(slash+1,'/'))
mode=R_OK|X_OK;
if (access(sc,mode)) {
mkdir(sc,0755);
mgDebug(5,"Made directory %s",sc);
}
if (!slash) break;
*slash='/';
p=slash+1;
}
free(sc);
return true;
}
void
mgTimestamp(const char *s) {
char *buf;
struct timeval tv;
gettimeofday(&tv,0);
msprintf(&buf,"%s:%ld %ld ",s,tv.tv_sec,tv.tv_usec);
mgDebug(5,buf);
free(buf);
}
|