1 | #include "paramtree.h"
|
---|
2 |
|
---|
3 | ParamTree::Node *ParamTree::addNode(ParamTree::Node* parent, const SString& name, int group)
|
---|
4 | {
|
---|
5 | std::shared_ptr<Node> new_it(new Node(this, parent, name, group));
|
---|
6 | Node *last = parent->first_child.get();
|
---|
7 | if (last)
|
---|
8 | {
|
---|
9 | Node *next;
|
---|
10 | while (true)
|
---|
11 | {
|
---|
12 | next = last->next_sibling.get();
|
---|
13 | if (next == NULL) break;
|
---|
14 | last = next;
|
---|
15 | }
|
---|
16 | last->next_sibling = new_it;
|
---|
17 | }
|
---|
18 | else
|
---|
19 | parent->first_child = new_it;
|
---|
20 | return new_it.get();
|
---|
21 | }
|
---|
22 |
|
---|
23 | ParamTree::Node *ParamTree::findNode(ParamTree::Node *parent, const SString& name)
|
---|
24 | {
|
---|
25 | for (ParamTree::Node *it = parent->first_child.get(); it != NULL; it = it->next_sibling.get())
|
---|
26 | if (it->name == name) return it;
|
---|
27 | return NULL;
|
---|
28 | }
|
---|
29 |
|
---|
30 | static int countChars(const char* str, char ch)
|
---|
31 | {
|
---|
32 | int count = 0;
|
---|
33 | for (; *str; str++)
|
---|
34 | if (*str == ch) count++;
|
---|
35 | return count;
|
---|
36 | }
|
---|
37 |
|
---|
38 | ParamTree::ParamTree(ParamInterface &_pi)
|
---|
39 | :pi(_pi)
|
---|
40 | {
|
---|
41 | int g;
|
---|
42 | int level = 0, hasmorelevels = 1;
|
---|
43 | root.tree = this;
|
---|
44 | root.name = "[Tree root]";
|
---|
45 | for (; hasmorelevels; level++)
|
---|
46 | {
|
---|
47 | hasmorelevels = 0;
|
---|
48 | for (g = 0; g<pi.getGroupCount(); g++)
|
---|
49 | {
|
---|
50 | const char* grname = pi.grname(g);
|
---|
51 | int colons = countChars(grname, ':');
|
---|
52 | if (colons>level) hasmorelevels = 1;
|
---|
53 | if (colons != level) continue;
|
---|
54 |
|
---|
55 | SString name = grname; // "abc:def:ghi"
|
---|
56 | Node *parentnode = &root;
|
---|
57 | int i = 0;
|
---|
58 | // search for parentnode for level 'level'
|
---|
59 | for (int haslevel = 0; haslevel < level; haslevel++)
|
---|
60 | {
|
---|
61 | i = name.indexOf(':', 0); // can't fail!
|
---|
62 | // "na:me" -> prefix="na",name="me"
|
---|
63 | SString prefix = name.substr(0, i);
|
---|
64 | // search for parent node in listview ('prefix')
|
---|
65 | Node *it = findNode(parentnode, prefix);
|
---|
66 | name = name.substr(i + 1);
|
---|
67 | if (it) parentnode = it;
|
---|
68 | else parentnode = addNode(parentnode, prefix, -1);
|
---|
69 | }
|
---|
70 | addNode(parentnode, name, g);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|