source: cpp/frams/_demos/serial_test.cpp @ 569

Last change on this file since 569 was 348, checked in by Maciej Komosinski, 10 years ago
  • explicit c_str() in SString instead of (const char*) cast
  • genetic converters and GenMan? are now thread-local which enables multi-threaded simulator separation
  • Property svn:eol-style set to native
File size: 2.2 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/vm/classes/collectionobj.h>
6
7void printIndent(int indent)
8{
9for(int i=0;i<indent;i++)
10        putchar(' ');
11}
12
13struct Trace
14{
15ExtObject object;
16Trace *previous;
17bool isAlreadyVisited(const ExtObject& o)
18        {
19        if (object==o) return true;
20        if (previous) return previous->isAlreadyVisited(o);
21        return false;
22        }
23};
24
25void print(ExtValue* v,int indent=0,Trace *prev_trace=NULL)
26{
27printIndent(indent);
28if (!v)
29        {puts("null"); return;}
30
31switch(v->getType())
32        {
33        case TUnknown: puts("null"); return;
34        case TInvalid: puts("invalid"); return;
35        case TInt: printf("int:"); break;
36        case TDouble: printf("double:"); break;
37        case TString: printf("string:"); break;
38        case TObj:
39                {
40                Trace next_trace={v->getObject(),prev_trace};
41                printf("%s object:",v->getObject().interfaceName());
42                if (prev_trace && prev_trace->isAlreadyVisited(next_trace.object))
43                        {
44                        printf(" already visited - breaking recursion\n");
45                        return;
46                        }
47                VectorObject *vec=VectorObject::fromObject(v->getObject(),false);
48                if (vec)
49                        {
50                        printf("\n");
51                        for(int i=0;i<vec->data.size();i++)
52                                print((ExtValue*)vec->data(i),indent+3,&next_trace);
53                        return;
54                        }
55                DictionaryObject *dict=DictionaryObject::fromObject(v->getObject(),false);
56                if (dict)
57                        {
58                        printf("\n");
59                        for(HashEntryIterator it(dict->hash);it.isValid();it++)
60                                {
61                                printIndent(indent+3);
62                                printf("key \"%s\"\n",it->key.c_str());
63                                print((ExtValue*)it->value,indent+6,&next_trace);
64                                }
65                        return;
66                        }
67                }
68        }
69puts(v->getString().c_str());
70}
71
72int main(int argc,char*argv[])
73{
74const char* in= (argc>1) ? argv[1] : "[1,2,3,null,{\"x\":3.4,\"y\":[7,77,777]},4]";
75
76// note: this is not an example of how to read a param field, but only a special case where the input data was
77// previously identified as "serialized" and the "@Serialized:" prefix was already removed
78
79printf("DESERIALIZATION TEST\n"
80       "input string = %s\n",in);
81ExtValue v;
82const char* ret=v.deserialize(in);
83if (ret==NULL)
84        {
85        printf("failed!\n");
86        return 1;
87        }
88else
89        {
90        printf("OK, consumed %d of %d characters\n\n",ret-in,strlen(in));
91        print(&v);
92        return 0;
93        }
94}
Note: See TracBrowser for help on using the repository browser.