]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/utility.h
merged CiaranG's stuff again: auto-admin powers to local user, DSTACK macro -Wformat...
[dragonfireclient.git] / src / utility.h
index 9c2099fb8e32e9f3ef9a34b97c6081844d6fa5b8..326ebf16125f002ffe95a63b19dab05925332648 100644 (file)
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <fstream>
 #include <string>
 #include <sstream>
+#include <vector>
 #include <jthread.h>
 #include <jmutex.h>
 #include <jmutexautolock.h>
@@ -731,6 +732,19 @@ inline std::string wide_to_narrow(const std::wstring& wcs)
        return *mbs;
 }
 
+// Split a string using the given delimiter. Returns a vector containing
+// the component parts.
+inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter)
+{
+       std::vector<std::wstring> parts;
+       std::wstringstream sstr(str);
+       std::wstring part;
+       while(std::getline(sstr, part, delimiter))
+               parts.push_back(part);
+       return parts;
+}
+
+
 /*
        See test.cpp for example cases.
        wraps degrees to the range of -360...360
@@ -791,6 +805,11 @@ inline s32 stoi(std::string s)
        return atoi(s.c_str());
 }
 
+inline s32 stoi(std::wstring s)
+{
+       return atoi(wide_to_narrow(s).c_str());
+}
+
 inline float stof(std::string s)
 {
        float f;
@@ -1911,7 +1930,9 @@ inline v3f intToFloat(v3s16 p, f32 d)
 // Creates a string with the length as the first two bytes
 inline std::string serializeString(const std::string &plain)
 {
-       assert(plain.size() <= 65535);
+       //assert(plain.size() <= 65535);
+       if(plain.size() > 65535)
+               throw SerializationError("String too long for serializeString");
        char buf[2];
        writeU16((u8*)&buf[0], plain.size());
        std::string s;
@@ -2001,7 +2022,8 @@ inline u32 time_to_daynight_ratio(u32 time_of_day)
        s32 d = daylength;
        s32 t = (((time_of_day)%24000)/(24000/d));
        if(t < nightlength/2 || t >= d - nightlength/2)
-               return 300;
+               //return 300;
+               return 350;
        else if(t >= d/2 - daytimelength/2 && t < d/2 + daytimelength/2)
                return 1000;
        else