mirror of
https://github.com/edubart/otclient.git
synced 2025-10-20 06:23:26 +02:00
rewrite and reoganize tools functions
* create stdext namespace which contains additional C++ algorithms * organize stdext in string, math, cast and exception utilities
This commit is contained in:
121
src/framework/stdext/cast.h
Normal file
121
src/framework/stdext/cast.h
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_CAST_H
|
||||
#define STDEXT_CAST_H
|
||||
|
||||
#include "string.h"
|
||||
#include "exception.h"
|
||||
#include "demangle.h"
|
||||
|
||||
namespace stdext {
|
||||
|
||||
// cast a type to another type
|
||||
template<typename T, typename R>
|
||||
bool cast(const T& in, R& out) {
|
||||
std::stringstream ss;
|
||||
ss << in;
|
||||
ss >> out;
|
||||
return !!ss && ss.eof();
|
||||
}
|
||||
|
||||
// cast a type to string
|
||||
template<typename T>
|
||||
bool cast(const T& in, std::string& out) {
|
||||
std::stringstream ss;
|
||||
ss << in;
|
||||
out = ss.str();
|
||||
return true;
|
||||
}
|
||||
|
||||
// cast string to string
|
||||
template<>
|
||||
inline bool cast(const std::string& in, std::string& out) {
|
||||
out = in;
|
||||
return true;
|
||||
}
|
||||
|
||||
// special cast from string to boolean
|
||||
template<>
|
||||
inline bool cast(const std::string& in, bool& b) {
|
||||
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}};
|
||||
bool ret = false;
|
||||
for(int i=0;i<4;++i) {
|
||||
if(in == validNames[0][i]) {
|
||||
b = true;
|
||||
ret = true;
|
||||
break;
|
||||
} else if(in == validNames[1][i]) {
|
||||
b = false;
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// special cast from boolean to string
|
||||
template<>
|
||||
inline bool cast(const bool& in, std::string& out) {
|
||||
out = (in ? "true" : "false");
|
||||
return true;
|
||||
}
|
||||
|
||||
// used by safe_cast
|
||||
class cast_exception : public exception {
|
||||
public:
|
||||
virtual ~cast_exception() throw() { }
|
||||
template<class T, class R>
|
||||
void update_what() {
|
||||
m_what = format("failed to cast value of type '%s' to type '%s'", demangle_type<T>(), demangle_type<R>());
|
||||
}
|
||||
virtual const char* what() { return m_what.c_str(); }
|
||||
private:
|
||||
std::string m_what;
|
||||
};
|
||||
|
||||
// cast a type to another type, any error throws a cast_exception
|
||||
template<typename R, typename T>
|
||||
R safe_cast(const T& t) {
|
||||
R r;
|
||||
if(!cast(t, r)) {
|
||||
cast_exception e;
|
||||
e.update_what<T,R>();
|
||||
throw e;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
// cast a type to another type, cast errors are ignored
|
||||
template<typename R, typename T>
|
||||
R unsafe_cast(const T& t, R def = R()) {
|
||||
try {
|
||||
return safe_cast<R,T>(t);
|
||||
} catch(cast_exception& e) {
|
||||
std::cout << "CAST ERROR: " << e.what() << std::endl;
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
59
src/framework/stdext/compiler.h
Normal file
59
src/framework/stdext/compiler.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_COMPILER_H
|
||||
#define STDEXT_COMPILER_H
|
||||
|
||||
#if !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
|
||||
#error "sorry, you need gcc 4.6 or greater to compile"
|
||||
#endif
|
||||
|
||||
#if !defined(__GXX_EXPERIMENTAL_CXX0X__)
|
||||
#error "sorry, you must enable C++0x to compile"
|
||||
#endif
|
||||
|
||||
// hack to enable std::thread on mingw32 4.6
|
||||
/*
|
||||
#if !defined(_GLIBCXX_HAS_GTHREADS) && defined(__GNUG__)
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/thread/locks.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
namespace std {
|
||||
using boost::thread;
|
||||
|
||||
using boost::mutex;
|
||||
using boost::timed_mutex;
|
||||
using boost::recursive_mutex;
|
||||
using boost::recursive_timed_mutex;
|
||||
|
||||
using boost::lock_guard;
|
||||
using boost::unique_lock;
|
||||
|
||||
using boost::condition_variable;
|
||||
using boost::condition_variable_any;
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
|
||||
#endif
|
52
src/framework/stdext/demangle.h
Normal file
52
src/framework/stdext/demangle.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_DEMANGLE_H
|
||||
#define STDEXT_DEMANGLE_H
|
||||
|
||||
#include <cxxabi.h>
|
||||
#include <string>
|
||||
|
||||
namespace stdext {
|
||||
|
||||
/// Demangle names for GNU g++ compiler
|
||||
inline std::string demangle_name(const char* name) {
|
||||
size_t len;
|
||||
int status;
|
||||
std::string ret;
|
||||
char* demangled = abi::__cxa_demangle(name, 0, &len, &status);
|
||||
if(demangled) {
|
||||
ret = demangled;
|
||||
free(demangled);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Returns the name of a type
|
||||
template<typename T>
|
||||
std::string demangle_type() {
|
||||
return demangle_name(typeid(T).name());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
57
src/framework/stdext/dumper.h
Normal file
57
src/framework/stdext/dumper.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_DUMPER_H
|
||||
#define STDEXT_DUMPER_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace stdext {
|
||||
|
||||
namespace dumper {
|
||||
|
||||
struct dumper_dummy {
|
||||
~dumper_dummy() { std::cout << std::endl; }
|
||||
template<class T>
|
||||
dumper_dummy& operator<<(const T& v) {
|
||||
std::cout << v << " ";
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
struct dumper_util {
|
||||
dumper_util() { }
|
||||
template<class T>
|
||||
dumper_dummy operator<<(const T& v) const {
|
||||
dumper_dummy d;
|
||||
d << v;
|
||||
return d;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
const static dumper::dumper_util dump;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
52
src/framework/stdext/exception.h
Normal file
52
src/framework/stdext/exception.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_EXCEPTION_H
|
||||
#define STDEXT_EXCEPTION_H
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
namespace stdext {
|
||||
|
||||
class exception : public std::exception
|
||||
{
|
||||
public:
|
||||
exception() { }
|
||||
exception(const std::string& what) : m_what(what) { }
|
||||
virtual ~exception() throw() { };
|
||||
|
||||
virtual const char* what() const throw() { return m_what.c_str(); }
|
||||
|
||||
protected:
|
||||
std::string m_what;
|
||||
};
|
||||
|
||||
/// Throws a generic exception
|
||||
template<typename... T>
|
||||
void throw_exception(const std::string& what) {
|
||||
throw exception(what);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
89
src/framework/stdext/math.h
Normal file
89
src/framework/stdext/math.h
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_MATH_H
|
||||
#define STDEXT_MATH_H
|
||||
|
||||
#include "types.h"
|
||||
#include <random>
|
||||
|
||||
namespace stdext {
|
||||
|
||||
inline uint32 generate_adler_checksum(uint8 *buffer, uint16 size) {
|
||||
register uint32 a = 1, b = 0, tlen;
|
||||
while(size > 0) {
|
||||
tlen = size > 5552 ? 5552 : size;
|
||||
size -= tlen;
|
||||
do {
|
||||
a += *buffer++;
|
||||
b += a;
|
||||
} while (--tlen);
|
||||
|
||||
a %= 65521;
|
||||
b %= 65521;
|
||||
}
|
||||
return (b << 16) | a;
|
||||
}
|
||||
|
||||
inline bool is_power_of_two(uint32 v) {
|
||||
return ((v != 0) && !(v & (v - 1)));
|
||||
}
|
||||
|
||||
inline uint32 to_power_of_two(uint32 v) {
|
||||
if(v == 0)
|
||||
return 0;
|
||||
uint32 r = 1;
|
||||
while(r < v && r != 0xffffffff)
|
||||
r <<= 1;
|
||||
return r;
|
||||
}
|
||||
|
||||
inline uint16 readLE16(uchar *addr) { return (uint16)addr[1] << 8 | addr[0]; }
|
||||
inline uint32 readLE32(uchar *addr) { return (uint32)readLE16(addr + 2) << 16 | readLE16(addr); }
|
||||
inline uint64 readLE64(uchar *addr) { return (uint64)readLE32(addr + 4) << 32 | readLE32(addr); }
|
||||
|
||||
inline void writeLE16(uchar *addr, uint16 value) { addr[1] = value >> 8; addr[0] = (uint8)value; }
|
||||
inline void writeLE32(uchar *addr, uint32 value) { writeLE16(addr + 2, value >> 16); writeLE16(addr, (uint16)value); }
|
||||
inline void writeLE64(uchar *addr, uint64 value) { writeLE16(addr + 4, value >> 32); writeLE32(addr, (uint32)value); }
|
||||
|
||||
template<typename T>
|
||||
T random_range(T min, T max);
|
||||
|
||||
template<>
|
||||
inline int random_range<int>(int min, int max) {
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
static std::uniform_int_distribution<int> dis(0, 2147483647);
|
||||
return min + (dis(gen) % (max - min + 1));
|
||||
}
|
||||
|
||||
template<>
|
||||
inline float random_range<float>(float min, float max) {
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
static std::uniform_real_distribution<float> dis(0.0, 1.0);
|
||||
return min + (max - min)*dis(gen);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
35
src/framework/stdext/stdext.h
Normal file
35
src/framework/stdext/stdext.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_H
|
||||
#define STDEXT_H
|
||||
|
||||
#include "compiler.h"
|
||||
#include "types.h"
|
||||
#include "exception.h"
|
||||
#include "demangle.h"
|
||||
#include "cast.h"
|
||||
#include "math.h"
|
||||
#include "string.h"
|
||||
#include "dumper.h"
|
||||
|
||||
#endif
|
235
src/framework/stdext/string.h
Normal file
235
src/framework/stdext/string.h
Normal file
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_STRING_H
|
||||
#define STDEXT_STRING_H
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include "types.h"
|
||||
#include "cast.h"
|
||||
#include "exception.h"
|
||||
|
||||
namespace stdext {
|
||||
|
||||
// casts declaration, definition will be included at the end of the file
|
||||
template<typename R, typename T> R safe_cast(const T& t);
|
||||
template<typename R, typename T> R unsafe_cast(const T& t, R def = R());
|
||||
|
||||
/// Convert any type to std::string
|
||||
template<typename T>
|
||||
std::string to_string(const T& t) { return unsafe_cast<std::string, T>(t); }
|
||||
|
||||
/// Convert any type from std::string
|
||||
template<typename T>
|
||||
T from_string(const std::string& str, T def = T()) { return unsafe_cast<T, std::string>(str, def); }
|
||||
|
||||
/// Cast non-class types like int, char, float, double and pointers
|
||||
template<typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value || std::is_pointer<T>::value || std::is_floating_point<T>::value, T>::type sprintf_cast(const T& t) { return t; }
|
||||
|
||||
/// Cast any class or struct convertible to std::string
|
||||
inline const char *sprintf_cast(const std::string& s) { return s.c_str(); }
|
||||
|
||||
template<int N>
|
||||
struct expand_snprintf{
|
||||
template<typename Tuple, typename... Args>
|
||||
static int call(char *s, size_t maxlen, const char *format, const Tuple& tuple, const Args&... args) {
|
||||
return expand_snprintf<N-1>::call(s, maxlen, format, tuple, sprintf_cast(std::get<N-1>(tuple)), args...);
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct expand_snprintf<0> {
|
||||
template<typename Tuple, typename... Args>
|
||||
static int call(char *s, size_t maxlen, const char *format, const Tuple& tuple, const Args&... args) {
|
||||
return snprintf(s, maxlen, format, args...);
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct replace_extent { typedef T type; };
|
||||
|
||||
template<class T>
|
||||
struct replace_extent<T[]> { typedef const T* type; };
|
||||
|
||||
template<class T, std::size_t N>
|
||||
struct replace_extent<T[N]> { typedef const T* type;};
|
||||
|
||||
/// Improved sprintf that accepts std::string and other types
|
||||
template<typename... Args>
|
||||
int snprintf(char *s, size_t maxlen, const char *format, const Args&... args) {
|
||||
typedef typename std::tuple<typename replace_extent<Args>::type...> Tuple;
|
||||
enum { N = std::tuple_size<Tuple>::value };
|
||||
Tuple tuple(args...);
|
||||
return expand_snprintf<N>::call(s, maxlen, format, tuple);
|
||||
}
|
||||
|
||||
/// Format strings with the sprintf style, accepting std::string and string convertible types for %s
|
||||
template<typename... Args>
|
||||
std::string format(const std::string& format, const Args&... args) {
|
||||
int n, size = 1024;
|
||||
std::string str;
|
||||
while(true) {
|
||||
str.resize(size);
|
||||
n = snprintf(&str[0], size, format.c_str(), args...);
|
||||
assert(n != -1);
|
||||
if(n < size) {
|
||||
str.resize(n);
|
||||
return str;
|
||||
}
|
||||
size *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
inline void fill_ostream(std::ostringstream&) { }
|
||||
|
||||
/// Fills an ostream by concatenating args
|
||||
template<class T, class... Args>
|
||||
void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) {
|
||||
stream << first;
|
||||
fill_ostream(stream, rest...);
|
||||
}
|
||||
|
||||
/// Makes a std::string by concatenating args
|
||||
template<class... T>
|
||||
std::string mkstr(const T&... args) {
|
||||
std::ostringstream buf;
|
||||
fill_ostream(buf, args...);
|
||||
return buf.str();
|
||||
}
|
||||
|
||||
/// Easy of use split
|
||||
template<typename T = std::string>
|
||||
std::vector<T> split(const std::string& str, const std::string& separators = " ") {
|
||||
std::vector<std::string> splitted;
|
||||
boost::split(splitted, str, boost::is_any_of(std::string(separators)));
|
||||
std::vector<T> results(splitted.size());
|
||||
for(uint i=0;i<splitted.size();++i)
|
||||
results[i] = safe_cast<T>(splitted[i]);
|
||||
return results;
|
||||
}
|
||||
|
||||
/// Resolve a file path by combining sourcePath with filePath
|
||||
inline std::string resolve_path(const std::string& filePath, std::string sourcePath) {
|
||||
if(boost::starts_with(filePath, "/"))
|
||||
return filePath;
|
||||
if(!boost::ends_with(sourcePath, "/")) {
|
||||
std::size_t slashPos = sourcePath.find_last_of("/");
|
||||
if(slashPos == std::string::npos)
|
||||
throw_exception(format("invalid source path '%s', for file '%s'", sourcePath, filePath));
|
||||
sourcePath = sourcePath.substr(0, slashPos + 1);
|
||||
}
|
||||
return sourcePath + filePath;
|
||||
}
|
||||
|
||||
/// Get current date and time in a std::string
|
||||
inline std::string date_time_string() {
|
||||
char date[32];
|
||||
std::time_t tnow;
|
||||
std::time(&tnow);
|
||||
std::tm *ts = std::localtime(&tnow);
|
||||
std::strftime(date, 32, "%b %d %Y %H:%M:%S", ts);
|
||||
return std::string(date);
|
||||
}
|
||||
|
||||
/// Convert decimal to hexadecimal
|
||||
inline std::string dec_to_hex(unsigned int num) {
|
||||
std::string str;
|
||||
std::ostringstream o;
|
||||
o << std::hex << num;
|
||||
str = o.str();
|
||||
return str;
|
||||
}
|
||||
|
||||
/// Convert hexadecimal to decimal
|
||||
inline unsigned int hex_to_dec(const std::string& str) {
|
||||
unsigned int num;
|
||||
std::istringstream i(str);
|
||||
i >> std::hex >> num;
|
||||
return num;
|
||||
}
|
||||
|
||||
/// Convert ip to string
|
||||
inline std::string ip_to_string(uint32 ip) {
|
||||
char host[16];
|
||||
sprintf(host, "%d.%d.%d.%d", (uint8)ip, (uint8)(ip >> 8), (uint8)(ip >> 16), (uint8)(ip >> 24));
|
||||
return std::string(host);
|
||||
}
|
||||
|
||||
/// Convert utf8 characters to latin1
|
||||
inline char utf8CharToLatin1(uchar *utf8, int *read) {
|
||||
char c = '?';
|
||||
uchar opt1 = utf8[0];
|
||||
*read = 1;
|
||||
if(opt1 == 0xc3) {
|
||||
*read = 2;
|
||||
uchar opt2 = utf8[1];
|
||||
c = 64 + opt2;
|
||||
} else if(opt1 == 0xc2) {
|
||||
*read = 2;
|
||||
uchar opt2 = utf8[1];
|
||||
if(opt2 > 0xa1 && opt2 < 0xbb)
|
||||
c = opt2;
|
||||
} else if(opt1 < 0xc2) {
|
||||
c = opt1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/// Convert utf8 strings to latin1
|
||||
inline std::string utf8StringToLatin1(uchar *utf8) {
|
||||
std::string out;
|
||||
int len = strlen((char*)utf8);
|
||||
for(int i=0; i<len;) {
|
||||
int read = 0;
|
||||
uchar *utf8char = &utf8[i];
|
||||
out += utf8CharToLatin1(utf8char, &read);
|
||||
i += read;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// utility for printing messages into stdout
|
||||
template<class... T>
|
||||
void print(const T&... args) {
|
||||
std::ostringstream buf;
|
||||
fill_ostream(buf, args...);
|
||||
std::cout << buf.str();
|
||||
}
|
||||
|
||||
template<class... T>
|
||||
void println(const T&... args) {
|
||||
print(args...);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#include "cast.h"
|
||||
|
||||
#endif
|
46
src/framework/stdext/types.h
Normal file
46
src/framework/stdext/types.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef STDEXT_TYPES_H
|
||||
#define STDEXT_TYPES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// easy handwriting types
|
||||
typedef unsigned char uchar;
|
||||
typedef unsigned short ushort;
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned long ulong;
|
||||
typedef uint64_t uint64;
|
||||
typedef uint32_t uint32;
|
||||
typedef uint16_t uint16;
|
||||
typedef uint8_t uint8;
|
||||
typedef int64_t int64;
|
||||
typedef int32_t int32;
|
||||
typedef int16_t int16;
|
||||
typedef int8_t int8;
|
||||
|
||||
// note that on 32 bit platforms the max ticks will overflow for values above 2,147,483,647
|
||||
// thus this means that the app may cause unknown behavior after running 24 days without restarting
|
||||
typedef long ticks_t;
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user