Athena IO Library
Utility.hpp
1 #ifndef __UTILITY_H__
2 #define __UTILITY_H__
3 
4 #include <string>
5 #include <vector>
6 #include <stdarg.h>
7 #include <string.h>
8 #include "athena/Global.hpp"
9 #include "athena/Types.hpp"
10 
11 namespace athena
12 {
13 namespace utility
14 {
15 inline bool isEmpty(atInt8* buf, atUint32 size) {return !memcmp(buf, buf + 1, size - 1);}
16 inline bool isSystemBigEndian() {return (*(atUint16*)"\xFE\xFF" == 0xFEFF);}
17 
18 inline atInt16 swap16(atInt16 val)
19 {
20 #if __GNUC__
21  return __builtin_bswap16(val);
22 #elif _WIN32
23  return _byteswap_ushort(val);
24 #else
25  return (val = (val << 8) | ((val >> 8) & 0xFF));
26 #endif
27 }
28 inline atUint16 swapU16(atUint16 val) {return (atUint16)swap16(val);}
29 inline atInt32 swap32(atInt32 val)
30 {
31 #if __GNUC__
32  return __builtin_bswap32(val);
33 #elif _WIN32
34  return _byteswap_ulong(val);
35 #else
36  val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
37  val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
38  return val;
39 #endif
40 }
41 inline atUint32 swapU32(atUint32 val) {return (atUint32)swap32(val);}
42 inline atInt64 swap64(atInt64 val)
43 {
44 #if __GNUC__
45  return __builtin_bswap64(val);
46 #elif _WIN32
47  return _byteswap_uint64(val);
48 #else
49  return (val = ((atInt64)((((atInt64)(val) & 0xFF00000000000000ULL) >> 56) |
50  (((atInt64)(val) & 0x00FF000000000000ULL) >> 40) |
51  (((atInt64)(val) & 0x0000FF0000000000ULL) >> 24) |
52  (((atInt64)(val) & 0x000000FF00000000ULL) >> 8) |
53  (((atInt64)(val) & 0x00000000FF000000ULL) << 8) |
54  (((atInt64)(val) & 0x0000000000FF0000ULL) << 24) |
55  (((atInt64)(val) & 0x000000000000FF00ULL) << 40) |
56  (((atInt64)(val) & 0x00000000000000FFULL) << 56))));
57 #endif
58 }
59 inline atUint64 swapU64(atUint64 val) {return (atUint64)swap64(val);}
60 inline float swapFloat(float val)
61 {
62  atInt32 ival = swap32(*((atInt32*)(&val)));
63  return *((float*)(&ival));
64 }
65 inline double swapDouble(double val)
66 {
67  atInt64 ival = swap64(*((atInt64*)(&val)));
68  return *((double*)(&ival));
69 }
70 inline atInt16 LittleInt16(atInt16& val)
71 {
72  if (athena::utility::isSystemBigEndian())
73  val = athena::utility::swap16(val);
74 
75  return val;
76 }
77 inline atUint16 LittleUint16(atUint16& val)
78 {
79  atInt16 ret = val;
80  LittleInt16(ret);
81  val = ret;
82 
83  return val;
84 }
85 inline atInt16 BigInt16(atInt16& val)
86 {
87  if (!athena::utility::isSystemBigEndian())
88  val = athena::utility::swap16(val);
89 
90  return val;
91 }
92 inline atUint16 BigUint16(atUint16& val)
93 {
94  atInt16 ret = val;
95  BigInt16(ret);
96  val = ret;
97 
98  return val;
99 }
100 inline atInt32 LittleInt32(atInt32& val)
101 {
102  if (athena::utility::isSystemBigEndian())
103  val = athena::utility::swap32(val);
104 
105  return val;
106 }
107 inline atUint32 LittleUint32(atUint32& val)
108 {
109  atInt32 ret = val;
110  LittleInt32(ret);
111  val = ret;
112 
113  return val;
114 }
115 inline atInt32 BigInt32(atInt32& val)
116 {
117  if (!athena::utility::isSystemBigEndian())
118  val = athena::utility::swap32(val);
119 
120  return val;
121 }
122 inline atUint32 BigUint32(atUint32& val)
123 {
124  atInt32 ret = val;
125  BigInt32(ret);
126  val = ret;
127 
128  return val;
129 }
130 inline atInt64 LittleInt64(atInt64& val)
131 {
132  if (athena::utility::isSystemBigEndian())
133  val = athena::utility::swap64(val);
134 
135  return val;
136 }
137 inline atUint64 LittleUint64(atUint64& val)
138 {
139  atInt64 ret = val;
140  LittleInt64(ret);
141  val = ret;
142 
143  return val;
144 }
145 inline atInt64 BigInt64(atInt64& val)
146 {
147  if (!athena::utility::isSystemBigEndian())
148  val = athena::utility::swap64(val);
149 
150  return val;
151 }
152 inline atUint64 BigUint64(atUint64& val)
153 {
154  atInt64 ret = val;
155  BigInt64(ret);
156  val = ret;
157 
158  return val;
159 }
160 
161 inline float LittleFloat(float& val)
162 {
163  if (athena::utility::isSystemBigEndian())
164  val = athena::utility::swapFloat(val);
165 
166  return val;
167 }
168 inline float BigFloat(float& val)
169 {
170  if (!athena::utility::isSystemBigEndian())
171  val = athena::utility::swapFloat(val);
172 
173  return val;
174 }
175 inline double LittleDouble(double& val)
176 {
177  if (athena::utility::isSystemBigEndian())
178  val = athena::utility::swapDouble(val);
179 
180  return val;
181 }
182 inline double BigDouble(double& val)
183 {
184  if (!athena::utility::isSystemBigEndian())
185  val = athena::utility::swapDouble(val);
186 
187  return val;
188 }
189 
190 void fillRandom(atUint8 * rndArea, atUint64 count);
191 std::vector<std::string> split(const std::string &s, char delim);
192 atUint64 rand64();
193 std::string join(const std::vector<std::string>& elems, const std::string& delims);
194 void tolower(std::string& str);
195 void toupper(std::string& str);
196 std::string vsprintf(const char* fmt, va_list list);
197 std::string sprintf(const char* fmt, ...);
198 bool parseBool(const std::string& boolean, bool* valid = NULL);
199 
200 int countChar(const std::string& str, const char chr, int* lastOccur = NULL);
201 
202 // trim from start
203 std::string& ltrim(std::string& s);
204 
205 // trim from end
206 std::string& rtrim(std::string& s);
207 
208 // trim from both ends
209 std::string& trim(std::string& s);
210 atUint64 fileSize(const std::string& filename);
211 #ifdef _MSC_VER
212 atUint64 fileSize(const std::wstring& filename);
213 #endif
214 
215 std::string wideToUtf8(const std::wstring& src);
216 
217 std::wstring utf8ToWide(const std::string& src);
218 
219 } // utility
220 } // Athena
221 #endif