1 /******************************************************************************
2  *  Warmux is a convivial mass murder game.
3  *  Copyright (C) 2001-2011 Warmux Team.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18  ******************************************************************************
19  * Refresh character strings.
20  *****************************************************************************/
21 
22 #include "tool/string_tools.h"
23 #include <sstream>
24 #include <assert.h>
25 
26 template<typename T>
str2type(const std::string & txt,T & value)27 bool str2type(const std::string &txt, T &value)
28 {
29   std::stringstream ss;
30   ss << txt;
31   ss >> value;
32   return true;
33 }
34 
str2uint(const std::string & txt,uint & value)35 bool str2uint(const std::string &txt, uint &value) { return str2type<uint>(txt, value); }
36 
str2int(const std::string & txt,int & value)37 bool str2int(const std::string &txt, int &value) { return str2type<int>(txt, value); }
38 
str2Double(const std::string & txt,Double & value)39 bool str2Double(const std::string &txt, Double &value) { return str2type<Double>(txt, value); }
40 
str2float(const std::string & txt,float & value)41 bool str2float(const std::string &txt, float& value) { return str2type<float>(txt, value); }
42 
str2bool(const std::string & str,bool & value)43 bool str2bool(const std::string &str, bool &value)
44 {
45   // Try to convert str to a boolean value
46   // return true and set the value on succeed
47   // return if false its not a boolean
48   if (str=="1" || str=="true" || str=="on") {
49     value = true;
50     return true;
51   }
52   if (str=="0" || str=="false" || str=="off") {
53     value = false;
54     return true;
55   }
56   return false;
57 }
58 
Double2str(Double x,int places)59 std::string Double2str(Double x, int places)
60 {
61   std::ostringstream ss;
62   printTo(ss, x, places);
63   return ss.str();
64 }
65 
66 template<typename T>
type2str(T value)67 std::string type2str(T value)
68 {
69   std::stringstream ss;
70   ss << value;
71   return ss.str();
72 }
73 
float2str(float x)74 std::string float2str(float x) { return type2str<float>(x); }
int2str(int x)75 std::string int2str(int x) { return type2str<int>(x); }
uint2str(uint x)76 std::string uint2str(uint x) { return type2str<uint>(x); }
77 
bool2str(bool x)78 std::string bool2str(bool x)
79 {
80   return (x) ? "true" : "false";
81 }
82 
83 #ifdef _WIN32
84 #  include <windows.h>
LocaleToUTF8(const char * data)85 char* LocaleToUTF8(const char* data)
86 {
87   assert(data);
88   //printf("Converting %s\n", data);
89 
90   // Convert from OEM to UTF-16
91   int len = 2*MultiByteToWideChar(CP_OEMCP, 0, data, -1, NULL, 0);
92   assert(len > 0);
93   char* utf16 = new char[len+1];
94   int ret = MultiByteToWideChar(CP_OEMCP, 0, data, -1, (LPWSTR)utf16, len);
95   assert(ret > 0);
96 
97   // Now convert from UTF-16 to UTF-8
98   len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)utf16, -1, NULL, 0, NULL, NULL);
99   assert(len > 0);
100   char* str = new char[len+1];
101   ret = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)utf16, -1, str, len, NULL, NULL);
102   assert(ret > 0);
103 
104   delete[] utf16;
105   return str;
106 }
107 #endif
108