ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/wordfile.c
Revision: 2.9
Committed: Tue Feb 25 02:47:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.8: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.8 static const char RCSid[] = "$Id$";
3 greg 2.1 #endif
4     /*
5     * Load whitespace separated words from a file into an array.
6     * Assume the passed pointer array is big enough to hold them all.
7 greg 2.8 *
8     * External symbols declared in standard.h
9     */
10    
11 greg 2.9 #include "copyright.h"
12 greg 2.1
13 greg 2.2 #include <ctype.h>
14    
15 greg 2.1 #define NULL 0
16    
17 greg 2.3 #define MAXFLEN 8192 /* file must be smaller than this */
18 greg 2.1
19     extern char *bmalloc();
20    
21    
22     int
23     wordfile(words, fname) /* get words from fname, put in words */
24     char **words;
25     char *fname;
26     {
27     int fd;
28 greg 2.3 char buf[MAXFLEN];
29 greg 2.1 register int n;
30 greg 2.3 /* load file into buffer */
31 greg 2.7 if (fname == NULL)
32     return(-1); /* no filename */
33 greg 2.1 if ((fd = open(fname, 0)) < 0)
34 greg 2.3 return(-1); /* open error */
35 greg 2.2 n = read(fd, buf, MAXFLEN);
36 greg 2.1 close(fd);
37 greg 2.3 if (n < 0) /* read error */
38     return(-1);
39 greg 2.2 if (n == MAXFLEN) /* file too big, take what we can */
40 greg 2.1 while (!isspace(buf[--n]))
41 greg 2.3 if (n <= 0) /* one long word! */
42     return(-1);
43     buf[n] = '\0'; /* terminate */
44     return(wordstring(words, buf)); /* wordstring does the rest */
45     }
46    
47    
48     int
49     wordstring(avl, str) /* allocate and load argument list */
50     char **avl;
51     char *str;
52     {
53     register char *cp, **ap;
54    
55     if (str == NULL)
56     return(-1);
57     cp = bmalloc(strlen(str)+1);
58     if (cp == NULL) /* ENOMEM */
59     return(-1);
60     strcpy(cp, str);
61 greg 2.4 ap = avl; /* parse into words */
62     for ( ; ; ) {
63     while (isspace(*cp)) /* nullify spaces */
64     *cp++ = '\0';
65     if (!*cp) /* all done? */
66     break;
67     *ap++ = cp; /* add argument to list */
68 greg 2.6 while (*++cp && !isspace(*cp))
69     ;
70 greg 2.1 }
71 greg 2.3 *ap = NULL;
72     return(ap - avl);
73 greg 2.1 }