File: settings.cpp

package info (click to toggle)
postal1 2015.git20250526%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 14,024 kB
  • sloc: cpp: 130,877; ansic: 38,942; python: 874; makefile: 351; sh: 61
file content (508 lines) | stat: -rw-r--r-- 13,798 bytes parent folder | download | duplicates (2)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// 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, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// settings.cpp
// Project: Nostril (aka Postal)
//
// This module impliments the CSettings class, which provides a standardized
// method of working with all the various game settings.
//
// History:
//		11/26/96 MJR	Started.
//		12/04/96 MJR	Implimented all the static stuff.
//		04/16/97 MJR	Added test for read-only file when writing prefs, and
//							returned special error code to indicate such a thing.
//
//		05/08/97	JMI	Added conditions for compiler versions' STL
//							differences.
//
//		06/09/97 MJR	Fixed memory leak -- forgot to free the memory that was
//							allocated by PreDemo().
//
//		06/29/97 MJR	Replaced STL vector with an RSP list.  STL is an evil
//							entity that should be banished from the face of the earth.
//							Whoever suggested we use it should be shot.  (Good thing
//							I'm the president -- it's against the rules to shoot me.)
//
//		07/10/97 MJR	Removed use of non-ANSI "t" in mode string for file open.
//
////////////////////////////////////////////////////////////////////////////////
#define SETTINGS_CPP

#include "RSPiX.h"
#ifdef PATHS_IN_INCLUDES
	#include "WishPiX/Prefs/prefs.h"
#else
	#include "prefs.h"
#endif

#include "main.h"
#include "settings.h"


////////////////////////////////////////////////////////////////////////////////
// Macros/types/etc.
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Variables/data
////////////////////////////////////////////////////////////////////////////////

// Pointer to container for all CSettings objects.  This is a pointer to the
// container for reasons explained in the constructor.  I'm relying on what I
// understand to be the defined C++ startup sequence: (1) initialize all static
// data without explicit initializers to 0, then (2) initialize global static
// objects in a translation unit (which gets into the whole order problem).
// I take that to mean that this will be set to 0 before any code is executed.
CSettings::SETTINGS* CSettings::ms_pSettings;

// Pointer to memory used for memory file.  While it isn't as critical as
// the above pointer, this one isn't explicitly set to 0 for the same reasons.
void* CSettings::ms_pMem;


////////////////////////////////////////////////////////////////////////////////
// Function prototypes
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
//
// Default (and only) constructor
//
////////////////////////////////////////////////////////////////////////////////
CSettings::CSettings(void)
	{
	// If the container itself doesn't exist yet, create it now.  This sucks
	// because we can't inform the caller if an error occurs.  However, there
	// wasn't much choice, as the alternate (having this be the actual object
	// rather than a pointer to it) led to the typical C++ problem of not knowing
	// which would be initialized first -- the container or an object that wants
	// to use it.  This way, the first object that tries to use the container
	// will create the container, so that problem goes away.
	if (ms_pSettings == 0)
		ms_pSettings = new SETTINGS;
	if (ms_pSettings != 0)
		{

		// Add this object to container
		m_pointer = ms_pSettings->InsertTail(this);

		}
	else
		TRACE("CSettings::CSettings(): Couldn't create new container!\n");
	}


////////////////////////////////////////////////////////////////////////////////
//
// Destructor
//
////////////////////////////////////////////////////////////////////////////////
CSettings::~CSettings()
	{
	// Make sure container exists
	if (ms_pSettings != 0)
		{

		// Remove this object from container
		ms_pSettings->Remove(m_pointer);

		// If there are no more objects, then delete the container itself
		if (ms_pSettings->GetHead() == 0)
			{
			delete ms_pSettings;
			ms_pSettings = 0;
			}

		// Make sure to delete memory if it wasn't already deleted
		if (ms_pMem)
			{
			free(ms_pMem);
			ms_pMem = 0;
			}
		}
	}


