wfut  0.2.4
A client side C++ implementation of WFUT (WorldForge Update Tool).
MirrorParser.cpp
1 // This file may be redistributed and modified only under the terms of
2 // the GNU Lesser General Public License (See COPYING for details).
3 // Copyright (C) 2005 - 2007 Simon Goodall
4 
5 
6 #include "tinyxml/tinyxml.h"
7 
8 #include "libwfut/types.h"
9 #include "libwfut/ChannelIO.h"
10 
11 namespace WFUT {
12 
13 static int parseMirror(TiXmlElement *element, MirrorObject &mirror) {
14  assert(element);
15  TiXmlNode * node;
16 
17  node = element->FirstChildElement(TAG_name);
18  if (node) node = node->FirstChild();
19  if (node) mirror.name = node->Value();
20 
21  node = element->FirstChildElement(TAG_url);
22  if (node) node = node->FirstChild();
23  if (node) mirror.url = node->Value();
24 
25  return 0;
26 }
27 
28 static int parseMirrors(TiXmlNode *element, MirrorList &mirrors) {
29  assert(element);
30 
31  TiXmlElement *e = element->FirstChildElement(TAG_mirror);
32  while (e) {
33  MirrorObject mirror;
34  parseMirror(e, mirror);
35  mirrors.push_back(mirror);
36  e = e->NextSiblingElement();
37  }
38  return 0;
39 }
40 int parseMirrorList(const std::string &filename, MirrorList &mirrors) {
41 
42  TiXmlDocument doc(filename);
43 
44  if (!doc.LoadFile()) {
45  return 1;
46  }
47 
48  TiXmlNode *node = doc.FirstChild(TAG_mirrorlist);
49 
50  if (!node) {
51  // missing root node
52  return 1;
53  }
54 
55  return parseMirrors(node, mirrors);
56 
57 }
58 
59 int parseMirrorListXML(const std::string &xml, MirrorList &mirrors) {
60 
61  TiXmlDocument doc;
62 
63  doc.Parse(xml.c_str());
64 
65  if (doc.Error()) {
66  return 1;
67  }
68 
69  TiXmlNode *node = doc.FirstChild(TAG_mirrorlist);
70 
71  if (!node) {
72  // missing root node
73  return 1;
74  }
75 
76  return parseMirrors(node, mirrors);
77 
78 }
79 
80 } /* namespace WFUT */