summaryrefslogtreecommitdiff
path: root/app/bin/paramfile.c
blob: 2dd9ac72a1c67d7f0c3911a03147d2e58821a14f (plain)
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
/** \file paramfile.c
 * Handling of parameter files
 */

 /*  XTrackkCad - Model Railroad CAD
  *  Copyright (C) 2019 Martin Fischer
  *
  *  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 2 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, write to the Free Software
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "compound.h"
#include "ctrain.h"
#include "custom.h"
#include "fileio.h"
#include "i18n.h"
#include "layout.h"
#include "messages.h"
#include "misc2.h"
#include "paths.h"
#include "include/paramfile.h"
#include "include/paramfilelist.h"
#include "include/utf8convert.h"

#if _MSC_VER >1300
#define stricmp( a, b ) _stricmp(a, b )
#endif

static long paramCheckSum;

typedef enum paramFileState(*GetCompatibilityFunction)(int index,
	SCALEINX_T scale);

GetCompatibilityFunction GetCompatibility[] = {
	GetTrackCompatibility,
	GetStructureCompatibility,
	GetCarProtoCompatibility,
	GetCarPartCompatibility
};

#define COMPATIBILITYCHECKSCOUNT (sizeof(GetCompatibility)/sizeof(GetCompatibility[0]))

/**
 * Check whether parameter file is still loaded
 *
 * \param fileInx
 * \return TRUE if loaded, FALSE otherwise
 */

wBool_t IsParamValid(
	int fileInx)
{
	if (fileInx == PARAM_DEMO) {
		return (curDemo >= 0);
	} else if (fileInx == PARAM_CUSTOM) {
		return TRUE;
	} else if (fileInx == PARAM_LAYOUT) {
		return TRUE;
	} else if (fileInx >= 0 && fileInx < paramFileInfo_da.cnt) {
		return (!paramFileInfo(fileInx).deleted);
	} else {
		return FALSE;
	}
}

char *GetParamFileDir(void)
{
	return (GetCurrentPath(PARAMETERPATHKEY));
}

void
SetParamFileDir(char *fullPath)
{
	SetCurrentPath(PARAMETERPATHKEY, fullPath);
}

char * GetParamFileName(
	int fileInx)
{
	return paramFileInfo(fileInx).name;
}

char * GetParamFileContents(
	int fileInx)
{
	return paramFileInfo(fileInx).contents;
}

bool IsParamFileDeleted(int inx)
{
	return paramFileInfo(inx).deleted;
}

bool IsParamFileFavorite(int inx)
{
	return paramFileInfo(inx).favorite;
}

void SetParamFileDeleted(int fileInx, bool deleted)
{
	paramFileInfo(fileInx).deleted = deleted;
}

void SetParamFileFavorite(int fileInx, bool favorite)
{
	paramFileInfo(fileInx).favorite = favorite;
}

void ParamCheckSumLine(char * line)
{
	long mult = 1;
	while (*line) {
		paramCheckSum += (((long)(*line++)) & 0xFF)*(mult++);
	}
}

/**
 * Set the compatibility state of a parameter file
 *
 * \param index parameter file number in list
 * \return
 */

void SetParamFileState(int index)
{
	enum paramFileState state = PARAMFILE_NOTUSABLE;
	enum paramFileState newState;
	SCALEINX_T scale = GetLayoutCurScale();

	for (int i = 0; i < COMPATIBILITYCHECKSCOUNT && state < PARAMFILE_FIT &&
		state != PARAMFILE_UNLOADED; i++) {
		newState = (*GetCompatibility[i])(index, scale);
		if (newState > state || newState == PARAMFILE_UNLOADED) {
			state = newState;
		}
	}

	paramFileInfo(index).trackState = state;
}

/**
 * Read a single parameter file and update the parameter file list
 *
 * \param fileName full path for parameter file
 * \return
 */

int
ReadParamFile(const char *fileName)
{
	DYNARR_APPEND(paramFileInfo_t, paramFileInfo_da, 10);
	curParamFileIndex = paramFileInfo_da.cnt - 1;
	paramFileInfo(curParamFileIndex).name = MyStrdup(fileName);
	paramFileInfo(curParamFileIndex).valid = TRUE;
	paramFileInfo(curParamFileIndex).deleted = !ReadParams(0, NULL, fileName);
	paramFileInfo(curParamFileIndex).contents = MyStrdup(curContents);

	SetParamFileState(curParamFileIndex);

	return (curParamFileIndex);
}

/**
 * Reload a single parameter file that had been unloaded before.
 *
 * \param  fileindex index of previously created paramFileInfo
 *
 * \returns
 */

int
ReloadDeletedParamFile(int fileindex)
{
	curParamFileIndex = fileindex;
	paramFileInfo(curParamFileIndex).valid = TRUE;
	paramFileInfo(curParamFileIndex).deleted = !ReadParams(0, NULL, paramFileInfo(curParamFileIndex).name);
	paramFileInfo(curParamFileIndex).contents = MyStrdup(curContents);

	SetParamFileState(curParamFileIndex);

	return (curParamFileIndex);
}

/**
 * Parameter file reader and interpreter
 *
 * \param key unused
 * \param dirName prefix for parameter file path
 * \param fileName name of parameter file
 * \return
 */

