summaryrefslogtreecommitdiff
path: root/RealtimeSync/xml_proc.cpp
blob: ac6c0eb9dac7332e6de401dfd0a914f1de506c43 (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
// **************************************************************************
// * 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) ZenJu (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#include "xml_proc.h"
#include <wx/filefn.h>
#include <zen/file_handling.h>
#include <wx+/string_conv.h>

using namespace zen;
using namespace xmlAccess;


namespace
{
void readConfig(const XmlIn& in, XmlRealConfig& config)
{
    in["Directories"](config.directories);
    in["Commandline"](config.commandline);
    in["Delay"      ](config.delay);
}


bool isXmlTypeRTS(const XmlDoc& doc) //throw()
{
    if (doc.root().getNameAs<std::string>() == "FreeFileSync")
    {
        std::string type;
        if (doc.root().getAttribute("XmlType", type))
            return type == "REAL";
    }
    return false;
}
}


void xmlAccess::readRealConfig(const Zstring& filename, XmlRealConfig& config)
{
    XmlDoc doc;
    loadXmlDocument(filename, doc); //throw FfsXmlError

    if (!isXmlTypeRTS(doc))
        throw FfsXmlError(replaceCpy(_("File %x does not contain a valid configuration."), L"%x", fmtFileName(filename)));

    XmlIn in(doc);
    ::readConfig(in, config);

    if (in.errorsOccured())
        throw FfsXmlError(replaceCpy(_("Configuration file %x loaded partially only."), L"%x", fmtFileName(filename)) + L"\n\n" +
                          getErrorMessageFormatted(in), FfsXmlError::WARNING);
}


namespace
{
void writeConfig(const XmlRealConfig& config, XmlOut& out)
{
    out["Directories"](config.directories);
    out["Commandline"](config.commandline);
    out["Delay"      ](config.delay);
}
}


void xmlAccess::writeRealConfig(const XmlRealConfig& config, const Zstring& filename)
{
    XmlDoc doc("FreeFileSync");
    doc.root().setAttribute("XmlType", "REAL");

    XmlOut out(doc);
    writeConfig(config, out);

    saveXmlDocument(doc, filename); //throw (FfsXmlError)
}
bgstack15