ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fgetword.c
Revision: 2.8
Committed: Thu Aug 10 19:10:44 2017 UTC (6 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad5R1, rad5R3
Changes since 2.7: +5 -2 lines
Log Message:
Fixed lack of EOF detection introduced in last change (months ago!)

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.8 static const char RCSid[] = "$Id: fgetword.c,v 2.7 2017/04/09 21:33:24 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Read white space separated words from stream
6 greg 2.2 *
7 greg 2.4 * External symbols declared in rtio.h
8 greg 2.2 */
9    
10 greg 2.3 #include "copyright.h"
11 greg 1.1
12 greg 2.4 #include "rtio.h"
13 greg 1.1
14     #include <ctype.h>
15    
16    
17     char *
18 greg 2.6 fgetword( /* get (quoted) word up to n-1 characters */
19     char *s,
20     int n,
21 greg 2.7 FILE *fp
22 greg 2.6 )
23 greg 1.1 {
24 greg 2.2 int quote = '\0';
25 greg 2.7 char *cp;
26     int c;
27 greg 2.6 /* sanity checks */
28 greg 2.7 if ((s == NULL) | (n < 2))
29 greg 2.6 return(NULL);
30 greg 1.1 /* skip initial white space */
31     do
32     c = getc(fp);
33     while (isspace(c));
34 greg 2.2 /* check for quote */
35 schorsch 2.5 if ((c == '"') | (c == '\'')) {
36 greg 2.2 quote = c;
37     c = getc(fp);
38     }
39 greg 1.1 /* get actual word */
40     cp = s;
41 greg 2.7 while (c != EOF && !(quote ? c==quote : isspace(c))) {
42 greg 2.2 if (--n <= 0) /* check length limit */
43 greg 1.1 break;
44     *cp++ = c;
45     c = getc(fp);
46 greg 2.7 }
47 greg 1.1 *cp = '\0';
48 greg 2.8 if (c == EOF) { /* hit end-of-file? */
49     if (cp == s) /* and got nothing? */
50     return(NULL);
51     } else if (!quote) /* replace white character */
52 greg 1.1 ungetc(c, fp);
53     return(s);
54     }