[307] | 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 |
|
---|
[308] | 5 | #include <frams/param/multiparamload.h>
|
---|
[307] | 6 | #include <frams/virtfile/stdiofile.h>
|
---|
| 7 | #include <frams/errmgr/stdouterr.h>
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | @file
|
---|
[308] | 11 | Sample code: Loading Framsticks "objects" (structures).
|
---|
[307] | 12 |
|
---|
[308] | 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 |
|
---|
[307] | 17 | \include loader_test_param.cpp
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | struct Data
|
---|
| 21 | {
|
---|
| 22 | public:
|
---|
| 23 | SString text, longtext;
|
---|
| 24 | int i1, i2, i3;
|
---|
| 25 | float f1, f2, f3;
|
---|
| 26 | ExtValue x;
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | #define FIELDSTRUCT Data
|
---|
| 30 | ParamEntry 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
|
---|
[308] | 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())
|
---|
[307] | 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 |
|
---|
| 45 | int 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);
|
---|
[308] | 54 | StdioFILEDontClose virt_stdout(stdout);
|
---|
[307] | 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(¶m);
|
---|
| 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:
|
---|
[308] | 70 | fprintf(stderr, "comment: %s\n", (const char*)loader.getComment());
|
---|
[307] | 71 | break;
|
---|
| 72 |
|
---|
| 73 | case MultiParamLoader::AfterObject:
|
---|
[308] | 74 | fprintf(stderr, "loaded:\n");
|
---|
[307] | 75 | for (int i = 0; i < param.getPropCount(); i++)
|
---|
[308] | 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);
|
---|
[307] | 80 | break;
|
---|
| 81 |
|
---|
| 82 | case MultiParamLoader::BeforeObject:
|
---|
[308] | 83 | fprintf(stderr, "----- object found, loading...\n");
|
---|
[307] | 84 | param.setDefault(); //reset (initialize) struct contents
|
---|
| 85 | break;
|
---|
| 86 |
|
---|
[308] | 87 | case MultiParamLoader::OnError:
|
---|
[307] | 88 | fprintf(stderr, "Error: %s", (const char*)loader.getError());
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
[308] | 91 | return 0;
|
---|
[307] | 92 | }
|
---|