getl.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "gis.h"
00003 /*
00004  ***********************************************************
00005  *  G_getl(buf, n, fd)
00006  *     char *buf         buffer to receive read data
00007  *     int n             max num of bytes to read
00008  *     FILE *fd          file descriptor structure
00009  *
00010  *  does fgets() and removes trailing newline
00011  *  
00012  *  returns: 1 ok, 0 eof
00013  ************************************************************/
00014 
00015 int G_getl ( char *buf, int n, FILE *fd)
00016 {
00017     if (!fgets (buf, n, fd))
00018         return 0;
00019 
00020     for (; *buf && *buf != '\n'; buf++)
00021             ;
00022     *buf = 0;
00023 
00024     return 1;
00025 }
00026 
00027 /*
00028  ***********************************************************
00029  *  G_getl2(buf, n, fd)
00030  *     char *buf         buffer to receive read data, at least n+1 must be allocated
00031  *     int n             max num of bytes to read
00032  *     FILE *fd          file descriptor structure
00033  *
00034  *  Reads in at most n characters from stream and stores them into the  buffer pointed to by buf.
00035  *  Reading stops after an EOF or a newline. New line is not stored in the buffer.
00036  *  It supports text files created on various platforms (UNIX, DOS, MacOS9),
00037  *  i.e. \n (\012), \r (\015) and \r\n (\015\012)
00038  *  
00039  *  returns: 1 ok, 0 eof
00040  ************************************************************/
00041 
00042 int G_getl2 ( char *buf, int n, FILE *fd)
00043 {
00044     int i = 0;
00045     int c;
00046     int ret = 1;
00047 
00048     while ( i < n  ) {
00049         c = fgetc(fd);
00050 
00051         if ( c == EOF ) { 
00052             if ( i == 0 ) { /* Read correctly (return 1) last line in file without '\n' */
00053                 ret = 0;
00054             }
00055             break;
00056         }
00057         
00058         if ( c == '\012' ) break;               /* UNIX */
00059 
00060         if ( c == '\015' ) {                    /* DOS or MacOS9 */
00061             if ( (c = fgetc(fd) ) != EOF ) {     
00062                 if ( c != '\012' ) {            /* MacOS9 - we have to return the char to stream */
00063                     ungetc ( c, fd );
00064                 }
00065             }
00066             break;
00067         }
00068         
00069         buf[i] = c;
00070 
00071         i++;
00072     }   
00073     buf[i] = '\0';
00074 
00075     G_debug ( 4, "G_getl2: ->%s<-", buf );
00076 
00077     return ret;
00078 }

Generated on Sat Jul 22 22:06:14 2006 for GRASS by  doxygen 1.4.7