////////////////////////////////////////////////////////////////////////////////
//
// Load all settings from preference file
//
////////////////////////////////////////////////////////////////////////////////
int16_t CSettings::LoadPrefs(					// Returns 0 if successfull, non-zero otherwise
	char* pszFile)									// In:  Name of prefs file
	{
	int16_t sResult = 0;

	// Make sure container exists
	if (ms_pSettings != 0)
		{

		// Open file for read access (text mode is default)
		RPrefs prefs;
		sResult = prefs.Open(pszFile, "r");
		if (sResult == 0)
			{

			// Do this for all CSettings objects
			for (SETTINGS::Pointer i = ms_pSettings->GetHead(); i != 0; i = ms_pSettings->GetNext(i))
				{
				sResult = ms_pSettings->GetData(i)->LoadPrefs(&prefs);
				if (sResult)
					break;
				}

			// If no errors detected, double-check to be sure no I/O errors occurred
			if (!sResult && prefs.IsError())
				{
				sResult = -1;
				TRACE("CSettings::LoadPrefs(): Error reading prefs file!\n");
				}

			prefs.Close();
			}
		else
			{
			sResult = -1;
			TRACE("CSettings::LoadPrefs(): Couldn't open prefs file: %s !\n", pszFile);
			}
		}
	else
		{
		sResult = -1;
		TRACE("CSettings::LoadPrefs(): No container!\n");
		}

	return sResult;
	}


////////////////////////////////////////////////////////////////////////////////
//
// Save all settings to preference file
//
// Return values:
//		0 = successfull
//		1 = read-only file (couldn't save)
//		<0 = some other error
//
////////////////////////////////////////////////////////////////////////////////
int16_t CSettings::SavePrefs(					// Returns 0 if successfull, non-zero otherwise
	char* pszFile)									// In:  Name of prefs file
	{
	int16_t sResult = 0;

	// Make sure container exists
	if (ms_pSettings != 0)
		{

		// First open file for read-only access.  If this works, then at least
		// we know the file exists, although it might be a read-only file.
		RPrefs prefs;
		sResult = prefs.Open(pszFile, "r");
		if (sResult == 0)
			{
			prefs.Close();

			// Open file again, this time for read+ (read plus write/append) access.
			// If this doesn't work, the file is most likely a read-only file.
			sResult = prefs.Open(pszFile, "r+");
			if (sResult == 0)
				{

				// Do this for all CSettings objects
				for (SETTINGS::Pointer i = ms_pSettings->GetHead(); i != 0; i = ms_pSettings->GetNext(i))
					{
					sResult = ms_pSettings->GetData(i)->SavePrefs(&prefs);
					if (sResult)
						break;
					}

				// If no errors detected, double-check to be sure no I/O errors occurred
				if (!sResult && prefs.IsError())
					{
					sResult = -1;
					TRACE("CSettings::SavePrefs(): Error writing prefs file!\n");
					}

				prefs.Close();
				}
			else
				{
				sResult = 1; // Special Read-Only error return!
				TRACE("CSettings::SavePrefs: Read-only prefs file: %s !\n", pszFile); 
				}
			}
		else
			{
			sResult = -1;
			TRACE("CSettings::SavePrefs(): Couldn't open prefs file: %s !\n", pszFile);
			}
		}
	else
		{
		sResult = -1;
		TRACE("CSettings::SavePrefs(): No container!\n");
		}

	return sResult;
	}


////////////////////////////////////////////////////////////////////////////////
//
// Load all settings from game file
//
////////////////////////////////////////////////////////////////////////////////
int16_t CSettings::LoadGame(						// Returns 0 if successfull, non-zero otherwise
	char* pszFile)									// In:  Name of prefs file
	{
	int16_t sResult = 0;

	// Make sure container exists
	if (ms_pSettings != 0)
		{

		// Open file for read access in binary mode
		RFile fileGame;
		sResult = fileGame.Open(pszFile, "rb", RFile::LittleEndian);
		if (sResult == 0)
			{

			// Do this for all CSettings objects
			for (SETTINGS::Pointer i = ms_pSettings->GetHead(); i != 0; i = ms_pSettings->GetNext(i))
				{
				sResult = ms_pSettings->GetData(i)->LoadGame(&fileGame);
				if (sResult)
					break;
				}

			// If no errors detected, double-check to be sure no I/O errors occurred
			if (!sResult && fileGame.Error())
				{
				sResult = -1;
				TRACE("CSettings::LoadGame(): Error reading game file!\n");
				}

			fileGame.Close();
			}
		else
			{
			sResult = -1;
			TRACE("CSettings::LoadGame(): Couldn't open game file: %s !\n", pszFile);
			}
		}
	else
		{
		sResult = -1;
		TRACE("CSettings::LoadGame(): No container!\n");
		}

	return sResult;
	}


////////////////////////////////////////////////////////////////////////////////
//
// Save all settings to game file
//
////////////////////////////////////////////////////////////////////////////////
int16_t CSettings::SaveGame(						// Returns 0 if successfull, non-zero otherwise
	char* pszFile)									// In:  Name of prefs file
	{
	int16_t sResult = 0;

	// Make sure container exists
	if (ms_pSettings != 0)
		{

		// Open file for write access in binary mode
		// Note: We don't care if the file already exists -- write mode will destroy it
		RFile fileGame;
		sResult = fileGame.Open(pszFile, "wb", RFile::LittleEndian);
		if (sResult == 0)
			{

			// Do this for all CSettings objects
			for (SETTINGS::Pointer i = ms_pSettings->GetHead(); i != 0; i = ms_pSettings->GetNext(i))
				{
				sResult = ms_pSettings->GetData(i)->SaveGame(&fileGame);
				if (sResult)
					break;
				}

			// If no errors detected, double-check to be sure no I/O errors occurred
			if (!sResult && fileGame.Error())
				{
				sResult = -1;
				TRACE("CSettings::SaveGame(): Error writing game file!\n");
				}

			fileGame.Close();
			}
		else
			{
			sResult = -1;
			TRACE("CSettings::SaveGame(): Couldn't open game file: %s !\n", pszFile);
			}
		}
	else
		{
		sResult = -1;
		TRACE("CSettings::SaveGame(): No container!\n");
		}

	return sResult;
	}


////////////////////////////////////////////////////////////////////////////////
//
// Temporarily set settings for demo mode
//
////////////////////////////////////////////////////////////////////////////////
int16_t CSettings::PreDemo(						// Returns 0 if successfull, non-zero otherwise
	void)
	{
	int16_t sResult = 0;

	// Make sure container exists
	if (ms_pSettings != 0)
		{

		// Allocate a chunk of memory for settings to be saved to
		ms_pMem = malloc(CSettings::MemFileSize);
		if (ms_pMem != 0)
			{

			// Open memory file
			RFile fileMem;
			sResult = fileMem.Open(ms_pMem, CSettings::MemFileSize, RFile::LittleEndian);
			if (sResult == 0)
				{

				// Do this for all CSettings objects
				for (SETTINGS::Pointer i = ms_pSettings->GetHead(); i != 0; i = ms_pSettings->GetNext(i))
					{
					sResult = ms_pSettings->GetData(i)->PreDemo(&fileMem);
					if (sResult)
						break;
					}

				// If no errors detected, double-check to be sure no I/O errors occurred
				if (!sResult && fileMem.Error())
					{
					sResult = -1;
					TRACE("CSettings::PreDemo(): Error writing to mem file (probably too much data)!\n");
					}

				fileMem.Close();
				}
			else
				{
				sResult = -1;
				TRACE("CSettings::PreDemo(): Couldn't open mem file!\n");
				}
			}
		else
			{
			sResult = -1;
			TRACE("CSettings::PreDemo(): Couldn't allocate memory for mem file!\n");
			}
		}
	else
		{
		sResult = -1;
		TRACE("CSettings::PreDemo(): No container!\n");
		}

	return sResult;
	}


////////////////////////////////////////////////////////////////////////////////
//
// Restore settings to what they were prior to demo mode
//
////////////////////////////////////////////////////////////////////////////////
int16_t CSettings::PostDemo(						// Returns 0 if successfull, non-zero otherwise
	void)
	{
	int16_t sResult = 0;

	// Make sure container exists
	if (ms_pSettings != 0)
		{

		// Make sure memory was allocated by PreDemo()
		if (ms_pMem != 0)
			{

			// Open previously allocated memory file
			RFile fileMem;
			sResult = fileMem.Open(ms_pMem, CSettings::MemFileSize, RFile::LittleEndian);
			if (sResult == 0)
				{

				// Do this for all CSettings objects
				for (SETTINGS::Pointer i = ms_pSettings->GetHead(); i != 0; i = ms_pSettings->GetNext(i))
					{
					sResult = ms_pSettings->GetData(i)->PostDemo(&fileMem);
					if (sResult)
						break;
					}

				// If no errors detected, double-check to be sure no I/O errors occurred
				if (!sResult && fileMem.Error())
					{
					sResult = -1;
					TRACE("CSettings::PostDemo(): Error reading from mem file!\n");
					}

				fileMem.Close();
				}
			else
				{
				sResult = -1;
				TRACE("CSettings::PostDemo(): Couldn't open mem file!\n");
				}

			// Free memory
			free(ms_pMem);
			ms_pMem = 0;
			}
		else
			{
			sResult = -1;
			TRACE("CSettings::PostDemo(): No memory file to read from! (did you forget to call CSettings::PreDemo?)\n");
			}
		}
	else
		{
		sResult = -1;
		TRACE("CSettings::PostDemo(): No container!\n");
		}

	return sResult;
	}


////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////