GCC Code Coverage Report


Directory: ./
File: pdserv/src/msrproto/Attribute.cpp
Date: 2025-08-17 04:10:43
Exec Total Coverage
Lines: 0 94 0.0%
Branches: 0 166 0.0%

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