-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathavahi-helper.h
103 lines (93 loc) · 2.75 KB
/
avahi-helper.h
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
#ifndef __AVAHI4VDR_HELPER_H
#define __AVAHI4VDR_HELPER_H
#include <vdr/tools.h>
class cAvahiHelper
{
private:
cStringList _list;
public:
// instantiate with an option string like "key1=value1,key2=value2,key2=value3,key3=value4,..."
// comma and backslashes in values must be escaped with a backslash
cAvahiHelper(const char *Options)
{
if (Options != NULL) {
char *s = strdup(Options);
size_t len = strlen(Options);
size_t pos1 = 0;
//dsyslog("avahi4vdr-helper: options = '%s'", s);
// skip initial commas
while ((pos1 < len) && (s[pos1] == ','))
pos1++;
size_t pos2 = pos1;
while (pos2 < len) {
// de-escape backslashed characters
if ((s[pos2] == '\\') && (pos2 < (len - 1))) {
memmove(s + pos2, s + pos2 + 1, len - pos2 - 1);
len--;
s[len] = 0;
pos2++;
continue;
}
if ((s[pos2] == ',') && (s[pos2 - 1] != '\\')) {
s[pos2] = 0;
//dsyslog("avahi4vdr-helper: add '%s'", s + pos1);
_list.Append(strdup(s + pos1));
pos1 = pos2 + 1;
}
pos2++;
}
if (pos2 > pos1) {
//dsyslog("avahi4vdr-helper: add '%s'", s + pos1);
_list.Append(strdup(s + pos1));
}
free(s);
}
};
// if Number is greater than zero, returns the n'th appearance of the parameter
const char *Get(const char *Name, int Number = 0)
{
if (Name == NULL)
return NULL;
size_t name_len = strlen(Name);
if (name_len == 0)
return NULL;
for (int i = 0; i < _list.Size(); i++) {
const char *text = _list[i];
if (text == NULL)
continue;
size_t len = strlen(text);
if (len > name_len) {
if ((strncmp(text, Name, name_len) == 0) && (text[name_len] == '=')) {
Number--;
if (Number < 0) {
//dsyslog("avahi4vdr-helper: found parameter '%s'", text);
return text + name_len + 1;
}
}
}
}
return NULL;
};
// get the number of appearances of parameter "Name"
int Count(const char *Name)
{
if (Name == NULL)
return 0;
size_t name_len = strlen(Name);
if (name_len == 0)
return 0;
int count = 0;
for (int i = 0; i < _list.Size(); i++) {
const char *text = _list[i];
if (text == NULL)
continue;
size_t len = strlen(text);
if (len > name_len) {
if ((strncmp(text, Name, name_len) == 0) && (text[name_len] == '='))
count++;
}
}
return count;
};
};
#endif