GCC Code Coverage Report


Directory: ./
File: pdserv/src/msrproto/DirectoryNode.cpp
Date: 2023-11-12 04:06:57
Exec Total Coverage
Lines: 104 139 74.8%
Branches: 125 256 48.8%

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 "config.h"
25
26 #include "DirectoryNode.h"
27 #include "HyperDirNode.h"
28 #include "XmlElement.h"
29 #include "Parameter.h"
30 #include "Channel.h"
31 #include "XmlParser.h"
32 #include "../Debug.h"
33 #include "../Parameter.h"
34
35 #include <locale>
36 #include <sstream>
37 #include <iostream>
38
39 using namespace MsrProto;
40
41 /////////////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////////////
43 5460 DirectoryNode::DirectoryNode(DirectoryNode* parent, const std::string& name):
44 5460 parent(this)
45 {
46
2/2
✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 4290 times.
5460 if (parent)
47
1/2
✓ Branch 4 taken 1170 times.
✗ Branch 5 not taken.
1170 parent->adopt(this, name);
48 5460 }
49
50 /////////////////////////////////////////////////////////////////////////////
51 12090 DirectoryNode::~DirectoryNode()
52 {
53 10790 for (ChildMap::iterator it = children.begin();
54
2/2
✓ Branch 6 taken 5330 times.
✓ Branch 7 taken 5460 times.
10790 it != children.end(); ++it)
55
1/2
✓ Branch 2 taken 5330 times.
✗ Branch 3 not taken.
5330 delete it->second;
56 6630 }
57
58 /////////////////////////////////////////////////////////////////////////////
59 6760 DirectoryNode* DirectoryNode::create(const std::string& name)
60 {
61 6760 DirectoryNode* dir = children[name];
62
3/4
✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 5590 times.
✓ Branch 5 taken 1170 times.
✗ Branch 6 not taken.
6760 return dir ? dir : new DirectoryNode(this, name);
63 }
64
65 /////////////////////////////////////////////////////////////////////////////
66 4160 void DirectoryNode::insert(DirectoryNode* node, const std::string& name)
67 {
68 4160 DirectoryNode* dir = children[name];
69
70 // log_debug("%s %p", name.c_str(), dir);
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4160 times.
4160 if (dir)
72 dir->insertLeaf(node);
73 else
74 4160 adopt(node, name);
75 4160 }
76
77 /////////////////////////////////////////////////////////////////////////////
78 3120 void DirectoryNode::traditionalPathInsert(Variable* var,
79 const std::string& path, char& hidden, char& persistent)
80 {
81
1/2
✓ Branch 2 taken 3120 times.
✗ Branch 3 not taken.
6240 DirQ dirQ;
82 3120 size_t pos = 0;
83
84 3120 hidden = 0;
85
2/2
✓ Branch 0 taken 12480 times.
✓ Branch 1 taken 3120 times.
28080 while (pos != path.npos) {
86
87
3/4
✓ Branch 2 taken 12480 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9360 times.
✓ Branch 6 taken 3120 times.
21840 std::string name = split(path, pos);
88
2/2
✓ Branch 1 taken 3120 times.
✓ Branch 2 taken 9360 times.
12480 if (name.empty())
89 3120 continue;
90
91 9360 size_t nameEnd = name.find('<');
92
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9360 times.
9360 if (nameEnd != name.npos) {
94 std::stringbuf buf(name);
95 XmlParser parser;
96 parser.read(&buf);
97
98 if (parser) {
99 const char *value;
100 if (parser.isTrue("hide"))
101 hidden = 1;
102 else if (parser.find("hide", &value))
103 hidden = *value;
104
105 if (parser.isTrue("unhide"))
106 hidden = 0;
107
108 if (parser.find("persistent"))
109 persistent = parser.isTrue("persistent");
110
111 while (nameEnd
112 and std::isspace(name[nameEnd-1],
113 std::locale::classic()))
114 nameEnd--;
115 }
116 else
117 nameEnd = name.npos;
118 }
119
120
3/6
✓ Branch 1 taken 9360 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9360 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9360 times.
9360 if (name.empty() or !nameEnd)
121 continue;
122
123
4/6
✓ Branch 2 taken 9360 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 9360 times.
✗ Branch 7 not taken.
✓ Branch 11 taken 9360 times.
✓ Branch 12 taken 3120 times.
9360 dirQ.push(std::string(name, 0, nameEnd));
124 }
125
126
1/2
✓ Branch 4 taken 3120 times.
✗ Branch 5 not taken.
3120 insert(var, dirQ);
127 3120 }
128
129 /////////////////////////////////////////////////////////////////////////////
130 9360 void DirectoryNode::insert(Variable* var, DirQ& dirQ)
131 {
132
2/4
✓ Branch 3 taken 9360 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 9360 times.
✗ Branch 8 not taken.
18720 std::string name = dirQ.front();
133
1/2
✓ Branch 2 taken 9360 times.
✗ Branch 3 not taken.
9360 dirQ.pop();
134
135
2/2
✓ Branch 2 taken 3120 times.
✓ Branch 3 taken 6240 times.
9360 if (dirQ.empty())
136
1/2
✓ Branch 4 taken 3120 times.
✗ Branch 5 not taken.
3120 return insert(var, name);
137
138
2/4
✓ Branch 4 taken 6240 times.
✗ Branch 5 not taken.
✓ Branch 10 taken 6240 times.
✗ Branch 11 not taken.
6240 return create(name)->insert(var, dirQ);
139 }
140
141
142 /////////////////////////////////////////////////////////////////////////////
143 void DirectoryNode::pathInsert(Variable* var, const std::string& path)
144 {
145 DirQ dirQ;
146 size_t pos = 0;
147
148 while (pos != path.npos) {
149
150 std::string name = split(path, pos);
151 if (!name.empty())
152 dirQ.push(name);
153 }
154
155 insert(var, dirQ);
156 }
157
158 /////////////////////////////////////////////////////////////////////////////
159 12748 std::string DirectoryNode::split(const std::string& path, size_t& pos)
160 {
161 // Find path separator
162 12748 size_t slash = path.find('/', pos);
163
164 12748 const size_t begin = pos;
165
166 // Place pos to point just after the next '/'
167 12748 pos = slash;
168
4/6
✓ Branch 3 taken 9502 times.
✓ Branch 4 taken 3246 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 9502 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 12748 times.
12748 while (pos < path.size() and path[++pos] == '/');
169
170 12748 return std::string(path, begin, slash - begin);
171 }
172
173 /////////////////////////////////////////////////////////////////////////////
174 void DirectoryNode::insertLeaf (DirectoryNode *node)
175 {
176 // This is a normal directory node. Insert a HyperDirNode to cope with
177 // nodes that have the same name
178 HyperDirNode *dir = new HyperDirNode(parent, name);
179 dir->insertLeaf(this);
180 dir->insertLeaf(node);
181 }
182
183 /////////////////////////////////////////////////////////////////////////////
184 5330 void DirectoryNode::adopt (DirectoryNode *child, const std::string& name)
185 {
186 // log_debug("%s %p", name.c_str(), child);
187
188 5330 children[name] = child;
189
1/2
✓ Branch 9 taken 5330 times.
✗ Branch 10 not taken.
5330 child->rename(&children.find(name)->first, this);
190 5330 }
191
192 /////////////////////////////////////////////////////////////////////////////
193 1011 bool DirectoryNode::isRoot() const
194 {
195 1011 return parent == this;
196 }
197
198 /////////////////////////////////////////////////////////////////////////////
199 5330 void DirectoryNode::rename (const std::string* name, DirectoryNode *parent)
200 {
201 5330 this->parent = parent;
202 5330 this->name = name;
203 5330 }
204
205 /////////////////////////////////////////////////////////////////////////////
206 30 void DirectoryNode::list( PdServ::Session *session, XmlElement& parent,
207 const std::string& path, size_t pos, bool hex, bool derived) const
208 {
209
2/2
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 20 times.
40 std::string name;
210
2/2
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 20 times.
40 ChildMap::const_iterator it;
211
212
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
30 if (pos != path.npos) {
213 10 do {
214
1/2
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 name = split(path, pos);
215
5/6
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 20 times.
30 } while (name.empty() and pos != name.npos);
216
217
1/2
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 it = children.find(name);
218
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (!name.empty()) {
219
1/2
✓ Branch 6 taken 20 times.
✗ Branch 7 not taken.
20 if (it != children.end())
220
1/2
✓ Branch 6 taken 20 times.
✗ Branch 7 not taken.
20 it->second->list(session, parent, path, pos, hex, derived);
221 20 return;
222 }
223 }
224
225
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 const Variable *var = dynamic_cast<const Variable *>(this);
226
4/4
✓ Branch 12 taken 15 times.
✓ Branch 13 taken 10 times.
✓ Branch 16 taken 10 times.
✓ Branch 17 taken 20 times.
25 for (it = children.begin(); it != children.end(); ++it) {
227 15 const DirectoryNode* node = it->second;
228
229
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 const Parameter *param = dynamic_cast<const Parameter *>(node);
230
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 5 times.
✗ Branch 8 not taken.
15 if (param and !param->hidden
231
2/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
5 and (derived or (!var or var->variable != param->variable))) {
232
1/2
✗ Branch 12 not taken.
✓ Branch 13 taken 5 times.
5 char buf[param->mainParam->memSize];
233 5 struct timespec ts;
234
235
1/2
✓ Branch 24 taken 5 times.
✗ Branch 25 not taken.
5 param->mainParam->getValue(session, buf, &ts);
236
237
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
10 XmlElement xml(parent.createChild("parameter"));
238
1/2
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
10 param->setXmlAttributes(xml, buf, ts, 0, hex, derived, 16);
239 }
240
241
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 const Channel *chan = dynamic_cast<const Channel *>(node);
242
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 5 times.
✗ Branch 8 not taken.
15 if (chan and !chan->hidden
243
2/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
5 and (derived or (!var or var->variable != chan->variable))) {
244
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
10 XmlElement xml(parent.createChild("channel"));
245
1/2
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 chan->setXmlAttributes(xml, 0, hex, derived, 0, 0, 0);
246 }
247
248 // If there are children, report this node as a directory
249
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 10 times.
25 if (!chan and !param
250
5/8
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5 times.
✗ Branch 13 not taken.
20 and (derived or node->hasChildren(var))) {
251
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
10 XmlElement el(parent.createChild("dir"));
252
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
10 XmlElement::Attribute(el, "path")
253
4/8
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 12 taken 5 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 5 times.
✗ Branch 17 not taken.
✓ Branch 20 taken 5 times.
✗ Branch 21 not taken.
10 .setEscaped(this->path() + '/' + it->first);
254 }
255 }
256 }
257
258 /////////////////////////////////////////////////////////////////////////////
259 231 bool DirectoryNode::hasChildren(const Variable* var) const
260 {
261
4/4
✓ Branch 4 taken 221 times.
✓ Branch 5 taken 10 times.
✓ Branch 7 taken 221 times.
✓ Branch 8 taken 10 times.
419 for (ChildMap::const_iterator it = children.begin();
262
2/2
✓ Branch 6 taken 198 times.
✓ Branch 7 taken 221 times.
419 it != children.end(); ++it)
263
3/4
✓ Branch 8 taken 198 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 10 times.
✓ Branch 11 taken 188 times.
198 if (it->second->hasChildren(var))
264 10 return true;
265
266 221 return false;
267 }
268
269 /////////////////////////////////////////////////////////////////////////////
270 1011 std::string DirectoryNode::path() const
271 {
272 // log_debug("this=%p parent=%p", this, parent);
273
13/24
✓ Branch 4 taken 274 times.
✓ Branch 5 taken 737 times.
✓ Branch 20 taken 737 times.
✗ Branch 21 not taken.
✓ Branch 24 taken 737 times.
✗ Branch 25 not taken.
✓ Branch 29 taken 737 times.
✗ Branch 30 not taken.
✓ Branch 31 taken 737 times.
✓ Branch 32 taken 274 times.
✓ Branch 34 taken 737 times.
✓ Branch 35 taken 274 times.
✓ Branch 37 taken 737 times.
✓ Branch 38 taken 274 times.
✓ Branch 40 taken 737 times.
✓ Branch 41 taken 274 times.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
1011 return isRoot() ? std::string() : parent->path() + '/' + *name;
274 }
275
276 /////////////////////////////////////////////////////////////////////////////
277 238 const DirectoryNode *DirectoryNode::find(
278 const std::string& path, size_t pos) const
279 {
280
1/2
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
476 std::string name = split(path, pos);
281
282
1/2
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
476 ChildMap::const_iterator it = children.find(name);
283
2/2
✓ Branch 6 taken 16 times.
✓ Branch 7 taken 222 times.
238 if (it == children.end())
284 16 return 0;
285
286 222 return pos < path.size()
287
3/4
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 102 times.
✓ Branch 8 taken 120 times.
✗ Branch 9 not taken.
324 ? it->second->find(path, pos)
288 324 : it->second;
289 }
290
291 /////////////////////////////////////////////////////////////////////////////
292 void DirectoryNode::dump() const
293 {
294 // log_debug("%s", path().c_str());
295 for (ChildMap::const_iterator it = children.begin();
296 it != children.end(); ++it) {
297 it->second->dump();
298 }
299 9 }
300