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 = nullptr;
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  return IniVariable();
62 
63  if (value(name).isNull())
64  setValue(name, data);
65 
66  return retrieveSetting(name);
67 }
68 
69 void IniSection::deleteSetting(const QString &name)
70 {
71  assert(!isNull());
72  if (name.isEmpty())
73  return;
74 
75  remove(name);
76 }
77 
78 bool IniSection::hasSetting(const QString &name) const
79 {
80  assert(!isNull());
81  return d->pIni->hasSetting(this->name(), name);
82 }
83 
84 bool IniSection::isNull() const
85 {
86  return d->pIni == nullptr;
87 }
88 
89 const QString &IniSection::name() const
90 {
91  return d->name;
92 }
93 
94 IniVariable IniSection::operator[](const QString &name)
95 {
96  return setting(name);
97 }
98 
99 const IniVariable IniSection::operator[](const QString &name) const
100 {
101  return retrieveSetting(name);
102 }
103 
104 void IniSection::remove(const QString &key)
105 {
106  d->pIni->removeKey(name() + "/" + key);
107 }
108 
110 {
111  assert(!isNull());
112  if (name.isEmpty())
113  return IniVariable();
114 
115  return IniVariable(*this, name);
116 }
117 
118 const IniVariable IniSection::retrieveSetting(const QString &name) const
119 {
120  assert(!isNull());
121  if (name.isEmpty())
122  return IniVariable();
123 
124  return IniVariable(*this, name);
125 }
126 
127 IniVariable IniSection::setting(const QString &name)
128 {
129  assert(!isNull());
130  if (name.isEmpty())
131  return IniVariable();
132 
133  IniVariable pVariable = retrieveSetting(name);
134  if (pVariable.isNull())
135  return createSetting(name, QVariant());
136 
137  return pVariable;
138 }
139 
140 void IniSection::setValue(const QString &key, const QVariant &value)
141 {
142  assert(!isNull());
143 
144  if (!isNull())
145  d->pIni->setValue(name() + "/" + key, value);
146 }
147 
148 QVariant IniSection::value(const QString &key) const
149 {
150  if (!isNull())
151  return d->pIni->value(name() + "/" + key);
152 
153  return QVariant();
154 }
155 
156 QVariant IniSection::value(const QString &key, QVariant defaultValue) const
157 {
158  QVariant val = value(key);
159  if (!val.isValid())
160  return defaultValue;
161  return val;
162 }