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

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

After loading, objects are printed ("saved") to standard output. Additional information and messages are printed to standard error.

File size: 2.9 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;
24        int i1, i2, i3;
25        float f1, f2, f3;
26        ExtValue x;
27};
28
29#define FIELDSTRUCT Data
30ParamEntry data_paramtab[] =
31{
32        { "Data", 1, 8, "data", },
33        { "text", 0, 0, "Text", "s 0 10", FIELD(text), }, // 10 = length limit
34        { "longtext", 0, 0, "Long text", "s 1", FIELD(longtext), }, // 1 = multiline, 0 = unlimited
35        { "i1", 0, 0, "Integer 1", "d", FIELD(i1), },          // unrestricted integer
36        { "i2", 0, 0, "Integer 2", "d -5 5 1", FIELD(i2), }, // [-5..5] integer, default=1
37        { "i3", 0, 0, "Integer 3", "d 0 3 0 ~Zero~One~Two~Three", FIELD(i3), }, // [0..3] + text labels (Param::getText())
38        { "f1", 0, 0, "Float 1", "d", FIELD(f1), },          // unrestricted float
39        { "f2", 0, 0, "Float 2", "f -100 100 -100", FIELD(f2), }, // [-100..100] float, default=-100
40        { "x", 0, 0, "Untyped", "x", FIELD(x), }, // any type (class ExtValue)
41        { 0, 0, 0, },
42};
43#undef FIELDSTRUCT
44
45int main(int argc, char*argv[])
46{
47        if (argc < 2)
48        {
49                fprintf(stderr, "Arguments: filename\n");
50                return 1;
51        }
52
53        StdioFILEDontClose virt_stderr(stderr);
54        StdioFILEDontClose virt_stdout(stdout);
55        StdoutErrorHandler error_handler(0, &virt_stderr);
56        StdioFileSystem_autoselect stdiofilesys;
57        MultiParamLoader loader(argv[1]);
58
59        Data data;
60        Param param(data_paramtab, &data);
61
62        loader.addObject(&param);
63        loader.breakOn(MultiParamLoader::OnError + MultiParamLoader::BeforeObject + MultiParamLoader::AfterObject + MultiParamLoader::OnComment);
64
65        while (int status = loader.go())
66        {
67                switch (status)
68                {
69                case MultiParamLoader::OnComment:
70                        fprintf(stderr, "comment: %s\n", (const char*)loader.getComment());
71                        break;
72
73                case MultiParamLoader::AfterObject:
74                        fprintf(stderr, "loaded:\n");
75                        for (int i = 0; i < param.getPropCount(); i++)
76                                fprintf(stderr, "%s=%s\n", param.id(i), (const char*)param.getText(i));
77                        fprintf(stderr, "type of 'x' is: %s\n", (const char*)data.x.typeDescription());
78                        fprintf(stderr, "-----\n\n");
79                        param.save(&virt_stdout);
80                        break;
81
82                case MultiParamLoader::BeforeObject:
83                        fprintf(stderr, "----- object found, loading...\n");
84                        param.setDefault(); //reset (initialize) struct contents
85                        break;
86
87                case MultiParamLoader::OnError:
88                        fprintf(stderr, "Error: %s", (const char*)loader.getError());
89                }
90        }
91        return 0;
92}
Note: See TracBrowser for help on using the repository browser.