diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2007-05-05 15:57:58 +0000 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2007-05-05 15:57:58 +0000 |
| commit | 0d2d8121cbb6a45180d88021fe2e5ac86b3532e3 (patch) | |
| tree | 512385f11f6d152f76ac36c7950bfe75992291ff /pokemod/Ini.cpp | |
| parent | 21cb50c82f18b3a2ee1e77a7de11413ce45e6587 (diff) | |
| download | sigen-0d2d8121cbb6a45180d88021fe2e5ac86b3532e3.tar.gz sigen-0d2d8121cbb6a45180d88021fe2e5ac86b3532e3.tar.xz sigen-0d2d8121cbb6a45180d88021fe2e5ac86b3532e3.zip | |
Got rid of XML for INI format, in process of moving over; added /ai/Net.{h, cpp}
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@8 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'pokemod/Ini.cpp')
| -rw-r--r-- | pokemod/Ini.cpp | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/pokemod/Ini.cpp b/pokemod/Ini.cpp new file mode 100644 index 00000000..4c108564 --- /dev/null +++ b/pokemod/Ini.cpp @@ -0,0 +1,175 @@ +/////////////////////////////////////////////////////////////////////////////
+// Name: pokemod/Ini.cpp
+// Purpose: Define sections for PokéMod files
+// Author: Ben Boeckel
+// Modified by: Ben Boeckel
+// 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
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 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.
+/////////////////////////////////////////////////////////////////////////////
+
+#include "Ini.h"
+
+PokeGen::PokeMod::Ini::Ini(const String &n)
+{
+ name = n;
+}
+
+PokeGen::PokeMod::Ini::Ini(std::ifstream &file)
+{
+ Load(file);
+}
+
+void PokeGen::PokeMod::Ini::Export(std::ofstream &file)
+{
+ file << '[' << name << "]\n";
+ for (std::map<String, String>::iterator i = fields.begin(); i != fields.end(); ++i)
+ file << i->first << '=' << i->second << '\n';
+ file << '\n';
+}
+
+void PokeGen::PokeMod::Ini::Export(const String &p)
+{
+ std::ofstream fout(p.c_str(), std::ios::out | std::ios::binary);
+ Export(fout);
+}
+
+template<typename T>
+void PokeGen::PokeMod::Ini::AddField<T>(const String &n, const T v)
+{
+ std::stringstream ss;
+ std::string s;
+ ss << v;
+ ss >> s;
+ fields[n] = s;
+}
+
+template<>
+void PokeGen::PokeMod::Ini::AddField<bool>(const String &n, const bool v)
+{
+ fields[n] = v ? "true" : "false";
+}
+
+void PokeGen::PokeMod::Ini::AddField(const String &n, const String &v)
+{
+ fields[n] = v;
+}
+
+PokeGen::PokeMod::String PokeGen::PokeMod::Ini::GetName()
+{
+ return name;
+}
+
+void PokeGen::PokeMod::Ini::GetValue(const String &field, int &val, int def)
+{
+ if (!fields.count(field))
+ {
+ val = def;
+ return;
+ }
+ long temp;
+ char *end;
+ temp = std::strtol(fields[field].c_str(), &end, 10);
+ val = ((temp < INT_MIN) || (INT_MAX < temp) || *end) ? def : temp;
+}
+
+void PokeGen::PokeMod::Ini::GetValue(const String &field, unsigned &val, unsigned def)
+{
+ if (!fields.count(field))
+ {
+ val = def;
+ return;
+ }
+ unsigned long temp;
+ char *end;
+ temp = std::strtoul(fields[field].c_str(), &end, 10);
+ val = ((UINT_MAX < temp) || *end) ? def : temp;
+}
+
+void PokeGen::PokeMod::Ini::GetValue(const String &field, String &val)
+{
+ if (!fields.count(field))
+ {
+ val = def;
+ return;
+ }
+ val = fields[field];
+}
+
+void PokeGen::PokeMod::Ini::GetValue(const String &field, bool &val, 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 String &field, double &val, double def)
+{
+ if (!fields.count(field))
+ {
+ val = def;
+ return;
+ }
+ char *end;
+ val = std::strtod(fields[field].c_str(), &end);
+ if (*end)
+ val = def;
+}
+
+bool PokeGen::PokeMod::Ini::IsValid()
+{
+ return isValid;
+}
+
+bool PokeGen::PokeMod::Ini::Load(std::ifstream &file)
+{
+ String line;
+ String field;
+ String value;
+ int pos;
+ fields.clear();
+ name = "";
+ isValid = true;
+ std::getline(file, line);
+ if (line[0] != '[')
+ return (isValid = false);
+ if ((pos = line.find(']')) != line.length() - 1)
+ return (isValid = false);
+ name.assign(line, 1, pos - 1);
+ std::getline(file, line);
+ while (!line.empty())
+ {
+ if (line[0] == '[')
+ return (isValid = false);
+ pos = line.find('=');
+ if (pos == std::string::npos)
+ return (isValid = false);
+ field.assign(line, 0, pos - 1);
+ value.assign(line, pos + 1, line.length() - pos);
+ if (field.empty())
+ return (isValid = false);
+ std::getline(file, line);
+ }
+ return true;
+}
|
