zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Serialization.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <zeni.h>
19 
20 #include <Zeni/Define.h>
21 #include <sstream>
22 #include <iomanip>
23 
24 /*** IPaddress functions ***/
25 
26 bool operator==(const IPaddress &lhs, const IPaddress &rhs) {
27  const IPaddress lhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&lhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&lhs.port))};
28  const IPaddress rhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&rhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&rhs.port))};
29  return lhs_l.host == rhs_l.host && lhs_l.port == rhs_l.port;
30 }
31 
32 bool operator!=(const IPaddress &lhs, const IPaddress &rhs) {
33  const IPaddress lhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&lhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&lhs.port))};
34  const IPaddress rhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&rhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&rhs.port))};
35  return lhs_l.host != rhs_l.host || lhs_l.port != rhs_l.port;
36 }
37 
38 bool operator<(const IPaddress &lhs, const IPaddress &rhs) {
39  const IPaddress lhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&lhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&lhs.port))};
40  const IPaddress rhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&rhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&rhs.port))};
41  return lhs_l.host < rhs_l.host || (lhs_l.host == rhs_l.host && lhs_l.port < rhs_l.port);
42 }
43 
44 bool operator<=(const IPaddress &lhs, const IPaddress &rhs) {
45  const IPaddress lhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&lhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&lhs.port))};
46  const IPaddress rhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&rhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&rhs.port))};
47  return lhs_l.host < rhs_l.host || (lhs_l.host == rhs_l.host && lhs_l.port <= rhs_l.port);
48 }
49 
50 bool operator>(const IPaddress &lhs, const IPaddress &rhs) {
51  const IPaddress lhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&lhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&lhs.port))};
52  const IPaddress rhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&rhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&rhs.port))};
53  return lhs_l.host > rhs_l.host || (lhs_l.host == rhs_l.host && lhs_l.port > rhs_l.port);
54 }
55 
56 bool operator>=(const IPaddress &lhs, const IPaddress &rhs) {
57  const IPaddress lhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&lhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&lhs.port))};
58  const IPaddress rhs_l = {SDLNet_Read32(const_cast<Uint32 *>(&rhs.host)), SDLNet_Read16(const_cast<Uint16 *>(&rhs.port))};
59  return lhs_l.host > rhs_l.host || (lhs_l.host == rhs_l.host && lhs_l.port >= rhs_l.port);
60 }
61 
62 namespace Zeni {
63  /*** Simple Helper Functions ***/
64 
65  String ustoa(const unsigned short &number) {
67  #ifdef _WINDOWS
68  sprintf_s
69  #else
70  sprintf
71  #endif
72  (buf, "%hu", number);
73  return buf;
74  }
75 
76  String stoa(const short &number) {
78  #ifdef _WINDOWS
79  sprintf_s
80  #else
81  sprintf
82  #endif
83  (buf, "%hd", number);
84  return buf;
85  }
86 
87  String uitoa(const unsigned int &number) {
89  #ifdef _WINDOWS
90  sprintf_s
91  #else
92  sprintf
93  #endif
94  (buf, "%u", number);
95  return buf;
96  }
97 
98  String itoa(const int &number) {
100  #ifdef _WINDOWS
101  sprintf_s
102  #else
103  sprintf
104  #endif
105  (buf, "%d", number);
106  return buf;
107  }
108 
109  String ultoa(const unsigned long &number) {
111  #ifdef _WINDOWS
112  sprintf_s
113  #else
114  sprintf
115  #endif
116  (buf, "%lu", number);
117  return buf;
118  }
119 
120  String ltoa(const long &number) {
122  #ifdef _WINDOWS
123  sprintf_s
124  #else
125  sprintf
126  #endif
127  (buf, "%ld", number);
128  return buf;
129  }
130 
131 #ifdef _WINDOWS
132  String ulltoa(const unsigned long long &number) {
134  sprintf_s(buf, "%llu", number);
135  return buf;
136  }
137 
138  String lltoa(const long long &number) {
140  sprintf_s(buf, "%lld", number);
141  return buf;
142  }
143 #else
144  String ulltoa(const unsigned long &number) {
146  sprintf(buf, "%lu", number);
147  return buf;
148  }
149 
150  String lltoa(const long &number) {
152  sprintf(buf, "%ld", number);
153  return buf;
154  }
155 #endif
156 
157  String ftoa(const float &number) {
159  #ifdef _WINDOWS
160  sprintf_s
161  #else
162  sprintf
163  #endif
164  (buf, "%f", number);
165  return buf;
166  }
167 
168  String ftoa(const float &number, const unsigned int &precision) {
169  std::ostringstream oss;
170  oss << std::setprecision(int(precision)) << number;
171  return String(oss.str());
172  }
173 
174  String dtoa(const double &number) {
176  #ifdef _WINDOWS
177  sprintf_s
178  #else
179  sprintf
180  #endif
181  (buf, "%f", number);
182  return buf;
183  }
184 
185  String dtoa(const double &number, const unsigned int &precision) {
186  std::ostringstream oss;
187  oss << std::setprecision(int(precision)) << number;
188  return String(oss.str());
189  }
190 
191  String ldtoa(const long double &number) {
193  #ifdef _WINDOWS
194  sprintf_s
195  #else
196  sprintf
197  #endif
198  (buf, "%Lf", number);
199  return buf;
200  }
201 
203  Uint32 host = SDLNet_Read32(const_cast<Uint32 *>(&address.host));
204  Uint16 port = SDLNet_Read16(const_cast<Uint16 *>(&address.port));
205 
206  return uitoa(host >> 24 & 0xFF) + '.' +
207  uitoa(host >> 16 & 0xFF) + '.' +
208  uitoa(host >> 8 & 0xFF) + '.' +
209  uitoa(host & 0xFF) + ':' + uitoa(port);
210  }
211 
212  size_t grab_bytes(std::istream &is, char * const &store, const size_t &num_bytes) {
213  char c;
214  size_t i = 0u;
215  for(char * store_ptr = store; i != num_bytes; ++i, ++store_ptr)
216  if(is.get(c))
217  *store_ptr = c;
218  else
219  break;
220  return i;
221  }
222 
223  size_t grab_bytes(std::istream &is, String &store, const size_t &num_bytes) {
224  store.resize(num_bytes);
225  const size_t rv = grab_bytes(is, const_cast<char * const>(store.c_str()), num_bytes);
226  store.resize(rv);
227  return rv;
228  }
229 
230  std::ostream & Serializable::serialize(std::ostream &os) const {
231  return Zeni::serialize(os, m_size);
232  }
233 
234  std::istream & Serializable::unserialize(std::istream &is) {
235  if(!Zeni::unserialize(is, m_size))
236  m_size = 0;
237 
238  return is;
239  }
240 
241  /*** Stand-Alone serialization/unserialization functions ***/
242 
243  std::ostream & serialize(std::ostream &os, const Sint32 &value) {
244  char buf[sizeof(Sint32)];
245  void *bp = buf;
246 
247  SDLNet_Write32(reinterpret_cast<const Uint32 &>(value), bp);
248 
249  return os.write(buf, sizeof(Sint32));
250  }
251 
252  std::ostream & serialize(std::ostream &os, const Uint32 &value) {
253  char buf[sizeof(Uint32)];
254  void *bp = buf;
255 
256  SDLNet_Write32(value, bp);
257 
258  return os.write(buf, sizeof(Uint32));
259  }
260 
261  std::ostream & serialize(std::ostream &os, const Sint16 &value) {
262  char buf[sizeof(Sint16)];
263  void *bp = buf;
264 
265  SDLNet_Write16(reinterpret_cast<const Uint16 &>(value), bp);
266 
267  return os.write(buf, sizeof(Sint16));
268  }
269 
270  std::ostream & serialize(std::ostream &os, const Uint16 &value) {
271  char buf[sizeof(Uint16)];
272  void *bp = buf;
273 
274  SDLNet_Write16(value, bp);
275 
276  return os.write(buf, sizeof(Uint16));
277  }
278 
279  std::ostream & serialize(std::ostream &os, const Sint8 &value) {
280  return os.write(reinterpret_cast<const char *>(&value), 1);
281  }
282 
283  std::ostream & serialize(std::ostream &os, const char &value) {
284  return os.write(&value, 1);
285  }
286 
287  std::ostream & serialize(std::ostream &os, const unsigned char &value) {
288  return os.write(reinterpret_cast<const char *>(&value), 1);
289  }
290 
291  std::ostream & serialize(std::ostream &os, const float &value) {
292  return os.write(reinterpret_cast<const char * const>(&value), sizeof(float));
293  }
294 
295  std::ostream & serialize(std::ostream &os, const double &value) {
296  return os.write(reinterpret_cast<const char * const>(&value), sizeof(double));
297  }
298 
299  //std::ostream & serialize(std::ostream &os, const bool &value) {
300  // const char c(value ? 1 : 0);
301  // return os.put(c);
302  //}
303 
304  std::ostream & serialize(std::ostream &os, const IPaddress &address) {
305  const char *buf = reinterpret_cast<const char *>(&address);
306 
307  return os.write(buf, sizeof(IPaddress));
308  }
309 
310  std::ostream & serialize(std::ostream &os, const String &string) {
311  const Uint16 sz = Uint16(string.size());
312  return serialize(os, sz).write(string.c_str(), sz);
313  }
314 
315  std::istream & unserialize(std::istream &is, Sint32 &value) {
316  char buf[sizeof(Sint32)];
317  void *bp = buf;
318 
319  if(is.read(buf, sizeof(Sint32))) {
320  const Uint32 s_value = SDLNet_Read32(bp);
321  value = reinterpret_cast<const Sint32 &>(s_value);
322  }
323 
324  return is;
325  }
326 
327  std::istream & unserialize(std::istream &is, Uint32 &value) {
328  char buf[sizeof(Uint32)];
329  void *bp = buf;
330 
331  if(is.read(buf, sizeof(Uint32)))
332  value = SDLNet_Read32(bp);
333 
334  return is;
335  }
336 
337  std::istream & unserialize(std::istream &is, Sint16 &value) {
338  char buf[sizeof(Sint16)];
339  void *bp = buf;
340 
341  if(is.read(buf, sizeof(Sint16))) {
342  const Uint16 s_value = SDLNet_Read16(bp);
343  value = reinterpret_cast<const Sint16 &>(s_value);
344  }
345 
346  return is;
347  }
348 
349  std::istream & unserialize(std::istream &is, Uint16 &value) {
350  char buf[sizeof(Uint16)];
351  void *bp = buf;
352 
353  if(is.read(buf, sizeof(Uint16)))
354  value = SDLNet_Read16(bp);
355 
356  return is;
357  }
358 
359  std::istream & unserialize(std::istream &is, Sint8 &value) {
360  is.read(reinterpret_cast<char *>(&value), 1);
361  return is;
362  }
363 
364  std::istream & unserialize(std::istream &is, char &value) {
365  is.read(&value, 1);
366  return is;
367  }
368 
369  std::istream & unserialize(std::istream &is, unsigned char &value) {
370  is.read(reinterpret_cast<char *>(&value), 1);
371  return is;
372  }
373 
374  std::istream & unserialize(std::istream &is, float &value) {
375  return is.read(reinterpret_cast<char * const>(&value), sizeof(float));
376  }
377 
378  std::istream & unserialize(std::istream &is, double &value) {
379  return is.read(reinterpret_cast<char * const>(&value), sizeof(double));
380  }
381 
382  //std::istream & unserialize(std::istream &is, bool &value) {
383  // char c;
384  //
385  // if(is.get(c))
386  // value = c ? true : false;
387  //
388  // return is;
389  //}
390 
391  std::istream & unserialize(std::istream &is, IPaddress &address) {
392  char buf[sizeof(IPaddress)];
393 
394  if(is.read(buf, sizeof(IPaddress)))
395  memcpy(&address, buf, sizeof(IPaddress));
396 
397  return is;
398  }
399 
400  std::istream & unserialize(std::istream &is, String &string) {
401  Uint16 sz;
402  unserialize(is, sz);
403 
404  if(is) {
405  string.resize(sz);
406  is.read(const_cast<char *>(string.c_str()), sz);
407  }
408 
409  return is;
410  }
411 
412 }
413 
414 #include <Zeni/Undefine.h>
#define SDLNet_Write16(value, areap)
Definition: SDL_net.h:376
Uint32 host
Definition: SDL_net.h:91
bool operator>(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:279
int32_t Sint32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:141
String stoa(const short &number)
String uitoa(const unsigned int &number)
String ustoa(const unsigned short &number)
bool operator<(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:273
bool operator==(const SourceLocation &lhs, const SourceLocation &rhs)
String ltoa(const long &number)
String lltoa(const long &number)
const char * c_str() const
Definition: String.cpp:467
String dtoa(const double &number)
#define SDLNet_Write32(value, areap)
Definition: SDL_net.h:377
std::istream & unserialize(std::istream &is, Color &value)
Definition: Color.cpp:72
std::ostream & serialize(std::ostream &os, const Color &value)
Definition: Color.cpp:68
void resize(size_t n, char c)
Definition: String.cpp:316
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
virtual std::istream & unserialize(std::istream &is)=0
ALuint u
Definition: alMain.h:58
String ftoa(const float &number)
String itoa(const int &number)
const GLfloat * c
Definition: glew.h:14913
GLuint GLuint64EXT address
Definition: glew.h:13266
#define SDLNet_Read32(areap)
Definition: SDL_net.h:381
Uint16 port
Definition: SDL_net.h:92
size_t grab_bytes(std::istream &is, char *const &store, const size_t &num_bytes)
static double bp[]
Definition: e_pow.c:72
bool operator!=(const SourceLocation &lhs, const SourceLocation &rhs)
String iptoa(const IPaddress &address)
EGLSurface EGLint void ** value
Definition: eglext.h:301
String ldtoa(const long double &number)
bool operator>=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:281
GLenum GLuint GLsizei const GLchar * buf
Definition: glew.h:2539
#define memcpy
Definition: SDL_malloc.c:634
#define ZENI_SPRINTF_BUFFER_SIZE
Definition: Define.h:106
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:133
virtual std::ostream & serialize(std::ostream &os) const =0
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:137
#define SDLNet_Read16(areap)
Definition: SDL_net.h:380
int i
Definition: pngrutil.c:1377
GLenum GLint GLint * precision
Definition: glew.h:3391
bool operator<=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:280
String ulltoa(const unsigned long &number)
String ultoa(const unsigned long &number)
int8_t Sint8
A signed 8-bit integer type.
Definition: SDL_stdinc.h:125
GLsizei size
Definition: gl2ext.h:1467