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 357 of file unitsdb.cpp.

358 {
359  return mCurrencies.find(name) != mCurrencies.end();
360 }
std::map< std::string, UnitDescription > mCurrencies
Definition: unitsdb.cpp:64

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 335 of file unitsdb.cpp.

337 {
338  if (mCurrencies.find(name) == mCurrencies.end())
339  name = DEFAULT_CURRENCY;
340  return formatUnit(value, mCurrencies[name]);
341 }
const std::string DEFAULT_CURRENCY
Definition: currency.h:27

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

◆ formatCurrency64() [1/2]

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

Definition at line 329 of file unitsdb.cpp.

330 {
331  return formatUnit(CAST_S32(value),
333 }
#define CAST_S32
Definition: cast.h:30

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 343 of file unitsdb.cpp.

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

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 352 of file unitsdb.cpp.

353 {
354  return formatUnit(value, defaultWeight);
355 }
UnitDescription defaultWeight
Definition: unitsdb.cpp:62

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 73 of file unitsdb.cpp.

74 {
75  logger->log1("Initializing unit database...");
76  { // Setup default weight
77  UnitDescription ud;
78 
79  ud.conversion = 1.0;
80  ud.mix = false;
81 
82  const UnitLevel bu =
83  {
84  "g",
85  1,
86  0,
87  ""
88  };
89  ud.levels.push_back(bu);
90 
91  const UnitLevel ul =
92  {
93  "kg",
94  1000,
95  2,
96  ""
97  };
98  ud.levels.push_back(ul);
99 
100  defaultWeight = ud;
101  }
102 
103  { // Setup default currency
104  UnitDescription ud;
105 
106  ud.conversion = 1.0;
107  ud.mix = false;
108 
109  const UnitLevel bu = {"ยค", 1, 0, ""};
110  ud.levels.push_back(bu);
111 
112  defaultCurrency = ud;
113  }
114 
116  loadXmlFile(paths.getStringValue("unitsPatchFile"), SkipError_true);
117  loadXmlDir("unitsPatchDir", loadXmlFile)
118 }
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:43
#define loadXmlDir(name, function)
Definition: beingcommon.h:39
std::string getStringValue(const std::string &key) const
void log1(const char *const log_text)
Definition: logger.cpp:238
Configuration paths
Logger * logger
Definition: logger.cpp:89
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30

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 200 of file unitsdb.cpp.

202 {
203  XML::Document doc(fileName, UseVirtFs_true, skipError);
204  XmlNodeConstPtrConst root = doc.rootNode();
205 
206  if ((root == nullptr) || !xmlNameEqual(root, "units"))
207  {
208  logger->log("Error loading unit definition file: "
209  + paths.getStringValue("unitsFile"));
210  return;
211  }
212 
213  for_each_xml_child_node(node, root)
214  {
215  if (xmlNameEqual(node, "include"))
216  {
217  const std::string name = XML::getProperty(node, "name", "");
218  if (!name.empty())
219  loadXmlFile(name, skipError);
220  continue;
221  }
222  else if (xmlNameEqual(node, "unit"))
223  {
224  const std::string type = XML::getProperty(node, "type", "");
225  UnitDescription ud = loadUnit(node);
226  if (type == "weight")
227  {
228  defaultWeight = ud;
229  }
230  else if (type == "currency")
231  {
232  defaultCurrency = ud;
234  }
235  else
236  {
237  logger->log("Error unknown unit type: %s", type.c_str());
238  }
239  }
240  else if (xmlNameEqual(node, "currency"))
241  {
242  loadCurrencies(node);
243  }
244  }
245 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
std::string fileName
Definition: testmain.cpp:39
static UnitDescription loadUnit(xmlNodePtr node)
Definition: unitsdb.cpp:126
static void loadCurrencies(xmlNodePtr parentNode)
Definition: unitsdb.cpp:181
const bool UseVirtFs_true
Definition: usevirtfs.h:30

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 ( )

Definition at line 120 of file unitsdb.cpp.

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

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

Referenced by DbManager::unloadDb().