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.hpp"
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  foreach (const QString &key, d->provider->allKeys())
63  {
64  if (key.startsWith(sectionName + "/", Qt::CaseInsensitive))
65  removeKey(key);
66  }
67 }
68 
69 void Ini::deleteSetting(const QString& sectionName, const QString& settingName)
70 {
71  removeKey(sectionName + "/" + settingName);
72 }
73 
74 bool Ini::hasSetting(const QString& sectionname, const QString& settingname) const
75 {
76  return d->provider->hasKey(sectionname + "/" + settingname);
77 }
78 
79 void Ini::removeKey(const QString& key)
80 {
81  d->provider->remove(key);
82 }
83 
84 IniVariable Ini::retrieveSetting(const QString& sectionName, const QString& variableName)
85 {
86  IniSection section = this->section(sectionName);
87  if (section.isNull())
88  {
89  return IniVariable();
90  }
91 
92  return section.retrieveSetting(variableName);
93 }
94 
95 IniSection Ini::section(const QString& name)
96 {
97  if (name.isEmpty())
98  {
99  return IniSection();
100  }
101 
102  return IniSection(this, name);
103 }
104 
105 QVector<IniSection> Ini::sectionsArray(const QString& regexPattern)
106 {
107  QVector<IniSection> sectionsReferencesArray;
108 
109  QRegExp regExp(regexPattern, Qt::CaseInsensitive);
110 
111  QStringList groups = d->provider->allSections();
112 
113  foreach (const QString& key, groups)
114  {
115  if (key.contains(regExp))
116  {
117  sectionsReferencesArray << IniSection(this, key);
118  }
119  }
120 
121  return sectionsReferencesArray;
122 }
123 
124 IniVariable Ini::setting(const QString& sectionName, const QString& variableName)
125 {
126  if (sectionName.isEmpty() || variableName.isEmpty())
127  {
128  return IniVariable();
129  }
130 
131  IniVariable var = retrieveSetting(sectionName, variableName);
132  if (var.isNull())
133  {
134  return createSetting(sectionName, variableName, QVariant());
135  }
136 
137  return var;
138 }
139 
140 void Ini::setValue(const QString& key, const QVariant& value)
141 {
142  assert(d->provider != NULL);
143 
144  d->provider->setValue(key, value);
145 }
146 
147 QVariant Ini::value(const QString& key) const
148 {
149  assert(d->provider != NULL);
150 
151  return d->provider->value(key);
152 }
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:74
QVector< IniSection > sectionsArray(const QString &regexPattern)
Definition: ini.cpp:105
IniVariable setting(const QString &sectionname, const QString &variablename)
Definition: ini.cpp:124
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:84
INI section representation.
Definition: inisection.h:40
bool isNull() const
If true, IniVariable object is not valid and should not be used to perform any actions on the Ini fil...
IniSection section(const QString &name)
Access configuration file section.
Definition: ini.cpp:95
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:69