]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/utility.h
Merge branch 'master' of github.com:erlehmann/minetest-delta
[dragonfireclient.git] / src / utility.h
index f32dc3acfce4103749157b0304a479ea8617bad2..497f79fa0dfed76ca441ec5e11824feb5b90a4d0 100644 (file)
@@ -244,6 +244,9 @@ inline f32 readF1000(std::istream &is)
 {
        char buf[2];
        is.read(buf, 2);
+       // TODO: verify if this gets rid of the valgrind warning
+       //if(is.gcount() != 2)
+       //      return 0;
        return readF1000((u8*)buf);
 }
 
@@ -1738,6 +1741,11 @@ void mysrand(unsigned seed);
 
 inline int myrand_range(int min, int max)
 {
+       if(max-min > MYRAND_MAX)
+       {
+               dstream<<"WARNING: myrand_range: max-min > MYRAND_MAX"<<std::endl;
+               assert(0);
+       }
        if(min > max)
        {
                assert(0);
@@ -1984,17 +1992,23 @@ inline std::string serializeString(const std::string &plain)
        return s;
 }
 
-/*// Reads a string with the length as the first two bytes
-inline std::string deSerializeString(const std::string encoded)
+// Creates a string with the length as the first two bytes from wide string
+inline std::string serializeWideString(const std::wstring &plain)
 {
-       u16 s_size = readU16((u8*)&encoded.c_str()[0]);
-       if(s_size > encoded.length() - 2)
-               return "";
+       //assert(plain.size() <= 65535);
+       if(plain.size() > 65535)
+               throw SerializationError("String too long for serializeString");
+       char buf[2];
+       writeU16((u8*)buf, plain.size());
        std::string s;
-       s.reserve(s_size);
-       s.append(&encoded.c_str()[2], s_size);
+       s.append(buf, 2);
+       for(u32 i=0; i<plain.size(); i++)
+       {
+               writeU16((u8*)buf, plain[i]);
+               s.append(buf, 2);
+       }
        return s;
-}*/
+}
 
 // Reads a string with the length as the first two bytes
 inline std::string deSerializeString(std::istream &is)
@@ -2014,6 +2028,27 @@ inline std::string deSerializeString(std::istream &is)
        return s;
 }
 
+// Reads a wide string with the length as the first two bytes
+inline std::wstring deSerializeWideString(std::istream &is)
+{
+       char buf[2];
+       is.read(buf, 2);
+       if(is.gcount() != 2)
+               throw SerializationError("deSerializeString: size not read");
+       u16 s_size = readU16((u8*)buf);
+       if(s_size == 0)
+               return L"";
+       std::wstring s;
+       s.reserve(s_size);
+       for(u32 i=0; i<s_size; i++)
+       {
+               is.read(&buf[0], 2);
+               wchar_t c16 = readU16((u8*)buf);
+               s.append(&c16, 1);
+       }
+       return s;
+}
+
 // Creates a string with the length as the first four bytes
 inline std::string serializeLongString(const std::string &plain)
 {
@@ -2025,18 +2060,6 @@ inline std::string serializeLongString(const std::string &plain)
        return s;
 }
 
-/*// Reads a string with the length as the first four bytes
-inline std::string deSerializeLongString(const std::string encoded)
-{
-       u32 s_size = readU32((u8*)&encoded.c_str()[0]);
-       if(s_size > encoded.length() - 4)
-               return "";
-       std::string s;
-       s.reserve(s_size);
-       s.append(&encoded.c_str()[4], s_size);
-       return s;
-}*/
-
 // Reads a string with the length as the first four bytes
 inline std::string deSerializeLongString(std::istream &is)
 {
@@ -2112,6 +2135,7 @@ class IntervalLimiter
        float m_accumulator;
 };
 
+std::string translatePassword(std::string playername, std::wstring password);
 
 #endif