ManaPlus
Functions
UnitsDb Namespace Reference

Functions

void load ()
 
void unload ()
 
void loadXmlFile (const std::string &fileName, const SkipError skipError)
 
std::string formatCurrency (const int value)
 
std::string formatCurrency64 (const int64_t value)
 
std::string formatCurrency (std::string name, const int value)
 
std::string formatCurrency64 (std::string name, const int64_t value)
 
std::string formatWeight (const int value)
 
bool existsCurrency (const std::string &name)
 

Function Documentation

◆ existsCurrency()

bool UnitsDb::existsCurrency ( const std::string &  name)

Definition at line 356 of file unitsdb.cpp.

357 {
358  return mCurrencies.find(name) != mCurrencies.end();
359 }

References anonymous_namespace{unitsdb.cpp}::mCurrencies.

Referenced by NPCDB::loadXmlFile().

◆ formatCurrency() [1/2]

std::string UnitsDb::formatCurrency ( const int  value)

◆ formatCurrency() [2/2]

std::string UnitsDb::formatCurrency ( std::string  name,
const int  value 
)

Formats the given number in the correct currency format.

Definition at line 334 of file unitsdb.cpp.

336 {
337  if (mCurrencies.find(name) == mCurrencies.end())
338  name = DEFAULT_CURRENCY;
339  return formatUnit(value, mCurrencies[name]);
340 }

References DEFAULT_CURRENCY, formatUnit(), and anonymous_namespace{unitsdb.cpp}::mCurrencies.

◆ formatCurrency64() [1/2]

std::string UnitsDb::formatCurrency64 ( const int64_t  value)

Definition at line 328 of file unitsdb.cpp.

