From afddb4800c5bebd1d3b97a4e3529921a2a1c9bc5 Mon Sep 17 00:00:00 2001 From: knarf Date: Thu, 6 Jan 2011 16:55:02 +0000 Subject: from Ian Hinder: Correctly load parameter files with DOS line-endings Before this commit, when a parameter file with DOS line-endings was used, certain features of Cactus did not work correctly. For example, setting an output directory of $parfile would create an output directory named "$parfile" instead of expanding the parameter filename. This commit converts any CR-LF sequences to LF immediately after the parameter file is loaded. git-svn-id: http://svn.cactuscode.org/flesh/trunk@4663 17b73243-c579-4c4c-a9d2-2d5706c11dac --- src/util/ParseFile.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/util') diff --git a/src/util/ParseFile.c b/src/util/ParseFile.c index 34e4f02a..7ccb9b20 100644 --- a/src/util/ParseFile.c +++ b/src/util/ParseFile.c @@ -42,6 +42,7 @@ static void CheckBuf(int, int); static void removeSpaces(char *stripMe); static char *ReadFile(FILE *file, unsigned long *filesize); static char *ParseDefines(char *buffer, unsigned long *buffersize); +static char *convert_crlf_to_lf(const char *buffer); int ParseBuffer(char *buffer, int (*set_function)(const char *, const char *, int), tFleshConfig *ConfigData); @@ -127,8 +128,16 @@ int ParseFile(FILE *ifp, int retval; unsigned long buffersize; char *buffer = ReadFile(ifp, &buffersize); + char *buffer2 = NULL; if (!buffer) return 1; + + /* Ensure Unix line endings */ + buffer2 = convert_crlf_to_lf(buffer); + free(buffer); + buffer = buffer2; + buffersize = strlen(buffer); + buffer = ParseDefines(buffer, &buffersize); /* ParseBuffer can get confused with detecting the end of the buffer (when in a comment or in a string), and may overrun. Therefore @@ -235,6 +244,33 @@ static void removeSpaces(char *stripMe) free(s); } +/* Convert string CRLF line endings to LF */ +char *convert_crlf_to_lf(const char *buffer) +{ + int dropped = 0; + int len = strlen(buffer); + char *buffer2 = malloc(len+1); + assert(buffer2); + char *to = buffer2; + const char *p = NULL; + + for (p = buffer; p < buffer+len+1; p++) + { + int is_crlf = (*p == '\r' && p < buffer + len && *(p+1) == '\n'); + if (!is_crlf) + { + *to = *p; + to++; + } + else + { + dropped++; + } + } + + return buffer2; +} + /* #define TEST_ParseFile */ -- cgit v1.2.3