ini.cpp
1 //------------------------------------------------------------------------------
2 // ini.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 "ini.h"
24 
25 #include "ini/settingsprovider.h"
26 #include "ini/settingsproviderqt.h"
27 #include "log.h"
28 #include "strings.h"
29 
30 #include <cassert>
31 
32 DClass<Ini>
33 {
34  public:
35  SettingsProvider* provider;
36 };
37 
38 DPointered(Ini)
39 
40 Ini::Ini(SettingsProvider* provider)
41 {
42  d->provider = provider;
43 }
44 
45 Ini::~Ini()
46 {
47 }
48 
49 IniVariable Ini::createSetting(const QString& sectionName, const QString& name, const QVariant& data)
50 {
51  IniSection s = section(sectionName);
52  if (s.isNull())
53  {
54  return IniVariable();
55  }
56 
57  return s.createSetting(name, data);
58 }
59 
60 void Ini::deleteSection(const QString& sectionName)
61 {
62  removeKey(sectionName);
63 }
64 
65 void Ini::deleteSetting(const QString& sectionName, const QString& settingName)
66 {
67  removeKey(sectionName + "/" + settingName);
68 }
69 
70 bool Ini::hasSetting(const QString& sectionname, const QString& settingname) const
71 {
72  return d->provider->hasKey(sectionname + "/" + settingname);
73 }
74 
75 void Ini::removeKey(const QString& key)
76 {
77  d->provider->remove(key);
78 }
79 
80 IniVariable Ini::retrieveSetting(const QString& sectionName, const QString& variableName)
81 {
82  IniSection section = this->section(sectionName);
83  if (section.isNull())
84  {
85  return IniVariable();
86  }
87 
88  return section.retrieveSetting(variableName);
89 }
90 
91 IniSection Ini::section(const QString& name)
92 {
93  if (name.isEmpty())
94  {
95  return IniSection();
96  }
97 
98  return IniSection(this, name);
99 }
100 
101 QVector<IniSection> Ini::sectionsArray(const QString& regexPattern)
102 {
103  QVector<IniSection> sectionsReferencesArray;
104 
105  QRegExp regExp(regexPattern, Qt::CaseInsensitive);
106 
107  QStringList groups = d->provider->allSections();
108 
109  foreach (const QString& key, groups)
110  {
111  if (key.contains(regExp))
112  {
113  sectionsReferencesArray << IniSection(this, key);
114  }
115  }
116 
117  return sectionsReferencesArray;
118 }
119 
120 IniVariable Ini::setting(const QString& sectionName, const QString& variableName)
121 {
122  if (sectionName.isEmpty() || variableName.isEmpty())
123  {
124  return IniVariable();
125  }
126 
127  IniVariable var = retrieveSetting(sectionName, variableName);
128  if (var.isNull())
129  {
130  return createSetting(sectionName, variableName, QVariant());
131  }
132 
133  return var;
134 }
135 
136 void Ini::setValue(const QString& key, const QVariant& value)
137 {
138  assert(d->provider != NULL);
139 
140  d->provider->setValue(key, value);
141 }
142 
143 QVariant Ini::value(const QString& key) const
144 {
145  assert(d->provider != NULL);
146 
147  return d->provider->value(key);
148 }
IniVariable createSetting(const QString &name, const QVariant &data)
Inits specified variable with specified data.
Definition: inisection.cpp:57
IniVariable createSetting(const QString &sectionname, const QString &name, const QVariant &data)
Definition: ini.cpp:49
INI variable representation.
Definition: inivariable.h:41
bool hasSetting(const QString &sectionname, const QString &settingname) const
true if setting of given name exists within given section.
Definition: ini.cpp:70
QVector< IniSection > sectionsArray(const QString &regexPattern)
Definition: ini.cpp:101
IniVariable setting(const QString &sectionname, const QString &variablename)
Definition: ini.cpp:120
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 retrieveSetting(const QString &sectionname, const QString &variablename)
Definition: ini.cpp:80
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...
IniSection section(const QString &name)
Access configuration file section.
Definition: ini.cpp:91
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
void deleteSection(const QString &sectionname)
Definition: ini.cpp:60
void deleteSetting(const QString &sectionname, const QString &settingname)
Definition: ini.cpp:65