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