inisection.cpp
1 //------------------------------------------------------------------------------
2 // inisection.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "inisection.h"
24 
25 #include "ini.h"
26 #include <cassert>
27 #include <QDebug>
28 
29 DClass<IniSection>
30 {
31  public:
32  QString name;
33 
37  Ini* pIni;
38 };
39 
40 DPointered(IniSection)
41 
43 {
44  d->pIni = NULL;
45 }
46 
47 IniSection::IniSection(Ini* pIni, const QString& sectionName)
48 {
49  d->pIni = pIni;
50  d->name = sectionName;
51 }
52 
53 IniSection::~IniSection()
54 {
55 }
56 
57 IniVariable IniSection::createSetting(const QString& name, const QVariant& data)
58 {
59  assert(!isNull());
60  if (name.isEmpty())
61  {
62  return IniVariable();
63  }
64 
65  if (value(name).isNull())
66  {
67  setValue(name, data);
68  }
69 
70  return retrieveSetting(name);
71 }
72 
73 void IniSection::deleteSetting(const QString& name)
74 {
75  assert(!isNull());
76  if (name.isEmpty())
77  {
78  return;
79  }
80 
81  remove(name);
82 }
83 
84 bool IniSection::hasSetting(const QString &name) const
85 {
86  assert(!isNull());
87  return d->pIni->hasSetting(this->name(), name);
88 }
89 
90 bool IniSection::isNull() const
91 {
92  return d->pIni == NULL;
93 }
94 
95 const QString &IniSection::name() const
96 {
97  return d->name;
98 }
99 
101 {
102  return setting(name);
103 }
104 
105 const IniVariable IniSection::operator[](const QString& name) const
106 {
107  return retrieveSetting(name);
108 }
109 
110 void IniSection::remove(const QString& key)
111 {
112  d->pIni->removeKey(name() + "/" + key);
113 }
114 
116 {
117  assert(!isNull());
118  if (name.isEmpty())
119  {
120  return IniVariable();
121  }
122 
123  return IniVariable(*this, name);
124 }
125 
126 const IniVariable IniSection::retrieveSetting(const QString& name) const
127 {
128  assert(!isNull());
129  if (name.isEmpty())
130  {
131  return IniVariable();
132  }
133 
134  return IniVariable(*this, name);
135 }
136 
138 {
139  assert(!isNull());
140  if (name.isEmpty())
141  {
142  return IniVariable();
143  }
144 
145  IniVariable pVariable = retrieveSetting(name);
146  if (pVariable.isNull())
147  {
148  return createSetting(name, QVariant());
149  }
150 
151  return pVariable;
152 }
153 
154 void IniSection::setValue(const QString& key, const QVariant& value)
155 {
156  assert(!isNull());
157 
158  if (!isNull())
159  {
160  d->pIni->setValue(name() + "/" + key, value);
161  }
162 }
163 
164 QVariant IniSection::value(const QString& key) const
165 {
166  if (!isNull())
167  {
168  return d->pIni->value(name() + "/" + key);
169  }
170 
171  return QVariant();
172 }
173 
174 QVariant IniSection::value(const QString& key, QVariant defaultValue) const
175 {
176  QVariant val = value(key);
177  if (!val.isValid())
178  {
179  return defaultValue;
180  }
181  return val;
182 }
IniVariable createSetting(const QString &name, const QVariant &data)
Inits specified variable with specified data.
Definition: inisection.cpp:57
INI variable representation.
Definition: inivariable.h:41
void deleteSetting(const QString &name)
Deletes specified variable.
Definition: inisection.cpp:73
const QString & name() const
A name (or path) of this section with lettercase preserved.
Definition: inisection.cpp:95
IniSection()
Creates an invalid IniSection object. Such object should not be used for read/write operations...
IniVariable operator[](const QString &name)
Calls setting().
Definition: inisection.cpp:100
QVariant value(const QString &key) const
Retrieves a variable directly; omits the IniVariable system.
Definition: inisection.cpp:164
bool hasSetting(const QString &name) const
true if setting of given name exists within the section.
Definition: inisection.cpp:84
IniVariable retrieveSetting(const QString &name)
Gets a variable but only if it already exists.
Definition: inisection.cpp:115
Configuration handler.
Definition: ini.h:69
IniVariable setting(const QString &name)
Gets a variable. Creates it first if it doesn&#39;t exist yet.
Definition: inisection.cpp:137
void setValue(const QString &key, const QVariant &value)
Sets a variable directly. Omits the IniVariable system.
Definition: inisection.cpp:154
INI section representation.
Definition: inisection.h:40
bool isNull() const
If true, IniSection object is not valid and should not be used to perform any actions on the Ini file...
bool isNull() const
If true, IniSection object is not valid and should not be used to perform any actions on the Ini file...
Definition: inisection.cpp:90