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
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) *
// **************************************************************************
//
#include "xmlBase.h"
#include "globalFunctions.h"
#include <wx/intl.h>
#include <wx/ffile.h>
#include "fileIO.h"
#include "stringConv.h"
#include "systemConstants.h"
#include <boost/scoped_array.hpp>
#include <wx/log.h>
using namespace FreeFileSync;
std::string getTypeName(xmlAccess::XmlType type)
{
switch (type)
{
case xmlAccess::XML_GUI_CONFIG:
return "GUI";
case xmlAccess::XML_BATCH_CONFIG:
return "BATCH";
case xmlAccess::XML_GLOBAL_SETTINGS:
return "GLOBAL";
case xmlAccess::XML_REAL_CONFIG:
return "REAL";
case xmlAccess::XML_OTHER:
break;
}
assert(false);
return "OTHER";
}
xmlAccess::XmlType xmlAccess::getXmlType(const wxString& filename) //throw()
{
#ifndef __WXDEBUG__
wxLogNull noWxLogs; //hide wxWidgets log messages in release build
#endif
if (!wxFileExists(filename))
return XML_OTHER;
//workaround to get a FILE* from a unicode filename
wxFFile configFile(filename, wxT("rb"));
if (!configFile.IsOpened())
return XML_OTHER;
FILE* inputFile = configFile.fp();
TiXmlDocument doc;
try
{
if (!doc.LoadFile(inputFile)) //fails if inputFile is no proper XML
return XML_OTHER;
}
catch (const std::exception&)
{
//unfortunately TiXml isn't very smart and tries to allocate space for the complete file: length_error exception is thrown for large files!
return XML_OTHER;
}
TiXmlElement* root = doc.RootElement();
if (!root || (root->ValueStr() != std::string("FreeFileSync"))) //check for FFS configuration xml
return XML_OTHER;
const char* cfgType = root->Attribute("XmlType");
if (!cfgType)
return XML_OTHER;
const std::string type(cfgType);
if (type == getTypeName(XML_GUI_CONFIG))
return XML_GUI_CONFIG;
else if (type == getTypeName(XML_BATCH_CONFIG))
return XML_BATCH_CONFIG;
else if (type == getTypeName(XML_GLOBAL_SETTINGS))
return XML_GLOBAL_SETTINGS;
else if (type == getTypeName(XML_REAL_CONFIG))
return XML_REAL_CONFIG;
return XML_OTHER;
}
namespace
{
//convert (0xD, 0xA) and (0xD) to 0xA: just like in TiXmlDocument::LoadFile(); not sure if actually needed
void normalize(std::vector<char>& stream)
{
std::vector<char> tmp;
for (std::vector<char>::const_iterator i = stream.begin(); i != stream.end(); ++i)
switch (*i)
{
case 0xD:
tmp.push_back(0xA);
if (i + 1 != stream.end() && *(i + 1) == 0xA)
++i;
break;
default:
tmp.push_back(*i);
}
stream.swap(tmp);
}
}
void xmlAccess::loadXmlDocument(const wxString& filename, const xmlAccess::XmlType type, TiXmlDocument& document) //throw FileError()
{
std::vector<char> inputStream;
try
{
const size_t BUFFER_SIZE = 512 * 1024;
static boost::scoped_array<unsigned char> buffer(new unsigned char[BUFFER_SIZE]);
FileInput inputFile(wxToZ(filename)); //throw FileError();
do
{
const size_t bytesRead = inputFile.read(buffer.get(), BUFFER_SIZE); //throw FileError()
inputStream.insert(inputStream.end(), buffer.get(), buffer.get() + bytesRead);
}
while (!inputFile.eof());
}
catch (const FileError& error) //more detailed error messages than with wxWidgets
{
throw XmlError(error.show());
}
if (!inputStream.empty())
{
inputStream.push_back(0);
//convert (0xD, 0xA) and (0xD) to (0xA): just like in TiXmlDocument::LoadFile(); not sure if actually needed
::normalize(inputStream);
TiXmlBase::SetCondenseWhiteSpace(false); //do not condense whitespace characters
document.Parse(&inputStream[0], 0, TIXML_DEFAULT_ENCODING);
TiXmlElement* root = document.RootElement();
if (root && (root->ValueStr() == std::string("FreeFileSync"))) //check for FFS configuration xml
{
const char* cfgType = root->Attribute("XmlType");
if (cfgType && std::string(cfgType) == getTypeName(type))
return; //finally... success!
}
}
throw XmlError(wxString(_("Error parsing configuration file:")) + wxT("\n\"") + filename + wxT("\""));
}
void xmlAccess::getDefaultXmlDocument(const XmlType type, TiXmlDocument& document)
{
TiXmlBase::SetCondenseWhiteSpace(false); //do not condense whitespace characters
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", ""); //delete won't be necessary later; ownership passed to TiXmlDocument!
document.LinkEndChild(decl);
TiXmlElement* root = new TiXmlElement("FreeFileSync");
root->SetAttribute("XmlType", getTypeName(type)); //xml configuration type
document.LinkEndChild(root);
}
void xmlAccess::saveXmlDocument(const wxString& filename, const TiXmlDocument& document) //throw (XmlError)
{
//convert XML into continuous byte sequence
TiXmlPrinter printer;
printer.SetLineBreak(wxString(globalFunctions::LINE_BREAK).ToUTF8());
document.Accept(&printer);
const std::string buffer = printer.Str();
try
{
FileOutput outputFile(wxToZ(filename)); //throw FileError()
outputFile.write(buffer.c_str(), buffer.length()); //
}
catch (const FileError& error) //more detailed error messages than with wxWidgets
{
throw XmlError(error.show());
}
}
//################################################################################################################
bool xmlAccess::readXmlElement(const std::string& name, const TiXmlElement* parent, std::string& output)
{
if (parent)
{
const TiXmlElement* child = parent->FirstChildElement(name);
if (child)
{
const char* text = child->GetText();
if (text) //may be NULL!!
output = text;
else //NULL means empty string
output.clear();
return true;
}
}
return false;
}
bool xmlAccess::readXmlElement(const std::string& name, const TiXmlElement* parent, wxString& output)
{
std::string tempString;
if (!readXmlElement(name, parent, tempString))
return false;
output = wxString::FromUTF8(tempString.c_str());
return true;
}
bool xmlAccess::readXmlElement(const std::string& name, const TiXmlElement* parent, int& output)
{
std::string temp;
if (!readXmlElement(name, parent, temp))
return false;
output = globalFunctions::stringToInt(temp);
return true;
}
bool xmlAccess::readXmlElement(const std::string& name, const TiXmlElement* parent, unsigned int& output)
{
int dummy = 0;
if (!xmlAccess::readXmlElement(name, parent, dummy))
return false;
output = static_cast<unsigned int>(dummy);
return true;
}
bool xmlAccess::readXmlElement(const std::string& name, const TiXmlElement* parent, long& output)
{
std::string temp;
if (readXmlElement(name, parent, temp))
{
output = globalFunctions::stringToLong(temp);
return true;
}
else
return false;
}
bool xmlAccess::readXmlElement(const std::string& name, const TiXmlElement* parent, bool& output)
{
std::string dummy;
if (readXmlElement(name, parent, dummy))
{
output = dummy == "true";
return true;
}
else
return false;
}
bool xmlAccess::readXmlElement(const std::string& name, const TiXmlElement* parent, std::vector<wxString>& output)
{
if (parent)
{
output.clear();
//load elements
const TiXmlElement* element = parent->FirstChildElement(name);
while (element)
{
const char* text = element->GetText();
if (text) //may be NULL!!
output.push_back(wxString::FromUTF8(text));
else
output.push_back(wxEmptyString);
element = element->NextSiblingElement();
}
return true;
}
return false;
}
bool xmlAccess::readXmlAttribute(const std::string& name, const TiXmlElement* node, std::string& output)
{
if (node)
{
const std::string* attrib = node->Attribute(name);
if (attrib) //may be NULL!
{
output = *attrib;
return true;
}
}
return false;
}
bool xmlAccess::readXmlAttribute(const std::string& name, const TiXmlElement* node, wxString& output)
{
std::string tempString;
if (!readXmlAttribute(name, node, tempString))
return false;
output = wxString::FromUTF8(tempString.c_str());
return true;
}
bool xmlAccess::readXmlAttribute(const std::string& name, const TiXmlElement* node, int& output)
{
std::string dummy;
if (readXmlAttribute(name, node, dummy))
{
output = globalFunctions::stringToInt(dummy);
return true;
}
else
return false;
}
bool xmlAccess::readXmlAttribute(const std::string& name, const TiXmlElement* node, unsigned int& output)
{
std::string dummy;
if (readXmlAttribute(name, node, dummy))
{
output = globalFunctions::stringToInt(dummy);
return true;
}
else
return false;
}
bool xmlAccess::readXmlAttribute(const std::string& name, const TiXmlElement* node, bool& output)
{
std::string dummy;
if (readXmlAttribute(name, node, dummy))
{
output = dummy == "true";
return true;
}
else
return false;
}
//################################################################################################################
void xmlAccess::addXmlElement(const std::string& name, const std::string& value, TiXmlElement* parent)
{
if (parent)
{
TiXmlElement* subElement = new TiXmlElement(name);
parent->LinkEndChild(subElement);
subElement->LinkEndChild(new TiXmlText(value));
}
}
void xmlAccess::addXmlElement(const std::string& name, const wxString& value, TiXmlElement* parent)
{
addXmlElement(name, std::string(value.ToUTF8()), parent);
}
void xmlAccess::addXmlElement(const std::string& name, const int value, TiXmlElement* parent)
{
addXmlElement(name, globalFunctions::numberToString(value), parent);
}
void xmlAccess::addXmlElement(const std::string& name, const unsigned int value, TiXmlElement* parent)
{
addXmlElement(name, static_cast<int>(value), parent);
}
void xmlAccess::addXmlElement(const std::string& name, const long value, TiXmlElement* parent)
{
addXmlElement(name, globalFunctions::numberToString(value), parent);
}
void xmlAccess::addXmlElement(const std::string& name, const bool value, TiXmlElement* parent)
{
if (value)
addXmlElement(name, std::string("true"), parent);
else
addXmlElement(name, std::string("false"), parent);
}
void xmlAccess::addXmlElement(const std::string& name, const std::vector<wxString>& value, TiXmlElement* parent)
{
for (std::vector<wxString>::const_iterator i = value.begin(); i != value.end(); ++i)
addXmlElement(name, std::string(i->ToUTF8()), parent);
}
void xmlAccess::addXmlAttribute(const std::string& name, const std::string& value, TiXmlElement* node)
{
if (node)
node->SetAttribute(name, value);
}
void xmlAccess::addXmlAttribute(const std::string& name, const wxString& value, TiXmlElement* node)
{
addXmlAttribute(name, std::string(value.ToUTF8()), node);
}
void xmlAccess::addXmlAttribute(const std::string& name, const int value, TiXmlElement* node)
{
addXmlAttribute(name, globalFunctions::numberToString(value), node);
}
void xmlAccess::addXmlAttribute(const std::string& name, const unsigned int value, TiXmlElement* node)
{
addXmlAttribute(name, globalFunctions::numberToString(value), node);
}
void xmlAccess::addXmlAttribute(const std::string& name, const bool value, TiXmlElement* node)
{
if (value)
addXmlAttribute(name, std::string("true"), node);
else
addXmlAttribute(name, std::string("false"), node);
}
using xmlAccess::XmlParser;
void XmlParser::logError(const std::string& nodeName)
{
failedNodes.push_back(wxString::FromUTF8(nodeName.c_str()));
}
bool XmlParser::errorsOccured() const
{
return !failedNodes.empty();
}
const wxString XmlParser::getErrorMessageFormatted() const
{
wxString errorMessage = wxString(_("Could not read values for the following XML nodes:")) + wxT("\n");
for (std::vector<wxString>::const_iterator i = failedNodes.begin(); i != failedNodes.end(); ++i)
errorMessage += wxString(wxT("<")) + *i + wxT(">") + ((i - failedNodes.begin() + 1) % 3 == 0 ? wxT("\n") : wxT(" "));
return errorMessage;
}
|