|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "tconvert.h"
|
|
Toshihiro Shimizu |
890ddd |
//#include "texception.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tfilepath.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifndef TNZCORE_LIGHT
|
|
Toshihiro Shimizu |
890ddd |
#include <qstring></qstring>
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
#pragma warning(disable : 4996)
|
|
Toshihiro Shimizu |
890ddd |
#include "windows.h"
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
class TStringConvertException : public TException
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
string m_string;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
TStringConvertException(const string str) : m_string(str) {}
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
wstring toWideString(string s)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef TNZCORE_LIGHT
|
|
Toshihiro Shimizu |
890ddd |
std::wstring ws;
|
|
Toshihiro Shimizu |
890ddd |
ws.assign(s.begin(), s.end());
|
|
Toshihiro Shimizu |
890ddd |
return ws;
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
QString testString = QString::fromStdString(s);
|
|
Toshihiro Shimizu |
890ddd |
QString qString = QString::fromUtf8(s.c_str());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// To detect if 's' is UTF-8 encoded or not
|
|
Toshihiro Shimizu |
890ddd |
if (qString != testString && string(qString.toUtf8()) == s)
|
|
Toshihiro Shimizu |
890ddd |
return qString.toStdWString();
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
return testString.toStdWString();
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toString(wstring ws)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef TNZCORE_LIGHT
|
|
Toshihiro Shimizu |
890ddd |
std::string s;
|
|
Toshihiro Shimizu |
890ddd |
s.assign(ws.begin(), ws.end());
|
|
Toshihiro Shimizu |
890ddd |
return s;
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
QString qString = QString::fromStdWString(ws);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Test if 'ws' is not unicode (UTF-8)
|
|
Toshihiro Shimizu |
890ddd |
#if 0
|
|
Toshihiro Shimizu |
890ddd |
if(qString.toAscii() == qString)
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
if (qString.toLatin1() == qString)
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
return qString.toStdString();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
QByteArray a = qString.toUtf8();
|
|
Toshihiro Shimizu |
890ddd |
return string(a);
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toString(const TFilePath &fp)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return toString(fp.getWideString());
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
wstring toWideString(int x)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return toWideString(toString(x));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toString(int value)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
ostrstream ss;
|
|
Toshihiro Shimizu |
890ddd |
ss << value << '\0';
|
|
Toshihiro Shimizu |
890ddd |
string s = ss.str();
|
|
Toshihiro Shimizu |
890ddd |
ss.freeze(false);
|
|
Toshihiro Shimizu |
890ddd |
return s;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toString(unsigned long value)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
ostrstream ss;
|
|
Toshihiro Shimizu |
890ddd |
ss << value << '\0';
|
|
Toshihiro Shimizu |
890ddd |
string s = ss.str();
|
|
Toshihiro Shimizu |
890ddd |
ss.freeze(false);
|
|
Toshihiro Shimizu |
890ddd |
return s;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toString(unsigned long long value)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
ostrstream ss;
|
|
Toshihiro Shimizu |
890ddd |
ss << value << '\0';
|
|
Toshihiro Shimizu |
890ddd |
string s = ss.str();
|
|
Toshihiro Shimizu |
890ddd |
ss.freeze(false);
|
|
Toshihiro Shimizu |
890ddd |
return s;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
/*!
|
|
Toshihiro Shimizu |
890ddd |
The default precision is six decimal places. If the
|
|
Toshihiro Shimizu |
890ddd |
precision is less than of the decimal places in the fractonal
|
|
Toshihiro Shimizu |
890ddd |
part, the remainder is not cut off but rounded.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toString(double value, int prec)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
ostrstream ss;
|
|
Toshihiro Shimizu |
890ddd |
ss.setf(std::ios_base::fixed, std::ios_base::floatfield);
|
|
Toshihiro Shimizu |
890ddd |
if (prec >= 0)
|
|
Toshihiro Shimizu |
890ddd |
ss.precision(prec);
|
|
Toshihiro Shimizu |
890ddd |
ss << value << '\0';
|
|
Toshihiro Shimizu |
890ddd |
string s = ss.str();
|
|
Toshihiro Shimizu |
890ddd |
ss.freeze(0);
|
|
Toshihiro Shimizu |
890ddd |
return s;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toString(void *p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
ostrstream ss;
|
|
Toshihiro Shimizu |
890ddd |
ss << p << '\0';
|
|
Toshihiro Shimizu |
890ddd |
string s = ss.str();
|
|
Toshihiro Shimizu |
890ddd |
ss.freeze(false);
|
|
Toshihiro Shimizu |
890ddd |
return s;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int toInt(string str)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
int value = 0;
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 0; i < (int)str.size(); i++)
|
|
Toshihiro Shimizu |
890ddd |
value = value * 10 + str[i] - '0';
|
|
Toshihiro Shimizu |
890ddd |
return value;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int toInt(wstring str)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return toInt(toString(str));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool isInt(string s)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
int i = 0, len = (int)s.size();
|
|
Toshihiro Shimizu |
890ddd |
if (len == 0)
|
|
Toshihiro Shimizu |
890ddd |
return false;
|
|
Toshihiro Shimizu |
890ddd |
if (s[0] == '-') {
|
|
Toshihiro Shimizu |
890ddd |
if (len == 1)
|
|
Toshihiro Shimizu |
890ddd |
return false;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
i++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
while (i < len) {
|
|
Toshihiro Shimizu |
890ddd |
if (s[i] < '0' || s[i] > '9')
|
|
Toshihiro Shimizu |
890ddd |
return false;
|
|
Toshihiro Shimizu |
890ddd |
i++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
return true;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool isDouble(string s)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
int i = 0, len = (int)s.size();
|
|
Toshihiro Shimizu |
890ddd |
if (len == 0)
|
|
Toshihiro Shimizu |
890ddd |
return false;
|
|
Toshihiro Shimizu |
890ddd |
if (i < len && s[i] == '-')
|
|
Toshihiro Shimizu |
890ddd |
i++;
|
|
Toshihiro Shimizu |
890ddd |
while (i < len && s[i] != '.') {
|
|
Toshihiro Shimizu |
890ddd |
if (s[i] < '0' || s[i] > '9')
|
|
Toshihiro Shimizu |
890ddd |
return false;
|
|
Toshihiro Shimizu |
890ddd |
i++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
if (i >= len)
|
|
Toshihiro Shimizu |
890ddd |
return true;
|
|
Toshihiro Shimizu |
890ddd |
i++;
|
|
Toshihiro Shimizu |
890ddd |
while (i < len) {
|
|
Toshihiro Shimizu |
890ddd |
if (s[i] < '0' || s[i] > '9')
|
|
Toshihiro Shimizu |
890ddd |
return false;
|
|
Toshihiro Shimizu |
890ddd |
i++;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
return true;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool isInt(wstring s) { return isInt(toString(s)); }
|
|
Toshihiro Shimizu |
890ddd |
bool isDouble(wstring s) { return isDouble(toString(s)); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double toDouble(string str)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
double value;
|
|
Toshihiro Shimizu |
890ddd |
istrstream ss(str.c_str(), (std::streamsize)str.length());
|
|
Toshihiro Shimizu |
890ddd |
ss >> value;
|
|
Toshihiro Shimizu |
890ddd |
return value;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double toDouble(wstring str)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return toDouble(toString(str));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
wstring toWideString(double v, int p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return toWideString(toString(v, p));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toUpper(string a)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
return _strupr(const_cast<char *="">(a.c_str()));</char>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
string ret = a;
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 0; i < (int)ret.length(); i++)
|
|
Toshihiro Shimizu |
890ddd |
ret[i] = toupper(ret[i]);
|
|
Toshihiro Shimizu |
890ddd |
return ret;
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
string toLower(string a)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
return _strlwr(const_cast<char *="">(a.c_str()));</char>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
string ret = a;
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 0; i < (int)ret.length(); i++)
|
|
Toshihiro Shimizu |
890ddd |
ret[i] = tolower(ret[i]);
|
|
Toshihiro Shimizu |
890ddd |
return ret;
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
wstring toUpper(wstring a)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
return _wcsupr(const_cast<wchar_t *="">(a.c_str()));</wchar_t>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
wstring ret;
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 0; i < (int)a.length(); i++) {
|
|
Toshihiro Shimizu |
890ddd |
wchar_t c = towupper(a[i]);
|
|
Toshihiro Shimizu |
890ddd |
ret += c;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
return ret;
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
wstring toLower(wstring a)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
#ifdef WIN32
|
|
Toshihiro Shimizu |
890ddd |
return _wcslwr(const_cast<wchar_t *="">(a.c_str()));</wchar_t>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
wstring ret;
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 0; i < (int)a.length(); i++) {
|
|
Toshihiro Shimizu |
890ddd |
wchar_t c = towlower(a[i]);
|
|
Toshihiro Shimizu |
890ddd |
ret += c;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
return ret;
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|