Noise: Prevent unittest crash caused by division by zero
[oweals/minetest.git] / src / itemstackmetadata.cpp
1 #include "itemstackmetadata.h"
2 #include "util/serialize.h"
3 #include "util/strfnd.h"
4
5 #define DESERIALIZE_START '\x01'
6 #define DESERIALIZE_KV_DELIM '\x02'
7 #define DESERIALIZE_PAIR_DELIM '\x03'
8 #define DESERIALIZE_START_STR "\x01"
9 #define DESERIALIZE_KV_DELIM_STR "\x02"
10 #define DESERIALIZE_PAIR_DELIM_STR "\x03"
11
12 void ItemStackMetadata::serialize(std::ostream &os) const
13 {
14         std::ostringstream os2;
15         os2 << DESERIALIZE_START;
16         for (StringMap::const_iterator
17                         it = m_stringvars.begin();
18                         it != m_stringvars.end(); ++it) {
19                 os2 << it->first << DESERIALIZE_KV_DELIM
20                     << it->second << DESERIALIZE_PAIR_DELIM;
21         }
22         os << serializeJsonStringIfNeeded(os2.str());
23 }
24
25 void ItemStackMetadata::deSerialize(std::istream &is)
26 {
27         std::string in = deSerializeJsonStringIfNeeded(is);
28
29         m_stringvars.clear();
30
31         if (!in.empty()) {
32                 if (in[0] == DESERIALIZE_START) {
33                         Strfnd fnd(in);
34                         fnd.to(1);
35                         while (!fnd.at_end()) {
36                                 std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
37                                 std::string var  = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
38                                 m_stringvars[name] = var;
39                         }
40                 } else {
41                         // BACKWARDS COMPATIBILITY
42                         m_stringvars[""] = in;
43                 }
44         }
45 }