From aea9e7f8c26644ba38db5eb9dbf412195f9851b2 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 31 Jan 2017 19:50:58 +0300 Subject: Add support for load different currencies from units.xml. --- src/resources/db/unitsdb.cpp | 134 +++++++++++++++++++++++++++---------------- 1 file changed, 84 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/resources/db/unitsdb.cpp b/src/resources/db/unitsdb.cpp index 56e83a718..3001ccb7b 100644 --- a/src/resources/db/unitsdb.cpp +++ b/src/resources/db/unitsdb.cpp @@ -25,9 +25,12 @@ #include "configuration.h" #include "logger.h" +#include "utils/checkutils.h" + #include "resources/beingcommon.h" #include +#include #include "debug.h" @@ -54,6 +57,8 @@ namespace UnitDescription defaultCurrency; UnitDescription defaultWeight; + + std::map mCurrencies; } // namespace static std::string formatUnit(const int value, @@ -108,6 +113,80 @@ void UnitsDb::loadUnits() loadXmlDir("unitsPatchDir", loadXmlFile); } +static UnitDescription loadUnit(XmlNodePtr node) +{ + UnitDescription ud; + int level = 1; + ud.conversion = XML::getProperty(node, "conversion", 1); + ud.mix = XML::getProperty(node, "mix", "no") == "yes"; + + UnitLevel bu; + bu.symbol = XML::getProperty(node, "base", "¤"); + bu.count = 1; + bu.round = XML::getProperty(node, "round", 2); + bu.separator = XML::getProperty(node, "separator", " "); + + ud.levels.push_back(bu); + + for_each_xml_child_node(uLevel, node) + { + if (xmlNameEqual(uLevel, "level")) + { + const UnitLevel ul = + { + XML::getProperty(uLevel, "symbol", + strprintf("¤%d", level)), + XML::getProperty(uLevel, "count", -1), + XML::getProperty(uLevel, "round", bu.round), + XML::getProperty(uLevel, "separator", bu.separator) + }; + + if (ul.count > 0) + { + ud.levels.push_back(ul); + level++; + } + else + { + logger->log("Error bad unit count: %d for %s in %s", + ul.count, + ul.symbol.c_str(), + bu.symbol.c_str()); + } + } + } + + // Add one more level for saftey + const UnitLevel lev = + { + "", + INT_MAX, + 0, + "" + }; + ud.levels.push_back(lev); + return ud; +} + +static void loadCurrencies(XmlNodePtr parentNode) +{ + for_each_xml_child_node(node, parentNode) + { + if (xmlNameEqual(node, "unit")) + { + const std::string name = XML::getProperty(node, "name", ""); + if (name == "") + { + reportAlways("Error: unknown currency name."); + continue; + } + mCurrencies[name] = loadUnit(node); + if (name == "default") + defaultCurrency = mCurrencies[name]; + } + } +} + void UnitsDb::loadXmlFile(const std::string &fileName, const SkipError skipError) { @@ -132,57 +211,8 @@ void UnitsDb::loadXmlFile(const std::string &fileName, } else if (xmlNameEqual(node, "unit")) { - UnitDescription ud; - int level = 1; const std::string type = XML::getProperty(node, "type", ""); - ud.conversion = XML::getProperty(node, "conversion", 1); - ud.mix = XML::getProperty(node, "mix", "no") == "yes"; - - UnitLevel bu; - bu.symbol = XML::getProperty(node, "base", "¤"); - bu.count = 1; - bu.round = XML::getProperty(node, "round", 2); - bu.separator = XML::getProperty(node, "separator", " "); - - ud.levels.push_back(bu); - - for_each_xml_child_node(uLevel, node) - { - if (xmlNameEqual(uLevel, "level")) - { - const UnitLevel ul = - { - XML::getProperty(uLevel, "symbol", - strprintf("¤%d", level)), - XML::getProperty(uLevel, "count", -1), - XML::getProperty(uLevel, "round", bu.round), - XML::getProperty(uLevel, "separator", bu.separator) - }; - - if (ul.count > 0) - { - ud.levels.push_back(ul); - level++; - } - else - { - logger->log("Error bad unit count: %d for %s in %s", - ul.count, ul.symbol.c_str(), - bu.symbol.c_str()); - } - } - } - - // Add one more level for saftey - const UnitLevel lev = - { - "", - INT_MAX, - 0, - "" - }; - ud.levels.push_back(lev); - + UnitDescription ud = loadUnit(node); if (type == "weight") defaultWeight = ud; else if (type == "currency") @@ -190,6 +220,10 @@ void UnitsDb::loadXmlFile(const std::string &fileName, else logger->log("Error unknown unit type: %s", type.c_str()); } + else if (xmlNameEqual(node, "currency")) + { + loadCurrencies(node); + } } } -- cgit v1.2.3-60-g2f50