ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/wordfile.c
Revision: 2.13
Committed: Sun Mar 28 20:33:12 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 2.12: +1 -3 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 schorsch 2.13 static const char RCSid[] = "$Id: wordfile.c,v 2.12 2003/10/22 02:06:34 greg Exp $";
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 schorsch 2.10 #include <string.h>
15     #include <stdio.h>
16     #include <sys/types.h>
17     #include <sys/stat.h>
18     #include <fcntl.h>
19    
20 greg 2.12 #include "platform.h"
21 schorsch 2.10 #include "standard.h"
22 greg 2.2
23 greg 2.1
24 greg 2.3 #define MAXFLEN 8192 /* file must be smaller than this */
25 greg 2.1
26    
27     int
28     wordfile(words, fname) /* get words from fname, put in words */
29     char **words;
30     char *fname;
31     {
32     int fd;
33 greg 2.3 char buf[MAXFLEN];
34 greg 2.1 register int n;
35 greg 2.3 /* load file into buffer */
36 greg 2.7 if (fname == NULL)
37     return(-1); /* no filename */
38 greg 2.1 if ((fd = open(fname, 0)) < 0)
39 greg 2.3 return(-1); /* open error */
40 greg 2.2 n = read(fd, buf, MAXFLEN);
41 greg 2.1 close(fd);
42 greg 2.3 if (n < 0) /* read error */
43     return(-1);
44 greg 2.2 if (n == MAXFLEN) /* file too big, take what we can */
45 greg 2.1 while (!isspace(buf[--n]))
46 greg 2.3 if (n <= 0) /* one long word! */
47     return(-1);
48     buf[n] = '\0'; /* terminate */
49     return(wordstring(words, buf)); /* wordstring does the rest */
50     }
51    
52    
53     int
54     wordstring(avl, str) /* allocate and load argument list */
55     char **avl;
56     char *str;
57     {
58     register char *cp, **ap;
59    
60     if (str == NULL)
61     return(-1);
62     cp = bmalloc(strlen(str)+1);
63     if (cp == NULL) /* ENOMEM */
64     return(-1);
65     strcpy(cp, str);
66 greg 2.4 ap = avl; /* parse into words */
67     for ( ; ; ) {
68     while (isspace(*cp)) /* nullify spaces */
69     *cp++ = '\0';
70     if (!*cp) /* all done? */
71     break;
72     *ap++ = cp; /* add argument to list */
73 greg 2.6 while (*++cp && !isspace(*cp))
74     ;
75 greg 2.1 }
76 greg 2.3 *ap = NULL;
77     return(ap - avl);
78 greg 2.1 }