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

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

Tester/demo for Param loader

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