Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IDeserializer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. 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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "IDeserializer.h"
21 
22 #include "lib/byte_order.h"
23 #include "lib/utf8.h"
24 #include "ps/CStr.h"
25 
27 {
28 }
29 
30 void IDeserializer::NumberU8(const char* name, uint8_t& out, uint8_t lower, uint8_t upper)
31 {
32  uint8_t value;
33  Get(name, (u8*)&value, sizeof(uint8_t));
34 
35  if (!(lower <= value && value <= upper))
37 
38  out = value;
39 }
40 
41 void IDeserializer::NumberI8(const char* name, int8_t& out, int8_t lower, int8_t upper)
42 {
43  int8_t value;
44  Get(name, (u8*)&value, sizeof(uint8_t));
45 
46  if (!(lower <= value && value <= upper))
48 
49  out = value;
50 }
51 
52 void IDeserializer::NumberU16(const char* name, uint16_t& out, uint16_t lower, uint16_t upper)
53 {
54  uint16_t value;
55  Get(name, (u8*)&value, sizeof(uint16_t));
56  value = to_le16(value);
57 
58  if (!(lower <= value && value <= upper))
60 
61  out = value;
62 }
63 
64 void IDeserializer::NumberI16(const char* name, int16_t& out, int16_t lower, int16_t upper)
65 {
66  int16_t value;
67  Get(name, (u8*)&value, sizeof(uint16_t));
68  value = (i16)to_le16((u16)value);
69 
70  if (!(lower <= value && value <= upper))
72 
73  out = value;
74 }
75 
76 void IDeserializer::NumberU32(const char* name, uint32_t& out, uint32_t lower, uint32_t upper)
77 {
78  uint32_t value;
79  Get(name, (u8*)&value, sizeof(uint32_t));
80  value = to_le32(value);
81 
82  if (!(lower <= value && value <= upper))
84 
85  out = value;
86 }
87 
88 void IDeserializer::NumberI32(const char* name, int32_t& out, int32_t lower, int32_t upper)
89 {
90  int32_t value;
91  Get(name, (u8*)&value, sizeof(uint32_t));
92  value = (i32)to_le32((u32)value);
93 
94  if (!(lower <= value && value <= upper))
96 
97  out = value;
98 }
99 
101 {
102  Get(name, (u8*)&out, sizeof(uint8_t));
103 }
104 
106 {
107  Get(name, (u8*)&out, sizeof(int8_t));
108 }
109 
111 {
112  uint16_t value;
113  Get(name, (u8*)&value, sizeof(uint16_t));
114  out = to_le16(value);
115 }
116 
118 {
119  int16_t value;
120  Get(name, (u8*)&value, sizeof(int16_t));
121  out = (i16)to_le16((u16)value);
122 }
123 
125 {
126  uint32_t value;
127  Get(name, (u8*)&value, sizeof(uint32_t));
128  out = to_le32(value);
129 }
130 
131 void IDeserializer::NumberI32_Unbounded(const char* name, int32_t& out)
132 {
133  int32_t value;
134  Get(name, (u8*)&value, sizeof(int32_t));
135  out = (i32)to_le32((u32)value);
136 }
137 
138 void IDeserializer::NumberFloat_Unbounded(const char* name, float& out)
139 {
140  Get(name, (u8*)&out, sizeof(float));
141 }
142 
143 void IDeserializer::NumberDouble_Unbounded(const char* name, double& out)
144 {
145  Get(name, (u8*)&out, sizeof(double));
146 }
147 
149 {
150  int32_t n;
151  NumberI32_Unbounded(name, n);
152  out.SetInternalValue(n);
153 }
154 
155 void IDeserializer::Bool(const char* name, bool& out)
156 {
157  uint8_t i;
158  NumberU8(name, i, 0, 1);
159  out = (i != 0);
160 }
161 
162 void IDeserializer::StringASCII(const char* name, std::string& out, uint32_t minlength, uint32_t maxlength)
163 {
164  uint32_t len;
165  NumberU32("string length", len, minlength, maxlength);
166 
168  out.resize(len);
169  Get(name, (u8*)out.data(), len);
170 
171  for (size_t i = 0; i < out.length(); ++i)
172  if (out[i] == 0 || (unsigned char)out[i] >= 128)
174 }
175 
176 void IDeserializer::String(const char* name, std::wstring& out, uint32_t minlength, uint32_t maxlength)
177 {
178  std::string str;
179  uint32_t len;
180  NumberU32_Unbounded("string length", len);
181 
183  str.resize(len);
184  Get(name, (u8*)str.data(), len);
185 
186  Status err;
187  out = wstring_from_utf8(str, &err);
188  if (err)
190 
191  if (!(minlength <= out.length() && out.length() <= maxlength))
193 }
194 
195 void IDeserializer::RawBytes(const char* name, u8* data, size_t len)
196 {
197  Get(name, data, len);
198 }
199 
201 {
202  debug_warn(L"GetVersion() not implemented in this subclass");
203  return 0;
204 }
signed char int8_t
Definition: wposix_types.h:37
virtual void StringASCII(const char *name, std::string &out, uint32_t minlength, uint32_t maxlength)
#define u8
Definition: types.h:39
A simple fixed-point number class.
Definition: Fixed.h:115
virtual void Bool(const char *name, bool &out)
virtual void NumberDouble_Unbounded(const char *name, double &out)
#define i32
Definition: types.h:36
#define to_le16(x)
Definition: byte_order.h:77
short int16_t
Definition: wposix_types.h:38
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
virtual void RequireBytesInStream(size_t numBytes)=0
Throws an exception if the stream definitely cannot provide the required number of bytes...
virtual void NumberU32_Unbounded(const char *name, uint32_t &out)
virtual void NumberFixed_Unbounded(const char *name, fixed &out)
virtual void NumberI16(const char *name, int16_t &out, int16_t lower, int16_t upper)
virtual void NumberU8_Unbounded(const char *name, uint8_t &out)
virtual int GetVersion() const
#define i16
Definition: types.h:35
void SetInternalValue(T n)
Definition: Fixed.h:132
virtual void RawBytes(const char *name, u8 *data, size_t len)
virtual void NumberI8(const char *name, int8_t &out, int8_t lower, int8_t upper)
#define to_le32(x)
Definition: byte_order.h:78
unsigned char uint8_t
Definition: wposix_types.h:51
i64 Status
Error handling system.
Definition: status.h:171
std::wstring wstring_from_utf8(const std::string &src, Status *err)
convert UTF-8 to a wide string (UTF-16 or UCS-4, depending on the platform&#39;s wchar_t).
Definition: utf8.cpp:225
virtual ~IDeserializer()
#define u16
Definition: types.h:40
virtual void Get(const char *name, u8 *data, size_t len)=0
virtual void NumberU8(const char *name, uint8_t &out, uint8_t lower, uint8_t upper)
#define u32
Definition: types.h:41
virtual void NumberFloat_Unbounded(const char *name, float &out)
virtual void String(const char *name, std::wstring &out, uint32_t minlength, uint32_t maxlength)
unsigned int uint32_t
Definition: wposix_types.h:53
#define debug_warn(expr)
display the error dialog with the given text.
Definition: debug.h:324
virtual void NumberU16(const char *name, uint16_t &out, uint16_t lower, uint16_t upper)
virtual void NumberU32(const char *name, uint32_t &out, uint32_t lower, uint32_t upper)
unsigned short uint16_t
Definition: wposix_types.h:52
virtual void NumberI8_Unbounded(const char *name, int8_t &out)
virtual void NumberI16_Unbounded(const char *name, int16_t &out)
virtual void NumberU16_Unbounded(const char *name, uint16_t &out)
virtual void NumberI32(const char *name, int32_t &out, int32_t lower, int32_t upper)
virtual void NumberI32_Unbounded(const char *name, int32_t &out)