source: cpp/frams/_demos/paramtree_stdin_test.cpp @ 724

Last change on this file since 724 was 724, checked in by Maciej Komosinski, 6 years ago

Two sample programs demonstrating usage of param tree

File size: 1.7 KB
Line 
1#include <frams/util/sstringutils.h>
2#include <common/virtfile/stdiofile.h>
3#include <frams/param/paramtree.h>
4#include "paramtree_print.h"
5
6class EmptyParamWithGroupsForTesting : public Param
7{
8        std::vector<ParamEntry> entries;
9        std::vector<std::shared_ptr<std::string>> strings; //could be a simple vector of strings, but then char* pointers can change when adding new strings and so ParamEntry structures would need updating. Therefore using "vector of string pointers" instead of "vector of strings".
10public:
11        EmptyParamWithGroupsForTesting()
12        {
13                ParamEntry zero_ending = { 0, 0, 0, 0 };
14                entries.push_back(zero_ending);
15        }
16        void addGroup(const char* name)
17        {
18                std::shared_ptr<string> str(new std::string(name));
19                strings.push_back(str);
20                ParamEntry tmp = { str.get()->c_str(), 0, 0, 0 };
21                entries.insert(entries.begin() + (entries.size() - 1), tmp);
22                entries[0].group = entries.size() - 1;
23                setParamTab(getParamTab());
24        }
25        ParamEntry *getParamTab()
26        {
27                return &entries[0];
28        }
29};
30
31int main()
32{
33        SString group_names;
34        StdioFILE::setStdio();//setup VirtFILE::Vstdin/out/err
35        loadSString(VirtFILE::Vstdin, group_names);
36        int pos = 0;
37        SString line;
38        EmptyParamWithGroupsForTesting param;
39        puts("(loading group names from stdin)");
40        while (group_names.getNextToken(pos, line, '\n'))
41        {
42                if ((line.len() > 0) && (line[line.len() - 1] == '\r')) //support for reading \r\n files...
43                        line = line.substr(0, line.len() - 1);
44                if (line.len() > 0 && line[0] != '#') // skip empty lines and #commment lines
45                        param.addGroup(line.c_str());
46        }
47        printf("%d groups\n\n", param.getGroupCount());
48
49        ParamTree tree(param);
50
51        printTree(&tree.root);
52}
Note: See TracBrowser for help on using the repository browser.