Line data Source code
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 :
24 141 : ISerializer::~ISerializer()
25 : {
26 141 : }
27 :
28 251 : void ISerializer::NumberU8(const char* name, uint8_t value, uint8_t lower, uint8_t upper)
29 : {
30 251 : if (!(lower <= value && value <= upper))
31 0 : throw PSERROR_Serialize_OutOfBounds();
32 251 : PutNumber(name, value);
33 251 : }
34 :
35 0 : void ISerializer::NumberI8(const char* name, int8_t value, int8_t lower, int8_t upper)
36 : {
37 0 : if (!(lower <= value && value <= upper))
38 0 : throw PSERROR_Serialize_OutOfBounds();
39 0 : PutNumber(name, value);
40 0 : }
41 :
42 0 : void ISerializer::NumberU16(const char* name, uint16_t value, uint16_t lower, uint16_t upper)
43 : {
44 0 : if (!(lower <= value && value <= upper))
45 0 : throw PSERROR_Serialize_OutOfBounds();
46 0 : PutNumber(name, value);
47 0 : }
48 :
49 0 : void ISerializer::NumberI16(const char* name, int16_t value, int16_t lower, int16_t upper)
50 : {
51 0 : if (!(lower <= value && value <= upper))
52 0 : throw PSERROR_Serialize_OutOfBounds();
53 0 : PutNumber(name, value);
54 0 : }
55 :
56 17 : void ISerializer::NumberU32(const char* name, uint32_t value, uint32_t lower, uint32_t upper)
57 : {
58 17 : if (!(lower <= value && value <= upper))
59 0 : throw PSERROR_Serialize_OutOfBounds();
60 17 : PutNumber(name, value);
61 17 : }
62 :
63 9 : void ISerializer::NumberI32(const char* name, int32_t value, int32_t lower, int32_t upper)
64 : {
65 9 : if (!(lower <= value && value <= upper))
66 2 : throw PSERROR_Serialize_OutOfBounds();
67 7 : PutNumber(name, value);
68 7 : }
69 :
70 29 : void ISerializer::StringASCII(const char* name, const std::string& value, uint32_t minlength, uint32_t maxlength)
71 : {
72 29 : if (!(minlength <= value.length() && value.length() <= maxlength))
73 0 : throw PSERROR_Serialize_OutOfBounds();
74 :
75 320 : for (size_t i = 0; i < value.length(); ++i)
76 291 : if (value[i] == 0 || (unsigned char)value[i] >= 128)
77 0 : throw PSERROR_Serialize_InvalidCharInString();
78 :
79 29 : PutString(name, value);
80 29 : }
81 :
82 19 : void ISerializer::String(const char* name, const std::wstring& value, uint32_t minlength, uint32_t maxlength)
83 : {
84 19 : if (!(minlength <= value.length() && value.length() <= maxlength))
85 0 : throw PSERROR_Serialize_OutOfBounds();
86 :
87 : Status err;
88 38 : std::string str = utf8_from_wstring(value, &err);
89 19 : if (err)
90 0 : throw PSERROR_Serialize_InvalidCharInString();
91 :
92 19 : PutString(name, str);
93 19 : }
94 :
95 118 : void ISerializer::ScriptVal(const char* name, JS::MutableHandleValue value)
96 : {
97 118 : PutScriptVal(name, value);
98 115 : }
99 :
100 235 : void ISerializer::RawBytes(const char* name, const u8* data, size_t len)
101 : {
102 235 : PutRaw(name, data, len);
103 235 : }
104 :
105 12 : bool ISerializer::IsDebug() const
106 : {
107 12 : return false;
108 : }
|