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

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

Fix crash when no data provided

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                setParamTab(getParamTab());
16        }
17        void addGroup(const char* name)
18        {
19                std::shared_ptr<string> str(new std::string(name));
20                strings.push_back(str);
21                ParamEntry tmp = { str.get()->c_str(), 0, 0, 0 };
22                entries.insert(entries.begin() + (entries.size() - 1), tmp);
23                entries[0].group = entries.size() - 1;
24                setParamTab(getParamTab());
25        }
26        ParamEntry *getParamTab()
27        {
28                return &entries[0];
29        }
30};
31
32int main()
33{
34        SString group_names;
35        StdioFILE::setStdio(); //setup VirtFILE::Vstdin/out/err
36        puts("(loading group names from stdin)");
37        loadSString(VirtFILE::Vstdin, group_names);
38        int pos = 0;
39        SString line;
40        EmptyParamWithGroupsForTesting param;
41        while (group_names.getNextToken(pos, line, '\n'))
42        {
43                if ((line.len() > 0) && (line[line.len() - 1] == '\r')) //support for reading \r\n files...
44                        line = line.substr(0, line.len() - 1);
45                if (line.len() > 0 && line[0] != '#') //skip empty lines and #commment lines
46                        param.addGroup(line.c_str());
47        }
48        printf("%d groups\n\n", param.getGroupCount());
49
50        ParamTree tree(param);
51
52        printTree(&tree.root);
53}
Note: See TracBrowser for help on using the repository browser.