[734] | 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 |
|
---|
[723] | 5 | #include "paramtree.h"
|
---|
| 6 |
|
---|
[755] | 7 | ParamTree::ParamTreeNode *ParamTree::addNode(ParamTree::ParamTreeNode* parent, const string& name, int group)
|
---|
[723] | 8 | {
|
---|
[755] | 9 | std::shared_ptr<ParamTreeNode> new_it(new ParamTreeNode(this, parent, name, group));
|
---|
| 10 | ParamTreeNode *last = parent->first_child.get();
|
---|
[723] | 11 | if (last)
|
---|
| 12 | {
|
---|
[755] | 13 | ParamTreeNode *next;
|
---|
[723] | 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 |
|
---|
[755] | 27 | ParamTree::ParamTreeNode *ParamTree::findNode(ParamTree::ParamTreeNode *parent, const string& name)
|
---|
[723] | 28 | {
|
---|
[755] | 29 | for (ParamTree::ParamTreeNode *it = parent->first_child.get(); it != NULL; it = it->next_sibling.get())
|
---|
[723] | 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 |
|
---|
[744] | 42 | ParamTree::ParamTree(ParamInterface *_pi)
|
---|
| 43 | :pi(*_pi)
|
---|
[723] | 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 |
|
---|
[734] | 59 | string name = grname; // "abc:def:ghi"
|
---|
[755] | 60 | ParamTreeNode *parentnode = &root;
|
---|
[723] | 61 | int i = 0;
|
---|
| 62 | // search for parentnode for level 'level'
|
---|
| 63 | for (int haslevel = 0; haslevel < level; haslevel++)
|
---|
| 64 | {
|
---|
[734] | 65 | i = name.find(':'); // always succeeds because we counted the colons before
|
---|
[723] | 66 | // "na:me" -> prefix="na",name="me"
|
---|
[734] | 67 | string prefix = name.substr(0, i);
|
---|
[723] | 68 | // search for parent node in listview ('prefix')
|
---|
[755] | 69 | ParamTreeNode *it = findNode(parentnode, prefix);
|
---|
[723] | 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 | }
|
---|