wfut  0.2.4
A client side C++ implementation of WFUT (WorldForge Update Tool).
FileParser.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 #include "tinyxml/tinyxml.h"
6 
7 #include "libwfut/types.h"
8 #include "libwfut/FileIO.h"
9 #include "libwfut/ChannelFileList.h"
10 #include "libwfut/Encoder.h"
11 
12 namespace WFUT {
13 
14 static int parseFile(TiXmlElement *element, FileObject &file) {
15  assert(element);
16 
17  const char *fname = element->Attribute(TAG_filename.c_str());
18  if (fname != NULL) {
19  file.filename = Encoder::decodeString(fname);
20  }
21  sscanf(element->Attribute(TAG_version.c_str()), "%d", &file.version);
22  sscanf(element->Attribute(TAG_crc32.c_str()), "%lu", &file.crc32);
23  sscanf(element->Attribute(TAG_size.c_str()), "%ld", &file.size);
24  // Check for execute flag
25  const char *exec = element->Attribute(TAG_execute.c_str());
26  if (exec && strlen(exec) >= 4 && strncmp(exec, "true", 4) == 0) file.execute = true;
27  else file.execute = false;
28 
29  // Check for deleted flag
30  const char *deleted = element->Attribute(TAG_deleted.c_str());
31  if (deleted && strlen(deleted) >= 4 && strncmp(deleted, "true", 4) == 0) file.deleted = true;
32  else file.deleted = false;
33 
34  return 0;
35 }
36 
37 static int parseFiles(TiXmlNode *node, ChannelFileList &files) {
38  assert(node);
39 
40  const char *dir = node->ToElement()->Attribute(TAG_dir.c_str());
41  if (dir) {
42  files.setName(dir);
43  }
44 
45  TiXmlElement *e = node->FirstChildElement(TAG_file);
46  while (e) {
47  FileObject file;
48  parseFile(e, file);
49  files.addFile(file);
50  e = e->NextSiblingElement();
51  }
52 
53  return 0;
54 }
55 
56 int parseFileList(const std::string &filename, ChannelFileList &files) {
57 
58  TiXmlDocument doc(filename);
59 
60  if (!doc.LoadFile()) {
61  // Error parsing file
62  return 1;
63  }
64 
65  TiXmlNode *node = doc.FirstChild(TAG_filelist);
66 
67  if (!node) {
68  // missing root node
69  return 1;
70  }
71 
72  return parseFiles(node, files);
73 
74 }
75 int parseFileListXML(const std::string &xml, ChannelFileList &files) {
76 
77  TiXmlDocument doc;
78 
79  doc.Parse(xml.c_str());
80 
81  if (doc.Error()) {
82  // Error parsing file
83  return 1;
84  }
85 
86  TiXmlNode *node = doc.FirstChild(TAG_filelist);
87 
88  if (!node) {
89  // missing root node
90  return 1;
91  }
92 
93  return parseFiles(node, files);
94 
95 }
96 
97 } /* namespaceWFUT */
WFUT::Encoder::decodeString
static std::string decodeString(const std::string &str)
Definition: Encoder.cpp:41