Line |
Branch |
Exec |
Source |
1 |
|
|
/***************************************************************************** |
2 |
|
|
* |
3 |
|
|
* $Id$ |
4 |
|
|
* |
5 |
|
|
* Copyright 2010 Richard Hacker (lerichi at gmx dot net) |
6 |
|
|
* |
7 |
|
|
* This file is part of the pdserv library. |
8 |
|
|
* |
9 |
|
|
* The pdserv library is free software: you can redistribute it and/or modify |
10 |
|
|
* it under the terms of the GNU Lesser General Public License as published |
11 |
|
|
* by the Free Software Foundation, either version 3 of the License, or (at |
12 |
|
|
* your option) any later version. |
13 |
|
|
* |
14 |
|
|
* The pdserv library is distributed in the hope that it will be useful, but |
15 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 |
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 |
|
|
* License for more details. |
18 |
|
|
* |
19 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
20 |
|
|
* along with the pdserv library. If not, see <http://www.gnu.org/licenses/>. |
21 |
|
|
* |
22 |
|
|
*****************************************************************************/ |
23 |
|
|
|
24 |
|
|
#include "Attribute.h" |
25 |
|
|
|
26 |
|
|
#include <cstring> |
27 |
|
|
#include <algorithm> |
28 |
|
|
#include <sstream> |
29 |
|
|
#include <locale> |
30 |
|
|
|
31 |
|
|
using namespace MsrProto; |
32 |
|
|
|
33 |
|
|
///////////////////////////////////////////////////////////////////////////// |
34 |
|
✗ |
void Attr::clear() |
35 |
|
|
{ |
36 |
|
✗ |
id = 0; |
37 |
|
✗ |
_id.clear(); |
38 |
|
✗ |
attrMap.clear(); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
///////////////////////////////////////////////////////////////////////////// |
42 |
|
✗ |
void Attr::adjust(ptrdiff_t delta) |
43 |
|
|
{ |
44 |
|
✗ |
for (AttrMap::iterator it = attrMap.begin(); it != attrMap.end(); it++) { |
45 |
|
✗ |
it->second.name += delta; |
46 |
|
✗ |
it->second.value += delta; |
47 |
|
|
} |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
///////////////////////////////////////////////////////////////////////////// |
51 |
|
✗ |
void Attr::insert(const char *name) |
52 |
|
|
{ |
53 |
|
|
//cout << "Binary attribute: Name=" << std::string(name, nameLen) << endl; |
54 |
|
✗ |
AttrPtrs a = {name, 0}; |
55 |
|
✗ |
attrMap.insert(std::pair<size_t,AttrPtrs>(strlen(name), a)); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
///////////////////////////////////////////////////////////////////////////// |
59 |
|
✗ |
void Attr::insert(const char *name, char *value) |
60 |
|
|
{ |
61 |
|
|
//cout << "Value attribute: Name=" << std::string(name, nameLen) |
62 |
|
|
//<< ", Value=" << std::string(value, valueLen) |
63 |
|
|
//<< endl; |
64 |
|
✗ |
size_t len = strlen(name); |
65 |
|
|
|
66 |
|
✗ |
if (len == 2 and !strncmp(name, "id", 2)) { |
67 |
|
✗ |
_id.assign(value); |
68 |
|
✗ |
id = &_id; |
69 |
|
✗ |
return; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
✗ |
AttrPtrs a = {name, value}; |
73 |
|
✗ |
attrMap.insert(std::pair<size_t,AttrPtrs>(len, a)); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
///////////////////////////////////////////////////////////////////////////// |
77 |
|
✗ |
bool Attr::find(const char *name, char * &value) const |
78 |
|
|
{ |
79 |
|
✗ |
size_t len = strlen(name); |
80 |
|
|
std::pair<AttrMap::const_iterator, AttrMap::const_iterator> |
81 |
|
✗ |
ret(attrMap.equal_range(len)); |
82 |
|
|
|
83 |
|
✗ |
for (AttrMap::const_iterator it(ret.first); it != ret.second; it++) { |
84 |
|
✗ |
if (!strncasecmp(name, it->second.name, len)) { |
85 |
|
✗ |
value = it->second.value; |
86 |
|
✗ |
return true; |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
|
90 |
|
✗ |
return false; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
///////////////////////////////////////////////////////////////////////////// |
94 |
|
✗ |
bool Attr::isEqual(const char *name, const char *s) const |
95 |
|
|
{ |
96 |
|
✗ |
char *value; |
97 |
|
|
|
98 |
|
✗ |
if (find(name, value) and value) |
99 |
|
✗ |
return !strcasecmp(value, s); |
100 |
|
|
|
101 |
|
✗ |
return false; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
///////////////////////////////////////////////////////////////////////////// |
105 |
|
✗ |
bool Attr::isTrue(const char *name) const |
106 |
|
|
{ |
107 |
|
✗ |
char *value; |
108 |
|
|
|
109 |
|
✗ |
if (!(find(name, value))) |
110 |
|
✗ |
return false; |
111 |
|
|
|
112 |
|
|
// Binary attribute, e.g <xsad sync> |
113 |
|
✗ |
if (!value) |
114 |
|
✗ |
return true; |
115 |
|
|
|
116 |
|
✗ |
size_t len = strlen(value); |
117 |
|
|
|
118 |
|
|
// Binary attribute, e.g <xsad sync=1 > |
119 |
|
✗ |
if (len == 1) |
120 |
|
✗ |
return *value == '1'; |
121 |
|
|
|
122 |
|
|
// Binary attribute, e.g <xsad sync="true"> |
123 |
|
✗ |
if (len == 4) |
124 |
|
✗ |
return !strncasecmp(value, "true", 4); |
125 |
|
|
|
126 |
|
|
// Binary attribute, e.g <xsad sync='on'/> |
127 |
|
✗ |
if (len == 2) |
128 |
|
✗ |
return !strncasecmp(value, "on", 2); |
129 |
|
|
|
130 |
|
✗ |
return false; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
///////////////////////////////////////////////////////////////////////////// |
134 |
|
✗ |
bool Attr::getString(const char *name, std::string &s) const |
135 |
|
|
{ |
136 |
|
✗ |
char *value; |
137 |
|
|
|
138 |
|
✗ |
s.clear(); |
139 |
|
|
|
140 |
|
✗ |
if (!(find(name, value)) or !value) |
141 |
|
✗ |
return false; |
142 |
|
|
|
143 |
|
✗ |
char *pptr, *eptr = value + strlen(value); |
144 |
|
✗ |
while ((pptr = std::find(value, eptr, '&')) != eptr) { |
145 |
|
✗ |
s.append(value, pptr - value); |
146 |
|
✗ |
size_t len = eptr - pptr; |
147 |
|
✗ |
if (len > 4 and !strncmp(pptr, ">", 4)) { |
148 |
|
✗ |
s.append(1, '>'); |
149 |
|
✗ |
value = pptr + 4; |
150 |
|
|
} |
151 |
|
✗ |
else if (len > 4 and !strncmp(pptr, "<", 4)) { |
152 |
|
✗ |
s.append(1, '<'); |
153 |
|
✗ |
value = pptr + 4; |
154 |
|
|
} |
155 |
|
✗ |
else if (len > 5 and !strncmp(pptr, "&", 5)) { |
156 |
|
✗ |
s.append(1, '&'); |
157 |
|
✗ |
value = pptr + 5; |
158 |
|
|
} |
159 |
|
✗ |
else if (len > 6 and !strncmp(pptr, """, 6)) { |
160 |
|
✗ |
s.append(1, '"'); |
161 |
|
✗ |
value = pptr + 6; |
162 |
|
|
} |
163 |
|
✗ |
else if (len > 6 and !strncmp(pptr, "'", 6)) { |
164 |
|
✗ |
s.append(1, '\''); |
165 |
|
✗ |
value = pptr + 6; |
166 |
|
|
} |
167 |
|
|
else { |
168 |
|
✗ |
s.append(1, '&'); |
169 |
|
✗ |
value = pptr + 1; |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
|
173 |
|
✗ |
s.append(value, eptr - value); |
174 |
|
✗ |
return true; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
///////////////////////////////////////////////////////////////////////////// |
178 |
|
✗ |
bool Attr::getUnsigned(const char *name, unsigned int &i) const |
179 |
|
|
{ |
180 |
|
✗ |
char *value; |
181 |
|
|
|
182 |
|
✗ |
if (!(find(name, value)) or !value) |
183 |
|
✗ |
return false; |
184 |
|
|
|
185 |
|
✗ |
i = strtoul(value, 0, 0); |
186 |
|
✗ |
return true; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
///////////////////////////////////////////////////////////////////////////// |
190 |
|
✗ |
bool Attr::getUnsignedList(const char *name, |
191 |
|
|
std::list<unsigned int> &intList) const |
192 |
|
|
{ |
193 |
|
✗ |
char *value; |
194 |
|
|
|
195 |
|
✗ |
if (!(find(name, value)) or !value) |
196 |
|
✗ |
return false; |
197 |
|
|
|
198 |
|
✗ |
std::istringstream is(value); |
199 |
|
✗ |
is.imbue(std::locale::classic()); |
200 |
|
|
|
201 |
|
✗ |
while (is) { |
202 |
|
✗ |
unsigned int i; |
203 |
|
✗ |
char comma; |
204 |
|
|
|
205 |
|
✗ |
is >> i; |
206 |
|
✗ |
if (is) |
207 |
|
✗ |
intList.push_back(i); |
208 |
|
✗ |
is >> comma; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
✗ |
return true; |
212 |
|
|
} |
213 |
|
|
|