summaryrefslogtreecommitdiff
path: root/xml-base.c
blob: 561f21e6cfc9f8a5905192ade02f9337744e0c20 (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
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
/*
 * xml-base.c
 */

#include "xml-base.h"

#include <vdr/tools.h>

#include <unistd.h>

using namespace std;

/* --- cXmlBase ------------------------------------------------------------- */

cXmlBase::cXmlBase(const char *_root_element)
{
    root_element = _root_element;
        document = NULL;
        root = NULL;
}


cXmlBase::~cXmlBase()
{
        if (document) {
                document->SaveFile();
        }

        delete document;
}


bool cXmlBase::load(const string &_path)
{
        if (document)
                return true;

    path = _path;

        dsyslog("[audiorecorder]: loading xml-file (%s) (%s ,%s())",
                path.c_str(), __FILE__, __func__);

        document = new TiXmlDocument(path);

        if (access(path.c_str(), F_OK) == -1) {
                dsyslog("[audiorecorder]: creating empty xml-file (%s) (%s ,"
                        "%s())", path.c_str(), __FILE__, __func__);
                TiXmlElement new_root(root_element);
                document->InsertEndChild(new_root);
                document->SaveFile();
        }

        if (! document->LoadFile()) {
                dsyslog("[audiorecorder]: error while parsing xml-file (%s) "
                        "(%s, %s())", path.c_str(), __FILE__, __func__);
                dsyslog("[audiorecorder]: %s, row: %d, column: %d (%s, %s())",
                        document->ErrorDesc(), document->ErrorRow(),
                        document->ErrorCol(), __FILE__, __func__);
                return false;
        }

        set_root();

        if (! root || root->ValueStr() != root_element)
                return false;

        copy_to_objects();

        return true;
}


void cXmlBase::clear(void)
{
        remove(path.c_str());

        delete document;
        document = new TiXmlDocument(path);

        root = new TiXmlElement(root_element);
}


void cXmlBase::add_subelement(TiXmlElement &main_element, const char *name,
        const string &text)
{
        if (! name || text.empty())
                return;

        TiXmlElement element(name);
        TiXmlText txt(text);
        element.InsertEndChild(txt);

        main_element.InsertEndChild(element);
}


void cXmlBase::set_root(void)
{
        if (document)
                root = document->RootElement();
}