Back to index

tetex-bin  3.0
eofeoln.c
Go to the documentation of this file.
00001 /* eofeoln.c: implement Pascal's ideas for end-of-file and end-of-line
00002    testing.  Public domain. */
00003 
00004 #include "config.h"
00005 
00006 
00007 /* Return true if we're at the end of FILE, else false.  This implements
00008    Pascal's `eof' builtin.  */
00009 
00010 boolean
00011 eof P1C(FILE *, file)
00012 {
00013   register int c;
00014 
00015   /* If FILE doesn't exist, return false. This happens when, for
00016      example, when a user does `mft foo.mf' -- there's no change file,
00017      so we never open it, so we end up calling this with a null pointer. */
00018   if (!file)
00019     return true;
00020     
00021   /* Maybe we're already at the end?  */
00022   if (feof (file))
00023     return true;
00024 
00025   if ((c = getc (file)) == EOF)
00026     return true;
00027 
00028   /* We weren't at the end.  Back up.  */
00029   (void) ungetc (c, file);
00030 
00031   return false;
00032 }
00033 
00034 
00035 /* Return true on end-of-line in FILE or at the end of FILE, else false.  */
00036 /* Accept both CR and LF as end-of-line. */
00037 
00038 boolean
00039 eoln P1C(FILE*, file)
00040 {
00041   register int c;
00042 
00043   if (feof (file))
00044     return true;
00045   
00046   c = getc (file);
00047   
00048   if (c != EOF)
00049     (void) ungetc (c, file);
00050     
00051   return c == '\n' || c == '\r' || c == EOF;
00052 }
00053 
00054 /* Consume input up and including the first eol encountered. */
00055 /* Handle CRLF as a single end-of-line. */
00056 
00057 void
00058 readln P1C(FILE*, f)
00059 {
00060     int c;
00061     while ((c = getc (f)) != '\n' && c != '\r' && c != EOF)
00062         ;
00063     if (c == '\r' && (c = getc (f)) != '\n' && c != EOF)
00064         ungetc (c, f);
00065 }