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
e280ae
#ifdef _MSC_VER
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
e280ae
#endif
e280ae
e280ae
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include "windows.h"
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9eb50d
#include <sstream></sstream>
Shinya Kitaoka 9eb50d
Shinya Kitaoka d1f6c4
class TStringConvertException final : public TException {
Shinya Kitaoka 120a6e
  std::string m_string;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TStringConvertException(const std::string str) : m_string(str) {}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::wstring to_wstring(std::string s) {
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_LIGHT
Shinya Kitaoka 120a6e
  std::wstring ws;
Shinya Kitaoka 120a6e
  ws.assign(s.begin(), s.end());
Shinya Kitaoka 120a6e
  return ws;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QString testString = QString::fromStdString(s);
Shinya Kitaoka 120a6e
  QString qString    = QString::fromUtf8(s.c_str());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // To detect if 's' is UTF-8 encoded or not
Shinya Kitaoka 120a6e
  if (qString != testString && std::string(qString.toUtf8()) == s)
Shinya Kitaoka 120a6e
    return qString.toStdWString();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return testString.toStdWString();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string to_string(std::wstring ws) {
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_LIGHT
Shinya Kitaoka 120a6e
  std::string s;
Shinya Kitaoka 120a6e
  s.assign(ws.begin(), ws.end());
Shinya Kitaoka 120a6e
  return s;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  QString const qString = QString::fromStdWString(ws);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Test if 'ws' is not unicode (UTF-8)
Shinya Kitaoka 120a6e
  if (qString.toLatin1() == qString) return qString.toStdString();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return std::string(qString.toUtf8());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun_iwasawa 0cae24
std::string to_string(const TFilePath& fp) {
Shinya Kitaoka 120a6e
  return ::to_string(fp.getWideString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Shinya Kitaoka 120a6e
  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 120a6e
std::string to_string(double value, int prec) {
Shinya Kitaoka 120a6e
  if (prec < 0) {
Shinya Kitaoka 120a6e
    return std::to_string(value);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::ostringstream out;
Shinya Kitaoka 120a6e
  out.setf(std::ios_base::fixed, std::ios_base::floatfield);
Shinya Kitaoka 120a6e
  out.precision(prec);
Shinya Kitaoka 120a6e
  out << value;
Shinya Kitaoka 120a6e
  return out.str();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun_iwasawa 0cae24
std::string to_string(void* p) {
Shinya Kitaoka 120a6e
  std::ostringstream out;
Shinya Kitaoka 120a6e
  out << p;
Shinya Kitaoka 120a6e
  return out.str();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool isInt(std::string s) {
Shinya Kitaoka 120a6e
  int i = 0, len = (int)s.size();
Shinya Kitaoka 120a6e
  if (len == 0) return false;
Shinya Kitaoka 120a6e
  if (s[0] == '-') {
Shinya Kitaoka 120a6e
    if (len == 1)
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      i++;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (i < len) {
Shinya Kitaoka 120a6e
    if (s[i] < '0' || s[i] > '9') return false;
Shinya Kitaoka 120a6e
    i++;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool isDouble(std::string s) {
Shinya Kitaoka 120a6e
  int i = 0, len = (int)s.size();
Shinya Kitaoka 120a6e
  if (len == 0) return false;
Shinya Kitaoka 120a6e
  if (i < len && s[i] == '-') i++;
Shinya Kitaoka 120a6e
  while (i < len && s[i] != '.') {
Shinya Kitaoka 120a6e
    if (s[i] < '0' || s[i] > '9') return false;
Shinya Kitaoka 120a6e
    i++;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (i >= len) return true;
Shinya Kitaoka 120a6e
  i++;
Shinya Kitaoka 120a6e
  while (i < len) {
Shinya Kitaoka 120a6e
    if (s[i] < '0' || s[i] > '9') return false;
Shinya Kitaoka 120a6e
    i++;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  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 120a6e
std::string toUpper(std::string a) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
shun_iwasawa 0cae24
  size_t size      = a.size();
7fe829
  const char* cstr = a.c_str();
7fe829
  std::vector<char> buf(cstr, cstr + size + 1);</char>
7fe829
  return _strupr(&buf[0]);
Toshihiro Shimizu 890ddd
#else
7fe829
  std::string ret(a);
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)ret.length(); i++) ret[i] = toupper(ret[i]);
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string toLower(std::string a) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
shun_iwasawa 0cae24
  size_t size      = a.size();
7fe829
  const char* cstr = a.c_str();
7fe829
  std::vector<char> buf(cstr, cstr + size + 1);</char>
7fe829
  return _strlwr(&buf[0]);
Toshihiro Shimizu 890ddd
#else
7fe829
  std::string ret(a);
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)ret.length(); i++) ret[i] = tolower(ret[i]);
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::wstring toUpper(std::wstring a) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
shun_iwasawa 0cae24
  size_t size         = a.size();
7fe829
  const wchar_t* cstr = a.c_str();
7fe829
  std::vector<wchar_t> buf(cstr, cstr + size + 1);</wchar_t>
7fe829
  return _wcsupr(&buf[0]);
Toshihiro Shimizu 890ddd
#else
7fe829
  std::wstring ret(a);
7fe829
  for (int i = 0; i < (int)ret.length(); i++) ret[i] = towupper(ret[i]);
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::wstring toLower(std::wstring a) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
shun_iwasawa 0cae24
  size_t size         = a.size();
7fe829
  const wchar_t* cstr = a.c_str();
7fe829
  std::vector<wchar_t> buf(cstr, cstr + size + 1);</wchar_t>
7fe829
  return _wcslwr(&buf[0]);
Toshihiro Shimizu 890ddd
#else
7fe829
  std::wstring ret(a);
7fe829
  for (int i = 0; i < (int)ret.length(); i++) ret[i] = towlower(ret[i]);
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}