source: cpp/frams/_demos/loader_test_param.cpp @ 311

Last change on this file since 311 was 311, checked in by Maciej Komosinski, 7 years ago

More fields and test cases

File size: 3.4 KB
Line 
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 <frams/virtfile/stdiofile.h>
7#include <frams/errmgr/stdouterr.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 both streams if needed.
16
17 \include loader_test_param.cpp
18 */
19
20struct Data
21{
22public:
23        SString text, longtext, deftext;
24        int i1, i2, i3;
25        float f1, f2, f3;
26        ExtValue x;
27        int notchanged,notloaded,notsaved;
28};
29
30#define FIELDSTRUCT Data
31ParamEntry data_paramtab[] =
32{
33        { "Data", 1, 12, "data", },
34        { "text", 0, 0, "Text", "s 0 10", FIELD(text), }, // 10 = length limit
35        { "longtext", 0, 0, "Long text", "s 1", FIELD(longtext), }, // 1 = multiline, 0 = unlimited
36        { "deftext", 0, 0, "Text with default value", "s 0 0 some text", FIELD(deftext), },
37        { "i1", 0, 0, "Integer 1", "d", FIELD(i1), },          // unrestricted integer
38        { "i2", 0, 0, "Integer 2", "d -5 5 1", FIELD(i2), }, // [-5..5] integer, default=1
39        { "i3", 0, 0, "Integer 3", "d -1 3 0 ~Minus~Zero~One~Two~Three", FIELD(i3), }, // [0..3] + text labels (Param::getText())
40        { "f1", 0, 0, "Float 1", "d", FIELD(f1), },          // unrestricted float
41        { "f2", 0, 0, "Float 2", "f -100 100 -100", FIELD(f2), }, // [-100..100] float, default=-100
42        { "x", 0, 0, "Untyped", "x", FIELD(x), }, // any type (class ExtValue)
43        { "notchanged", 0, PARAM_READONLY, "Read only field", "d", FIELD(notchanged), }, // neither load() nor setDefault() can change this
44        { "notloaded", 0, PARAM_DONTLOAD, "Unloadable field", "d", FIELD(notloaded), }, // load() does not change this
45        { "notsaved", 0, PARAM_DONTSAVE, "Unsaveable field", "d", FIELD(notsaved), }, // save() skips this field
46        { 0, 0, 0, },
47};
48#undef FIELDSTRUCT
49
50int main(int argc, char*argv[])
51{
52        if (argc < 2)
53        {
54                fprintf(stderr, "Arguments: filename\n");
55                return 1;
56        }
57
58        StdioFILEDontClose virt_stderr(stderr);
59        StdioFILEDontClose virt_stdout(stdout);
60        StdoutErrorHandler error_handler(0, &virt_stderr);
61        StdioFileSystem_autoselect stdiofilesys;
62        MultiParamLoader loader(argv[1]);
63
64        Data data;
65        Param param(data_paramtab, &data);
66
67        data.notchanged=100;
68        data.notloaded=200;
69
70        loader.addObject(&param);
71        loader.breakOn(MultiParamLoader::OnError + MultiParamLoader::BeforeObject + MultiParamLoader::AfterObject + MultiParamLoader::OnComment);
72
73        while (int status = loader.go())
74        {
75                switch (status)
76                {
77                case MultiParamLoader::OnComment:
78                        fprintf(stderr, "comment: %s\n", (const char*)loader.getComment());
79                        break;
80
81                case MultiParamLoader::AfterObject:
82                        fprintf(stderr, "loaded:\n");
83                        for (int i = 0; i < param.getPropCount(); i++)
84                                fprintf(stderr, "%s=%s\n", param.id(i), (const char*)param.getText(i));
85                        fprintf(stderr, "type of 'x' is: %s\n", (const char*)data.x.typeDescription());
86                        fprintf(stderr, "-----\n\n");
87                        param.save(&virt_stdout);
88                        break;
89
90                case MultiParamLoader::BeforeObject:
91                        fprintf(stderr, "----- object found, loading...\n");
92                        data.notchanged++;
93                        param.setDefault(); //reset (initialize) struct contents
94                        break;
95
96                case MultiParamLoader::OnError:
97                        fprintf(stderr, "Error: %s", (const char*)loader.getError());
98                }
99        }
100        return 0;
101}
Note: See TracBrowser for help on using the repository browser.