summaryrefslogtreecommitdiff
path: root/src/resources/db/unitsdb.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2017-01-31 19:50:58 +0300
committerAndrei Karas <akaras@inbox.ru>2017-01-31 19:54:19 +0300
commitaea9e7f8c26644ba38db5eb9dbf412195f9851b2 (patch)
treeea7f6965596d58e0adf02585e945d6eef91ddb83 /src/resources/db/unitsdb.cpp
parent54fef2b6515e55cf54e51541441da49068ff5afc (diff)
downloadplus-aea9e7f8c26644ba38db5eb9dbf412195f9851b2.tar.gz
plus-aea9e7f8c26644ba38db5eb9dbf412195f9851b2.tar.bz2
plus-aea9e7f8c26644ba38db5eb9dbf412195f9851b2.tar.xz
plus-aea9e7f8c26644ba38db5eb9dbf412195f9851b2.zip
Add support for load different currencies from units.xml.
Diffstat (limited to 'src/resources/db/unitsdb.cpp')
-rw-r--r--src/resources/db/unitsdb.cpp134
1 files changed, 84 insertions, 50 deletions
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 <climits>
+#include <map>
#include "debug.h"
@@ -54,6 +57,8 @@ namespace
UnitDescription defaultCurrency;
UnitDescription defaultWeight;
+
+ std::map<std::string, UnitDescription> 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);
+ }
}
}