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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2007 by Bradford W. Mott and the Stella team
//
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: Props.cxx,v 1.20 2007/02/06 23:34:33 stephena Exp $
//============================================================================
#include <cctype>
#include <algorithm>
#include <sstream>
#include <string>
using namespace std;
#include "Props.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Properties::Properties()
{
setDefaults();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Properties::Properties(const Properties& properties)
{
copy(properties);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Properties::~Properties()
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const string& Properties::get(PropertyType key) const
{
if(key >= 0 && key < LastPropType)
return myProperties[key];
else {
static std::string EmptyString("");
return EmptyString;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::set(PropertyType key, const string& value)
{
if(key >= 0 && key < LastPropType)
{
myProperties[key] = value;
switch(key)
{
case Cartridge_Sound:
case Cartridge_Type:
case Console_LeftDifficulty:
case Console_RightDifficulty:
case Console_TelevisionType:
case Console_SwapPorts:
case Controller_Left:
case Controller_Right:
case Controller_SwapPaddles:
case Display_Format:
case Display_Phosphor:
case Emulation_HmoveBlanks:
{
transform(myProperties[key].begin(), myProperties[key].end(),
myProperties[key].begin(), (int(*)(int)) toupper);
break;
}
case Display_PPBlend:
{
int blend = atoi(myProperties[key].c_str());
if(blend < 0 || blend > 100) blend = 77;
ostringstream buf;
buf << blend;
myProperties[key] = buf.str();
break;
}
default:
break;
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::load(istream& in)
{
setDefaults();
string line, key, value;
string::size_type one, two, three, four, garbage;
// Loop reading properties
while(getline(in, line))
{
// Strip all tabs from the line
while((garbage = line.find("\t")) != string::npos)
line.erase(garbage, 1);
// Ignore commented and empty lines
if((line.length() == 0) || (line[0] == ';'))
continue;
// End of this record
if(line == "\"\"")
break;
one = line.find("\"", 0);
two = line.find("\"", one + 1);
three = line.find("\"", two + 1);
four = line.find("\"", three + 1);
// Invalid line if it doesn't contain 4 quotes
if((one == string::npos) || (two == string::npos) ||
(three == string::npos) || (four == string::npos))
break;
// Otherwise get the key and value
key = line.substr(one + 1, two - one - 1);
value = line.substr(three + 1, four - three - 1);
// Set the property
PropertyType type = getPropertyType(key);
set(type, value);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::save(ostream& out) const
{
// Write out each of the key and value pairs
bool changed = false;
for(int i = 0; i < LastPropType; ++i)
{
// Try to save some space by only saving the items that differ from default
if(myProperties[i] != ourDefaultProperties[i])
{
writeQuotedString(out, ourPropertyNames[i]);
out.put(' ');
writeQuotedString(out, myProperties[i]);
out.put('\n');
changed = true;
}
}
if(changed)
{
// Put a trailing null string so we know when to stop reading
writeQuotedString(out, "");
out.put('\n');
out.put('\n');
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string Properties::readQuotedString(istream& in)
{
char c;
// Read characters until we see a quote
while(in.get(c))
{
if(c == '"')
{
break;
}
}
// Read characters until we see the close quote
string s;
while(in.get(c))
{
if((c == '\\') && (in.peek() == '"'))
{
in.get(c);
}
else if((c == '\\') && (in.peek() == '\\'))
{
in.get(c);
}
else if(c == '"')
{
break;
}
else if(c == '\r')
{
continue;
}
s += c;
}
return s;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::writeQuotedString(ostream& out, const string& s)
{
out.put('"');
for(uInt32 i = 0; i < s.length(); ++i)
{
if(s[i] == '\\')
{
out.put('\\');
out.put('\\');
}
else if(s[i] == '\"')
{
out.put('\\');
out.put('"');
}
else
{
out.put(s[i]);
}
}
out.put('"');
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Properties& Properties::operator = (const Properties& properties)
{
// Do the assignment only if this isn't a self assignment
if(this != &properties)
{
// Now, make myself a copy of the given object
copy(properties);
}
return *this;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::copy(const Properties& properties)
{
// Now, copy each property from properties
for(int i = 0; i < LastPropType; ++i)
myProperties[i] = properties.myProperties[i];
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::print() const
{
cerr << get(Cartridge_MD5) << "|"
<< get(Cartridge_Name) << "|"
<< get(Cartridge_Rarity) << "|"
<< get(Cartridge_Manufacturer) << "|"
<< get(Cartridge_Note)
<< endl;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::setDefaults()
{
for(int i = 0; i < LastPropType; ++i)
myProperties[i] = ourDefaultProperties[i];
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyType Properties::getPropertyType(const string& name)
{
for(int i = 0; i < LastPropType; ++i)
if(ourPropertyNames[i] == name)
return (PropertyType)i;
// Otherwise, indicate that the item wasn't found
return LastPropType;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const char* Properties::ourDefaultProperties[LastPropType] = {
"", // Cartridge.MD5
"", // Cartridge.Manufacturer
"", // Cartridge.ModelNo
"Untitled", // Cartridge.Name
"", // Cartridge.Note
"", // Cartridge.Rarity
"MONO", // Cartridge.Sound
"AUTO-DETECT", // Cartridge.Type
"B", // Console.LeftDifficulty
"B", // Console.RightDifficulty
"COLOR", // Console.TelevisionType
"NO", // Console.SwapPorts
"JOYSTICK", // Controller.Left
"JOYSTICK", // Controller.Right
"NO", // Controller.SwapPaddles
"AUTO-DETECT", // Display.Format
"34", // Display.YStart
"210", // Display.Height
"NO", // Display.Phosphor
"77", // Display.PPBlend
"YES" // Emulation.HmoveBlanks
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const char* Properties::ourPropertyNames[LastPropType] = {
"Cartridge.MD5",
"Cartridge.Manufacturer",
"Cartridge.ModelNo",
"Cartridge.Name",
"Cartridge.Note",
"Cartridge.Rarity",
"Cartridge.Sound",
"Cartridge.Type",
"Console.LeftDifficulty",
"Console.RightDifficulty",
"Console.TelevisionType",
"Console.SwapPorts",
"Controller.Left",
"Controller.Right",
"Controller.SwapPaddles",
"Display.Format",
"Display.YStart",
"Display.Height",
"Display.Phosphor",
"Display.PPBlend",
"Emulation.HmoveBlanks"
};