1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/
|
---|
2 | // Copyright (C) 1999-2018 Maciej Komosinski and Szymon Ulatowski.
|
---|
3 | // See LICENSE.txt for details.
|
---|
4 |
|
---|
5 | #include "paramtree.h"
|
---|
6 |
|
---|
7 | ParamTree::ParamTreeNode *ParamTree::addNode(ParamTree::ParamTreeNode* parent, const string& name, int group)
|
---|
8 | {
|
---|
9 | std::shared_ptr<ParamTreeNode> new_it(new ParamTreeNode(this, parent, name, group));
|
---|
10 | ParamTreeNode *last = parent->first_child.get();
|
---|
11 | if (last)
|
---|
12 | {
|
---|
13 | ParamTreeNode *next;
|
---|
14 | while (true)
|
---|
15 | {
|
---|
16 | next = last->next_sibling.get();
|
---|
17 | if (next == NULL) break;
|
---|
18 | last = next;
|
---|
19 | }
|
---|
20 | last->next_sibling = new_it;
|
---|
21 | }
|
---|
22 | else
|
---|
23 | parent->first_child = new_it;
|
---|
24 | return new_it.get();
|
---|
25 | }
|
---|
26 |
|
---|
27 | ParamTree::ParamTreeNode *ParamTree::findNode(ParamTree::ParamTreeNode *parent, const string& name)
|
---|
28 | {
|
---|
29 | for (ParamTree::ParamTreeNode *it = parent->first_child.get(); it != NULL; it = it->next_sibling.get())
|
---|
30 | if (it->name == name) return it;
|
---|
31 | return NULL;
|
---|
32 | }
|
---|
33 |
|
---|
34 | static int countChars(const char* str, char ch)
|
---|
35 | {
|
---|
36 | int count = 0;
|
---|
37 | for (; *str; str++)
|
---|
38 | if (*str == ch) count++;
|
---|
39 | return count;
|
---|
40 | }
|
---|
41 |
|
---|
42 | ParamTree::ParamTree(ParamInterface *_pi)
|
---|
43 | :pi(*_pi)
|
---|
44 | {
|
---|
45 | int g;
|
---|
46 | int level = 0, hasmorelevels = 1;
|
---|
47 | root.tree = this;
|
---|
48 | root.name = "[Tree root]";
|
---|
49 | for (; hasmorelevels; level++)
|
---|
50 | {
|
---|
51 | hasmorelevels = 0;
|
---|
52 | for (g = 0; g<pi.getGroupCount(); g++)
|
---|
53 | {
|
---|
54 | const char* grname = pi.grname(g);
|
---|
55 | int colons = countChars(grname, ':');
|
---|
56 | if (colons>level) hasmorelevels = 1;
|
---|
57 | if (colons != level) continue;
|
---|
58 |
|
---|
59 | string name = grname; // "abc:def:ghi"
|
---|
60 | ParamTreeNode *parentnode = &root;
|
---|
61 | int i = 0;
|
---|
62 | // search for parentnode for level 'level'
|
---|
63 | for (int haslevel = 0; haslevel < level; haslevel++)
|
---|
64 | {
|
---|
65 | i = name.find(':'); // always succeeds because we counted the colons before
|
---|
66 | // "na:me" -> prefix="na",name="me"
|
---|
67 | string prefix = name.substr(0, i);
|
---|
68 | // search for parent node in listview ('prefix')
|
---|
69 | ParamTreeNode *it = findNode(parentnode, prefix);
|
---|
70 | name = name.substr(i + 1);
|
---|
71 | if (it) parentnode = it;
|
---|
72 | else parentnode = addNode(parentnode, prefix, -1);
|
---|
73 | }
|
---|
74 | addNode(parentnode, name, g);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|