cfunc.mod in table 2D and 3D, add proper 0-termination to file read

This commit is contained in:
h_vogt 2016-08-21 23:16:05 +02:00 committed by rlar
parent 607d481388
commit 6f5c5b162d
2 changed files with 19 additions and 15 deletions

View File

@ -370,6 +370,7 @@ cm_table2D(ARGS) /* structure holding parms, inputs, outputs, etc. */
char *cFile, *cThisPtr, *cThisLine, *cThisLinePtr;
int isNewline; /* Boolean indicating we've read a CR or LF */
long lFileLen; /* Length of file */
long lFileRead; /* Length of file read in */
long lIndex; /* Index into cThisLine array */
int lLineCount; /* Current line number */
long lStartPos; /* Offset of start of current line */
@ -419,9 +420,11 @@ cm_table2D(ARGS) /* structure holding parms, inputs, outputs, etc. */
loc->init_err = 1;
return;
}
/* read whole file into memory */
fread(cFile, (size_t) lFileLen, 1, loc->state->fp); /* Read the entire file into cFile */
/* read whole file into cFile */
lFileRead = fread(cFile, sizeof(char), lFileLen, loc->state->fp);
fclose(loc->state->fp);
/* Number of chars read may be less than lFileLen, because /r are skipt by 'fread' */
cFile[lFileRead] = '\0';
cThisPtr = cFile;
cThisLinePtr = cThisLine;
@ -434,14 +437,12 @@ cm_table2D(ARGS) /* structure holding parms, inputs, outputs, etc. */
isNewline = 0;
lStartPos = lTotalChars;
while (*cThisPtr) { /* Read until reaching null char */
if (!isNewline) { /* Haven't read a CR or LF yet */
if (*cThisPtr == '\r' || *cThisPtr == '\n') /* This char IS a CR or LF */
while (*cThisPtr) {
if (!isNewline) { /* Haven't read a LF yet */
if (*cThisPtr == '\n') /* This char IS or LF */
isNewline = 1; /* Set flag */
}
else if (*cThisPtr != '\r' && *cThisPtr != '\n') /* Already found CR or LF */
else if (*cThisPtr != '\n') /* Already found LF */
break; /* Done with line */
cThisLinePtr[lIndex++] = *cThisPtr++; /* Add char to output and increment */
@ -451,7 +452,7 @@ cm_table2D(ARGS) /* structure holding parms, inputs, outputs, etc. */
cThisLinePtr[lIndex] = '\0'; /* Terminate the string */
lLineCount++; /* Increment the line counter */
/* continue if comment or empty */
if (cThisLinePtr[0] == '*' || cThisLinePtr[0] == '\0') {
if (cThisLinePtr[0] == '*' || cThisLinePtr[0] == '\n') {
lLineCount--; /* we count only real lines */
continue;
}

View File

@ -378,6 +378,7 @@ cm_table3D(ARGS) /* structure holding parms, inputs, outputs, etc. */
char *cFile, *cThisPtr, *cThisLine, *cThisLinePtr;
int isNewline; /* Boolean indicating we've read a CR or LF */
long lFileLen; /* Length of file */
long lFileRead; /* Length of file read in */
long lIndex; /* Index into cThisLine array */
int lLineCount; /* Current line number */
long lStartPos; /* Offset of start of current line */
@ -429,9 +430,11 @@ cm_table3D(ARGS) /* structure holding parms, inputs, outputs, etc. */
loc->init_err = 1;
return;
}
/* read whole file into memory */
fread(cFile, (size_t) lFileLen, 1, loc->state->fp); /* Read the entire file into cFile */
/* read whole file into cFile */
lFileRead = fread(cFile, sizeof(char), lFileLen, loc->state->fp);
fclose(loc->state->fp);
/* Number of chars read may be less than lFileLen, because /r are skipt by 'fread' */
cFile[lFileRead] = '\0';
cThisPtr = cFile;
cThisLinePtr = cThisLine;
@ -444,12 +447,12 @@ cm_table3D(ARGS) /* structure holding parms, inputs, outputs, etc. */
lStartPos = lTotalChars;
while (*cThisPtr) { /* Read until reaching null char */
if (!isNewline) { /* Haven't read a CR or LF yet */
if (*cThisPtr == '\r' || *cThisPtr == '\n') /* This char IS a CR or LF */
if (!isNewline) { /* Haven't read a LF yet */
if (*cThisPtr == '\n') /* This char IS a LF */
isNewline = 1; /* Set flag */
}
else if (*cThisPtr != '\r' && *cThisPtr != '\n') /* Already found CR or LF */
else if (*cThisPtr != '\n') /* Already found LF */
break; /* Done with line */
cThisLinePtr[lIndex++] = *cThisPtr++; /* Add char to output and increment */
@ -459,7 +462,7 @@ cm_table3D(ARGS) /* structure holding parms, inputs, outputs, etc. */
cThisLinePtr[lIndex] = '\0'; /* Terminate the string */
lLineCount++; /* Increment the line counter */
/* continue if comment or empty */
if (cThisLinePtr[0] == '*' || cThisLinePtr[0] == '\0') {
if (cThisLinePtr[0] == '*' || cThisLinePtr[0] == '\n') {
lLineCount--; /* we count only real lines */
continue;
}