Athena IO Library
Utility.hpp
1 // This file is part of libAthena.
2 //
3 // libAthena is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // libAthena is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with libAthena. If not, see <http://www.gnu.org/licenses/>
15 
16 
17 #ifndef __UTILITY_H__
18 #define __UTILITY_H__
19 
20 #include <string>
21 #include <vector>
22 #include <stdarg.h>
23 #include <string.h>
24 #include "Athena/Global.hpp"
25 #include "Athena/Types.hpp"
26 
27 namespace Athena
28 {
29 namespace utility
30 {
31 inline bool isEmpty(atInt8* buf, atUint32 size) {return !memcmp(buf, buf + 1, size - 1);}
32 bool isSystemBigEndian();
33 
34 inline atInt16 swap16(atInt16 val)
35 {
36 #if __GNUC__
37  return __builtin_bswap16(val);
38 #elif _WIN32
39  return _byteswap_ushort(val);
40 #else
41  return (val = (val << 8) | ((val >> 8) & 0xFF));
42 #endif
43 }
44 inline atUint16 swapU16(atUint16 val) {return (atUint16)swap16(val);}
45 inline atInt32 swap32(atInt32 val)
46 {
47 #if __GNUC__
48  return __builtin_bswap32(val);
49 #elif _WIN32
50  return _byteswap_ulong(val);
51 #else
52  val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
53  val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
54  return val;
55 #endif
56 }
57 inline atUint32 swapU32(atUint32 val) {return (atUint32)swap32(val);}
58 inline atInt64 swap64(atInt64 val)
59 {
60 #if __GNUC__
61  return __builtin_bswap64(val);
62 #elif _WIN32
63  return _byteswap_uint64(val);
64 #else
65  return (val = ((atInt64)((((atInt64)(val) & 0xFF00000000000000ULL) >> 56) |
66  (((atInt64)(val) & 0x00FF000000000000ULL) >> 40) |
67  (((atInt64)(val) & 0x0000FF0000000000ULL) >> 24) |
68  (((atInt64)(val) & 0x000000FF00000000ULL) >> 8) |
69  (((atInt64)(val) & 0x00000000FF000000ULL) << 8) |
70  (((atInt64)(val) & 0x0000000000FF0000ULL) << 24) |
71  (((atInt64)(val) & 0x000000000000FF00ULL) << 40) |
72  (((atInt64)(val) & 0x00000000000000FFULL) << 56))));
73 #endif
74 }
75 inline atUint64 swapU64(atUint64 val) {return (atUint64)swap64(val);}
76 inline float swapFloat(float val)
77 {
78  atInt32 ival = swap64(static_cast<atInt32>(val));
79  return static_cast<float>(ival);
80 }
81 inline double swapDouble(double val)
82 {
83  atInt64 ival = swap64(static_cast<atInt64>(val));
84  return static_cast<double>(ival);
85 }
86 inline atInt16 LittleInt16(atInt16& val)
87 {
88  if (Athena::utility::isSystemBigEndian())
89  val = Athena::utility::swap16(val);
90 
91  return val;
92 }
93 inline atUint16 LittleUint16(atUint16& val)
94 {
95  atInt16 ret = val;
96  LittleInt16(ret);
97  val = ret;
98 
99  return val;
100 }
101 inline atInt16 BigInt16(atInt16& val)
102 {
103  if (!Athena::utility::isSystemBigEndian())
104  val = Athena::utility::swap16(val);
105 
106  return val;
107 }
108 inline atUint16 BigUint16(atUint16& val)
109 {
110  atInt16 ret = val;
111  BigInt16(ret);
112  val = ret;
113 
114  return val;
115 }
116 inline atInt32 LittleInt32(atInt32& val)
117 {
118  if (Athena::utility::isSystemBigEndian())
119  val = Athena::utility::swap32(val);
120 
121  return val;
122 }
123 inline atUint32 LittleUint32(atUint32& val)
124 {
125  atInt32 ret = val;
126  LittleInt32(ret);
127  val = ret;
128 
129  return val;
130 }
131 inline atInt32 BigInt32(atInt32& val)
132 {
133  if (!Athena::utility::isSystemBigEndian())
134  val = Athena::utility::swap32(val);
135 
136  return val;
137 }
138 inline atUint32 BigUint32(atUint32& val)
139 {
140  atInt32 ret = val;
141  BigInt32(ret);
142  val = ret;
143 
144  return val;
145 }
146 inline atInt64 LittleInt64(atInt64& val)
147 {
148  if (Athena::utility::isSystemBigEndian())
149  val = Athena::utility::swap64(val);
150 
151  return val;
152 }
153 inline atUint64 LittleUint64(atUint64& val)
154 {
155  atInt64 ret = val;
156  LittleInt64(ret);
157  val = ret;
158 
159  return val;
160 }
161 inline atInt64 BigInt64(atInt64& val)
162 {
163  if (!Athena::utility::isSystemBigEndian())
164  val = Athena::utility::swap64(val);
165 
166  return val;
167 }
168 inline atUint64 BigUint64(atUint64& val)
169 {
170  atInt64 ret = val;
171  BigInt64(ret);
172  val = ret;
173 
174  return val;
175 }
176 
177 inline float LittleFloat(float& val)
178 {
179  if (Athena::utility::isSystemBigEndian())
180  val = Athena::utility::swapFloat(val);
181 
182  return val;
183 }
184 inline float BigFloat(float& val)
185 {
186  if (!Athena::utility::isSystemBigEndian())
187  val = Athena::utility::swapFloat(val);
188 
189  return val;
190 }
191 inline double LittleDouble(double& val)
192 {
193  if (Athena::utility::isSystemBigEndian())
194  val = Athena::utility::swapDouble(val);
195 
196  return val;
197 }
198 inline double BigDouble(double& val)
199 {
200  if (!Athena::utility::isSystemBigEndian())
201  val = Athena::utility::swapDouble(val);
202 
203  return val;
204 }
205 
206 void fillRandom(atUint8 * rndArea, atUint64 count);
207 std::vector<std::string> split(const std::string &s, char delim);
208 atUint64 rand64();
209 std::string join(const std::vector<std::string>& elems, const std::string& delims);
210 void tolower(std::string& str);
211 void toupper(std::string& str);
212 std::string vsprintf(const char* fmt, va_list list);
213 std::string sprintf(const char* fmt, ...);
214 bool parseBool(const std::string& boolean, bool* valid = NULL);
215 
216 int countChar(const std::string& str, const char chr, int* lastOccur = NULL);
217 
218 // trim from start
219 std::string& ltrim(std::string& s);
220 
221 // trim from end
222 std::string& rtrim(std::string& s);
223 
224 // trim from both ends
225 std::string& trim(std::string& s);
226 atUint64 fileSize(const std::string& filename);
227 } // utility
228 } // Athena
229 #endif