summaryrefslogtreecommitdiff
path: root/tools.c
blob: b4eef16619da5e77e62c857cfa0dc7c0801db6f3 (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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <vdr/osd.h>
#include <vdr/plugin.h>
#include <vdr/skins.h>
#include "services/epgsearch.h"

#include "tools.h"

cPlugin *GetScraperPlugin(void) {
    static cPlugin *pScraper = cPluginManager::GetPlugin("scraper2vdr");
    if( !pScraper ) // if it doesn't exit, try tvscraper
        pScraper = cPluginManager::GetPlugin("tvscraper");
    return pScraper;
}

/****************************************************************************************
*            CUTTEXT
****************************************************************************************/
std::string CutText(std::string text, int width, const cFont *font) {
    if (width <= font->Size())
        return text.c_str();
    if (font->Width(text.c_str()) < width)
        return text.c_str();
    cTextWrapper twText;
    twText.Set(text.c_str(), font, width);
    std::string cuttedTextNative = twText.GetLine(0);
    std::stringstream sstrText;
    sstrText << cuttedTextNative << "...";
    std::string cuttedText = sstrText.str();
    int actWidth = font->Width(cuttedText.c_str());
    if (actWidth > width) {
        int overlap = actWidth - width;
        int charWidth = font->Width(".");
        if (charWidth == 0)
            charWidth = 1;
        int cutChars = overlap / charWidth;
        if (cutChars > 0) {
            cuttedTextNative = cuttedTextNative.substr(0, cuttedTextNative.length() - cutChars);
            std::stringstream sstrText2;
            sstrText2 << cuttedTextNative << "...";
            cuttedText = sstrText2.str();
        }
    }
    return cuttedText;
}

/****************************************************************************************
*            StrToLowerCase
****************************************************************************************/
std::string StrToLowerCase(std::string str) {
    std::string lowerCase = str;
    const int length = lowerCase.length();
    for(int i=0; i < length; ++i) {
        lowerCase[i] = std::tolower(lowerCase[i]);
    }
    return lowerCase;
}

/****************************************************************************************
*            GetDirectoryFromTimer
****************************************************************************************/
std::string GetDirectoryFromTimer(std::string file) {
    std::string dir = "";
    size_t found = file.find_last_of('~');
    if (found != std::string::npos) {
        dir = file.substr(0, found);
    }
    return dir;
}

/****************************************************************************************
*            GetDirectoryFromTimer
****************************************************************************************/
void ReadRecordingDirectories(std::vector<std::string> *folders, cList<cNestedItem> *rootFolders, cString path) {
    cPlugin *epgSearchPlugin = NULL;
    epgSearchPlugin = cPluginManager::GetPlugin("epgsearch");
    if (epgSearchPlugin) {
        Epgsearch_services_v1_0 *epgSearch = new Epgsearch_services_v1_0;
        if (epgSearchPlugin->Service("Epgsearch-services-v1.0", epgSearch)) {
            std::set<std::string> epgSearchDirs = epgSearch->handler->DirectoryList();
            std::set<std::string>::iterator it;
            for (it = epgSearchDirs.begin(); it != epgSearchDirs.end(); it++) {
                std::string newFolder = *it;
                std::replace(newFolder.begin(), newFolder.end(), '/', '~');
                folders->push_back(newFolder);
            }
        }
    } else {
        cList<cNestedItem> *foldersLevel = NULL;
        if (rootFolders) {
            foldersLevel = rootFolders;
        } else {
            foldersLevel = &Folders;
        }
        for (cNestedItem *folder = foldersLevel->First(); folder; folder = foldersLevel->Next(folder)) {
            std::string strFolder = *cString::sprintf("%s%s", *path, folder->Text());
            std::replace(strFolder.begin(), strFolder.end(), '/', '~');
            folders->push_back(strFolder);
            cList<cNestedItem> *subItems = folder->SubItems();
            if (subItems) {
                std::string strFolder2 = *cString::sprintf("%s%s", *path, folder->Text());
                std::replace(strFolder2.begin(), strFolder2.end(), '/', '~');
                ReadRecordingDirectories(folders, subItems, strFolder2.c_str());
            }
        }
    }
}


/****************************************************************************************
*            DrawRoundedCorners
****************************************************************************************/
void DrawRoundedCorners(cPixmap *p, int posX, int posY, int width, int height, int radius, int borderWidth, tColor borderColor) {
    if( height > 2*radius) {
        p->DrawEllipse(cRect(posX, posY, radius, radius), borderColor, -2);
        p->DrawEllipse(cRect(posX - borderWidth, posY - borderWidth, radius, radius), clrTransparent, -2);

        p->DrawEllipse(cRect(posX+width - radius, posY, radius, radius), borderColor, -1);
        p->DrawEllipse(cRect(posX+width - radius + borderWidth, posY - borderWidth, radius, radius), clrTransparent, -1);
        
        p->DrawEllipse(cRect(posX, posY + height - radius, radius, radius), borderColor, -3);
        p->DrawEllipse(cRect(posX - borderWidth, posY + height - radius + borderWidth, radius, radius), clrTransparent, -3);
        
        p->DrawEllipse(cRect(posX + width - radius, posY + height - radius, radius, radius), borderColor, -4);
        p->DrawEllipse(cRect(posX + width - radius + borderWidth, posY + height - radius + borderWidth, radius, radius), clrTransparent, -4);
    }
}


/****************************************************************************************
*            SPLTSTRING
****************************************************************************************/
// split: receives a char delimiter; returns a vector of strings
// By default ignores repeated delimiters, unless argument rep == 1.
std::vector<std::string>& splitstring::split(char delim, int rep) {
    if (!flds.empty()) flds.clear();  // empty vector if necessary
    std::string work = data();
    std::string buf = "";
    int i = 0;
    while (i < work.length()) {
        if (work[i] != delim)
            buf += work[i];
        else if (rep == 1) {
            flds.push_back(buf);
            buf = "";
        } else if (buf.length() > 0) {
            flds.push_back(buf);
            buf = "";
        }
        i++;
    }
    if (!buf.empty())
        flds.push_back(buf);
    return flds;
}

/****************************************************************************************
*            FINDIGNORECASE
****************************************************************************************/
int FindIgnoreCase(const std::string& expr, const std::string& query)
{
  const char *p = expr.c_str();
  const char *r = strcasestr(p, query.c_str());

  if (!r)
     return -1;
  return r - p;
}


/****************************************************************************************
*            GetAuxValue
****************************************************************************************/
char* GetAuxValue(const char* aux, const char* name) {
	if (isempty(aux))
    	return NULL;
    
	char* descr = strdup(aux);
   	char* beginaux = strstr(descr, "<epgsearch>");
   	char* endaux = strstr(descr, "</epgsearch>");
   	if (!beginaux || !endaux) {
        free(descr);
    	return NULL;
	}

	beginaux +=  11;  // strlen("<epgsearch>");
   	endaux[0] = 0;
   	memmove(descr, beginaux, endaux - beginaux + 1);

	if (strcmp(name, "epgsearch") == 0) 
		return descr; // full aux

   	int namelen = strlen(name);
   	char catname[100] = "";
   	catname[0] = '<';
   	memcpy(catname + 1, name, namelen);
   	catname[1 + namelen] = '>';
   	catname[2 + namelen] = 0;

   	char* cat = strcasestr(descr, catname);
   	if (!cat) {
    	free(descr);
    	return NULL;
    }
      
	cat += namelen + 2;
   	char* end = strstr(cat, "</");
   	if (!end) {
    	free(descr);	
    	return NULL;
    }
	end[0] = 0;

   	int catlen = end - cat + 1;
   	char* value = (char *) malloc(catlen);
   	memcpy(value, cat, catlen);

   	free(descr);
   	return value;
}

char* GetAuxValue(const cRecording *recording, const char* name) {
   	if (!recording || !recording->Info()) 
        return NULL;
    return GetAuxValue(recording->Info()->Aux(), name);
}

char* GetAuxValue(const cTimer *timer, const char* name) {
	if (!timer || !timer->Aux()) 
        return NULL;
    return GetAuxValue(timer->Aux(), name);
}

/****************************************************************************************
*            FUZZYSEARCH
****************************************************************************************/

/******************************************************************************
FUNCTION afuzzy_init() 
	Initialization of the fuzzy search routine. This applies to the consequent
	calls of the afuzzy_CheckRTR (whole string matching) and afuzzy_CheckSUB
	(substring match) routines. afuzzy_init() should be called for each
	new pattern or error length. The search is case sensitive

ARGUMENTS: 
	p			Pattern
	kerr		Number of possible errors. Shouldn't exceed pattern length
	UseFilter	Use agrep filter algorithm that speeds up search.
	fuzzy		pointer to the structure that will be later passes to Check*
					(the first 6 elements should be NULLs for the first call)
	
RETURN VALUE:
	none	

ALGORITHM
	see. the article on agrep algorithms.
	The only change is accounting transpositions as one edit operation .
******************************************************************************/
void afuzzy_init(const char *p, int kerr, int UseFilter, AFUZZY *fuzzy)
{
	int cnt, p_len, i, j, l, d, m, dd;
	char PatFilter[sizeof(Uint)*8 + 1];

	fuzzy->k = kerr;
	m = strlen(p);
	fuzzy->FilterSet = 0;
	memset(fuzzy->Map, 0 , sizeof(fuzzy->Map) );

	if (fuzzy->S)
		free(fuzzy->S);
	if (fuzzy->R)
		free(fuzzy->R);
	if (fuzzy->R1)
		free(fuzzy->R1);
	if (fuzzy->RP)
		free(fuzzy->RP);
	if (fuzzy->RI)
		free(fuzzy->RI);
	if (fuzzy->FilterS)
		free(fuzzy->FilterS);

	fuzzy->FilterS = NULL;
	fuzzy->S = (Uint *)calloc(m + 1, sizeof(Uint));
	fuzzy->R = (Uint *)calloc(fuzzy->k + 1, sizeof(Uint));
	fuzzy->R1 = (Uint *)calloc(fuzzy->k + 1, sizeof(Uint));
	fuzzy->RI = (Uint *)calloc(fuzzy->k + 1, sizeof(Uint));
	fuzzy->RP = (Uint *)calloc(fuzzy->k + 1, sizeof(Uint));

	for (i = 0, cnt = 0; i < m; i++)
	{
		l = fuzzy->Map[(unsigned char)p[i]];
		if (!l)
		{
			l = fuzzy->Map[(unsigned char)p[i]] = ++cnt;
			fuzzy->S[l] = 0;
		}
		fuzzy->S[l] |= 1 << i;
	}


	for (d = 0; d <= fuzzy->k; d++)
		fuzzy->RI[d] = (1 << d) - 1;

	fuzzy->mask_ok = (1 << (m - 1));
	fuzzy->r_size  = sizeof(Uint) * (fuzzy->k + 1);
	p_len = m;

	if (p_len > (int) sizeof(Uint)*8)
		p_len = (int) sizeof(Uint)*8;

	/* If k is zero then no filter is needed! */
	if (fuzzy->k && (p_len >= 2*(fuzzy->k + 1)) )
	{
		if (UseFilter)
		{
			fuzzy->FilterSet = 1;
			memset(fuzzy->FilterMap, 0 , sizeof(fuzzy->FilterMap) );
			fuzzy->FilterS = (Uint *)calloc(m + 1, sizeof(Uint));

			/* Not let's fill the interleaved pattern */
			dd = p_len / (fuzzy->k + 1);
			p_len  = dd * (fuzzy->k + 1);

			for (i = 0, cnt = 0; i < dd; i++)
				for (j = 0; j < fuzzy->k + 1; j++, cnt++)
					PatFilter[cnt] = (unsigned char)p[j*dd + i];
			PatFilter[p_len] = 0;

			for (i = 0, cnt = 0; i < p_len; i++)
			{
				l = fuzzy->FilterMap[(unsigned char)PatFilter[i]];
				if (!l)
				{
					l = fuzzy->FilterMap[(unsigned char)PatFilter[i]] = ++cnt;
					fuzzy->FilterS[l] = 0;
				}
				fuzzy->FilterS[l] |= 1 << i;
			}
			fuzzy->filter_ok = 0;
			for (i = p_len - fuzzy->k - 1; i <= p_len - 1; i++) /* k+1 times */
				fuzzy->filter_ok |= 1 << i;

			 /* k+1 first bits set to 1 */
			fuzzy->filter_shift = (1 << (fuzzy->k + 2)) - 1;
		}
	}
}

/******************************************************************************
FUNCTION afuzzy_free() 
	Cleaning up after previous afuzzy_init() call.

ARGUMENTS: 
	fuzzy		pointer to the afuzzy parameters structure
	
RETURN VALUE:
	none	
******************************************************************************/
void afuzzy_free(AFUZZY *fuzzy)
{
	if (fuzzy->S)
	{
		free(fuzzy->S);
		fuzzy->S = NULL;
	}
	if (fuzzy->R)
	{
		free(fuzzy->R);
		fuzzy->R = NULL;
	}
	if (fuzzy->R1)
	{
		free(fuzzy->R1);
		fuzzy->R1 = NULL;
	}
	if (fuzzy->RP)
	{
		free(fuzzy->RP);
		fuzzy->RP = NULL;
	}
	if (fuzzy->RI)
	{
		free(fuzzy->RI);
		fuzzy->RI = NULL;
	}
	if (fuzzy->FilterS)
	{
		free(fuzzy->FilterS);
		fuzzy->FilterS = NULL;
	}
}


/******************************************************************************
FUNCTION afuzzy_CheckSUB() 
	Perform a fuzzy pattern substring matching. afuzzy_init() should be 
	called previously to initialize the pattern and error length. 
	Positive result means that some part of the string given matches the 
	pattern with no more than afuzzy->k errors (1 error = 1 letter 
	replacement or transposition)

ARGUMENTS: 
	t			the string to test
	fuzzy		pointer to the afuzzy parameters structure

RETURN VALUE:
	0	- no match
	> 0	- strings match

ALGORITHM
	????????????????
******************************************************************************/
int afuzzy_checkSUB(const char *t, AFUZZY *fuzzy)
{
	register char c;
	register int j, d;

	/* For eficciency this case should be little bit optimized */
	if (!fuzzy->k)
	{
		Uint R = 0, R1;

		for (j = 0; (c = t[j]) != '\0'; j++)
		{
			R1 = ( ((R<<1) | 1) & fuzzy->S[fuzzy->Map[(unsigned char)c]]);
			R = R1;

			if (R1 & fuzzy->mask_ok)
				return 1;
		} /* end for (register int j = 0 ... */
		return 0;
	}

	if (fuzzy->FilterSet && !afuzzy_checkFLT(t, fuzzy))
		return 0;

	memcpy(fuzzy->R, fuzzy->RI, fuzzy->r_size); /* R = RI */

	for (j = 0; (c = t[j]); j++)
	{
		for (d = 0; d <= fuzzy->k; d++)
		{
			fuzzy->R1[d] = (((fuzzy->R[d]<<1) | 1) & 
										fuzzy->S[fuzzy->Map[(unsigned char)c]]);
			if (d > 0)
				fuzzy->R1[d] |= ((fuzzy->R[d-1] | fuzzy->R1[d-1])<<1) | 1 | 
															fuzzy->R[d-1];
		}
		if (fuzzy->R1[fuzzy->k] & fuzzy->mask_ok)
			return j;

		memcpy(fuzzy->R, fuzzy->R1, fuzzy->r_size);

	} /* end for (register int j = 0 ... */

	return 0;
}

static int afuzzy_checkFLT(const char *t, AFUZZY *fuzzy)
{
	register Uint FilterR = 0;
	register Uint FilterR1;
	register int j;

	for (j = 0; t[j] != '\0'; j++)
	{
		FilterR1 = ( ((FilterR<<(fuzzy->k+1)) | fuzzy->filter_shift) & 
						fuzzy->FilterS[fuzzy->FilterMap[(unsigned char)t[j]]]);
		if (FilterR1 & fuzzy->filter_ok)
			return 1;
		FilterR = FilterR1;
	} /* end for (register int j = 0 ... */

	return 0;
}