summaryrefslogtreecommitdiffstats
path: root/src/Common/XmlData.h
blob: 4028508ce81d524e3265bb9b4618ace895480adf (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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * XmlData.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 Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MAD_COMMON_XMLDATA_H_
#define MAD_COMMON_XMLDATA_H_

#include "export.h"

#include <Net/Packet.h>
#include <Core/Exception.h>

#include <map>
#include <set>
#include <sstream>
#include <vector>

#include <libxml/parser.h>

#include <boost/cstdint.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/noncopyable.hpp>
#include <boost/variant/get.hpp>
#include <boost/variant/variant.hpp>
#include <boost/smart_ptr.hpp>

namespace Mad {
namespace Common {

class MAD_COMMON_EXPORT XmlData {
  public:
    class Entry;
    class List;

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

    class MAD_COMMON_EXPORT Element : private boost::noncopyable {
      private:
        friend class Entry;

        enum Type {
          NONE, BINARY, TIME,
          INT, UINT, INT64, UINT64,
          FLOAT, DOUBLE, LONGDOUBLE, STRING
        };

        typedef boost::variant<long, unsigned long, long long, unsigned long long,
          float, double, long double, std::vector<boost::uint8_t>, boost::posix_time::ptime> Variant;

        xmlNodePtr elementNode;

        Type type;
        Variant value;

        Core::String str;

        void updateStr();

        Element(xmlNodePtr node);

        template <typename T>
        void set(T val, Type type0) {
          type = type0;
          value = val;

          updateStr();
        }

        void set(int val) {
          set<long>(val, INT);
        }

        void set(unsigned int val) {
          set<unsigned long>(val, UINT);
        }

        void set(long val) {
          set(val, INT);
        }

        void set(unsigned long val) {
          set(val, UINT);
        }

        void set(long long val) {
          set(val, INT64);
        }

        void set(unsigned long long val) {
          set(val, UINT64);
        }

        void set(float val) {
          set(val, FLOAT);
        }

        void set(double val) {
          set(val, DOUBLE);
        }

        void set(long double val) {
          set(val, LONGDOUBLE);
        }

        void set(const Core::String &val) {
          type = STRING;
          value = Element::Variant();
          str = val;
          updateStr();
        }

        void set(const std::vector<boost::uint8_t> &val) {
          set(val, BINARY);
        }

        void set(const boost::posix_time::ptime &val) {
          set(val, TIME);
        }
    };

  public:
    class MAD_COMMON_EXPORT Entry : private boost::noncopyable {
      private:
        friend class List;
        friend class XmlData;

        xmlNodePtr entryNode;

        std::map<Core::String, Element*> elements;
        std::map<Core::String, List*> lists;

        Element* getElement(const Core::String &name) {
          std::map<Core::String, Element*>::iterator element = elements.find(name);

          if(element != elements.end())
            return element->second;

          if(lists.find(name) != lists.end())
            return 0;

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

          Element* newElement = new Element(newNode);

          elements.insert(std::make_pair(name, newElement));

          return newElement;
        }

        Element* getElement(const Core::String &name) const {
          std::map<Core::String, Element*>::const_iterator element = elements.find(name);

          if(element != elements.end())
            return element->second;
          else
            return 0;
        }

        Entry(xmlNodePtr node);
        ~Entry() {
          for(std::map<Core::String, Element*>::iterator element = elements.begin(); element != elements.end(); ++element)
            delete element->second;

          for(std::map<Core::String, List*>::iterator list = lists.begin(); list != lists.end(); ++list)
            delete list->second;
        }

      public:
        template <typename T>
        bool set(const Core::String &name, T val) {
          Element *element = getElement(name);
          if(!element)
            return false;

          element->set(val);

          return true;
        }

        template <typename T>
        T get(const Core::String &name) const {
          Element *element = getElement(name);
          if(!element)
            return T();

          switch(element->type) {
            case Element::INT:
              return static_cast<T>(boost::get<long>(element->value));
            case Element::UINT:
              return static_cast<T>(boost::get<unsigned long>(element->value));
            case Element::INT64:
              return static_cast<T>(boost::get<long long>(element->value));
            case Element::UINT64:
              return static_cast<T>(boost::get<unsigned long long>(element->value));
            case Element::FLOAT:
              return static_cast<T>(boost::get<float>(element->value));
            case Element::DOUBLE:
              return static_cast<T>(boost::get<double>(element->value));
            case Element::LONGDOUBLE:
              return static_cast<T>(boost::get<long double>(element->value));
            default:
              return static_cast<T>(0);
          }
        }

        List* createList(const Core::String &name, bool unique = false) {
          std::map<Core::String, List*>::iterator list = lists.find(name);

          if(list != lists.end())
            return unique ? 0 : list->second;

          if(elements.find(name) != elements.end())
            return 0;

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

          List *newList = new List(newNode);

          lists.insert(std::make_pair(name, newList));

          return newList;
        }

        List* getList(const Core::String &name) {
          std::map<Core::String, List*>::iterator list = lists.find(name);

          if(list == lists.end())
            return 0;

          return list->second;
        }

        const List* getList(const Core::String &name) const {
          std::map<Core::String, List*>::const_iterator list = lists.find(name);

          if(list == lists.end())
            return 0;

          return list->second;
        }

        void unset(const Core::String &name) {
          std::map<Core::String, Element*>::iterator element = elements.find(name);

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

            delete element->second;

            elements.erase(element);

            return;
          }

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

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

            delete list->second;

            lists.erase(list);

            return;
          }

          return;
        }

        std::set<Core::String> getChildren() const {
          std::set<Core::String> childNames;

          for(std::map<Core::String, Element*>::const_iterator it = elements.begin(); it != elements.end(); ++it)
            childNames.insert(it->first);

          return childNames;
        }

        std::set<Core::String> getLists() const {
          std::set<Core::String> listNames;

          for(std::map<Core::String, List*>::const_iterator it = lists.begin(); it != lists.end(); ++it)
            listNames.insert(it->first);

          return listNames;
        }
    };

    class MAD_COMMON_EXPORT List : private boost::noncopyable {
      private:
        friend class Entry;

        xmlNodePtr elementNode;

        std::vector<Entry*> entries;

        template <typename Type, typename IteratorType>
        class iterator_base {
          public:
            friend class List;

            typedef Type value_type;

            typedef value_type &reference;
            typedef value_type *pointer;

            typedef long difference_type;

          private:
            IteratorType it;

            iterator_base(IteratorType it0) : it(it0) {}

          public:
            iterator_base() {}

            reference operator*() const {
              return **it;
            }

            iterator_base operator+(const difference_type &n) const {
              return iterator_base(it+n);
            }

            iterator_base operator++(int) {
              return iterator_base(it++);
            }

            iterator_base& operator++() {
              ++it;
              return *this;
            }

            iterator_base& operator+=(const difference_type &n) {
              it += n;
              return *this;
            }

            iterator_base operator-(const difference_type &n) const {
              return iterator_base(it-n);
            }

            iterator_base operator--(int) {
              return iterator_base(it--);
            }

            iterator_base& operator--() {
              --it;
              return *this;
            }

            iterator_base& operator-=(const difference_type &n) {
              it -= n;
              return *this;
            }

            bool operator==(const iterator_base &it2) {
              return it2.it == it;
            }

            bool operator!=(const iterator_base &it2) {
              return it2.it != it;
            }

            pointer operator->() const {
              return *it;
            }

            reference operator[](const difference_type &n) const {
              return *it[n];
            }
        };

        List(xmlNodePtr node);
        ~List() {
          for(std::vector<Entry*>::iterator entry = entries.begin(); entry != entries.end(); ++entry)
            delete *entry;
        }

      public:
        typedef iterator_base<Entry, std::vector<Entry*>::iterator> iterator;
        typedef iterator_base<const Entry, std::vector<Entry*>::const_iterator> const_iterator;

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

        bool isEmpty() const {
          return entries.empty();
        }

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

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

        iterator begin() {
          return iterator(entries.begin());
        }

        const_iterator begin() const {
          return const_iterator(entries.begin());
        }

        iterator end() {
          return iterator(entries.end());
        }

        const_iterator end() const {
          return const_iterator(entries.end());
        }

        iterator insertEntry(iterator it) {
          xmlNodePtr newNode = xmlNewNode(0, (xmlChar*)"entry");

          if(it == end())
            xmlAddChild(elementNode, newNode);
          else
            xmlAddPrevSibling(it->entryNode, newNode);

          return iterator(entries.insert(it.it, new Entry(newNode)));
        }

        iterator addEntry() {
          return insertEntry(end());
        }

        void removeEntry(iterator it) {
          xmlUnlinkNode(it->entryNode);
          xmlFreeNode(it->entryNode);
          delete *it.it;

          entries.erase(it.it);
        }
    };

    XmlData();
    XmlData(const XmlData &o);
    XmlData(const Net::Packet &packet);
    XmlData(const std::string &filename) throw (Core::Exception);

    XmlData& operator=(const XmlData &o);

    virtual ~XmlData() {
      delete entry;

      xmlFreeDoc(doc);
    }

    Core::String getType() const;
    void setType(const Core::String &type);

    template <typename T>
    bool set(const Core::String &name, T val) {
      return entry->set(name, val);
    }

    template <typename T>
    T get(const Core::String &name) const {
      return entry->get<T>(name);
    }

    List* createList(const Core::String &name, bool unique = false) {
      return entry->createList(name, unique);
    }

    List* getList(const Core::String &name) {
      return entry->getList(name);
    }

    const List* getList(const Core::String &name) const {
      return entry->getList(name);
    }

    void unset(const Core::String &name) {
      entry->unset(name);
    }

    Net::Packet toPacket(boost::uint16_t requestId) const;

    void toFile(const std::string &filename) const throw (Core::Exception);
};

template <> MAD_COMMON_EXPORT Core::String XmlData::Entry::get<Core::String>(const Core::String &name) const;
template <> MAD_COMMON_EXPORT const Core::String& XmlData::Entry::get<const Core::String&>(const Core::String &name) const;

template <> MAD_COMMON_EXPORT std::vector<boost::uint8_t> XmlData::Entry::get<std::vector<unsigned char> >(const Core::String &name) const;
template <> MAD_COMMON_EXPORT const std::vector<boost::uint8_t>& XmlData::Entry::get<const std::vector<unsigned char>&>(const Core::String &name) const;

template <> MAD_COMMON_EXPORT boost::posix_time::ptime XmlData::Entry::get<boost::posix_time::ptime>(const Core::String &name) const;

}
}

#endif /* MAD_COMMON_XMLDATA_H_ */