summaryrefslogtreecommitdiffstats
path: root/general/Ini.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'general/Ini.cpp')
-rw-r--r--general/Ini.cpp139
1 files changed, 68 insertions, 71 deletions
diff --git a/general/Ini.cpp b/general/Ini.cpp
index 269a832d..859dc59b 100644
--- a/general/Ini.cpp
+++ b/general/Ini.cpp
@@ -1,45 +1,42 @@
-/////////////////////////////////////////////////////////////////////////////
-// Name: general/Ini.cpp
-// Purpose: Define sections for data files
-// Author: Ben Boeckel
-// Modified by: Ben Boeckel
-// Created: Fri May 4 23:27:37 2007
-// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions
-// Licence:
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License along
-// with this program. If not, see <http://www.gnu.org/licenses/>.
-/////////////////////////////////////////////////////////////////////////////
-
+/*
+ * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Qt includes
#include <QDir>
#include <QStringList>
#include <QTextStream>
+// Header include
#include "Ini.h"
Ini::Ini()
{
}
-Ini::Ini(const QString& fname) throw(Exception)
+Ini::Ini(const QString& fileName) throw(Exception)
{
- load(fname);
+ load(fileName);
}
-void Ini::load(const QString& fname) throw(Exception)
+void Ini::load(const QString& fileName) throw(Exception)
{
- QFile fin(fname);
+ QFile fin(fileName);
if (!fin.exists())
- throw(OpenException("Ini", fname));
+ throw(OpenException("Ini", fileName));
load(fin);
fin.close();
}
@@ -51,30 +48,30 @@ void Ini::load(QFile& fin) throw(InvalidException)
QString field;
QString value;
int pos;
- fields.clear();
+ m_fields.clear();
while (!file.atEnd())
{
pos = line.indexOf('=');
- if (pos == -1)
+ if (pos == INT_MAX)
throw(InvalidException("Ini", fin.fileName()));
field = line.mid(0, pos - 1);
value = line.mid(pos + 1, line.length() - pos);
if (field.isEmpty())
throw(InvalidException("Ini", fin.fileName()));
- fields[field] = value;
+ m_fields[field] = value;
line = file.readLine();
}
}
-void Ini::save(const QString& fname) const throw(Exception)
+void Ini::save(const QString& fileName) const throw(Exception)
{
- QStringList path = fname.split(QDir::separator(), QString::SkipEmptyParts);
+ QStringList path = fileName.split(QDir::separator(), QString::SkipEmptyParts);
path.removeLast();
if (!QDir().mkpath(path.join("/")))
throw(DirException("Ini", path.join("/")));
- QFile fout(fname);
+ QFile fout(fileName);
if (!fout.open(QIODevice::WriteOnly))
- throw(OpenException("Ini", fname));
+ throw(OpenException("Ini", fileName));
save(fout);
fout.close();
}
@@ -82,96 +79,96 @@ void Ini::save(const QString& fname) const throw(Exception)
void Ini::save(QFile& file) const
{
QTextStream fout(&file);
- for (QMapIterator<QString, QString> i(fields); i.hasNext(); i.next())
- fout << i.key() << '=' << i.value() << '\n';
+ foreach (QString field, m_fields)
+ fout << field << '=' << m_fields[field] << '\n';
fout << '\n';
}
-void Ini::addField(const QString& n, const bool v)
+void Ini::addField(const QString& field, const bool value)
{
- fields[n] = v ? "true" : "false";
+ m_fields[field] = value ? "true" : "false";
}
-void Ini::addField(const QString& n, const unsigned char v)
+void Ini::addField(const QString& field, const unsigned char value)
{
- fields[n] = QString::number(v);
+ m_fields[field] = QString::number(value);
}
-void Ini::addField(const QString& n, const int v)
+void Ini::addField(const QString& field, const int value)
{
- fields[n] = QString::number(v);
+ m_fields[field] = QString::number(value);
}
-void Ini::addField(const QString& n, const double v)
+void Ini::addField(const QString& field, const double value)
{
- fields[n] = QString::number(v);
+ m_fields[field] = QString::number(value);
}
-void Ini::addField(const QString& n, const QString& v)
+void Ini::addField(const QString& field, const QString& value)
{
- fields[n] = v;
+ m_fields[field] = value;
}
-void Ini::getValue(const QString& field, bool& val, const bool def)
+void Ini::getValue(const QString& field, bool& value, const bool defaultValue)
{
- if (!fields.contains(field))
+ if (!m_fields.contains(field))
{
- val = def;
+ value = defaultValue;
return;
}
- val = (fields[field] == "true") ? true : ((fields[field] == "false") ? false : def);
+ value = (m_fields[field] == "true") ? true : ((m_fields[field] == "false") ? false : defaultValue);
}
-void Ini::getValue(const QString& field, unsigned char& val, const unsigned char def)
+void Ini::getValue(const QString& field, unsigned char& value, const unsigned char defaultValue)
{
- if (!fields.contains(field))
+ if (!m_fields.contains(field))
{
- val = def;
+ value = defaultValue;
return;
}
bool ok;
unsigned temp;
- temp = fields[field].toUInt(&ok);
- val = (ok && (temp <= UCHAR_MAX)) ? temp : def;
+ temp = m_fields[field].toUInt(&ok);
+ value = (ok && (temp <= UCHAR_MAX)) ? temp : defaultValue;
}
-void Ini::getValue(const QString& field, int& val, const int def)
+void Ini::getValue(const QString& field, int& value, const int defaultValue)
{
- if (!fields.contains(field))
+ if (!m_fields.contains(field))
{
- val = def;
+ value = defaultValue;
return;
}
bool ok;
- val = fields[field].toInt(&ok);
+ value = m_fields[field].toInt(&ok);
if (!ok)
- val = def;
+ value = defaultValue;
}
-void Ini::getValue(const QString& field, double& val, const double def)
+void Ini::getValue(const QString& field, double& value, const double defaultValue)
{
- if (!fields.contains(field))
+ if (!m_fields.contains(field))
{
- val = def;
+ value = defaultValue;
return;
}
bool ok;
- val = fields[field].toDouble(&ok);
+ value = m_fields[field].toDouble(&ok);
if (!ok)
- val = def;
+ value = defaultValue;
}
-void Ini::getValue(const QString& field, QString& val, const QString& def)
+void Ini::getValue(const QString& field, QString& value, const QString& defaultValue)
{
- if (!fields.contains(field))
+ if (!m_fields.contains(field))
{
- val = def;
+ value = defaultValue;
return;
}
- val = fields[field];
+ value = m_fields[field];
}
-QStringList Ini::getFields() const
+QStringList Ini::fields() const
{
- return fields.keys();
+ return m_fields.keys();
}