2
2
Numdiff - compare putatively similar files,
3
3
ignoring small numeric differences
4
Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Ivano Primi <ivprimi@libero.it>
4
Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Ivano Primi <ivprimi@libero.it>
6
6
This program is free software: you can redistribute it and/or modify
7
7
it under the terms of the GNU General Public License as published by
17
17
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#ifdef _TEST_READ_LINE_
32
#define READING_ERROR 3
33
#define OUT_OF_MEMORY 4
37
#define FILE_IS_BINARY 7
39
#endif /* _TEST_READ_LINE_ */
20
41
char* read_line (FILE* pf, int* errcode)
22
43
char buffer[BUFF_SIZE];
23
44
char *ptr, *line = NULL;
26
while ((ptr = fgets (buffer, BUFF_SIZE,pf)))
49
int exception_occurred = 0;
28
lline += strlen (buffer);
30
ptr = (char*) calloc (lline + 1, sizeof(char));
32
ptr = (char*) realloc ((void*)line, (lline + 1) * sizeof(char));
37
*errcode = OUT_OF_MEMORY;
43
strcat (line, buffer);
45
if (lline > 0 && line[lline-1] == '\n')
53
for (n = 0; n < BUFF_SIZE-1 && (ch = getc(pf)) != EOF && ch != '\0' && ch != '\n'; n++)
55
if ( !(exception_occurred = ((ch == EOF && n == 0) || ch == '\0')) )
58
If we enter this block, then CH is not the nul character.
59
Thus either is n == BUFF_SIZE-1, or is CH == EOF or is CH == NEWLINE.
61
if (n == BUFF_SIZE-1 || ch == EOF)
73
ptr = (char*) calloc (lline + 1, sizeof(char));
75
ptr = (char*) realloc ((void*)line, (lline + 1) * sizeof(char));
80
*errcode = OUT_OF_MEMORY;
86
strcat (line, buffer);
88
if (lline > 0 && line[lline-1] == '\n')
91
} while (!exception_occurred);
92
if ((exception_occurred))
50
94
if ( (ferror(pf)) )
51
95
*errcode = READING_ERROR;
96
else if ( ch == '\0' )
97
*errcode = FILE_IS_BINARY;
52
98
else if (lline > 0)
53
99
*errcode = LINE_INTERR;
108
#ifdef _TEST_READ_LINE_
110
int main (int argc, char* argv[])
116
fprintf (stderr, "\n*** Usage: %s FILEPATH\n", argv[0]);
119
else if ( !(fp = fopen (argv[1], "r")) )
121
fprintf (stderr, "\n*** Cannot open \"%s\" for reading:\n", argv[1]);
132
line = read_line (fp, &errcode);
136
fputs ("\n*** Cannot read from a binary file\n", stderr);
139
fputs (line, stdout);
143
fputs ("\n*** Error while reading from file\n", stderr);
146
fputs ("\n*** Memory exhausted while reading from file, abort now...\n", stderr);
152
fputs (line, stdout);
157
} while (errcode == OK);
160
fprintf (stderr, "\n*** Could not close \"%s\":\n", argv[1]);
171
#endif /* _TEST_READ_LINE_ */