summaryrefslogtreecommitdiffstats
path: root/src/Common/XmlPacket.cpp
blob: ff8ca6ac117f694d5779a8fc0b1e53a9bd92e31f (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
/*
 * XmlProcessor.cpp
 *
 * 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/>.
 */

#include "XmlPacket.h"
#include "Base64Encoder.h"
#include <cstdlib>
#include <cstring>

#include <iostream>

namespace Mad {
namespace Common {

void XmlPacket::Element::updateStr() {
  std::string typeStr;

  if(type == NONE) {
    str = "";
  }
  else if(type == BINARY) {
    str = Base64Encoder::encode(boost::get<std::vector<boost::uint8_t> >(value));
    typeStr = "binary";
  }
  else if(type != STRING) {
    std::ostringstream buf;

    switch(type) {
      case INT:
        buf << boost::get<long>(value);
        typeStr = "int";
        break;
      case UINT:
        buf << boost::get<unsigned long>(value);
        typeStr = "uint";
        break;
      case INT64:
        buf << boost::get<long long>(value);
        typeStr = "int64";
        break;
      case UINT64:
        buf << boost::get<unsigned long long>(value);
        typeStr = "uint64";
        break;
      case FLOAT:
        buf << boost::get<float>(value);
        typeStr = "float";
        break;
      case DOUBLE:
        buf << boost::get<double>(value);
        typeStr = "double";
        break;
      case LONGDOUBLE:
        buf << boost::get<long double>(value);
        typeStr = "longdouble";
      default:
        break;
    }

    str = buf.str();
  }
  else
    typeStr = "string";

  xmlNodePtr newNode = xmlNewText((xmlChar*)str.c_str());
  xmlNodePtr oldNode = elementNode->children;

  xmlReplaceNode(oldNode, newNode);
  xmlFreeNode(oldNode);

  xmlSetProp(elementNode, (xmlChar*)"type", (xmlChar*)typeStr.c_str());
}

XmlPacket::Element::Element(xmlNodePtr node) : elementNode(node), type(NONE) {
  if(!node)
    return;

  xmlChar *typestr = xmlGetProp(node, (xmlChar*)"type");
  if(!typestr)
    return;

  xmlChar *content = xmlNodeGetContent(node->children);
  if(!content) {
    xmlFree(typestr);
    return;
  }

  str = (char*)content;
  std::istringstream buf(str);

  if(!xmlStrcmp(typestr, (xmlChar*)"binary")) {
    type = BINARY;

    value = Base64Encoder::decode(str);
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"int")) {
    type = INT;

    long tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"uint")) {
    type = UINT;

    unsigned long tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"int64")) {
    type = INT64;
    
    long long tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"uint64")) {
    type = UINT64;

    unsigned long long tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"float")) {
    type = FLOAT;

    float tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"double")) {
    type = DOUBLE;
    
    double tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"longdouble")) {
    type = LONGDOUBLE;
    
    long double tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"string")) {
    type = STRING;
  }

  xmlFree(typestr);
  xmlFree(content);

  if(type != NONE)
    updateStr();
}

XmlPacket::Entry::Entry(xmlNodePtr node) : entryNode(node) {
  if(!node)
    return;

  for(xmlNodePtr element = node->children; element != 0; element = element->next) {
    if(element->type != XML_ELEMENT_NODE)
      continue;

    xmlChar *name = xmlGetProp(element, (xmlChar*)"name");

    if(!xmlStrcmp(element->name, (xmlChar*)"list"))
      lists.insert(std::make_pair(std::string((char*)name), new List(element)));
    else if(!xmlStrcmp(element->name, (xmlChar*)"value"))
      elements.insert(std::make_pair(std::string((char*)name), new Element(element)));

    xmlFree(name);
  }
}



template <>
std::string XmlPacket::Entry::get<std::string>(const std::string &name) const {
  Element *element = getElement(name);
  if(!element)
    return std::string();

  return element->str;
}

template <>
const std::string& XmlPacket::Entry::get<const std::string&>(const std::string &name) const {
  static std::string empty;

  Element *element = getElement(name);
  if(!element)
    return empty;

  return element->str;
}

template <>
std::vector<boost::uint8_t> XmlPacket::Entry::get<std::vector<unsigned char> >(const std::string &name) const {
  Element *element = getElement(name);
  if(!element)
    return std::vector<boost::uint8_t>();

    switch(element->type) {
      case Element::BINARY:
        return boost::get<std::vector<boost::uint8_t> >(element->value);
      default:
        return std::vector<boost::uint8_t>();
    }
}

template <>
const std::vector<boost::uint8_t>& XmlPacket::Entry::get<const std::vector<unsigned char>&>(const std::string &name) const {
  static std::vector<boost::uint8_t> empty;

  Element *element = getElement(name);
  if(!element)
    return empty;

    switch(element->type) {
      case Element::BINARY:
        return boost::get<std::vector<boost::uint8_t> >(element->value);
      default:
        return empty;
    }
}

XmlPacket::List::List(xmlNodePtr node) : elementNode(node) {
  for(xmlNodePtr entry = node->children; entry != 0; entry = entry->next) {
    if(entry->type != XML_ELEMENT_NODE || xmlStrcmp(entry->name, (xmlChar*)"entry"))
      continue;

    entries.push_back(new Entry(entry));
  }
}

XmlPacket::XmlPacket() {
  doc = xmlNewDoc((xmlChar*)"1.0");

  packetNode = xmlNewNode(0, (xmlChar*)"packet");
  xmlDocSetRootElement(doc, packetNode);

  entry = new Entry(packetNode);
}

XmlPacket::XmlPacket(const XmlPacket &o) {
  doc = xmlCopyDoc(o.doc, 1);
  packetNode = xmlDocGetRootElement(doc);
  entry = new Entry(packetNode);
}

XmlPacket::XmlPacket(const Net::Packet &packet) {
  doc = xmlParseMemory((const char*)packet.getData(), packet.getLength());
  packetNode = xmlDocGetRootElement(doc);
  entry = new Entry(packetNode);
}

XmlPacket& XmlPacket::operator=(const XmlPacket &o) {
  delete entry;
  xmlFreeDoc(doc);

  doc = xmlCopyDoc(o.doc, 1);
  packetNode = xmlDocGetRootElement(doc);
  entry = new Entry(packetNode);

  return *this;
}

std::string XmlPacket::getType() const {
  xmlChar *type = xmlGetProp(packetNode, (xmlChar*)"type");

  std::string ret(type ? (char*)type : "");

  xmlFree(type);

  return ret;
}

void XmlPacket::setType(const std::string &type) {
  xmlSetProp(packetNode, (xmlChar*)"type", (xmlChar*)type.c_str());
}

Net::Packet XmlPacket::encode(boost::uint16_t requestId) const {
  xmlChar *buf;
  int length;

  xmlDocDumpMemory(doc, &buf, &length);

  Net::Packet packet(requestId, buf, length);

  xmlFree(buf);

  return packet;
}

}
}