summaryrefslogtreecommitdiffstats
path: root/pokemod/Ini.cpp
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2007-07-21 01:39:22 +0000
committerBen Boeckel <MathStuf@gmail.com>2007-07-21 01:39:22 +0000
commite94d9893b8753e72adb92b2c5eb203830ddf641c (patch)
treefe283d6ede1cfe1a1613742811fb5b34fb8db68c /pokemod/Ini.cpp
parent65cc463f1d91fe99acf1c4dd9bce7e0038593022 (diff)
downloadsigen-e94d9893b8753e72adb92b2c5eb203830ddf641c.tar.gz
sigen-e94d9893b8753e72adb92b2c5eb203830ddf641c.tar.xz
sigen-e94d9893b8753e72adb92b2c5eb203830ddf641c.zip
Moved to GPLv3 and Qt4, Changed String -> QString, other minor fixes
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@23 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'pokemod/Ini.cpp')
-rw-r--r--pokemod/Ini.cpp238
1 files changed, 107 insertions, 131 deletions
diff --git a/pokemod/Ini.cpp b/pokemod/Ini.cpp
index e78adef6..48efc499 100644
--- a/pokemod/Ini.cpp
+++ b/pokemod/Ini.cpp
@@ -6,176 +6,152 @@
// Created: Fri May 4 23:27:37 2007
// Copyright: ©2007 Nerdy Productions
// Licence:
-// This program is free software; you can redistribute it and/or modify
+// 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 2 of the License, or
+// 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, write to the Free Software Foundation, Inc.,
-// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+// with this program. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////
#include "Ini.h"
-PokeGen::PokeMod::Ini::Ini(const String &n) :
+PokeGen::PokeMod::Ini::Ini(const QString &n) :
name(n)
{
}
-PokeGen::PokeMod::Ini::Ini(std::ifstream &file)
+PokeGen::PokeMod::Ini::Ini(QFile &file)
{
isValid = Load(file);
}
-void PokeGen::PokeMod::Ini::Export(std::ofstream &file) const
+void PokeGen::PokeMod::Ini::Export(QFile &file) const
{
- file << '[' << name << "]\n";
- for (std::map<String, String>::const_iterator i = fields.begin(); i != fields.end(); ++i)
- file << i->first << '=' << i->second << '\n';
- file << '\n';
+ QTextStream fout(&file);
+ fout << '[' << name << "]\n";
+ for (QMap<QString, QString>::ConstIterator i = fields.begin(); i != fields.end(); ++i)
+ fout << i.key() << '=' << i.value() << '\n';
+ fout << '\n';
}
-void PokeGen::PokeMod::Ini::Export(const String &p) const
+void PokeGen::PokeMod::Ini::Export(const QString &p) const
{
- std::ofstream fout(p.c_str(), std::ios::out | std::ios::binary);
+ QFile fout(p);
+ fout.open(QIODevice::WriteOnly);
Export(fout);
+ fout.close();
}
-void PokeGen::PokeMod::Ini::AddField(const String &n, const bool v)
-{
- fields[n] = v ? "true" : "false";
-}
-
-void PokeGen::PokeMod::Ini::AddField(const String &n, const char v)
-{
- fields[n] = String("%c", v);
-}
-
-void PokeGen::PokeMod::Ini::AddField(const String &n, const unsigned char v)
-{
- fields[n] = String("%x", v);
-}
-
-void PokeGen::PokeMod::Ini::AddField(const String &n, const int v)
+void PokeGen::PokeMod::Ini::AddField(const QString &n, const bool v)
{
- fields[n] = String("%d", v);
+ fields[n] = v ? "true" : "false";
}
-void PokeGen::PokeMod::Ini::AddField(const String &n, const unsigned v)
+void PokeGen::PokeMod::Ini::AddField(const QString &n, const int v)
{
- fields[n] = String("%u", v);
+ fields[n] = QString("%1").arg(v);
}
-void PokeGen::PokeMod::Ini::AddField(const String &n, const double v)
-{
- fields[n] = String("%f", v);
-}
-
-void PokeGen::PokeMod::Ini::AddField(const String &n, const String &v)
+void PokeGen::PokeMod::Ini::AddField(const QString &n, const unsigned v)
+{
+ fields[n] = QString("%1").arg(v);
+}
+
+void PokeGen::PokeMod::Ini::AddField(const QString &n, const float v)
+{
+ fields[n] = QString("%1").arg(v);
+}
+
+void PokeGen::PokeMod::Ini::AddField(const QString &n, const double v)
+{
+ fields[n] = QString("%1").arg(v);
+}
+
+void PokeGen::PokeMod::Ini::AddField(const QString &n, const QString &v)
{
fields[n] = v;
}
-PokeGen::PokeMod::String PokeGen::PokeMod::Ini::GetName() const
+QString PokeGen::PokeMod::Ini::GetName() const
{
return name;
}
-void PokeGen::PokeMod::Ini::GetValue(const String &field, bool &val, const bool def)
-{
- if (!fields.count(field))
- {
- val = def;
- return;
- }
- if (!fields[field].Convert(val))
- val = def;
-}
-
-void PokeGen::PokeMod::Ini::GetValue(const String &field, char &val, const char def)
-{
- if (!fields.count(field))
- {
- val = def;
- return;
- }
- if (!fields[field].Convert(val))
- val = def;
-}
-
-void PokeGen::PokeMod::Ini::GetValue(const String &field, unsigned char &val, const unsigned char def)
-{
- if (!fields.count(field))
- {
- val = def;
- return;
- }
- if (!fields[field].Convert(val))
- val = def;
-}
-
-void PokeGen::PokeMod::Ini::GetValue(const String &field, int &val, const int def)
+void PokeGen::PokeMod::Ini::GetValue(const QString &field, bool &val, const bool def)
+{
+ if (!fields.count(field))
+ {
+ val = def;
+ return;
+ }
+ if (fields[field] == "true")
+ val = true;
+ else if (fields[field] == "false")
+ val = false;
+ else
+ val = def;
+}
+
+void PokeGen::PokeMod::Ini::GetValue(const QString &field, int &val, const int def)
+{
+ if (!fields.count(field))
+ {
+ val = def;
+ return;
+ }
+ bool ok;
+ val = fields[field].toInt(&ok);
+ if (!ok)
+ val = def;
+}
+
+void PokeGen::PokeMod::Ini::GetValue(const QString &field, unsigned &val, const unsigned def)
+{
+ if (!fields.count(field))
+ {
+ val = def;
+ return;
+ }
+ bool ok;
+ val = fields[field].toUInt(&ok);
+ if (!ok)
+ val = def;
+}
+
+void PokeGen::PokeMod::Ini::GetValue(const QString &field, float &val, const float def)
{
if (!fields.count(field))
{
val = def;
return;
}
- if (!fields[field].Convert(val))
- val = def;
+ bool ok;
+ val = fields[field].toFloat(&ok);
+ if (!ok)
+ val = def;
}
-void PokeGen::PokeMod::Ini::GetValue(const String &field, unsigned &val, const unsigned def)
+void PokeGen::PokeMod::Ini::GetValue(const QString &field, double &val, const double def)
{
if (!fields.count(field))
{
val = def;
return;
}
- if (!fields[field].Convert(val))
- val = def;
-}
-
-void PokeGen::PokeMod::Ini::GetValue(const String &field, long &val, const long def)
-{
- if (!fields.count(field))
- {
- val = def;
- return;
- }
- if (!fields[field].Convert(val))
- val = def;
-}
-
-void PokeGen::PokeMod::Ini::GetValue(const String &field, unsigned long &val, const unsigned long def)
-{
- if (!fields.count(field))
- {
- val = def;
- return;
- }
- if (!fields[field].Convert(val))
- val = def;
-}
-
-void PokeGen::PokeMod::Ini::GetValue(const String &field, double &val, const double def)
-{
- if (!fields.count(field))
- {
- val = def;
- return;
- }
- if (!fields[field].Convert(val))
- val = def;
-}
-
-void PokeGen::PokeMod::Ini::GetValue(const String &field, String &val, const String &def)
+ bool ok;
+ val = fields[field].toDouble(&ok);
+ if (!ok)
+ val = def;
+}
+
+void PokeGen::PokeMod::Ini::GetValue(const QString &field, QString &val, const QString &def)
{
if (!fields.count(field))
{
@@ -190,34 +166,34 @@ bool PokeGen::PokeMod::Ini::IsValid() const
return isValid;
}
-bool PokeGen::PokeMod::Ini::Load(std::ifstream &file)
+bool PokeGen::PokeMod::Ini::Load(QFile &file)
{
- std::string line;
- std::string field;
- std::string value;
+ QTextStream fin(&file);
+ QString line = fin.readLine();
+ QString field;
+ QString value;
fields.clear();
- size_t pos;
+ int pos;
name = "";
isValid = true;
- std::getline(file, line);
if (line.at(0) != '[')
return (isValid = false);
if (line.at(line.length() - 1) != ']')
return (isValid = false);
- name.assign(line, 1, line.length() - 2);
- std::getline(file, line);
- while (!line.empty())
+ name = line.mid(1, line.length() - 2);
+ line = fin.readLine();
+ while (!fin.atEnd())
{
if (line.at(0) == '[')
return (isValid = false);
- pos = line.find('=');
- if (pos == std::string::npos)
+ pos = line.indexOf('=');
+ if (pos == -1)
return (isValid = false);
- field.assign(line, 0, pos - 1);
- value.assign(line, pos + 1, line.length() - pos);
- if (field.empty())
+ field = line.mid(0, pos - 1);
+ value = line.mid(pos + 1, line.length() - pos);
+ if (field.isEmpty())
return (isValid = false);
- std::getline(file, line);
+ line = fin.readLine();
}
return true;
}