]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/utility.h
(hopefully) fixed stoi/stof compile problems on vc2010
[dragonfireclient.git] / src / utility.h
index 19946354c1eb6fcb280dde7cd12776e310832bf5..28732a099f3a8cc77b6361530f4cde4ebe6d7d94 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>
@@ -215,8 +216,8 @@ inline void writeU16(std::ostream &os, u16 p)
 }
 inline u16 readU16(std::istream &is)
 {
-       char buf[12];
-       is.read(buf, 12);
+       char buf[2];
+       is.read(buf, 2);
        return readU16((u8*)buf);
 }
 
@@ -228,8 +229,8 @@ inline void writeF1000(std::ostream &os, f32 p)
 }
 inline f32 readF1000(std::istream &is)
 {
-       char buf[12];
-       is.read(buf, 12);
+       char buf[2];
+       is.read(buf, 2);
        return readF1000((u8*)buf);
 }
 
@@ -496,8 +497,6 @@ class MutexedVariable
        TimeTaker
 */
 
-class IrrlichtWrapper;
-
 class TimeTaker
 {
 public:
@@ -733,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
@@ -788,6 +800,10 @@ inline s32 stoi(const std::string &s, s32 min, s32 max)
        return i;
 }
 
+
+// MSVC2010 includes it's own versions of these
+#if !defined(_MSC_VER) || _MSC_VER < 1600
+
 inline s32 stoi(std::string s)
 {
        return atoi(s.c_str());
@@ -801,6 +817,13 @@ inline float stof(std::string s)
        return f;
 }
 
+#endif
+
+inline s32 stoi(std::wstring s)
+{
+       return atoi(wide_to_narrow(s).c_str());
+}
+
 inline std::string itos(s32 i)
 {
        std::ostringstream o;
@@ -1331,6 +1354,14 @@ class Settings
                return value;
        }
 
+       void setBool(std::string name, bool value)
+       {
+               if(value)
+                       set(name, "true");
+               else
+                       set(name, "false");
+       }
+
        void setS32(std::string name, s32 value)
        {
                set(name, itos(value));
@@ -1903,9 +1934,11 @@ 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)
+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;
@@ -1945,7 +1978,7 @@ inline std::string deSerializeString(std::istream &is)
 }
 
 // Creates a string with the length as the first four bytes
-inline std::string serializeLongString(const std::string plain)
+inline std::string serializeLongString(const std::string &plain)
 {
        char buf[4];
        writeU32((u8*)&buf[0], plain.size());
@@ -1995,7 +2028,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
@@ -2033,12 +2067,9 @@ class IntervalLimiter
        {
                m_accumulator += dtime;
                if(m_accumulator < wanted_interval)
-               {
-                       dtime = 0;
-                       return true;
-               }
+                       return false;
                m_accumulator -= wanted_interval;
-               return false;
+               return true;
        }
 protected:
        float m_accumulator;