File: OptionsXML.cc

package info (click to toggle)
camstream 0.27%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,320 kB
  • ctags: 5,393
  • sloc: cpp: 17,031; sh: 8,154; asm: 455; ansic: 440; makefile: 343
file content (140 lines) | stat: -rw-r--r-- 3,253 bytes parent folder | download | duplicates (2)
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
/**
  \class COptionsXML
  \brief Store configuration options in an XML tree.
  \author Nemosoft Unv.

  With this class configuration options for applications can be easily
  stored and retrieved in XML format. Rather than wading through a complex
  XML tree, this class lets you store a simple list of options and
  references their values by their name.

  This class won't do recursive storage or grouping. It's a simple 1-to-1
  list. For 90% of the cases, that is enough.

  The class should be initialized with a list of variable names and their
  defaults using \ref DeclareVariable . Trying to set a variable name
  that isn't in the list will be ignored; getting a variable that isn't
  there will return a null variable.

*/


#include "OptionsXML.h"

COptionsXML::COptionsXML()
{
   m_Variables.setAutoDelete(true);
}

COptionsXML::~COptionsXML()
{
   /* Empty dummy destructor */
}



// private

// protected


/**
  \brief Declare a variable in this class
  \param name The name by which the variable is known
  \param defvalue A default (start) value

  With this function you can declare a variable that can be retrieved
  and stored in an XML subtree.
*/
void COptionsXML::DeclareVariable(const QString &name, const QString &defvalue)
{
   m_Variables.insert(name, new QString(defvalue));
}


// public

QString COptionsXML::Get(const QString &name) const
{
   return *m_Variables[name];
}

int COptionsXML::GetInt(const QString &name) const
{
   return Get(name).toInt();
}

bool COptionsXML::GetBool(const QString &name) const
{
   return (Get(name) == "true");
}

void COptionsXML::Set(const QString &name, const QString &value)
{
   if (m_Variables.find(name) == 0)
     return;
   m_Variables.replace(name, new QString(value)); // autodelete is very handy :)
}

void COptionsXML::Set(const QString &name, int value)
{
   Set(name, QString::number(value));
}

void COptionsXML::Set(const QString &name, bool value)
{
   Set(name, value ? QString("true") : QString("false"));
}

/**
  \brief Parse XML tree

  For every variable declared, retrieve the value from this node
  and store it.
*/
void COptionsXML::SetXML(const QDomNode &node)
{
   QDictIterator<QString> it(m_Variables);
   QString name;
   QDomNode v;

   while (it.current()) {
     name = it.currentKey();
     ++it; // to prevent since replace() below from skipping
     v = node.namedItem(name);
     if (!v.isNull() && v.isElement()) {
       m_Variables.replace(name, new QString(v.toElement().text()));
     }
   }
}

/**
  \brief Return XML tree

  For every variable declared, set its value as a subnode in the XML node.
  In the XML file, it looks like this:
  <name>value</name>

  \note No subnodes are cleared, so existing subnodes that are not
  in our declared variables remain intact.
*/
void COptionsXML::GetXML(QDomNode &node) const
{
   QDictIterator<QString> it(m_Variables);
   QString name;
   QDomNode v, w;

   while (it.current()) {
     name = it.currentKey();
     w = node.ownerDocument().createElement(name);
     w.appendChild(node.ownerDocument().createTextNode(*it.current()));
     v = node.namedItem(name);
     if (v.isNull()) {
       node.appendChild(w);
     }
     else {
       node.replaceChild(w, v);
     }
     ++it;
   }
}