wfut  0.2.4
A client side C++ implementation of WFUT (WorldForge Update Tool).
FileWriter.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 <sstream>
6 
7 #include "tinyxml/tinyxml.h"
8 
9 #include "libwfut/types.h"
10 #include "libwfut/FileIO.h"
11 #include "libwfut/ChannelFileList.h"
12 #include "libwfut/Encoder.h"
13 
14 namespace WFUT {
15 
16 static int writeFile(TiXmlElement *element, const FileObject &file) {
17  assert(element);
18 
19  // TODO need to convert numbers to string as tinyxml doesn't support
20  // unsigned longs
21  std::stringstream ss_crc32, ss_size;
22  ss_crc32 << file.crc32;
23  ss_size << file.size;
24 
25 
26  element->SetAttribute(TAG_filename, Encoder::encodeString(file.filename));
27  element->SetAttribute(TAG_version, file.version);
28  element->SetAttribute(TAG_crc32, ss_crc32.str());
29  element->SetAttribute(TAG_size, ss_size.str());
30  // This also need to be converted to a string
31  element->SetAttribute(TAG_execute, file.execute);
32 
33  return 0;
34 }
35 
36 int writeFileList(const std::string &filename, const ChannelFileList &files) {
37  TiXmlDocument doc;
38  doc.InsertEndChild(TiXmlDeclaration("1.0", "", ""));
39 
40  TiXmlElement flist(TAG_filelist);
41  flist.SetAttribute(TAG_dir, files.getName());
42 
43  const FileMap filemap = files.getFiles();
44  FileMap::const_iterator itr = filemap.begin();
45  while (itr != filemap.end()) {
46  TiXmlElement file(TAG_file);
47  writeFile(&file, itr->second);
48  flist.InsertEndChild(file);
49  ++itr;
50  }
51 
52  doc.InsertEndChild(flist);
53  if (!doc.SaveFile(filename)) {
54  // error writing file
55  return 1;
56  }
57  return 0;
58 }
59 
60 } /* namespace WFUT */
WFUT::Encoder::encodeString
static std::string encodeString(const std::string &str)
Definition: Encoder.cpp:13