-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.c
179 lines (166 loc) · 4.58 KB
/
tools.c
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
/*
* tools.c: RSS Reader plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include "log.h"
#include "tools.h"
#include "common.h"
// --- Static -----------------------------------------------------------------
struct conv_table {
const char *from;
const char *to;
};
static struct conv_table preConversionTableS[] =
{
// 'to' field must be smaller than 'from'
{"<br />", "\n"}
};
// Conversion page: http://www.ltg.ed.ac.uk/~richard/utf-8.cgi
static struct conv_table postConversionTableS[] =
{
// 'to' field must be smaller than 'from'
{""", "\x22"},
{""", "\x22"},
{"&", "\x26"},
{"&", "\x26"},
{"&", "\x26"},
{"'", "\x27"},
{"'", "\x27"},
{"(", "\x28"},
{")", "\x29"},
{":", "\x3a"},
{"<", "\x3c"},
{"<", "\x3c"},
{">", "\x3e"},
{">", "\x3e"},
{"[", "\x5b"},
{"]", "\x5d"},
{" ", "\xc2\xa0"},
{" ", "\xc2\xa0"},
{"°", "\xc2\xb0"},
{"°", "\xc2\xb0"},
{"´", "\xc2\xb4"},
{"´", "\xc2\xb4"},
{"Ä", "\xc3\x84"},
{"Ä", "\xc3\x84"},
{"Å", "\xc3\x85"},
{"Å", "\xc3\x85"},
{"Ö", "\xc3\x96"},
{"Ö", "\xc3\x96"},
{"Ü", "\xc3\x9c"},
{"Ü", "\xc3\x9c"},
{"ß", "\xc3\x9f"},
{"ß", "\xc3\x9f"},
{"â", "\xc3\xa2"},
{"â", "\xc3\xa2"},
{"ä", "\xc3\xa4"},
{"ä", "\xc3\xa4"},
{"å", "\xc3\xa5"},
{"å", "\xc3\xa5"},
{"ç", "\xc3\xa7"},
{"ç", "\xc3\xa7"},
{"é", "\xc3\xa9"},
{"é", "\xc3\xa9"},
{"ê", "\xc3\xaa"},
{"ê", "\xc3\xaa"},
{"ö", "\xc3\xb6"},
{"ö", "\xc3\xb6"},
{"ü", "\xc3\xbc"},
{"ü", "\xc3\xbc"},
{"–", "\xe2\x80\x93"},
{"–", "\xe2\x80\x93"},
{"—", "\xe2\x80\x94"},
{"—", "\xe2\x80\x94"},
{"‘", "\xe2\x80\x98"},
{"‘", "\xe2\x80\x98"},
{"’", "\xe2\x80\x99"},
{"’", "\xe2\x80\x99"},
{"‚", "\xe2\x80\x9a"},
{"‚", "\xe2\x80\x9a"},
{"“", "\xe2\x80\x9c"},
{"“", "\xe2\x80\x9c"},
{"”", "\xe2\x80\x9d"},
{"”", "\xe2\x80\x9d"},
{"„", "\xe2\x80\x9e"},
{"„", "\xe2\x80\x9e"},
{"′", "\xe2\x80\xb3"},
{"″", "\xe2\x80\xb3"},
{"€", "\xe2\x82\xac"},
{"€", "\xe2\x82\xac"},
{"\n\n", "\n"}, // let's also strip multiple linefeeds
};
static char *htmlcharconv(char *strP, struct conv_table *convP, unsigned int elemP)
{
if (strP && convP) {
for (unsigned int i = 0; i < elemP; ++i) {
char *ptr = strstr(strP, convP[i].from);
while (ptr) {
long of = ptr - strP;
size_t l = strlen(strP);
size_t l1 = strlen(convP[i].from);
size_t l2 = strlen(convP[i].to);
if (l2 > l1) {
error("%s Cannot reallocate string", __PRETTY_FUNCTION__);
return strP;
}
if (l2 != l1)
memmove(strP + of + l2, strP + of + l1, l - of - l1 + 1);
strncpy(strP + of, convP[i].to, l2);
ptr = strstr(strP, convP[i].from);
}
}
return strP;
}
return NULL;
}
// --- General functions ------------------------------------------------------
char *striphtml(char *strP)
{
if (strP) {
char *c, t = 0, *r;
strP = htmlcharconv(strP, preConversionTableS, ELEMENTS(preConversionTableS));
c = strP;
r = strP;
while (*strP != '\0') {
if (*strP == '<')
t++;
else if (*strP == '>')
t--;
else if (t < 1)
*(c++) = *strP;
strP++;
}
*c = '\0';
return htmlcharconv(r, postConversionTableS, ELEMENTS(postConversionTableS));
}
return NULL;
}
void *myrealloc(void *ptrP, size_t sizeP)
{
/* There might be a realloc() out there that doesn't like reallocing
NULL pointers, so we take care of it here */
if (ptrP)
return realloc(ptrP, sizeP);
else
return malloc(sizeP);
}
bool isimage(const char *textP)
{
if (endswith(textP, ".jpg") || endswith(textP, ".gif") || endswith(textP, ".png"))
return true;
return false;
}
bool isvideo(const char *textP)
{
if (endswith(textP, ".mpg") || endswith(textP, ".avi") || endswith(textP, ".ts"))
return true;
return false;
}
bool ismusic(const char *textP)
{
if (endswith(textP, ".mp3") || endswith(textP, ".wav") || endswith(textP, ".ogg"))
return true;
return false;
}