Back to index

radiance  4R0+20100331
fgetword.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: fgetword.c,v 2.6 2005/10/12 03:37:54 greg Exp $";
00003 #endif
00004 /*
00005  * Read white space separated words from stream
00006  *
00007  *  External symbols declared in rtio.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include  "rtio.h"
00013 
00014 #include  <ctype.h>
00015 
00016 
00017 char *
00018 fgetword(                   /* get (quoted) word up to n-1 characters */
00019        char  *s,
00020        int  n,
00021        register FILE  *fp
00022 )
00023 {
00024        int  quote = '\0';
00025        register char  *cp;
00026        register int  c;
00027                                    /* sanity checks */
00028        if ((s == NULL) | (n <= 0))
00029               return(NULL);
00030                                    /* skip initial white space */
00031        do
00032               c = getc(fp);
00033        while (isspace(c));
00034                                    /* check for quote */
00035        if ((c == '"') | (c == '\'')) {
00036               quote = c;
00037               c = getc(fp);
00038        }
00039                                    /* check for end of file */
00040        if (c == EOF)
00041               return(NULL);
00042                                    /* get actual word */
00043        cp = s;
00044        do {
00045               if (--n <= 0)        /* check length limit */
00046                      break;
00047               *cp++ = c;
00048               c = getc(fp);
00049        } while (c != EOF && !(quote ? c==quote : isspace(c)));
00050        *cp = '\0';
00051        if ((c != EOF) & (!quote))  /* replace space */
00052               ungetc(c, fp);
00053        return(s);
00054 }