| 1 |
/* eofeoln.c: implement Pascal's ideas for end-of-file and end-of-line |
| 2 |
testing. Public domain. */ |
| 3 |
|
| 4 |
#include <w2c/config.h> |
| 5 |
#include "lib.h" |
| 6 |
|
| 7 |
/* Return true if we're at the end of FILE, else false. This implements |
| 8 |
Pascal's `eof' builtin. */ |
| 9 |
|
| 10 |
boolean |
| 11 |
eof (FILE *file) |
| 12 |
{ |
| 13 |
register int c; |
| 14 |
|
| 15 |
/* If FILE doesn't exist, return true. This happens, for example, |
| 16 |
when a user does `mft foo.mf' -- there's no change file, |
| 17 |
so we never open it, so we end up calling this with a null pointer. */ |
| 18 |
if (!file) |
| 19 |
return true; |
| 20 |
|
| 21 |
/* Maybe we're already at the end? */ |
| 22 |
if (feof (file)) |
| 23 |
return true; |
| 24 |
|
| 25 |
if ((c = getc (file)) == EOF) |
| 26 |
return true; |
| 27 |
|
| 28 |
/* We weren't at the end. Back up. */ |
| 29 |
(void) ungetc (c, file); |
| 30 |
|
| 31 |
return false; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
/* Return true on end-of-line in FILE or at the end of FILE, else false. */ |
| 36 |
/* Accept both CR and LF as end-of-line. */ |
| 37 |
|
| 38 |
boolean |
| 39 |
eoln (FILE *file) |
| 40 |
{ |
| 41 |
register int c; |
| 42 |
|
| 43 |
if (feof (file)) |
| 44 |
return true; |
| 45 |
|
| 46 |
c = getc (file); |
| 47 |
|
| 48 |
if (c != EOF) |
| 49 |
(void) ungetc (c, file); |
| 50 |
|
| 51 |
return c == '\n' || c == '\r' || c == EOF; |
| 52 |
} |
| 53 |
|
| 54 |
/* Consume input up and including the first eol encountered. */ |
| 55 |
/* Handle CRLF as a single end-of-line. */ |
| 56 |
|
| 57 |
void |
| 58 |
readln (FILE *f) |
| 59 |
{ |
| 60 |
int c; |
| 61 |
while ((c = getc (f)) != '\n' && c != '\r' && c != EOF) |
| 62 |
; |
| 63 |
if (c == '\r' && (c = getc (f)) != '\n' && c != EOF) |
| 64 |
ungetc (c, f); |
| 65 |
} |