1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/
|
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski.
|
---|
3 | // See LICENSE.txt for details.
|
---|
4 |
|
---|
5 | #include <frams/param/multiparamload.h>
|
---|
6 | #include <common/virtfile/stdiofile.h>
|
---|
7 | #include <common/loggers/loggertostdout.h>
|
---|
8 |
|
---|
9 | /**
|
---|
10 | @file
|
---|
11 | Sample code: Loading Framsticks "objects" (structures).
|
---|
12 |
|
---|
13 | After loading, objects are printed ("saved") to standard output.
|
---|
14 | Additional information and messages are printed to standard error.
|
---|
15 | You can redirect one or both streams if needed.
|
---|
16 |
|
---|
17 | A sample input file for this program is "loader_test_param.in",
|
---|
18 | so you can run this program from the "cpp" directory as
|
---|
19 | ./loader_test_param frams/_demos/loader_test_param.in
|
---|
20 |
|
---|
21 | \include loader_test_param.cpp
|
---|
22 | */
|
---|
23 |
|
---|
24 | struct Data
|
---|
25 | {
|
---|
26 | public:
|
---|
27 | SString text, longtext, deftext;
|
---|
28 | paInt i1, i2, i3, i4;
|
---|
29 | double f1, f2, f3, f4;
|
---|
30 | ExtValue x1, x2;
|
---|
31 | int notchanged, notloaded, notsaved;
|
---|
32 | };
|
---|
33 |
|
---|
34 | #define FIELDSTRUCT Data
|
---|
35 | ParamEntry data_paramtab[] =
|
---|
36 | {
|
---|
37 | { "Data", 1, 16, "data", },
|
---|
38 | { "text", 0, 0, "Text", "s 0 10", FIELD(text), }, // 10 = length limit
|
---|
39 | { "longtext", 0, 0, "Long text", "s 1", FIELD(longtext), }, // 1 = multiline, 0 = unlimited
|
---|
40 | { "deftext", 0, 0, "Text with default value", "s 0 0 some text", FIELD(deftext), },
|
---|
41 | { "i1", 0, 0, "Integer 1", "d", FIELD(i1), }, // unrestricted integer
|
---|
42 | { "i2", 0, 0, "Integer 2", "d -5 5 1", FIELD(i2), }, // [-5..5] integer, default=1
|
---|
43 | { "i3", 0, 0, "Integer 3", "d -1 3 0 ~Minus~Zero~One~Two~Three", FIELD(i3), }, // [0..3] + text labels (Param::getText())
|
---|
44 | { "i4", 0, 0, "Integer 4", "d 0 10 15", FIELD(i4), }, // invalid default
|
---|
45 | { "f1", 0, 0, "Float 1", "d", FIELD(f1), }, // unrestricted float
|
---|
46 | { "f2", 0, 0, "Float 2", "f -100 100 -100", FIELD(f2), }, // [-100..100] float, default=-100
|
---|
47 | { "f3", 0, 0, "Float 3", "f -10 10", FIELD(f3), }, // [-10..10] float
|
---|
48 | { "f4", 0, 0, "Float 4", "f 1 -1 44", FIELD(f4), }, // unrestricted float (because min>max), default=44
|
---|
49 | { "x1", 0, 0, "Untyped 1", "x", FIELD(x1), }, // any type (class ExtValue)
|
---|
50 | { "x2", 0, 0, "Untyped 2", "x", FIELD(x2), }, // any type (class ExtValue)
|
---|
51 | { "notchanged", 0, PARAM_READONLY, "Read only field", "d", FIELD(notchanged), }, // neither load() nor setDefault() can change this field
|
---|
52 | { "notloaded", 0, PARAM_DONTLOAD, "Non-loadable field", "d", FIELD(notloaded), }, // load() does not change this field
|
---|
53 | { "notsaved", 0, PARAM_DONTSAVE, "Non-saveable field", "d", FIELD(notsaved), }, // saveSingle/MultiLine() skips this field
|
---|
54 | { 0, 0, 0, },
|
---|
55 | };
|
---|
56 | #undef FIELDSTRUCT
|
---|
57 |
|
---|
58 | int main(int argc, char*argv[])
|
---|
59 | {
|
---|
60 | if (argc < 2)
|
---|
61 | {
|
---|
62 | fprintf(stderr, "Arguments: filename\n");
|
---|
63 | return 1;
|
---|
64 | }
|
---|
65 |
|
---|
66 | StdioFILEDontClose virt_stderr(stderr);
|
---|
67 | StdioFILEDontClose virt_stdout(stdout);
|
---|
68 | LoggerToStdout messages_to_stderr(LoggerBase::Enable, &virt_stderr);
|
---|
69 | StdioFileSystem_autoselect stdiofilesys;
|
---|
70 | MultiParamLoader loader(argv[1]);
|
---|
71 |
|
---|
72 | Data data;
|
---|
73 | Param param(data_paramtab, &data);
|
---|
74 |
|
---|
75 | data.notchanged = 100;
|
---|
76 | data.notloaded = 200;
|
---|
77 |
|
---|
78 | loader.addObject(¶m);
|
---|
79 | loader.breakOn(MultiParamLoader::OnError + MultiParamLoader::BeforeObject + MultiParamLoader::AfterObject + MultiParamLoader::OnComment + MultiParamLoader::BeforeUnknown);
|
---|
80 |
|
---|
81 | while (int status = loader.go())
|
---|
82 | {
|
---|
83 | switch (status)
|
---|
84 | {
|
---|
85 | case MultiParamLoader::OnComment:
|
---|
86 | fprintf(stderr, "comment: '%s'\n", loader.getComment().c_str());
|
---|
87 | break;
|
---|
88 |
|
---|
89 | case MultiParamLoader::BeforeUnknown:
|
---|
90 | // At this point we could change our mind and load the unknown object using MultiParamLoader::loadObjectNow() functions.
|
---|
91 | // It is "unknown", so we would have to provide its ParamInterface.
|
---|
92 | // In fact, this method is used not just for truly unknown objects but also for
|
---|
93 | // dynamic objects that cannot be added using MultiParamLoader.addObject().
|
---|
94 | fprintf(stderr, "unknown object found: '%s' (will be skipped)\n", loader.getObjectName().c_str());
|
---|
95 | break;
|
---|
96 |
|
---|
97 | case MultiParamLoader::AfterObject:
|
---|
98 | fprintf(stderr, "loaded:\n");
|
---|
99 | for (int i = 0; i < param.getPropCount(); i++)
|
---|
100 | fprintf(stderr, "%s=%s\n", param.id(i), param.getText(i).c_str());
|
---|
101 | fprintf(stderr, "type of 'x1' is: %s\n", data.x1.typeDescription().c_str());
|
---|
102 | fprintf(stderr, "type of 'x2' is: %s\n", data.x2.typeDescription().c_str());
|
---|
103 | fprintf(stderr, "-----\n\n");
|
---|
104 | param.saveMultiLine(&virt_stdout);
|
---|
105 | break;
|
---|
106 |
|
---|
107 | case MultiParamLoader::BeforeObject:
|
---|
108 | fprintf(stderr, "----- object found, loading...\n");
|
---|
109 | data.notchanged++;
|
---|
110 | param.setDefault(); //reset (initialize) struct contents
|
---|
111 | break;
|
---|
112 |
|
---|
113 | case MultiParamLoader::OnError:
|
---|
114 | fprintf(stderr, "Error: %s", loader.getError().c_str());
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return 0;
|
---|
118 | }
|
---|