ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/wordfile.c
(Generate patch)

Comparing ray/src/common/wordfile.c (file contents):
Revision 2.2 by greg, Fri Oct 16 13:02:46 1992 UTC vs.
Revision 2.13 by schorsch, Sun Mar 28 20:33:12 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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   * 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 + #include <string.h>
15 + #include <stdio.h>
16 + #include <sys/types.h>
17 + #include <sys/stat.h>
18 + #include <fcntl.h>
19  
20 < #define NULL            0
20 > #include "platform.h"
21 > #include "standard.h"
22  
16 #define MAXFLEN         10240           /* file must be smaller than this */
23  
24 < extern char     *bmalloc();
24 > #define MAXFLEN         8192            /* file must be smaller than this */
25  
26  
27   int
# Line 23 | Line 29 | wordfile(words, fname)         /* get words from fname, put i
29   char    **words;
30   char    *fname;
31   {
26        char    **wp = words;
32          int     fd;
33 <        char    *buf;
33 >        char    buf[MAXFLEN];
34          register int    n;
35 <        register char   *cp;
36 <                                        /* allocate memory */
37 <        if ((cp = buf = bmalloc(MAXFLEN)) == NULL)
33 <                return(-1);
35 >                                        /* load file into buffer */
36 >        if (fname == NULL)
37 >                return(-1);                     /* no filename */
38          if ((fd = open(fname, 0)) < 0)
39 <                goto err;
39 >                return(-1);                     /* open error */
40          n = read(fd, buf, MAXFLEN);
41          close(fd);
42 <        if (n < 0)
43 <                goto err;
42 >        if (n < 0)                              /* read error */
43 >                return(-1);
44          if (n == MAXFLEN)               /* file too big, take what we can */
45                  while (!isspace(buf[--n]))
46 <                        if (n <= 0)
47 <                                goto err;
48 <        bfree(buf+n+1, MAXFLEN-n-1);    /* return unneeded memory */
49 <        while (n > 0) {                 /* break buffer into words */
50 <                while (isspace(*cp)) {
51 <                        cp++;
52 <                        if (--n <= 0)
53 <                                return(wp - words);
54 <                }
55 <                *wp++ = cp;
56 <                while (!isspace(*cp)) {
57 <                        cp++;
58 <                        if (--n <= 0)
59 <                                break;
60 <                }
61 <                *cp++ = '\0'; n--;
46 >                        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 >        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 >                while (*++cp && !isspace(*cp))
74 >                        ;
75          }
76 <        *wp = NULL;                     /* null terminator */
77 <        return(wp - words);
61 < err:
62 <        bfree(buf, MAXFLEN);
63 <        return(-1);
76 >        *ap = NULL;
77 >        return(ap - avl);
78   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines