summaryrefslogtreecommitdiffstats
path: root/src/Common/XmlPacket.h
blob: 007404a9d38b4bc79b29d9eb1b0de4d567e174a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * XmlProcessor.h
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MAD_COMMON_XMLPACKET_H_
#define MAD_COMMON_XMLPACKET_H_

#include <Net/Packet.h>

#include <map>
#include <string>
#include <sstream>
#include <vector>

#include <libxml/parser.h>

namespace Mad {
namespace Common {

class XmlPacket {
  public:
    class Entry;
    class List;

  private:
    xmlDocPtr doc;
    xmlNodePtr packetNode;
    Entry *entry;

  public:

    class Element {
      public:
        enum Type {
          NONE, LIST,
          INT, UINT, INT64, UINT64,
          FLOAT, DOUBLE, LONGDOUBLE, STRING
        };

      protected:
        friend class Entry;

        xmlNodePtr elementNode;

      private:
        std::string str;

        Type type;
        union Variant {
          int var_int;
          unsigned int var_uint;

          long long var_int64;
          unsigned long long var_uint64;

          float var_float;
          double var_double;
          long double var_ldouble;
        } value;

        static Entry nilEntry;

        template<typename T>
        void set(T val) {
          switch(type) {
            case INT:
              value.var_int = static_cast<int>(val);
              break;
            case UINT:
              value.var_uint = static_cast<unsigned int>(val);
              break;
            case INT64:
              value.var_int64 = static_cast<long long>(val);
              break;
            case UINT64:
              value.var_uint64 = static_cast<unsigned long long>(val);
              break;
            case FLOAT:
              value.var_float = static_cast<float>(val);
              break;
            case DOUBLE:
              value.var_double = static_cast<double>(val);
              break;
            case LONGDOUBLE:
              value.var_ldouble = static_cast<long double>(val);
              break;
            default:
              break;
          }

          updateStr();
        }

        void updateStr();

      protected:
        Element(xmlNodePtr node, Type type0) : elementNode(node), type(type0) {}

      public:
        Element(xmlNodePtr node);

        Type getType() const {
          return type;
        }

        virtual size_t getSize() const {
          return 0;
        }

        virtual Entry& operator[](size_t) {
          return nilEntry;
        }

        virtual const Entry& operator[](size_t) const {
          return nilEntry;
        }

        virtual bool insertEntry(size_t) {return false;}
        virtual bool addEntry() {return false;}
        virtual bool removeEntry(size_t) {return false;}

        template<typename T>
        operator T() {
          switch(type) {
            case INT:
              return static_cast<T>(value.var_int);
            case UINT:
              return static_cast<T>(value.var_uint);
            case INT64:
              return static_cast<T>(value.var_int64);
            case UINT64:
              return static_cast<T>(value.var_uint64);
            case FLOAT:
              return static_cast<T>(value.var_float);
            case DOUBLE:
              return static_cast<T>(value.var_double);
            case LONGDOUBLE:
              return static_cast<T>(value.var_ldouble);
            default:
              return static_cast<T>(0);
          }
        }

        operator std::string() {
          return str;
        }

        const Element& operator=(int val) {
          set(val);
          return *this;
        }

        const Element& operator=(unsigned int val) {
          set(val);
          return *this;
        }

        const Element& operator=(long long val) {
          set(val);
          return *this;
        }

        const Element& operator=(unsigned long long val) {
          set(val);
          return *this;
        }

        const Element& operator=(float val) {
          set(val);
          return *this;
        }

        const Element& operator=(double val) {
          set(val);
          return *this;
        }

        const Element& operator=(long double val) {
          set(val);
          return *this;
        }

        const Element& operator=(const std::string &val) {
          if(type == STRING) {
            str = val;
            updateStr();
          }

          return *this;
        }
    };

    class Entry {
      private:
        friend class List;

        xmlNodePtr entryNode;

        std::map<std::string, Element> elements;
        std::map<std::string, List> lists;

        static Element nilElement;

      private:
        bool add(const std::string &name, const std::string &value, const char *type) {
          if(!entryNode)
            return false;

          if(lists.find(name) != lists.end() || elements.find(name) != elements.end())
            return false;

          xmlNodePtr newNode = xmlNewTextChild(entryNode, 0, (xmlChar*)"value", (xmlChar*)value.c_str());
          xmlSetProp(newNode, (xmlChar*)"name", (xmlChar*)name.c_str());
          xmlSetProp(newNode, (xmlChar*)"type", (xmlChar*)type);

          if(!elements.insert(std::make_pair(name, Element(newNode))).second) {
            xmlUnlinkNode(newNode);
            xmlFreeNode(newNode);

            return false;
          }

          return true;
        }

      public:
        Entry(xmlNodePtr node);

        Element& operator[](const std::string &name);
        const Element& operator[](const std::string &name) const;

        bool add(const std::string &name, int val) {
          std::ostringstream buf;
          buf << val;

          return add(name, buf.str(), "int");
        }

        bool add(const std::string &name, unsigned int val) {
          std::ostringstream buf;
          buf << val;

          return add(name, buf.str(), "uint");
        }

        bool add(const std::string &name, long long val) {
          std::ostringstream buf;
          buf << val;

          return add(name, buf.str(), "int64");
        }

        bool add(const std::string &name, unsigned long long val) {
          std::ostringstream buf;
          buf << val;

          return add(name, buf.str(), "uint64");
        }

        bool add(const std::string &name, float val) {
          std::ostringstream buf;
          buf << val;

          return add(name, buf.str(), "float");
        }

        bool add(const std::string &name, double val) {
          std::ostringstream buf;
          buf << val;

          return add(name, buf.str(), "double");
        }

        bool add(const std::string &name, long double val) {
          std::ostringstream buf;
          buf << val;

          return add(name, buf.str(), "longdouble");
        }

        bool add(const std::string &name, const std::string &val) {
          return add(name, val, "string");
        }

        bool addList(const std::string &name) {
          if(!entryNode)
            return false;

          if(elements.find(name) != elements.end() || lists.find(name) != lists.end())
            return false;

          xmlNodePtr newNode = xmlNewChild(entryNode, 0, (xmlChar*)"list", 0);
          xmlSetProp(newNode, (xmlChar*)"name", (xmlChar*)name.c_str());

          if(!lists.insert(std::make_pair(name, List(newNode))).second) {
            xmlUnlinkNode(newNode);
            xmlFreeNode(newNode);

            return false;
          }

          return true;
        }

        bool remove(const std::string &name) {
          std::map<std::string, Element>::iterator element = elements.find(name);

          if(element != elements.end()) {
            xmlUnlinkNode(element->second.elementNode);
            xmlFreeNode(element->second.elementNode);

            elements.erase(element);

            return true;
          }

          std::map<std::string, List>::iterator list = lists.find(name);

          if(list != lists.end()) {
            xmlUnlinkNode(list->second.elementNode);
            xmlFreeNode(list->second.elementNode);

            lists.erase(list);

            return true;
          }

          return false;
        }
    };

    class List : public Element {
      private:
        std::vector<Entry> entries;

      public:
        List(xmlNodePtr node);

        virtual size_t getSize() const {
          return entries.size();
        }

        virtual Entry& operator[](size_t i) {
          return entries[i];
        }

        virtual const Entry& operator[](size_t i) const {
          return entries[i];
        }

        virtual bool insertEntry(size_t i) {
          if(i > getSize())
            return false;

          xmlNodePtr newNode = xmlNewNode(0, (xmlChar*)"entry");
          xmlAddChild(elementNode, newNode);

          entries.insert(entries.begin()+i, Entry(newNode));

          return true;
        }

        virtual bool addEntry() {
          return insertEntry(getSize());
        }

        virtual bool removeEntry(size_t i) {
          if(i >= getSize())
            return false;

          xmlUnlinkNode(entries[i].entryNode);
          xmlFreeNode(entries[i].entryNode);

          entries.erase(entries.begin()+i);

          return true;
        }
    };

    XmlPacket();
    XmlPacket(const XmlPacket &o);
    XmlPacket(const Net::Packet &packet);

    XmlPacket& operator=(const XmlPacket &o);

    virtual ~XmlPacket() {
      delete entry;

      xmlFreeDoc(doc);
    }

    std::string getType() const;
    void setType(const std::string &type);

    Element& operator[](const std::string &name) {
      return entry->operator[](name);
    }

    const Element& operator[](const std::string &name) const {
      return entry->operator[](name);
    }

    template <typename T>
    bool add(const std::string &name, T val) {
      return entry->add(name, val);
    }

    bool addList(const std::string &name) {
      return entry->addList(name);
    }

    bool remove(const std::string &name) {
      return entry->remove(name);
    }

    Net::Packet encode(uint16_t requestId);
};

}
}

#endif /* MAD_COMMON_XMLPACKET_H_ */