cleaned all trailing white space from source files.
[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 * word)
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(word, "id"))        { GetWord(); id = CopyString(word); }
59             else if(!strcmpi(word, "type")) { GetWord(); type = (ObjectType)atoi(word); }
60             else if(!strcmpi(word, "desc")) { GetWord(); desc = CopyString(word); }
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(word, "x"))          { GetWord(); position.x = atof(word); }
87             else if(!strcmpi(word, "y"))     { GetWord(); position.y = atof(word); }
88             else if(!strcmpi(word, "z"))     { GetWord(); position.z = atof(word); }
89             else if(!strcmpi(word, "speed")) { GetWord(); object.speed = atof(word); }
90             else if(!strcmpi(word, "dir"))   { GetWord(); object.direction = atof(word); }
91          }
92          object.position = position;
93       }
94    }
95 };