Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ISerializer.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 "ISerializer.h"
21 
22 #include "lib/utf8.h"
23 
25 {
26 }
27 
28 void ISerializer::NumberU8(const char* name, uint8_t value, uint8_t lower, uint8_t upper)
29 {
30  if (!(lower <= value && value <= upper))
32  PutNumber(name, value);
33 }
34 
35 void ISerializer::NumberI8(const char* name, int8_t value, int8_t lower, int8_t upper)
36 {
37  if (!(lower <= value && value <= upper))
39  PutNumber(name, value);
40 }
41 
42 void ISerializer::NumberU16(const char* name, uint16_t value, uint16_t lower, uint16_t upper)
43 {
44  if (!(lower <= value && value <= upper))
46  PutNumber(name, value);
47 }
48 
49 void ISerializer::NumberI16(const char* name, int16_t value, int16_t lower, int16_t upper)
50 {
51  if (!(lower <= value && value <= upper))
53  PutNumber(name, value);
54 }
55 
56 void ISerializer::NumberU32(const char* name, uint32_t value, uint32_t lower, uint32_t upper)
57 {
58  if (!(lower <= value && value <= upper))
60  PutNumber(name, value);
61 }
62 
63 void ISerializer::NumberI32(const char* name, int32_t value, int32_t lower, int32_t upper)
64 {
65  if (!(lower <= value && value <= upper))
67  PutNumber(name, value);
68 }
69 
70 void ISerializer::StringASCII(const char* name, const std::string& value, uint32_t minlength, uint32_t maxlength)
71 {
72  if (!(minlength <= value.length() && value.length() <= maxlength))
74 
75  for (size_t i = 0; i < value.length(); ++i)
76  if (value[i] == 0 || (unsigned char)value[i] >= 128)
78 
79  PutString(name, value);
80 }
81 
82 void ISerializer::String(const char* name, const std::wstring& value, uint32_t minlength, uint32_t maxlength)
83 {
84  if (!(minlength <= value.length() && value.length() <= maxlength))
86 
87  Status err;
88  std::string str = utf8_from_wstring(value, &err);
89  if (err)
91 
92  PutString(name, str);
93 }
94 
95 void ISerializer::ScriptVal(const char* name, jsval value)
96 {
97  PutScriptVal(name, value);
98 }
99 
100 void ISerializer::ScriptVal(const char* name, CScriptVal value)
101 {
102  PutScriptVal(name, value.get());
103 }
104 
105 void ISerializer::ScriptVal(const char* name, CScriptValRooted value)
106 {
107  PutScriptVal(name, value.get());
108 }
109 
110 void ISerializer::RawBytes(const char* name, const u8* data, size_t len)
111 {
112  PutRaw(name, data, len);
113 }
114 
116 {
117  return false;
118 }
signed char int8_t
Definition: wposix_types.h:37
#define u8
Definition: types.h:39
virtual bool IsDebug() const
Returns true if the serializer is being used in debug mode.
std::string utf8_from_wstring(const std::wstring &src, Status *err)
opposite of wstring_from_utf8
Definition: utf8.cpp:208
virtual void PutRaw(const char *name, const u8 *data, size_t len)=0
short int16_t
Definition: wposix_types.h:38
const jsval & get() const
Returns the current value.
Definition: ScriptVal.h:38
A trivial wrapper around a jsval.
Definition: ScriptVal.h:29
void ScriptVal(const char *name, jsval value)
Serialize a jsval.
Definition: ISerializer.cpp:95
void NumberI8(const char *name, int8_t value, int8_t lower, int8_t upper)
Definition: ISerializer.cpp:35
void NumberI32(const char *name, int32_t value, int32_t lower, int32_t upper)
Serialize a number, which must be lower &lt;= value &lt;= upper.
Definition: ISerializer.cpp:63
virtual void PutString(const char *name, const std::string &value)=0
void NumberI16(const char *name, int16_t value, int16_t lower, int16_t upper)
Serialize a number, which must be lower &lt;= value &lt;= upper.
Definition: ISerializer.cpp:49
virtual void PutNumber(const char *name, uint8_t value)=0
void StringASCII(const char *name, const std::string &value, uint32_t minlength, uint32_t maxlength)
Serialize an ASCII string.
Definition: ISerializer.cpp:70
unsigned char uint8_t
Definition: wposix_types.h:51
i64 Status
Error handling system.
Definition: status.h:171
virtual void PutScriptVal(const char *name, jsval value)=0
void String(const char *name, const std::wstring &value, uint32_t minlength, uint32_t maxlength)
Serialize a Unicode string.
Definition: ISerializer.cpp:82
void RawBytes(const char *name, const u8 *data, size_t len)
Serialize a stream of bytes.
void NumberU16(const char *name, uint16_t value, uint16_t lower, uint16_t upper)
Serialize a number, which must be lower &lt;= value &lt;= upper.
Definition: ISerializer.cpp:42
unsigned int uint32_t
Definition: wposix_types.h:53
virtual ~ISerializer()
Definition: ISerializer.cpp:24
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Definition: ScriptVal.cpp:45
void NumberU32(const char *name, uint32_t value, uint32_t lower, uint32_t upper)
Serialize a number, which must be lower &lt;= value &lt;= upper.
Definition: ISerializer.cpp:56
unsigned short uint16_t
Definition: wposix_types.h:52
void NumberU8(const char *name, uint8_t value, uint8_t lower, uint8_t upper)
Serialize a number, which must be lower &lt;= value &lt;= upper.
Definition: ISerializer.cpp:28