forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathBitFlagsIO.h
More file actions
232 lines (196 loc) · 6.15 KB
/
BitFlagsIO.h
File metadata and controls
232 lines (196 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: BitFlagsIO.h /////////////////////////////////////////////////////////////////////////////
// Author: Steven Johnson, March 2002
// Desc:
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "Common/BitFlags.h"
#include "Common/INI.h"
#include "Common/Xfer.h"
//-------------------------------------------------------------------------------------------------
/*
template <size_t NUMBITS>
void BitFlags<NUMBITS>::buildDescription( AsciiString* str ) const
{
if ( str == nullptr )
return;//sanity
for( Int i = 0; i < size(); ++i )
{
const char* bitName = getBitNameIfSet(i);
if (bitName != nullptr)
{
str->concat( bitName );
str->concat( ",\n");
}
}
}
*/
//-------------------------------------------------------------------------------------------------
template <size_t NUMBITS>
void BitFlags<NUMBITS>::parse(INI* ini, AsciiString* str)
{
// m_bits.reset();
if (str)
str->clear();
Bool foundNormal = false;
Bool foundAddOrSub = false;
// loop through all tokens
for (const char *token = ini->getNextTokenOrNull(); token != nullptr; token = ini->getNextTokenOrNull())
{
if (str)
{
if (str->isNotEmpty())
str->concat(" ");
str->concat(token);
}
if (stricmp(token, "NONE") == 0)
{
if (foundNormal || foundAddOrSub)
{
DEBUG_CRASH(("you may not mix normal and +- ops in bitstring lists"));
throw INI_INVALID_NAME_LIST;
}
clear();
break;
}
if (token[0] == '+')
{
if (foundNormal)
{
DEBUG_CRASH(("you may not mix normal and +- ops in bitstring lists"));
throw INI_INVALID_NAME_LIST;
}
Int bitIndex = INI::scanIndexList(token+1, s_bitNameList); // this throws if the token is not found
set(bitIndex, 1);
foundAddOrSub = true;
}
else if (token[0] == '-')
{
if (foundNormal)
{
DEBUG_CRASH(("you may not mix normal and +- ops in bitstring lists"));
throw INI_INVALID_NAME_LIST;
}
Int bitIndex = INI::scanIndexList(token+1, s_bitNameList); // this throws if the token is not found
set(bitIndex, 0);
foundAddOrSub = true;
}
else
{
if (foundAddOrSub)
{
DEBUG_CRASH(("you may not mix normal and +- ops in bitstring lists"));
throw INI_INVALID_NAME_LIST;
}
if (!foundNormal)
clear();
Int bitIndex = INI::scanIndexList(token, s_bitNameList); // this throws if the token is not found
set(bitIndex, 1);
foundNormal = true;
}
}
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
template <size_t NUMBITS>
/*static*/ void BitFlags<NUMBITS>::parseFromINI(INI* ini, void* /*instance*/, void *store, const void* /*userData*/)
{
((BitFlags*)store)->parse(ini, nullptr);
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
template <size_t NUMBITS>
/*static*/ void BitFlags<NUMBITS>::parseSingleBitFromINI(INI* ini, void* /*instance*/, void *store, const void* /*userData*/)
{
const char *token = ini->getNextToken();
Int bitIndex = INI::scanIndexList(token, s_bitNameList); // this throws if the token is not found
Int *storeAsInt = (Int*)store;
*storeAsInt = bitIndex;
}
//-------------------------------------------------------------------------------------------------
/** Xfer method
* Version Info:
* 1: Initial version */
//-------------------------------------------------------------------------------------------------
template <size_t NUMBITS>
void BitFlags<NUMBITS>::xfer(Xfer* xfer)
{
// this deserves a version number
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
if( xfer->getXferMode() == XFER_SAVE )
{
// save how many entries are to follow
Int c = count();
xfer->xferInt( &c );
// save each of the string data
for( Int i = 0; i < size(); ++i )
{
const char* bitName = getBitNameIfSet(i);
// ignore if this kindof is not set in our mask data
if (bitName == nullptr)
continue;
// this bit is set, write the string value
AsciiString bitNameA = bitName;
xfer->xferAsciiString( &bitNameA );
}
}
else if( xfer->getXferMode() == XFER_LOAD )
{
// clear the kind of mask data
clear();
// read how many entries follow
Int c;
xfer->xferInt( &c );
// read each of the string entries
AsciiString string;
for( Int i = 0; i < c; ++i )
{
// read ascii string
xfer->xferAsciiString( &string );
// set in our mask type data
Bool valid = setBitByName( string.str() );
if (!valid)
{
DEBUG_CRASH(("invalid bit name %s",string.str()));
throw XFER_READ_ERROR;
}
}
}
else if( xfer->getXferMode() == XFER_CRC )
{
// just call the xfer implementation on the data values
#if RETAIL_COMPATIBLE_CRC
xfer->xferUser( this, sizeof( this ) );
#else
xfer->xferUser( this, sizeof( *this ) );
#endif
}
else
{
DEBUG_CRASH(( "BitFlagsXfer - Unknown xfer mode '%d'", xfer->getXferMode() ));
throw XFER_MODE_UNKNOWN;
}
}