|
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 |
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
#pragma warning(disable : 4996)
|
|
Toshihiro Shimizu |
890ddd |
#include "windows.h"
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9eb50d |
#include <sstream></sstream>
|
|
Shinya Kitaoka |
9eb50d |
|
|
Toshihiro Shimizu |
890ddd |
class TStringConvertException : public TException
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Shinya Kitaoka |
3bfa54 |
std::string m_string;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
3bfa54 |
TStringConvertException(const std::string str) : m_string(str) {}
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9eb50d |
std::wstring to_wstring(std::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
|
|
Shinya Kitaoka |
3bfa54 |
if (qString != testString && std::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 |
|
|
Shinya Kitaoka |
9eb50d |
std::string to_string(std::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
|
|
Shinya Kitaoka |
9eb50d |
QString const qString = QString::fromStdWString(ws);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9eb50d |
// Test if 'ws' is not unicode (UTF-8)
|
|
Toshihiro Shimizu |
890ddd |
if (qString.toLatin1() == qString)
|
|
Shinya Kitaoka |
9eb50d |
return qString.toStdString();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9eb50d |
return std::string(qString.toUtf8());
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9eb50d |
std::string to_string(const TFilePath &fp)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Shinya Kitaoka |
9eb50d |
return ::to_string(fp.getWideString());
|
|
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 |
|
|
Shinya Kitaoka |
9eb50d |
std::string to_string(double value, int prec)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Shinya Kitaoka |
9eb50d |
if (prec < 0) {
|
|
Shinya Kitaoka |
9eb50d |
return std::to_string(value);
|
|
Shinya Kitaoka |
9eb50d |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9eb50d |
std::ostringstream out;
|
|
Shinya Kitaoka |
9eb50d |
out.setf(std::ios_base::fixed, std::ios_base::floatfield);
|
|
Shinya Kitaoka |
9eb50d |
out.precision(prec);
|
|
Shinya Kitaoka |
9eb50d |
out << value;
|
|
Shinya Kitaoka |
9eb50d |
return out.str();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9eb50d |
std::string to_string(void* p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Shinya Kitaoka |
9eb50d |
std::ostringstream out;
|
|
Shinya Kitaoka |
9eb50d |
out << p;
|
|
Shinya Kitaoka |
9eb50d |
return out.str();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
3bfa54 |
bool isInt(std::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 |
|
|
Shinya Kitaoka |
3bfa54 |
bool isDouble(std::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 |
|
|
Shinya Kitaoka |
9eb50d |
bool isInt(std::wstring s) { return isInt(::to_string(s)); }
|
|
Shinya Kitaoka |
9eb50d |
bool isDouble(std::wstring s) { return isDouble(::to_string(s)); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
3bfa54 |
std::string toUpper(std::string a)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
return _strupr(const_cast<char *="">(a.c_str()));</char>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
3bfa54 |
std::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 |
|
|
Shinya Kitaoka |
3bfa54 |
std::string toLower(std::string a)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
return _strlwr(const_cast<char *="">(a.c_str()));</char>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
3bfa54 |
std::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 |
|
|
Shinya Kitaoka |
3bfa54 |
std::wstring toUpper(std::wstring a)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
return _wcsupr(const_cast<wchar_t *="">(a.c_str()));</wchar_t>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
3bfa54 |
std::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 |
|
|
Shinya Kitaoka |
3bfa54 |
std::wstring toLower(std::wstring a)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
return _wcslwr(const_cast<wchar_t *="">(a.c_str()));</wchar_t>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
3bfa54 |
std::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 |
}
|