From 4e1f50035e860a00636ca5d804c267119df99601 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Sun, 11 Aug 2013 04:09:45 +0200 Subject: Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu --- src/util/container.h | 2 + src/util/numeric.h | 1 - src/util/pointedthing.cpp | 1 + src/util/serialize.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++ src/util/serialize.h | 99 +++----------------------------------------- src/util/string.cpp | 24 +++++++++++ src/util/string.h | 52 +++++++++++------------ 7 files changed, 162 insertions(+), 119 deletions(-) (limited to 'src/util') 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 #include #include "../porting.h" // For sleep_ms #include #include +#include /* 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 #include // 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 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 #include +// 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 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 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 #include -#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 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 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 wcs(wcl+1); + size_t l = mbstowcs(*wcs, mbs.c_str(), wcl); + if(l == (size_t)(-1)) + return L""; + wcs[l] = 0; + return *wcs; +} + +std::string wide_to_narrow(const std::wstring& wcs) +{ + size_t mbl = wcs.size()*4; + SharedBuffer 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 #include #include #include @@ -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 wcs(wcl+1); - size_t l = mbstowcs(*wcs, mbs.c_str(), wcl); - if(l == (size_t)(-1)) - return L""; - wcs[l] = 0; - return *wcs; -} - -inline std::string wide_to_narrow(const std::wstring& wcs) -{ - size_t mbl = wcs.size()*4; - SharedBuffer 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 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)); -- cgit v1.2.3