329 {
330  return formatUnit(CAST_S32(value),
332 }

References CAST_S32, anonymous_namespace{unitsdb.cpp}::defaultCurrency, and formatUnit().

Referenced by StatusWindow::attributeChanged(), and CharacterDisplay::mouseMoved().

◆ formatCurrency64() [2/2]

std::string UnitsDb::formatCurrency64 ( std::string  name,
const int64_t  value 
)

Definition at line 342 of file unitsdb.cpp.

344 {
345  if (mCurrencies.find(name) == mCurrencies.end())
346  name = DEFAULT_CURRENCY;
347  return formatUnit(CAST_S32(value),
348  mCurrencies[name]);
349 }

References CAST_S32, DEFAULT_CURRENCY, formatUnit(), and anonymous_namespace{unitsdb.cpp}::mCurrencies.

◆ formatWeight()

std::string UnitsDb::formatWeight ( const int  value)

Formats the given number in the correct weight/mass format.

Definition at line 351 of file unitsdb.cpp.

352 {
353  return formatUnit(value, defaultWeight);
354 }

References anonymous_namespace{unitsdb.cpp}::defaultWeight, and formatUnit().

Referenced by ItemPopup::setItem(), InventoryWindow::updateWeight(), and StatusWindow::updateWeightBar().

◆ load()

void UnitsDb::load ( )

Loads and parses the units.xml file (if found).

Definition at line 72 of file unitsdb.cpp.

73 {
74  logger->log1("Initializing unit database...");
75  { // Setup default weight
76  UnitDescription ud;
77 
78  ud.conversion = 1.0;
79  ud.mix = false;
80 
81  const UnitLevel bu =
82  {
83  "g",
84  1,
85  0,
86  ""
87  };
88  ud.levels.push_back(bu);
89 
90  const UnitLevel ul =
91  {
92  "kg",
93  1000,
94  2,
95  ""
96  };
97  ud.levels.push_back(ul);
98 
99  defaultWeight = ud;
100  }
101 
102  { // Setup default currency
103  UnitDescription ud;
104 
105  ud.conversion = 1.0;
106  ud.mix = false;
107 
108  const UnitLevel bu = {"ยค", 1, 0, ""};
109  ud.levels.push_back(bu);
110 
111  defaultCurrency = ud;
112  }
113 
115  loadXmlFile(paths.getStringValue("unitsPatchFile"), SkipError_true);
116  loadXmlDir("unitsPatchDir", loadXmlFile)
117 }

References anonymous_namespace{unitsdb.cpp}::UnitDescription::conversion, anonymous_namespace{unitsdb.cpp}::defaultCurrency, anonymous_namespace{unitsdb.cpp}::defaultWeight, Configuration::getStringValue(), loadXmlDir, loadXmlFile(), Logger::log1(), logger, paths, SkipError_false, and SkipError_true.

Referenced by DbManager::loadDb().

◆ loadXmlFile()

void UnitsDb::loadXmlFile ( const std::string &  fileName,
const SkipError  skipError 
)

Definition at line 199 of file unitsdb.cpp.

201 {
202  XML::Document doc(fileName, UseVirtFs_true, skipError);
203  XmlNodeConstPtrConst root = doc.rootNode();
204 
205  if ((root == nullptr) || !xmlNameEqual(root, "units"))
206  {
207  logger->log("Error loading unit definition file: "
208  + paths.getStringValue("unitsFile"));
209  return;
210  }
211 
212  for_each_xml_child_node(node, root)
213  {
214  if (xmlNameEqual(node, "include"))
215  {
216  const std::string name = XML::getProperty(node, "name", "");
217  if (!name.empty())
218  loadXmlFile(name, skipError);
219  continue;
220  }
221  else if (xmlNameEqual(node, "unit"))
222  {
223  const std::string type = XML::getProperty(node, "type", "");
224  UnitDescription ud = loadUnit(node);
225  if (type == "weight")
226  {
227  defaultWeight = ud;
228  }
229  else if (type == "currency")
230  {
231  defaultCurrency = ud;
233  }
234  else
235  {
236  logger->log("Error unknown unit type: %s", type.c_str());
237  }
238  }
239  else if (xmlNameEqual(node, "currency"))
240  {
241  loadCurrencies(node);
242  }
243  }
244 }

References DEFAULT_CURRENCY, anonymous_namespace{unitsdb.cpp}::defaultCurrency, anonymous_namespace{unitsdb.cpp}::defaultWeight, fileName, for_each_xml_child_node, XML::getProperty(), Configuration::getStringValue(), loadCurrencies(), loadUnit(), loadXmlFile(), Logger::log(), logger, anonymous_namespace{unitsdb.cpp}::mCurrencies, paths, XML::Document::rootNode(), and UseVirtFs_true.

◆ unload()

void UnitsDb::unload ( )

Clear the chars data

Definition at line 119 of file unitsdb.cpp.

120 {
121  logger->log1("Unloading unit database...");
122  mCurrencies.clear();
123 }

References Logger::log1(), logger, and anonymous_namespace{unitsdb.cpp}::mCurrencies.

Referenced by DbManager::unloadDb().

DEFAULT_CURRENCY
const std::string DEFAULT_CURRENCY
Definition: currency.h:26
SkipError_true
const bool SkipError_true
Definition: skiperror.h:29
loadCurrencies
static void loadCurrencies(xmlNodePtr parentNode)
Definition: unitsdb.cpp:180
paths
Configuration paths
Definition: configuration.cpp:55
anonymous_namespace{unitsdb.cpp}::defaultWeight
UnitDescription defaultWeight
Definition: unitsdb.cpp:61
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
loadUnit
static UnitDescription loadUnit(xmlNodePtr node)
Definition: unitsdb.cpp:125
XML::Document
Definition: libxml.h:52
XML::getProperty
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:173
logger
Logger * logger
Definition: logger.cpp:88
fileName
std::string fileName
Definition: testmain.cpp:38
loadXmlDir
#define loadXmlDir(name, function)
Definition: beingcommon.h:38
loadXmlFile
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:42
UseVirtFs_true
const bool UseVirtFs_true
Definition: usevirtfs.h:29
Configuration::getStringValue
std::string getStringValue(const std::string &key) const
Definition: configuration.cpp:487
anonymous_namespace{unitsdb.cpp}::defaultCurrency
UnitDescription defaultCurrency
Definition: unitsdb.cpp:60
Logger::log1
void log1(const char *const log_text)
Definition: logger.cpp:237
CAST_S32
#define CAST_S32
Definition: cast.h:29
formatUnit
static std::string formatUnit(const int value, const UnitDescription &ud)
Definition: unitsdb.cpp:246
anonymous_namespace{unitsdb.cpp}::mCurrencies
std::map< std::string, UnitDescription > mCurrencies
Definition: unitsdb.cpp:63
for_each_xml_child_node
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:160
anonymous_namespace{unitsdb.cpp}::UnitDescription::conversion
double conversion
Definition: unitsdb.cpp:56
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268