Leviathan  0.8.0.0
Leviathan game engine
Convert.cpp
Go to the documentation of this file.
1 #include "Include.h"
2 // ------------------------------------ //
3 #include "Convert.h"
4 
5 #include "Define.h"
7 #include "../Common/StringOperations.h"
8 #include "utf8/checked.h"
9 #include <iosfwd>
10 using namespace Leviathan;
11 using namespace std;
12 // ------------------------------------ //
13 double Convert::DegreesToRadians(float degrees){
14  return (degrees*(PI/180.f));
15 
16 }
17 double Convert::RadiansToDegrees(float radians){
18  return radians*(180.f/PI);
19 }
20 
21 
22 int Convert::WstringFromBoolToInt(const wstring &i){
23  if(StringOperations::CompareInsensitive<wstring>(i, L"true") || i == L"1"){
24  return true;
25  } else {
26  return false;
27  }
28 }
29 int Convert::StringFromBoolToInt(const string &i){
30  if(StringOperations::CompareInsensitive<string>(i, "true") || i == "1"){
31  return true;
32  } else {
33  return false;
34  }
35 }
36 
37 DLLEXPORT bool Convert::IsStringBool(const string &val, bool* receiver){
38 
39  if(StringOperations::CompareInsensitive<string>(val, "true") || val == "1"){
40 
41  *receiver = true;
42  return true;
43  }
44 
45  if(StringOperations::CompareInsensitive<string>(val, "false") || val == "0"){
46 
47  *receiver = false;
48  return true;
49  }
50 
51  return false;
52 }
53 
54 DLLEXPORT int Convert::StringTypeNameCheck(const std::string &name){
55 
56  if(name == "int")
57  return DATABLOCK_TYPE_INT;
58  if(name == "float")
59  return DATABLOCK_TYPE_FLOAT;
60  if(name == "bool")
61  return DATABLOCK_TYPE_BOOL;
62  if(name == "wstring")
64  if(name == "string")
65  return DATABLOCK_TYPE_STRING;
66  if(name == "char")
67  return DATABLOCK_TYPE_CHAR;
68  if(name == "double")
69  return DATABLOCK_TYPE_DOUBLE;
70  if(name == "void" || name == "void*")
72 
73  return -1;
74 }
75 // ------------------------------------ //
76 DLLEXPORT std::wstring Leviathan::Convert::Utf8ToUtf16(const string &utf8str){
77 
78  // Store enough memory for the result //
79  wstring results;
80  results.reserve(utf8str.size());
81 
82  try {
83 
84  utf8::utf8to16(utf8str.begin(), utf8str.end(), back_inserter(results));
85 
86  } catch(utf8::invalid_code_point &e1){
87 
88  #ifndef LEVIATHAN_UE_PLUGIN
89  Logger::Get()->Error("Convert: invalid UTF-8 code point: "+
91  #else
92  NOT_UNUSED(e1);
93  #endif //LEVIATHAN_UE_PLUGIN
94  return wstring();
95 
96  } catch(utf8::not_enough_room&){
97 
98  #ifndef LEVIATHAN_UE_PLUGIN
99  Logger::Get()->Error("Convert: not enough memory for string conversion");
100  #endif //LEVIATHAN_UE_PLUGIN
101  return wstring();
102  }
103 
104  // The result is now done //
105  return results;
106 }
107 
108 DLLEXPORT std::string Leviathan::Convert::Utf16ToUtf8(const wstring &utf16str){
109  // Store enough memory for the result //
110  string results;
111  results.reserve(utf16str.size());
112 
113  try {
114 
115  utf8::utf16to8(utf16str.begin(), utf16str.end(), back_inserter(results));
116 
117  } catch(utf8::invalid_code_point &e1){
118  #ifndef LEVIATHAN_UE_PLUGIN
119  Logger::Get()->Error("Convert: invalid UTF-8 code point: "+
121  #else
122  NOT_UNUSED(e1);
123  #endif //LEVIATHAN_UE_PLUGIN
124  return string();
125 
126  } catch(utf8::not_enough_room&){
127  #ifndef LEVIATHAN_UE_PLUGIN
128  Logger::Get()->Error("Convert: not enough memory for string conversion");
129  #endif //LEVIATHAN_UE_PLUGIN
130  return string();
131  }
132 
133  // The result is now done //
134  return results;
135 }
136 
137 DLLEXPORT std::string Leviathan::Convert::CodePointToUtf8(int32_t codepoint) {
138 
139  std::string result;
140  utf8::utf32to8(&codepoint, (&codepoint) + 1, std::back_inserter(result));
141 
142  return result;
143 }
144 
145 // ------------------------------------ //
146 
#define NOT_UNUSED(x)
Definition: Include.h:135
static DLLEXPORT int StringFromBoolToInt(const std::string &i)
Definition: Convert.cpp:29
static DLLEXPORT std::string Utf16ToUtf8(const std::wstring &utf16str)
Encodes an UTF8 string from a wide string (wstring/utf16)
Definition: Convert.cpp:108
static DLLEXPORT std::wstring Utf8ToUtf16(const std::string &utf8str)
Decodes an UTF8 string to an UTF16 string (wide string/wstring)
Definition: Convert.cpp:76
STL namespace.
uint32_t code_point() const
Definition: checked.h:46
static DLLEXPORT double RadiansToDegrees(float radians)
Definition: Convert.cpp:17
#define DATABLOCK_TYPE_INT
Definition: DataBlock.h:22
#define DATABLOCK_TYPE_VOIDPTR
Definition: DataBlock.h:29
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
Definition: checked.h:207
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
Definition: checked.h:249
static DLLEXPORT int WstringFromBoolToInt(const std::wstring &i)
Definition: Convert.cpp:22
#define DATABLOCK_TYPE_WSTRING
Definition: DataBlock.h:25
#define DATABLOCK_TYPE_FLOAT
Definition: DataBlock.h:23
#define DATABLOCK_TYPE_STRING
Definition: DataBlock.h:26
#define DATABLOCK_TYPE_DOUBLE
Definition: DataBlock.h:28
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT std::string CodePointToUtf8(int32_t codepoint)
Encodes a Unicode code point as an UTF8 string.
Definition: Convert.cpp:137
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
static DLLEXPORT int StringTypeNameCheck(const std::string &name)
Definition: Convert.cpp:54
constexpr float PI
Definition: Define.h:58
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
Definition: checked.h:234
#define DLLEXPORT
Definition: Include.h:115
#define DATABLOCK_TYPE_BOOL
Definition: DataBlock.h:24
static DLLEXPORT double DegreesToRadians(float degrees)
Definition: Convert.cpp:13
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177
#define DATABLOCK_TYPE_CHAR
Definition: DataBlock.h:27
static DLLEXPORT bool IsStringBool(const std::string &val, bool *receiver)
Definition: Convert.cpp:37