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

Comparing ray/src/common/header.c (file contents):
Revision 1.2 by greg, Thu Apr 18 12:56:58 1991 UTC vs.
Revision 2.3 by greg, Thu Nov 12 16:20:09 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1988 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 8 | Line 8 | static char SCCSid[] = "$SunId$ LBL";
8   *  header.c - routines for reading and writing information headers.
9   *
10   *      8/19/88
11 + *
12 + *  printargs(ac,av,fp) print an argument list to fp, followed by '\n'
13 + *  isformat(s)         returns true if s is of the form "FORMAT=*"
14 + *  formatval(r,s)      copy the format value in s to r
15 + *  fputformat(s,fp)    write "FORMAT=%s" to fp
16 + *  getheader(fp,f,p)   read header from fp, calling f(s,p) on each line
17 + *  checkheader(i,p,o)  check header format from i against p and copy to o
18 + *
19 + *  To copy header from input to output, use getheader(fin, fputs, fout)
20   */
21  
22   #include  <stdio.h>
23 + #include  <ctype.h>
24  
25 < #define  MAXLINE        512
25 > #define  MAXLINE        512
26  
27 + #ifndef BSD
28 + #define  index  strchr
29 + #endif
30 +
31 + extern char  *index();
32 +
33   char  FMTSTR[] = "FORMAT=";
34   int  FMTSTRL = 7;
35  
# Line 21 | Line 37 | int  FMTSTRL = 7;
37   printargs(ac, av, fp)           /* print arguments to a file */
38   int  ac;
39   char  **av;
40 < FILE  *fp;
40 > register FILE  *fp;
41   {
42 +        int  quote;
43 +
44          while (ac-- > 0) {
45 <                fputs(*av++, fp);
45 >                if (index(*av, ' ') != NULL) {          /* quote it */
46 >                        if (index(*av, '\'') != NULL)
47 >                                quote = '"';
48 >                        else
49 >                                quote = '\'';
50 >                        putc(quote, fp);
51 >                        fputs(*av++, fp);
52 >                        putc(quote, fp);
53 >                } else
54 >                        fputs(*av++, fp);
55                  putc(' ', fp);
56          }
57          putc('\n', fp);
# Line 43 | Line 70 | register char  *r;
70   register char  *s;
71   {
72          s += FMTSTRL;
73 <        while (*s && *s != '\n')
74 <                *r++ = *s++;
73 >        while (isspace(*s)) s++;
74 >        if (!*s) { *r = '\0'; return; }
75 >        while(*s) *r++ = *s++;
76 >        while (isspace(r[-1])) r--;
77          *r = '\0';
78   }
79  
# Line 68 | Line 97 | char  *p;
97  
98          for ( ; ; ) {
99                  buf[MAXLINE-2] = '\n';
100 <                if (fgets(buf, sizeof(buf), fp) == NULL)
100 >                if (fgets(buf, MAXLINE, fp) == NULL)
101                          return(-1);
102                  if (buf[0] == '\n')
103                          return(0);
104 + #ifdef MSDOS
105 +                if (buf[0] == '\r' && buf[1] == '\n')
106 +                        return(0);
107 + #endif
108                  if (buf[MAXLINE-2] != '\n') {
109                          ungetc(buf[MAXLINE-2], fp);     /* prevent false end */
110                          buf[MAXLINE-2] = '\0';
# Line 84 | Line 117 | char  *p;
117  
118   struct check {
119          FILE    *fp;
120 <        char    fs[32];
120 >        char    fs[64];
121   };
122  
123  
# Line 100 | Line 133 | register struct check  *cp;
133   }
134  
135  
136 < checkheader(fin, fmt, fout)     /* check data format in header */
136 > /*
137 > * Copymatch(pat,str) checks pat for wildcards, and
138 > * copies str into pat if there is a match (returning true).
139 > */
140 >
141 > #ifdef COPYMATCH
142 > copymatch(pat, str)
143 > char    *pat, *str;
144 > {
145 >        int     docopy = 0;
146 >        register char   *p = pat, *s = str;
147 >
148 >        do {
149 >                switch (*p) {
150 >                case '?':                       /* match any character */
151 >                        if (!*s++)
152 >                                return(0);
153 >                        docopy++;
154 >                        break;
155 >                case '*':                       /* match any string */
156 >                        while (p[1] == '*') p++;
157 >                        do
158 >                                if ( (p[1]=='?' || p[1]==*s)
159 >                                                && copymatch(p+1,s) ) {
160 >                                        strcpy(pat, str);
161 >                                        return(1);
162 >                                }
163 >                        while (*s++);
164 >                        return(0);
165 >                case '\\':                      /* literal next */
166 >                        p++;
167 >                /* fall through */
168 >                default:                        /* normal character */
169 >                        if (*p != *s)
170 >                                return(0);
171 >                        s++;
172 >                        break;
173 >                }
174 >        } while (*p++);
175 >        if (docopy)
176 >                strcpy(pat, str);
177 >        return(1);
178 > }
179 > #else
180 > #define copymatch(pat, s)       (!strcmp(pat, s))
181 > #endif
182 >
183 >
184 > /*
185 > * Checkheader(fin,fmt,fout) returns a value of 1 if the input format
186 > * matches the specification in fmt, 0 if no input format was found,
187 > * and -1 if the input format does not match or there is an
188 > * error reading the header.  If fmt is empty, then -1 is returned
189 > * if any input format is found (or there is an error), and 0 otherwise.
190 > * If fmt contains any '*' or '?' characters, then checkheader
191 > * does wildcard expansion and copies a matching result into fmt.
192 > * Be sure that fmt is big enough to hold the match in such cases!
193 > * The input header (minus any format lines) is copied to fout
194 > * if fout is not NULL.
195 > */
196 >
197 > checkheader(fin, fmt, fout)
198   FILE  *fin;
199   char  *fmt;
200   FILE  *fout;
# Line 111 | Line 205 | FILE  *fout;
205          cdat.fs[0] = '\0';
206          if (getheader(fin, mycheck, &cdat) < 0)
207                  return(-1);
208 <        if (fmt != NULL && cdat.fs[0] != '\0')
209 <                return(strcmp(fmt, cdat.fs) ? -1 : 1);
208 >        if (cdat.fs[0] != '\0')
209 >                return(copymatch(fmt, cdat.fs) ? 1 : -1);
210          return(0);
211   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines