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
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 3bfa54
std::wstring toWideString(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 3bfa54
std::string toString(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
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();
Shinya Kitaoka 3bfa54
	return std::string(a);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::string toString(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return toString(fp.getWideString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::wstring toWideString(int x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return toWideString(toString(x));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::string toString(int value)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::ostrstream ss;
Toshihiro Shimizu 890ddd
	ss << value << '\0';
Shinya Kitaoka 3bfa54
	std::string s = ss.str();
kogaki 5bbc3c
	ss.freeze(false);
Toshihiro Shimizu 890ddd
	return s;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::string toString(unsigned long value)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::ostrstream ss;
Toshihiro Shimizu 890ddd
	ss << value << '\0';
Shinya Kitaoka 3bfa54
	std::string s = ss.str();
kogaki 5bbc3c
	ss.freeze(false);
Toshihiro Shimizu 890ddd
	return s;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::string toString(unsigned long long value)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::ostrstream ss;
Toshihiro Shimizu 890ddd
	ss << value << '\0';
Shinya Kitaoka 3bfa54
	std::string s = ss.str();
kogaki 5bbc3c
	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
Shinya Kitaoka 3bfa54
std::string toString(double value, int prec)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::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';
Shinya Kitaoka 3bfa54
	std::string s = ss.str();
kogaki 5bbc3c
	ss.freeze(0);
Toshihiro Shimizu 890ddd
	return s;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::string toString(void *p)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::ostrstream ss;
Toshihiro Shimizu 890ddd
	ss << p << '\0';
Shinya Kitaoka 3bfa54
	std::string s = ss.str();
kogaki 5bbc3c
	ss.freeze(false);
Toshihiro Shimizu 890ddd
	return s;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
int toInt(std::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
Shinya Kitaoka 3bfa54
int toInt(std::wstring str)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return toInt(toString(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 3bfa54
bool isInt(std::wstring s) { return isInt(toString(s)); }
Shinya Kitaoka 3bfa54
bool isDouble(std::wstring s) { return isDouble(toString(s)); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
double toDouble(std::string str)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double value;
Shinya Kitaoka 3bfa54
	std::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
Shinya Kitaoka 3bfa54
double toDouble(std::wstring str)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return toDouble(toString(str));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::wstring toWideString(double v, int p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return toWideString(toString(v, p));
Toshihiro Shimizu 890ddd
}
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
}