1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Read white space separated words from stream |
6 |
+ |
* |
7 |
+ |
* External symbols declared in rtio.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
+ |
#include "rtio.h" |
13 |
+ |
|
14 |
|
#include <ctype.h> |
15 |
|
|
16 |
|
|
17 |
+ |
#define isquote(c) (((c) == '"') | ((c) == '\'')) |
18 |
+ |
|
19 |
+ |
|
20 |
|
char * |
21 |
< |
fgetword(s, n, fp) /* get a word, maximum n-1 characters */ |
22 |
< |
char *s; |
23 |
< |
int n; |
24 |
< |
register FILE *fp; |
21 |
> |
fgetword( /* get (quoted) word up to n-1 characters */ |
22 |
> |
char *s, |
23 |
> |
int n, |
24 |
> |
FILE *fp |
25 |
> |
) |
26 |
|
{ |
27 |
< |
register char *cp; |
28 |
< |
register int c; |
27 |
> |
int quote = '\0'; |
28 |
> |
char *cp; |
29 |
> |
int c; |
30 |
> |
/* sanity checks */ |
31 |
> |
if ((s == NULL) | (n < 2)) |
32 |
> |
return(NULL); |
33 |
|
/* skip initial white space */ |
34 |
|
do |
35 |
|
c = getc(fp); |
36 |
|
while (isspace(c)); |
37 |
< |
/* check for end of file */ |
38 |
< |
if (c == EOF) |
39 |
< |
return(NULL); |
31 |
< |
/* get actual word */ |
32 |
< |
cp = s; |
33 |
< |
do { |
34 |
< |
if (--n <= 0) /* check length limit */ |
35 |
< |
break; |
36 |
< |
*cp++ = c; |
37 |
> |
/* check for quote */ |
38 |
> |
if (isquote(c)) { |
39 |
> |
quote = c; |
40 |
|
c = getc(fp); |
41 |
< |
} while (c != EOF && !isspace(c)); |
41 |
> |
} |
42 |
> |
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 |
> |
} |
57 |
|
*cp = '\0'; |
58 |
< |
if (c != EOF) /* replace delimiter */ |
59 |
< |
ungetc(c, fp); |
58 |
> |
if ((c == EOF) & (cp == s)) /* hit end-of-file? */ |
59 |
> |
return(NULL); |
60 |
|
return(s); |
43 |
– |
} |
44 |
– |
|
45 |
– |
|
46 |
– |
isint(s) /* check integer format */ |
47 |
– |
char *s; |
48 |
– |
{ |
49 |
– |
register char *cp = s; |
50 |
– |
|
51 |
– |
while (isspace(*cp)) |
52 |
– |
cp++; |
53 |
– |
if (*cp == '-' || *cp == '+') |
54 |
– |
cp++; |
55 |
– |
do |
56 |
– |
if (!isdigit(*cp)) |
57 |
– |
return(0); |
58 |
– |
while (*++cp); |
59 |
– |
return(1); |
60 |
– |
} |
61 |
– |
|
62 |
– |
|
63 |
– |
isflt(s) /* check float format */ |
64 |
– |
char *s; |
65 |
– |
{ |
66 |
– |
register char *cp = s; |
67 |
– |
|
68 |
– |
while (isspace(*cp)) |
69 |
– |
cp++; |
70 |
– |
if (*cp == '-' || *cp == '+') |
71 |
– |
cp++; |
72 |
– |
s = cp; |
73 |
– |
while (isdigit(*cp)) |
74 |
– |
cp++; |
75 |
– |
if (*cp == '.') { |
76 |
– |
cp++; s++; |
77 |
– |
while (isdigit(*cp)) |
78 |
– |
cp++; |
79 |
– |
} |
80 |
– |
if (cp == s) |
81 |
– |
return(0); |
82 |
– |
if (*cp == 'e' || *cp == 'E') |
83 |
– |
return(isint(cp+1)); |
84 |
– |
return(1); |
61 |
|
} |