ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fgetword.c
Revision: 2.6
Committed: Wed Oct 12 03:37:54 2005 UTC (18 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2, rad4R1, rad4R0, rad3R8, rad3R9, rad4R2P1
Changes since 2.5: +9 -5 lines
Log Message:
Added sanity checks and fixed parameters

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.6 static const char RCSid[] = "$Id: fgetword.c,v 2.5 2003/07/27 22:12:01 schorsch 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     register FILE *fp
22     )
23 greg 1.1 {
24 greg 2.2 int quote = '\0';
25 greg 1.1 register char *cp;
26     register int c;
27 greg 2.6 /* sanity checks */
28     if ((s == NULL) | (n <= 0))
29     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 /* check for end of file */
40     if (c == EOF)
41     return(NULL);
42     /* get actual word */
43     cp = s;
44     do {
45 greg 2.2 if (--n <= 0) /* check length limit */
46 greg 1.1 break;
47     *cp++ = c;
48     c = getc(fp);
49 greg 2.2 } while (c != EOF && !(quote ? c==quote : isspace(c)));
50 greg 1.1 *cp = '\0';
51 schorsch 2.5 if ((c != EOF) & (!quote)) /* replace space */
52 greg 1.1 ungetc(c, fp);
53     return(s);
54     }