bool ReadParams(
	long key,
	const char * dirName,
	const char * fileName)
{
	FILE * oldFile;
	char *cp;
	wIndex_t oldLineNum;
	wIndex_t pc;
	long oldCheckSum;
	long checkSum = 0;
	BOOL_T checkSummed;
	paramVersion = -1;
	char *oldLocale = NULL;

	if (dirName) {
		MakeFullpath(&paramFileName, dirName, fileName, NULL);
	} else {
		MakeFullpath(&paramFileName, fileName, NULL);
	}
	paramLineNum = 0;
	curBarScale = -1;
	curContents = MyStrdup(fileName);
	curSubContents = curContents;

	//LOG1( log_paramFile, ("ReadParam( %s )\n", fileName ) )

	oldLocale = SaveLocale("C");

	paramFile = fopen(paramFileName, "r");
	if (paramFile == NULL) {
		/* Reset the locale settings */
		RestoreLocale(oldLocale);

		NoticeMessage(MSG_OPEN_FAIL, _("Continue"), NULL, _("Parameter"), paramFileName,
			strerror(errno));

		return FALSE;
	}
	paramCheckSum = key;
	paramLineNum = 0;
	checkSummed = FALSE;
	BOOL_T skip = false;
	int skipLines = 0;
	while (paramFile && (fgets(paramLine, 256, paramFile)) != NULL) {
		paramLineNum++;
		Stripcr(paramLine);
		if (strncmp(paramLine, "CHECKSUM ", 9) == 0) {
			checkSum = atol(paramLine + 9);
			checkSummed = TRUE;
			goto nextLine;
		}
		ParamCheckSumLine(paramLine);
		if (paramLine[0] == '#') {
			/* comment */
		} else if (paramLine[0] == 0) {
			/* empty paramLine */
		} else if (strncmp(paramLine, "INCLUDE ", 8) == 0) {
			cp = &paramLine[8];
			while (*cp && isspace((unsigned char)*cp)) {
				cp++;
			}
			if (!*cp) {
				InputError("INCLUDE - no file name", TRUE);

				/* Close file and reset the locale settings */
				if (paramFile) {
					fclose(paramFile);
				}
				RestoreLocale(oldLocale);

				return FALSE;
			}
			oldFile = paramFile;
			oldLineNum = paramLineNum;
			oldCheckSum = paramCheckSum;
			if (!ReadParams(key, dirName, cp)) {
				RestoreLocale(oldLocale);
				return FALSE;
			}
			paramFile = oldFile;
			paramLineNum = oldLineNum;
			paramCheckSum = oldCheckSum;
			if (dirName) {
				MakeFullpath(&paramFileName, dirName, fileName, NULL);
			} else {
				MakeFullpath(&paramFileName, fileName);
			}
			skip = FALSE;
		} else if (strncmp(paramLine, "CONTENTS ", 9) == 0) {
#ifdef WINDOWS
			ConvertUTF8ToSystem(paramLine + 9);
#endif
			curContents = MyStrdup(paramLine + 9);
			curSubContents = curContents;
			skip = FALSE;
		} else if (strncmp(paramLine, "SUBCONTENTS ", 12) == 0) {
#ifdef WINDOWS
			ConvertUTF8ToSystem(paramLine + 12);
#endif // WINDOWS
			curSubContents = MyStrdup(paramLine + 12);
			skip = FALSE;
		} else if (strncmp(paramLine, "PARAM ", 6) == 0) {
			paramVersion = strtol(paramLine + 8, &cp, 10);
			if (cp)
				while (*cp && isspace((unsigned char)*cp)) cp++;
			if (paramVersion > iParamVersion) {
				if (cp && *cp) {
					NoticeMessage(MSG_PARAM_UPGRADE_VERSION1, _("Ok"), NULL, paramVersion, iParamVersion, sProdName, cp);
				} else {
					NoticeMessage(MSG_PARAM_UPGRADE_VERSION2, _("Ok"), NULL, paramVersion, iParamVersion, sProdName);
				}
				break;
			}
			if (paramVersion < iMinParamVersion) {
				NoticeMessage(MSG_PARAM_BAD_FILE_VERSION, _("Ok"), NULL, paramVersion, iMinParamVersion, sProdName);
				break;
			}
		} else if (skip && (strncmp(paramLine, "  ", 1) == 0)) {
			//Always skip to next line starting in LeftHandColumn
			skipLines++;
			goto nextLine;
		} else {
			for (pc = 0; pc < paramProc_da.cnt; pc++) {
				if (strncmp(paramLine, paramProc(pc).name,
					strlen(paramProc(pc).name)) == 0) {
					skip = FALSE;					//Stop skip so we re-message
					paramProc(pc).proc(paramLine);
					goto nextLine;
				}
			}
			if (!skip) {
				if (InputError(_("Unknown param file line - skip until next good object?"), TRUE)) {   //OK to carry on
					/* SKIP until next main line we recognize */
					skip = TRUE;
					skipLines++;
					goto nextLine;
				} else {
					if (skipLines > 0)
						NoticeMessage(MSG_PARAM_LINES_SKIPPED, _("Ok"), NULL, paramFileName, skipLines);
					if (paramFile) {
						fclose(paramFile);
						paramFile = NULL;
					}
					if (paramFileName) {
						free(paramFileName);
						paramFileName = NULL;
					}
					RestoreLocale(oldLocale);
					return FALSE;
				}
			}
			skipLines++;
		}
	nextLine:
		;
	}
	if (key) {
		if (!checkSummed || checkSum != paramCheckSum) {
			/* Close file and reset the locale settings */
			if (paramFile) {
				fclose(paramFile);
			}
			RestoreLocale(oldLocale);

			NoticeMessage(MSG_PROG_CORRUPTED, _("Ok"), NULL, paramFileName);

			return FALSE;
		}
	}
	if (skipLines > 0)
		NoticeMessage(MSG_PARAM_LINES_SKIPPED, _("Ok"), NULL, paramFileName, skipLines);
	if (paramFile) {
		fclose(paramFile);
	}
	free(paramFileName);
	paramFileName = NULL;
	RestoreLocale(oldLocale);

	return TRUE;
}