ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/words.c
Revision: 2.4
Committed: Fri Jan 29 11:18:23 1999 UTC (25 years, 3 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.3: +28 -1 lines
Log Message:
added nextword() function for reading quoted strings

File Contents

# User Rev Content
1 greg 2.2 /* Copyright (c) 1996 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Routines for recognizing and moving about words in strings.
9     */
10    
11     #include <ctype.h>
12    
13     #ifdef BSD
14     #define strchr index
15     #endif
16    
17     #define NULL 0
18    
19     extern char *strchr();
20    
21    
22     char *
23 gwlarson 2.4 atos(rs, nb, s) /* get word from string, returning rs */
24 greg 1.2 char *rs;
25     register int nb;
26     register char *s;
27     {
28     register char *cp = rs;
29    
30     while (isspace(*s))
31     s++;
32     while (--nb > 0 && *s && !isspace(*s))
33     *cp++ = *s++;
34     *cp = '\0';
35     return(rs);
36 gwlarson 2.4 }
37    
38    
39     char *
40     nextword(cp, nb, s) /* get (quoted) word, returning new s */
41     register char *cp;
42     register int nb;
43     register char *s;
44     {
45     int quote = 0;
46    
47     if (s == NULL) return(NULL);
48     while (isspace(*s))
49     s++;
50     switch (*s) {
51     case '\0':
52     return(NULL);
53     case '"':
54     case '\'':
55     quote = *s++;
56     }
57     while (--nb > 0 && *s && (quote ? *s!=quote : !isspace(*s)))
58     *cp++ = *s++;
59     *cp = '\0';
60     if (quote && *s==quote)
61     s++;
62     return(s);
63 greg 1.2 }
64    
65    
66     char *
67 greg 2.3 sskip(s) /* skip word in string, leaving on space */
68 greg 1.1 register char *s;
69     {
70     while (isspace(*s))
71     s++;
72     while (*s && !isspace(*s))
73 greg 2.2 s++;
74 greg 2.3 return(s);
75     }
76    
77    
78     char *
79     sskip2(s, n) /* skip word(s) in string, leaving on word */
80     register char *s;
81     register int n;
82     {
83 greg 2.2 while (isspace(*s))
84 greg 1.1 s++;
85 greg 2.3 while (n-- > 0) {
86     while (*s && !isspace(*s))
87     s++;
88     while (isspace(*s))
89     s++;
90     }
91 greg 1.1 return(s);
92     }
93    
94    
95     char *
96     iskip(s) /* skip integer in string */
97 greg 1.2 register char *s;
98 greg 1.1 {
99 greg 1.2 while (isspace(*s))
100     s++;
101     if (*s == '-' || *s == '+')
102     s++;
103     if (!isdigit(*s))
104     return(NULL);
105     do
106     s++;
107     while (isdigit(*s));
108     return(s);
109 greg 1.1 }
110    
111    
112     char *
113     fskip(s) /* skip float in string */
114 greg 1.2 register char *s;
115 greg 1.1 {
116 greg 1.4 register char *cp;
117 greg 1.1
118 greg 1.4 while (isspace(*s))
119     s++;
120     if (*s == '-' || *s == '+')
121     s++;
122     cp = s;
123 greg 1.1 while (isdigit(*cp))
124     cp++;
125     if (*cp == '.') {
126 greg 1.2 cp++; s++;
127 greg 1.1 while (isdigit(*cp))
128     cp++;
129     }
130 greg 1.2 if (cp == s)
131     return(NULL);
132 greg 1.1 if (*cp == 'e' || *cp == 'E')
133     return(iskip(cp+1));
134     return(cp);
135     }
136    
137    
138     isint(s) /* check integer format */
139     char *s;
140     {
141     register char *cp;
142    
143     cp = iskip(s);
144 greg 1.2 return(cp != NULL && *cp == '\0');
145 greg 1.1 }
146    
147    
148     isintd(s, ds) /* check integer format with delimiter set */
149     char *s, *ds;
150     {
151     register char *cp;
152    
153     cp = iskip(s);
154 greg 1.3 return(cp != NULL && strchr(ds, *cp) != NULL);
155 greg 1.1 }
156    
157    
158     isflt(s) /* check float format */
159     char *s;
160     {
161     register char *cp;
162    
163     cp = fskip(s);
164 greg 1.2 return(cp != NULL && *cp == '\0');
165 greg 1.1 }
166    
167    
168     isfltd(s, ds) /* check integer format with delimiter set */
169     char *s, *ds;
170     {
171     register char *cp;
172    
173     cp = fskip(s);
174 greg 1.3 return(cp != NULL && strchr(ds, *cp) != NULL);
175 greg 1.1 }