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  return IniVariable();
54 
55  return s.createSetting(name, data);
56 }
57 
58 void Ini::deleteSection(const QString &sectionName)
59 {
60  for (const QString &key : d->provider->allKeys())
61  {
62  if (key.startsWith(sectionName + "/", Qt::CaseInsensitive))
63  removeKey(key);
64  }
65 }
66 
67 void Ini::deleteSetting(const QString &sectionName, const QString &settingName)
68 {
69  removeKey(sectionName + "/" + settingName);
70 }
71 
72 bool Ini::hasSetting(const QString &sectionname, const QString &settingname) const
73 {
74  return d->provider->hasKey(sectionname + "/" + settingname);
75 }
76 
77 void Ini::removeKey(const QString &key)
78 {
79  d->provider->remove(key);
80 }
81 
82 IniVariable Ini::retrieveSetting(const QString &sectionName, const QString &variableName)
83 {
84  IniSection section = this->section(sectionName);
85  if (section.isNull())
86  return IniVariable();
87 
88  return section.retrieveSetting(variableName);
89 }
90 
91 IniSection Ini::section(const QString &name)
92 {
93  if (name.isEmpty())
94  return IniSection();
95 
96  return IniSection(this, name);
97 }
98 
99 QVector<IniSection> Ini::sectionsArray(const QString &regexPattern)
100 {
101  QVector<IniSection> sectionsReferencesArray;
102 
103  QRegExp regExp(regexPattern, Qt::CaseInsensitive);
104 
105  QStringList groups = d->provider->allSections();
106 
107  for (const QString &key : groups)
108  {
109  if (key.contains(regExp))
110  sectionsReferencesArray << IniSection(this, key);
111  }
112 
113  return sectionsReferencesArray;
114 }
115 
116 IniVariable Ini::setting(const QString &sectionName, const QString &variableName)
117 {
118  if (sectionName.isEmpty() || variableName.isEmpty())
119  return IniVariable();
120 
121  IniVariable var = retrieveSetting(sectionName, variableName);
122  if (var.isNull())
123  return createSetting(sectionName, variableName, QVariant());
124 
125  return var;
126 }
127 
128 void Ini::setValue(const QString &key, const QVariant &value)
129 {
130  assert(d->provider != nullptr);
131 
132  d->provider->setValue(key, value);
133 }
134 
135 QVariant Ini::value(const QString &key) const
136 {
137  assert(d->provider != nullptr);
138 
139  return d->provider->value(key);
140 }