summaryrefslogtreecommitdiff
path: root/app/bin/paramfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/bin/paramfile.c')
-rw-r--r--app/bin/paramfile.c191
1 files changed, 108 insertions, 83 deletions
diff --git a/app/bin/paramfile.c b/app/bin/paramfile.c
index 2dd9ac7..994a47d 100644
--- a/app/bin/paramfile.c
+++ b/app/bin/paramfile.c
@@ -2,54 +2,40 @@
* 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>
+/* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
#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
+#include "common-ui.h"
static long paramCheckSum;
typedef enum paramFileState(*GetCompatibilityFunction)(int index,
- SCALEINX_T scale);
+ SCALEINX_T scale);
GetCompatibilityFunction GetCompatibility[] = {
GetTrackCompatibility,
@@ -58,7 +44,7 @@ GetCompatibilityFunction GetCompatibility[] = {
GetCarPartCompatibility
};
-#define COMPATIBILITYCHECKSCOUNT (sizeof(GetCompatibility)/sizeof(GetCompatibility[0]))
+#define COMPATIBILITYCHECKSCOUNT COUNT(GetCompatibility)
/**
* Check whether parameter file is still loaded
@@ -68,7 +54,7 @@ GetCompatibilityFunction GetCompatibility[] = {
*/
wBool_t IsParamValid(
- int fileInx)
+ int fileInx)
{
if (fileInx == PARAM_DEMO) {
return (curDemo >= 0);
@@ -95,13 +81,13 @@ SetParamFileDir(char *fullPath)
}
char * GetParamFileName(
- int fileInx)
+ int fileInx)
{
return paramFileInfo(fileInx).name;
}
char * GetParamFileContents(
- int fileInx)
+ int fileInx)
{
return paramFileInfo(fileInx).contents;
}
@@ -147,11 +133,14 @@ void SetParamFileState(int index)
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;
+ //Set yet?
+ if (scale>=0) {
+ 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;
+ }
}
}
@@ -159,6 +148,24 @@ void SetParamFileState(int index)
}
/**
+ * Check whether file exists and is readable
+ *
+ * \param file The file.
+ *
+ * \returns True if it succeeds, false if it fails.
+ */
+
+static bool
+CheckFileReadable(const char *file)
+{
+ if(!access( file, R_OK )) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}
+
+/**
* Read a single parameter file and update the parameter file list
*
* \param fileName full path for parameter file
@@ -168,15 +175,18 @@ void SetParamFileState(int index)
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);
-
+ if (!CheckFileReadable(fileName)) {
+ return(-1);
+ } else {
+ 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);
}
@@ -193,7 +203,8 @@ ReloadDeletedParamFile(int fileindex)
{
curParamFileIndex = fileindex;
paramFileInfo(curParamFileIndex).valid = TRUE;
- paramFileInfo(curParamFileIndex).deleted = !ReadParams(0, NULL, paramFileInfo(curParamFileIndex).name);
+ paramFileInfo(curParamFileIndex).deleted = !ReadParams(0, NULL,
+ paramFileInfo(curParamFileIndex).name);
paramFileInfo(curParamFileIndex).contents = MyStrdup(curContents);
SetParamFileState(curParamFileIndex);
@@ -211,9 +222,9 @@ ReloadDeletedParamFile(int fileindex)
*/
bool ReadParams(
- long key,
- const char * dirName,
- const char * fileName)
+ long key,
+ const char * dirName,
+ const char * fileName)
{
FILE * oldFile;
char *cp;
@@ -222,8 +233,8 @@ bool ReadParams(
long oldCheckSum;
long checkSum = 0;
BOOL_T checkSummed;
+ BOOL_T bFoundContents = FALSE;
paramVersion = -1;
- char *oldLocale = NULL;
if (dirName) {
MakeFullpath(&paramFileName, dirName, fileName, NULL);
@@ -237,15 +248,15 @@ bool ReadParams(
//LOG1( log_paramFile, ("ReadParam( %s )\n", fileName ) )
- oldLocale = SaveLocale("C");
+ SetCLocale();
paramFile = fopen(paramFileName, "r");
if (paramFile == NULL) {
/* Reset the locale settings */
- RestoreLocale(oldLocale);
+ SetUserLocale();
NoticeMessage(MSG_OPEN_FAIL, _("Continue"), NULL, _("Parameter"), paramFileName,
- strerror(errno));
+ strerror(errno));
return FALSE;
}
@@ -254,7 +265,7 @@ bool ReadParams(
checkSummed = FALSE;
BOOL_T skip = false;
int skipLines = 0;
- while (paramFile && (fgets(paramLine, 256, paramFile)) != NULL) {
+ while (paramFile && (fgets(paramLine, 1024, paramFile)) != NULL) {
paramLineNum++;
Stripcr(paramLine);
if (strncmp(paramLine, "CHECKSUM ", 9) == 0) {
@@ -278,16 +289,16 @@ bool ReadParams(
/* Close file and reset the locale settings */
if (paramFile) {
fclose(paramFile);
+ paramFile = NULL;
}
- RestoreLocale(oldLocale);
-
+ SetUserLocale();
return FALSE;
}
oldFile = paramFile;
oldLineNum = paramLineNum;
oldCheckSum = paramCheckSum;
if (!ReadParams(key, dirName, cp)) {
- RestoreLocale(oldLocale);
+ SetUserLocale();
return FALSE;
}
paramFile = oldFile;
@@ -300,32 +311,39 @@ bool ReadParams(
}
skip = FALSE;
} else if (strncmp(paramLine, "CONTENTS ", 9) == 0) {
-#ifdef WINDOWS
+#ifdef UTFCONVERT
ConvertUTF8ToSystem(paramLine + 9);
#endif
- curContents = MyStrdup(paramLine + 9);
- curSubContents = curContents;
+ if ( bFoundContents == FALSE ) {
+ // Only use the first CONTENTS
+ curContents = MyStrdup(paramLine + 9);
+ curSubContents = curContents;
+ bFoundContents = TRUE;
+ }
skip = FALSE;
} else if (strncmp(paramLine, "SUBCONTENTS ", 12) == 0) {
-#ifdef WINDOWS
+#ifdef UTFCONVERT
ConvertUTF8ToSystem(paramLine + 12);
-#endif // WINDOWS
+#endif // UTFCONVERT
curSubContents = MyStrdup(paramLine + 12);
skip = FALSE;
} else if (strncmp(paramLine, "PARAM ", 6) == 0) {
- paramVersion = strtol(paramLine + 8, &cp, 10);
+ paramVersion = strtol(paramLine + 6, &cp, 10);
if (cp)
- while (*cp && isspace((unsigned char)*cp)) 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);
+ NoticeMessage(MSG_PARAM_UPGRADE_VERSION1, _("Ok"), NULL, paramVersion,
+ iParamVersion, sProdName, cp);
} else {
- NoticeMessage(MSG_PARAM_UPGRADE_VERSION2, _("Ok"), NULL, paramVersion, iParamVersion, sProdName);
+ 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);
+ NoticeMessage(MSG_PARAM_BAD_FILE_VERSION, _("Ok"), NULL, paramVersion,
+ iMinParamVersion, sProdName);
break;
}
} else if (skip && (strncmp(paramLine, " ", 1) == 0)) {
@@ -335,21 +353,24 @@ bool ReadParams(
} else {
for (pc = 0; pc < paramProc_da.cnt; pc++) {
if (strncmp(paramLine, paramProc(pc).name,
- strlen(paramProc(pc).name)) == 0) {
+ 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
+ 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 (skipLines > 0) {
+ NoticeMessage(MSG_PARAM_LINES_SKIPPED, _("Ok"), NULL, paramFileName,
+ skipLines);
+ }
if (paramFile) {
fclose(paramFile);
paramFile = NULL;
@@ -358,13 +379,13 @@ bool ReadParams(
free(paramFileName);
paramFileName = NULL;
}
- RestoreLocale(oldLocale);
+ SetUserLocale();
return FALSE;
}
}
skipLines++;
}
- nextLine:
+nextLine:
;
}
if (key) {
@@ -372,22 +393,26 @@ bool ReadParams(
/* Close file and reset the locale settings */
if (paramFile) {
fclose(paramFile);
+ paramFile = NULL;
}
- RestoreLocale(oldLocale);
+ SetUserLocale();
NoticeMessage(MSG_PROG_CORRUPTED, _("Ok"), NULL, paramFileName);
return FALSE;
}
}
- if (skipLines > 0)
- NoticeMessage(MSG_PARAM_LINES_SKIPPED, _("Ok"), NULL, paramFileName, skipLines);
+ if (skipLines > 0) {
+ NoticeMessage(MSG_PARAM_LINES_SKIPPED, _("Ok"), NULL, paramFileName,
+ skipLines);
+ }
if (paramFile) {
fclose(paramFile);
+ paramFile = NULL;
}
free(paramFileName);
paramFileName = NULL;
- RestoreLocale(oldLocale);
+ SetUserLocale();
return TRUE;
}