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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #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 *
8 * External symbols declared in standard.h
9 */
10
11 #include "copyright.h"
12
13 #include <ctype.h>
14
15 #define NULL 0
16
17 #define MAXFLEN 8192 /* file must be smaller than this */
18
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 char buf[MAXFLEN];
29 register int n;
30 /* load file into buffer */
31 if (fname == NULL)
32 return(-1); /* no filename */
33 if ((fd = open(fname, 0)) < 0)
34 return(-1); /* open error */
35 n = read(fd, buf, MAXFLEN);
36 close(fd);
37 if (n < 0) /* read error */
38 return(-1);
39 if (n == MAXFLEN) /* file too big, take what we can */
40 while (!isspace(buf[--n]))
41 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 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 while (*++cp && !isspace(*cp))
69 ;
70 }
71 *ap = NULL;
72 return(ap - avl);
73 }