#ifndef __TOOLS_H__
#define __TOOLS_H__
#include <wx/mstream.h>
#include <wx/string.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/icon.h>
#include <time.h>
#include <string>
#include <sstream>
#include <stdexcept>
#include <zlib.h>
#ifndef wxTHICK_FRAME
#define wxTHICK_FRAME wxRESIZE_BORDER
#endif
#define wxBitmapFromMemory(name) wxBitmapFromMemory2(name, sizeof(name))
static inline wxBitmap wxBitmapFromMemory2(const char *data, int len) {
wxMemoryInputStream is(data, len);
return wxBitmap(wxImage(is, wxBITMAP_TYPE_PNG, -1), -1);
}
#define wxIconFromMemory(name) wxIconFromMemory2(name, sizeof(name))
static inline wxIcon wxIconFromMemory2(const char *data, int len) {
wxIcon icon;
icon.CopyFromBitmap( wxBitmapFromMemory2(data, len) );
return icon;
}
static inline wxString strSTL2WX(const std::string& str) {
return wxString(str.data(), wxConvUTF8, str.size());
}
static inline std::string strWX2STL(const wxString& str) {
#if wxUSE_UNICODE
size_t outlen;
const wxCharBuffer cbuf = wxConvUTF8.cWC2MB(str.GetData(), str.Length(), &outlen);
return std::string(cbuf.data(), outlen);
#else
return std::string(str.GetData(), str.Length());
#endif
}
template <typename T>
static inline std::string toSTLString(const T& value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}
static inline std::string strTimeStampNow() {
time_t timenow = time(NULL);
return std::string((char*)&timenow, sizeof(timenow));
}
static inline std::string decompress(const char* str, unsigned int slen, unsigned int possiblelen=0)
{
z_stream zs;
memset(&zs, 0, sizeof(zs));
if (inflateInit(&zs) != Z_OK)
throw(std::runtime_error("inflateInit failed while decompressing."));
zs.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(str));
zs.avail_in = slen;
int ret;
char outbuffer[32768];
std::string outstring;
outstring.reserve(possiblelen);
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
if (ret != Z_STREAM_END) {
std::ostringstream oss;
oss << "Exception during zlib uncompression: (" << ret << ") "
<< zs.msg;
throw(std::runtime_error(oss.str()));
}
return outstring;
}
#endif