]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/util/string.h
Add capability to read table flag fields from Lua API
[dragonfireclient.git] / src / util / string.h
index d081b365b42fd837846dedfaa84208c28d32a6a5..9bb89f14aaac6d91894ae245d991bd19741b023b 100644 (file)
@@ -1,6 +1,6 @@
 /*
-Minetest-c55
-Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
@@ -21,13 +21,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #define UTIL_STRING_HEADER
 
 #include "../irrlichttypes.h"
-#include "../strfnd.h" // For trim()
-#include "pointer.h"
+#include <stdlib.h>
 #include <string>
 #include <cstring>
 #include <vector>
 #include <sstream>
 
+struct FlagDesc {
+       const char *name;
+       u32 flag;
+};
+
+std::wstring narrow_to_wide(const std::string& mbs);
+std::string wide_to_narrow(const std::wstring& wcs);
+
 static inline std::string padStringRight(std::string s, size_t len)
 {
        if(len > s.size())
@@ -90,29 +97,6 @@ inline bool str_starts_with(const std::wstring& str, const std::wstring& prefix,
        return true;
 }
 
-inline std::wstring narrow_to_wide(const std::string& mbs)
-{
-       size_t wcl = mbs.size();
-       Buffer<wchar_t> wcs(wcl+1);
-       size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
-       if(l == (size_t)(-1))
-               return L"<invalid multibyte string>";
-       wcs[l] = 0;
-       return *wcs;
-}
-
-inline std::string wide_to_narrow(const std::wstring& wcs)
-{
-       size_t mbl = wcs.size()*4;
-       SharedBuffer<char> mbs(mbl+1);
-       size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
-       if(l == (size_t)(-1))
-               mbs[0] = 0;
-       else
-               mbs[l] = 0;
-       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)
@@ -138,10 +122,33 @@ inline std::string lowercase(const std::string &s)
        return s2;
 }
 
+inline std::string trim(const std::string &s)
+{
+       size_t front = 0;
+       while(s[front] == ' '    ||
+             s[front] == '\t'   ||
+             s[front] == '\r'   ||
+             s[front] == '\n'
+            )
+               ++front;
+
+       size_t back = s.size();
+       while(back > front &&
+             (s[back-1] == ' '  ||
+              s[back-1] == '\t' ||
+              s[back-1] == '\r' ||
+              s[back-1] == '\n'
+             )
+            )
+               --back;
+
+       return s.substr(front, back - front);
+}
+
 inline bool is_yes(const std::string &s)
 {
        std::string s2 = lowercase(trim(s));
-       if(s2 == "y" || s2 == "yes" || s2 == "true" || s2 == "1")
+       if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0)
                return true;
        return false;
 }
@@ -156,6 +163,12 @@ inline s32 mystoi(const std::string &s, s32 min, s32 max)
        return i;
 }
 
+inline s64 stoi64(const std::string &s) {
+       std::stringstream tmp(s);
+       long long t;
+       tmp >> t;
+       return t;
+}
 
 // MSVC2010 includes it's own versions of these
 //#if !defined(_MSC_VER) || _MSC_VER < 1600
@@ -193,6 +206,12 @@ inline std::string itos(s32 i)
        return o.str();
 }
 
+inline std::string i64tos(s64 i) {
+       std::ostringstream o;
+       o<<i;
+       return o.str();
+}
+
 inline std::string ftos(float f)
 {
        std::ostringstream o;
@@ -281,9 +300,30 @@ inline std::string wrap_rows(const std::string &from, u32 rowlen)
        return to;
 }
 
+/*
+       Removes all \\ from a string that had been escaped (FormSpec strings)
+*/
+inline std::string unescape_string(std::string &s)
+{
+       std::string res;
+       
+       for (size_t i = 0; i < s.length(); i++) {
+               if (s[i] == '\\')
+                       i++;
+               res += s[i];
+       }
+       
+       return res;
+}
+
 std::string translatePassword(std::string playername, std::wstring password);
-size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata);
+std::string urlencode(std::string str);
+std::string urldecode(std::string str);
+u32 readFlagString(std::string str, FlagDesc *flagdesc, u32 *flagmask);
+std::string writeFlagString(u32 flags, FlagDesc *flagdesc, u32 flagmask);
+size_t mystrlcpy(char *dst, const char *src, size_t size);
 char *mystrtok_r(char *s, const char *sep, char **lasts);
+u64 read_seed(const char *str);
 
 #endif