summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/container.h2
-rw-r--r--src/util/numeric.h1
-rw-r--r--src/util/pointedthing.cpp1
-rw-r--r--src/util/serialize.cpp102
-rw-r--r--src/util/serialize.h99
-rw-r--r--src/util/string.cpp24
-rw-r--r--src/util/string.h52
7 files changed, 162 insertions, 119 deletions
diff --git a/src/util/container.h b/src/util/container.h
index 84616d2db..d5854909a 100644
--- a/src/util/container.h
+++ b/src/util/container.h
@@ -21,11 +21,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define UTIL_CONTAINER_HEADER
#include "../irrlichttypes.h"
+#include "../exceptions.h"
#include <jmutex.h>
#include <jmutexautolock.h>
#include "../porting.h" // For sleep_ms
#include <list>
#include <vector>
+#include <map>
/*
Queue with unique values with fast checking of value existence
diff --git a/src/util/numeric.h b/src/util/numeric.h
index 076a08efc..b96c94faa 100644
--- a/src/util/numeric.h
+++ b/src/util/numeric.h
@@ -24,7 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "../irr_v2d.h"
#include "../irr_v3d.h"
#include "../irr_aabb3d.h"
-#include <irrList.h>
#include <list>
// Calculate the borders of a "d-radius" cube
diff --git a/src/util/pointedthing.cpp b/src/util/pointedthing.cpp
index dc39e313a..cd13000b5 100644
--- a/src/util/pointedthing.cpp
+++ b/src/util/pointedthing.cpp
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "pointedthing.h"
#include "serialize.h"
+#include "../exceptions.h"
#include <sstream>
PointedThing::PointedThing():
diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp
index f32813551..d6be5c487 100644
--- a/src/util/serialize.cpp
+++ b/src/util/serialize.cpp
@@ -18,10 +18,112 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "serialize.h"
+#include "pointer.h"
+#include "../exceptions.h"
#include <sstream>
#include <iomanip>
+// Creates a string with the length as the first two bytes
+std::string serializeString(const std::string &plain)
+{
+ //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;
+ s.append(buf, 2);
+ s.append(plain);
+ return s;
+}
+
+// Creates a string with the length as the first two bytes from wide string
+std::string serializeWideString(const std::wstring &plain)
+{
+ //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.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
+std::string deSerializeString(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 "";
+ Buffer<char> buf2(s_size);
+ is.read(&buf2[0], s_size);
+ std::string s;
+ s.reserve(s_size);
+ s.append(&buf2[0], s_size);
+ return s;
+}
+
+// Reads a wide string with the length as the first two bytes
+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
+std::string serializeLongString(const std::string &plain)
+{
+ char buf[4];
+ writeU32((u8*)&buf[0], plain.size());
+ std::string s;
+ s.append(buf, 4);
+ s.append(plain);
+ return s;
+}
+
+// Reads a string with the length as the first four bytes
+std::string deSerializeLongString(std::istream &is)
+{
+ char buf[4];
+ is.read(buf, 4);
+ if(is.gcount() != 4)
+ throw SerializationError("deSerializeLongString: size not read");
+ u32 s_size = readU32((u8*)buf);
+ if(s_size == 0)
+ return "";
+ Buffer<char> buf2(s_size);
+ is.read(&buf2[0], s_size);
+ std::string s;
+ s.reserve(s_size);
+ s.append(&buf2[0], s_size);
+ return s;
+}
+
// Creates a string encoded in JSON format (almost equivalent to a C string literal)
std::string serializeJsonString(const std::string &plain)
{
diff --git a/src/util/serialize.h b/src/util/serialize.h
index bb44c7f96..7a37cd70f 100644
--- a/src/util/serialize.h
+++ b/src/util/serialize.h
@@ -20,14 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef UTIL_SERIALIZE_HEADER
#define UTIL_SERIALIZE_HEADER
-#include "../irrlichttypes.h"
#include "../irrlichttypes_bloated.h"
-#include "../irr_v2d.h"
-#include "../irr_v3d.h"
#include <iostream>
#include <string>
-#include "../exceptions.h"
-#include "pointer.h"
inline void writeU64(u8 *data, u64 i)
{
@@ -383,104 +378,22 @@ inline video::SColor readARGB8(std::istream &is)
*/
// Creates a string with the length as the first two bytes
-inline std::string serializeString(const std::string &plain)
-{
- //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;
- s.append(buf, 2);
- s.append(plain);
- return s;
-}
+std::string serializeString(const std::string &plain);
// Creates a string with the length as the first two bytes from wide string
-inline std::string serializeWideString(const std::wstring &plain)
-{
- //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.append(buf, 2);
- for(u32 i=0; i<plain.size(); i++)
- {
- writeU16((u8*)buf, plain[i]);
- s.append(buf, 2);
- }
- return s;
-}
+std::string serializeWideString(const std::wstring &plain);
// Reads a string with the length as the first two bytes
-inline std::string deSerializeString(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 "";
- Buffer<char> buf2(s_size);
- is.read(&buf2[0], s_size);
- std::string s;
- s.reserve(s_size);
- s.append(&buf2[0], s_size);
- return s;
-}
+std::string deSerializeString(std::istream &is);
// 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;
-}
+std::wstring deSerializeWideString(std::istream &is);
// Creates a string with the length as the first four bytes
-inline std::string serializeLongString(const std::string &plain)
-{
- char buf[4];
- writeU32((u8*)&buf[0], plain.size());
- std::string s;
- s.append(buf, 4);
- s.append(plain);
- return s;
-}
+std::string serializeLongString(const std::string &plain);
// Reads a string with the length as the first four bytes
-inline std::string deSerializeLongString(std::istream &is)
-{
- char buf[4];
- is.read(buf, 4);
- if(is.gcount() != 4)
- throw SerializationError("deSerializeLongString: size not read");
- u32 s_size = readU32((u8*)buf);
- if(s_size == 0)
- return "";
- Buffer<char> buf2(s_size);
- is.read(&buf2[0], s_size);
- std::string s;
- s.reserve(s_size);
- s.append(&buf2[0], s_size);
- return s;
-}
+std::string deSerializeLongString(std::istream &is);
// Creates a string encoded in JSON format (almost equivalent to a C string literal)
std::string serializeJsonString(const std::string &plain);
diff --git a/src/util/string.cpp b/src/util/string.cpp
index 3bd8b7364..2c1dea497 100644
--- a/src/util/string.cpp
+++ b/src/util/string.cpp
@@ -18,11 +18,35 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "string.h"
+#include "pointer.h"
#include "../sha1.h"
#include "../base64.h"
#include "../porting.h"
+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;
+}
+
+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;
+}
+
// Get an sha-1 hash of the player's name combined with
// the password entered. That's what the server uses as
// their password. (Exception : if the password field is
diff --git a/src/util/string.h b/src/util/string.h
index d8cedc3e8..1cb4ae8d8 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -21,8 +21,7 @@ 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>
@@ -33,6 +32,9 @@ struct FlagDesc {
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())
@@ -95,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)
@@ -143,6 +122,29 @@ 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));