From fafbdab97afb5ec6498ea0ab66b693b9e335f6f1 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Fri, 11 May 2007 01:33:42 +0000 Subject: Starting INI migration git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@9 6ecfd1a5-f3ed-3746-8530-beee90d26b22 --- pokemod/Matrix.cpp | 171 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 97 insertions(+), 74 deletions(-) (limited to 'pokemod/Matrix.cpp') diff --git a/pokemod/Matrix.cpp b/pokemod/Matrix.cpp index d2c917c0..9fb32398 100644 --- a/pokemod/Matrix.cpp +++ b/pokemod/Matrix.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: pokemod/Matrix.cpp +// Name: pokemod/Map.cpp // Purpose: A 2D vector class // Author: Ben Boeckel // Modified by: Ben Boeckel @@ -24,7 +24,7 @@ #include "Matrix.h" template -PokeGen::PokeMod::Matrix::Matrix(unsigned w, unsigned h, T &d) +PokeGen::PokeMod::Matrix::Matrix(unsigned w, unsigned h, const T &d) { matrix.resize(w, std::vector(h, d)); width = w; @@ -32,112 +32,135 @@ PokeGen::PokeMod::Matrix::Matrix(unsigned w, unsigned h, T &d) } template -PokeGen::PokeMod::Matrix::Matrix(XmlElement &x) +PokeGen::PokeMod::Matrix::Matrix(Ini &ini) { - ImportXml(xml); + width = 0; + height = 0; + matrix.clear(); } -template -void PokeGen::PokeMod::Matrix::AddRow(T &d) +template<> +PokeGen::PokeMod::Matrix::Matrix(Ini &ini) { - for (std::vector *i = matrix.begin(); i != matrix.end(); ++i) - i->push_back(d); - ++height; + ImportIni(ini); +} + +template<> +PokeGen::PokeMod::Matrix::Matrix(Ini &ini) +{ + ImportIni(ini); } template -void PokeGen::PokeMod::Matrix::ImportXml(XmlElement &xml) +void PokeGen::PokeMod::Matrix::ImportIni(Ini &ini) +{ +} + +template<> +void PokeGen::PokeMod::Matrix::ImportIni(Ini &ini) { LogImportStart("Matrix"); - String curName; - width = 0; - height = 0; - bool haveHeight = false; - bool haveWidth = false; - xml.ClearCounter(); - for (XmlElement *child = xml.NextElement(); child; child = xml.NextElement()) + ini.GetValue("width", width, 0); + ini.GetValue("height", height, 0); + if (!(width && height)) + return; + matrix.resize(width, std::vector(height, Point(0, 0))); + for (unsigned i = 0; i < width; ++i) { - curName = child->GetName(); - if (curName == "height") + for (unsigned j = 0; j < height; ++j) { - child->GetValue(height); - if (height) - haveHeight = true; + int k; + int l; + ini.GetValue(String("Element-%d-%d-x", i, j), k, 0); + ini.GetValue(String("Element-%d-%d-y", i, j), l, 0); + matrix[i][j].Set(k, l); } - else if (curName == "width") + } + LogImportOver("Matrix"); +} + +template<> +void PokeGen::PokeMod::Matrix::ImportIni(Ini &ini) +{ + LogImportStart("Matrix"); + ini.GetValue("width", width, 0); + ini.GetValue("height", height, 0); + if (!(width && height)) + return; + matrix.resize(width, std::vector(height, Frac(1, 1))); + for (unsigned i = 0; i < width; ++i) + { + for (unsigned j = 0; j < height; ++j) { - if (haveHeight) - { - child->GetValue(width); - if (width) - haveWidth = true; - matrix.resize(w, std::vector(h, T())); - } - else - Log("Matrix Import: Elements out of order", PM_DEBUG_ERROR); + unsigned k; + unsigned l; + bool m; + ini.GetValue(String("Element-%d-%d-n", i, j), k, 0); + ini.GetValue(String("Element-%d-%d-d", i, j), l, 0); + ini.GetValue(String("Element-%d-%d-i", i, j), m, 0); + matrix[i][j].Set(k, l, m); } - else if (curName == "col") + } + LogImportOver("Matrix"); +} + +template<> +void PokeGen::PokeMod::Matrix::ExportIni(const String &val) +{ + Log(String("Matrix Export: Starting %s", val.c_str()), PM_DEBUG_INFO); + // Declare the elements + Ini exMatrix(val); + exMatrix.AddField("height", height); + exMatrix.AddField("width", width); + for (int i = 0; i < width; ++i) + { + for (unsigned j = 0; j < height; ++j) { - if (height && width) - { - unsigned colNum; - unsigned rowNum; - child->GetValue(colNum); - if (colNum < width) - { - child->ClearCounter(); - for (XmlElement *item = child->NextElement(); item; item = child->NextElement()) - { - item->GetValue(rowNum); - if ((item->GetName() == "item") && (rowNum < height)) - matrix[colNum][rowNum].ImportXml(*item); - else - Log("Matrix Import: Item out of bounds", PM_DEBUG_ERROR); - } - } - else - Log("Matrix Import: Column out of bounds", PM_DEBUG_ERROR); - } - else - Log("Matrix Import: Elements out of order", PM_DEBUG_ERROR); + exMatrix.AddField(String("Element-%d-%d-x", i, j), matrix[i][j].GetX()); + exMatrix.AddField(String("Element-%d-%d-y", i, j), matrix[i][j].GetY()); } - else - LogUnknownXml("Matrix", curName); } - LogImportOver("Matrix", id, name); + Log(String("Matrix Export: Finished %s", val.c_str()), PM_DEBUG_INFO); } -template -PokeGen::PokeMod::XmlElement PokeGen::PokeMod::Matrix::ExportXml(const String &val) +template<> +void PokeGen::PokeMod::Matrix::ExportIni(std::ofstream &fout, const String &val) { Log(String("Matrix Export: Starting %s", val.c_str()), PM_DEBUG_INFO); // Declare the elements - XmlElement exMatrix(val, 0, true); - exMatrix.AddElement("height", height); - exMatrix.AddElement("width", width); - for (int i = 0; i < width; ++i) + Ini exMatrix(val); + exMatrix.AddField("height", height); + exMatrix.AddField("width", width); + for (unsigned i = 0; i < width; ++i) { - XmlElement col("column", i, true); - for (int j = 0; j < height; ++j) + for (unsigned j = 0; j < height; ++j) { - XmlElement item("item", j, true); - item.AddElement(matrix[i][j].ExportXml()); + exMatrix.AddField(String("Element-%d-%d-i", i, j), matrix[i][j].GetImproper()); + exMatrix.AddField(String("Element-%d-%d-n", i, j), matrix[i][j].GetNum()); + exMatrix.AddField(String("Element-%d-%d-d", i, j), matrix[i][j].GetDenom()); } - exMatrix.AddElement(col); } + exMatrix.Export(fout); Log(String("Matrix Export: Finished %s", val.c_str()), PM_DEBUG_INFO); - return exMatrix; } template -void PokeGen::PokeMod::Matrix::AddCol(T &d) +void PokeGen::PokeMod::Matrix::AddRow(const T &d) +{ + for (std::vector *i = matrix.begin(); i != matrix.end(); ++i) + i->push_back(d); + ++height; +} + +template +void PokeGen::PokeMod::Matrix::AddCol(const T &d) { matrix.push_back(std::vector(GetHeight(), d)); ++width; } template -bool PokeGen::PokeMod::Matrix::InsertRow(unsigned pos, T &d) +bool PokeGen::PokeMod::Matrix::InsertRow(unsigned pos, const T &d) { if (height < pos) return false; @@ -148,7 +171,7 @@ bool PokeGen::PokeMod::Matrix::InsertRow(unsigned pos, T &d) } template -bool PokeGen::PokeMod::Matrix::InsertCol(unsigned pos, T &d) +bool PokeGen::PokeMod::Matrix::InsertCol(unsigned pos, const T &d) { if (width < pos) return false; @@ -179,7 +202,7 @@ bool PokeGen::PokeMod::Matrix::DeleteCol(unsigned pos) } template -bool PokeGen::PokeMod::Matrix::Set(unsigned row, unsigned col, T &s) +bool PokeGen::PokeMod::Matrix::Set(unsigned row, unsigned col, const T &s) { if ((width <= col) || (height <= col)) return false; -- cgit