5bd3f23b54ee4502248655532163bd9fced461e2
[sdk] / samples / net / XMLSample / xmlSample.ec
1 /********************************************************
2    This sample demonstrates how to use the XMLParser
3    class to parse an XML communication protocol and
4    update model objects accordingly.
5 ********************************************************/
6
7 import "XMLParser"
8
9 enum ObjectType
10 {
11    plane, truck, human
12 };
13
14 class Track
15 {
16    String id;
17    ObjectType type;
18    String description;
19    Vector3Df position;
20    Degrees direction;
21    float speed;
22
23    ~Track()
24    {
25       delete id;
26       delete description;
27    }
28 }
29
30 List<Track> tracks { };
31
32 TrackXMLParser parser { };
33
34 class TrackingSocket : Socket
35 {
36    unsigned int OnReceive(unsigned char * buffer, unsigned int count)
37    {
38       parser.Parse(buffer, count);
39
40       // Update Things Here
41       return count;
42    }
43 }
44
45 class TrackXMLParser : XMLParser
46 {
47    Track object;
48
49    void ProcessKeyword(char * keyWord)
50    {
51       if(!strcmpi(keyWord, "object"))
52       {
53          char * id = null;
54          ObjectType type = 0;
55          char * desc = null;
56          while(GetWord())
57          {
58             if(!strcmpi(keyWord, "id"))        { GetWord(); id = CopyString(keyWord); }
59             else if(!strcmpi(keyWord, "type")) { GetWord(); type = (ObjectType)atoi(keyWord); }
60             else if(!strcmpi(keyWord, "desc")) { GetWord(); desc = CopyString(keyWord); }
61          }
62          for(object : tracks)
63          {
64             if(!strcmp(object.id, id)) break;
65          }
66          if(object)
67          {
68             object.type = type;
69             object.description = desc;
70          }
71          else
72          {
73             tracks.Add({ id = id, type = type, description = desc });
74          }
75          delete desc;
76       }
77       else if(!strcmpi(keyWord, "/object"))
78       {
79          object = null;
80       }
81       else if(!strcmpi(keyWord, "pos"))
82       {
83          Vector3Df position { };
84          while(GetWord())
85          {
86             if(!strcmpi(keyWord, "x"))          { GetWord(); position.x = (float)atof(keyWord); }
87             else if(!strcmpi(keyWord, "y"))     { GetWord(); position.y = (float)atof(keyWord); }
88             else if(!strcmpi(keyWord, "z"))     { GetWord(); position.z = (float)atof(keyWord); }
89             else if(!strcmpi(keyWord, "speed")) { GetWord(); object.speed = (float)atof(keyWord); }
90             else if(!strcmpi(keyWord, "dir"))   { GetWord(); object.direction = (float)atof(keyWord); }
91          }
92          object.position = position;
93       }
94    }
95 };
96
97 enum MyTag
98 {
99    none,
100    myTag1,
101    myTag2,
102    myTag3
103 };
104
105 class MyParser : XMLParser
106 {
107    MyTag tag;
108
109    void ProcessCharacterData(char * data)
110    {
111       switch(tag)
112       {
113          case myTag1: PrintLn("myTag1: ", data); break;
114          case myTag2: PrintLn("myTag2: ", data); break;
115          case myTag3: PrintLn("myTag3: ", data); break;
116       }
117    }
118
119    void ProcessKeyword(char * keyWord)
120    {
121       NamedLink64 nl;
122       EnumClassData tagData = class(MyTag).data;
123       for(nl = tagData.values.first; nl; nl = nl.next)
124       {
125          if(!strcmpi(keyWord, nl.name))
126          {
127             MyTag curTag = (MyTag)nl.data;
128             if(openingTag)
129                tag = curTag;
130             else
131                tag = none;
132             break;
133          }
134       }
135    }
136 }