ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fgetword.c
Revision: 2.9
Committed: Fri Feb 10 04:32:19 2023 UTC (2 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.8: +21 -14 lines
Log Message:
perf: Eliminated call to ungetc() for better performance

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.9 static const char RCSid[] = "$Id: fgetword.c,v 2.8 2017/08/10 19:10:44 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 greg 2.9 #define isquote(c) (((c) == '"') | ((c) == '\''))
18    
19    
20 greg 1.1 char *
21 greg 2.6 fgetword( /* get (quoted) word up to n-1 characters */
22     char *s,
23     int n,
24 greg 2.7 FILE *fp
25 greg 2.6 )
26 greg 1.1 {
27 greg 2.2 int quote = '\0';
28 greg 2.7 char *cp;
29     int c;
30 greg 2.6 /* sanity checks */
31 greg 2.7 if ((s == NULL) | (n < 2))
32 greg 2.6 return(NULL);
33 greg 1.1 /* skip initial white space */
34     do
35     c = getc(fp);
36     while (isspace(c));
37 greg 2.2 /* check for quote */
38 greg 2.9 if (isquote(c)) {
39 greg 2.2 quote = c;
40     c = getc(fp);
41     }
42 greg 2.9 cp = s; /* get actual word */
43     while (c != EOF) {
44     if (c == quote) /* end quote? */
45     quote = '\0';
46     else if (!quote && isquote(c))
47     quote = c; /* started new quote */
48     else {
49     if (!quote && isspace(c))
50     break; /* end of word */
51     if (--n <= 0)
52     break; /* hit length limit */
53     *cp++ = c;
54     }
55     c = getc(fp); /* get next character */
56 greg 2.7 }
57 greg 1.1 *cp = '\0';
58 greg 2.9 if ((c == EOF) & (cp == s)) /* hit end-of-file? */
59     return(NULL);
60 greg 1.1 return(s);
61     }