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 2.2 by greg, Thu Feb 20 11:40:36 1992 UTC vs.
Revision 2.11 by greg, Sat Feb 22 02:07:22 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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   *  header.c - routines for reading and writing information headers.
6   *
7 < *      8/19/88
7 > *  Externals declared in resolu.h
8   *
9 < *  printargs(ac,av,fp) print an argument list to fp, followed by '\n'
9 > *  newheader(t,fp)     start new information header identified by string t
10 > *  isheadid(s)         returns true if s is a header id line
11 > *  headidval(r,s)      copy header identifier value in s to r
12 > *  dateval(t,s)        get capture date value
13 > *  isdate(s)           returns true if s is a date line
14 > *  fputdate(t,fp)      put out the given capture date and time
15 > *  fputnow(fp)         put out the current date and time
16 > *  printargs(ac,av,fp) print an argument list to fp, followed by '\n'
17   *  isformat(s)         returns true if s is of the form "FORMAT=*"
18   *  formatval(r,s)      copy the format value in s to r
19   *  fputformat(s,fp)    write "FORMAT=%s" to fp
20   *  getheader(fp,f,p)   read header from fp, calling f(s,p) on each line
21 + *  globmatch(pat, str) check for glob match of str against pat
22   *  checkheader(i,p,o)  check header format from i against p and copy to o
23   *
24   *  To copy header from input to output, use getheader(fin, fputs, fout)
25   */
26  
27 + /* ====================================================================
28 + * The Radiance Software License, Version 1.0
29 + *
30 + * Copyright (c) 1990 - 2002 The Regents of the University of California,
31 + * through Lawrence Berkeley National Laboratory.   All rights reserved.
32 + *
33 + * Redistribution and use in source and binary forms, with or without
34 + * modification, are permitted provided that the following conditions
35 + * are met:
36 + *
37 + * 1. Redistributions of source code must retain the above copyright
38 + *         notice, this list of conditions and the following disclaimer.
39 + *
40 + * 2. Redistributions in binary form must reproduce the above copyright
41 + *       notice, this list of conditions and the following disclaimer in
42 + *       the documentation and/or other materials provided with the
43 + *       distribution.
44 + *
45 + * 3. The end-user documentation included with the redistribution,
46 + *           if any, must include the following acknowledgment:
47 + *             "This product includes Radiance software
48 + *                 (http://radsite.lbl.gov/)
49 + *                 developed by the Lawrence Berkeley National Laboratory
50 + *               (http://www.lbl.gov/)."
51 + *       Alternately, this acknowledgment may appear in the software itself,
52 + *       if and wherever such third-party acknowledgments normally appear.
53 + *
54 + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
55 + *       and "The Regents of the University of California" must
56 + *       not be used to endorse or promote products derived from this
57 + *       software without prior written permission. For written
58 + *       permission, please contact [email protected].
59 + *
60 + * 5. Products derived from this software may not be called "Radiance",
61 + *       nor may "Radiance" appear in their name, without prior written
62 + *       permission of Lawrence Berkeley National Laboratory.
63 + *
64 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
65 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
66 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
67 + * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
68 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
69 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
70 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
71 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
72 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
73 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
74 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 + * SUCH DAMAGE.
76 + * ====================================================================
77 + *
78 + * This software consists of voluntary contributions made by many
79 + * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
80 + * information on Lawrence Berkeley National Laboratory, please see
81 + * <http://www.lbl.gov/>.
82 + */
83 +
84   #include  <stdio.h>
85 + #include  <string.h>
86 + #include  <time.h>
87   #include  <ctype.h>
88  
89 < #define  MAXLINE        512
89 > #define  MAXLINE        512
90  
91   #ifndef BSD
92 < #define  index  strchr
92 > #define  index  strchr
93   #endif
94  
95   extern char  *index();
96  
97 < char  FMTSTR[] = "FORMAT=";
34 < int  FMTSTRL = 7;
97 > char  HDRSTR[] = "#?";          /* information header magic number */
98  
99 + char  FMTSTR[] = "FORMAT=";     /* format identifier */
100  
101 < printargs(ac, av, fp)           /* print arguments to a file */
102 < int  ac;
103 < char  **av;
101 > char  TMSTR[] = "CAPDATE=";     /* capture date identifier */
102 >
103 > static int mycheck();
104 >
105 >
106 > void
107 > newheader(s, fp)                /* identifying line of information header */
108 > char  *s;
109   register FILE  *fp;
110   {
111 <        int  quote;
112 <
44 <        while (ac-- > 0) {
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 <        }
111 >        fputs(HDRSTR, fp);
112 >        fputs(s, fp);
113          putc('\n', fp);
114   }
115  
116  
117 < isformat(s)                     /* is line a format line? */
117 > int
118 > headidval(r,s)                  /* get header id (return true if is id) */
119 > register char  *r, *s;
120 > {
121 >        register char  *cp = HDRSTR;
122 >
123 >        while (*cp) if (*cp++ != *s++) return(0);
124 >        if (r == NULL) return(1);
125 >        while (*s && !isspace(*s)) *r++ = *s++;
126 >        *r = '\0';
127 >        return(1);
128 > }
129 >
130 >
131 > int
132 > isheadid(s)                     /* check to see if line is header id */
133   char  *s;
134   {
135 <        return(!strncmp(s,FMTSTR,FMTSTRL));
135 >        return(headidval(NULL, s));
136   }
137  
138  
139 < formatval(r, s)                 /* return format value */
139 > int
140 > dateval(tloc, s)                /* get capture date value */
141 > time_t  *tloc;
142 > char    *s;
143 > {
144 >        struct tm       tms;
145 >        register char  *cp = TMSTR;
146 >
147 >        while (*cp) if (*cp++ != *s++) return(0);
148 >        while (isspace(*s)) s++;
149 >        if (!*s) return(0);
150 >        if (sscanf(s, "%d:%d:%d %d:%d:%d",
151 >                        &tms.tm_year, &tms.tm_mon, &tms.tm_mday,
152 >                        &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6)
153 >                return(0);
154 >        if (tloc == NULL)
155 >                return(1);
156 >        tms.tm_mon--;
157 >        tms.tm_year -= 1900;
158 >        tms.tm_isdst = -1;      /* ask mktime() to figure out DST */
159 >        *tloc = mktime(&tms);
160 >        return(1);
161 > }
162 >
163 >
164 > int
165 > isdate(s)                       /* is the given line a capture date? */
166 > char *s;
167 > {
168 >        return(dateval(NULL, s));
169 > }
170 >
171 >
172 > void
173 > fputdate(tv, fp)                /* write out the given time value */
174 > time_t  tv;
175 > FILE    *fp;
176 > {
177 >        struct tm       *tm = localtime(&tv);
178 >        if (tm == NULL)
179 >                return;
180 >        fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR,
181 >                        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
182 >                        tm->tm_hour, tm->tm_min, tm->tm_sec);
183 > }
184 >
185 >
186 > void
187 > fputnow(fp)                     /* write out the current time */
188 > FILE    *fp;
189 > {
190 >        time_t  tv;
191 >        time(&tv);
192 >        fputdate(tv, fp);
193 > }
194 >
195 >
196 > void
197 > printargs(ac, av, fp)           /* print arguments to a file */
198 > int  ac;
199 > char  **av;
200 > FILE  *fp;
201 > {
202 >        int  quote;
203 >
204 >        while (ac-- > 0) {
205 >                fputword(*av++, fp);
206 >                fputc(ac ? ' ' : '\n', fp);
207 >        }
208 > }
209 >
210 >
211 > int
212 > formatval(r, s)                 /* get format value (return true if format) */
213   register char  *r;
214   register char  *s;
215   {
216 <        s += FMTSTRL;
216 >        register char  *cp = FMTSTR;
217 >
218 >        while (*cp) if (*cp++ != *s++) return(0);
219          while (isspace(*s)) s++;
220 <        if (!*s) { *r = '\0'; return; }
221 <        while(*s) *r++ = *s++;
222 <        while (isspace(r[-1])) r--;
220 >        if (!*s) return(0);
221 >        if (r == NULL) return(1);
222 >        do
223 >                *r++ = *s++;
224 >        while(*s && !isspace(*s));
225          *r = '\0';
226 +        return(1);
227   }
228  
229  
230 + int
231 + isformat(s)                     /* is line a format line? */
232 + char  *s;
233 + {
234 +        return(formatval(NULL, s));
235 + }
236 +
237 +
238 + void
239   fputformat(s, fp)               /* put out a format value */
240   char  *s;
241   FILE  *fp;
# Line 88 | Line 246 | FILE  *fp;
246   }
247  
248  
249 + int
250   getheader(fp, f, p)             /* get header from file */
251   FILE  *fp;
252   int  (*f)();
# Line 97 | Line 256 | char  *p;
256  
257          for ( ; ; ) {
258                  buf[MAXLINE-2] = '\n';
259 <                if (fgets(buf, sizeof(buf), fp) == NULL)
259 >                if (fgets(buf, MAXLINE, fp) == NULL)
260                          return(-1);
261                  if (buf[0] == '\n')
262                          return(0);
263 + #ifdef MSDOS
264 +                if (buf[0] == '\r' && buf[1] == '\n')
265 +                        return(0);
266 + #endif
267                  if (buf[MAXLINE-2] != '\n') {
268                          ungetc(buf[MAXLINE-2], fp);     /* prevent false end */
269                          buf[MAXLINE-2] = '\0';
270                  }
271 <                if (f != NULL)
272 <                        (*f)(buf, p);
271 >                if (f != NULL && (*f)(buf, p) < 0)
272 >                        return(-1);
273          }
274   }
275  
# Line 117 | Line 280 | struct check {
280   };
281  
282  
283 < static
283 > static int
284   mycheck(s, cp)                  /* check a header line for format info. */
285   char  *s;
286   register struct check  *cp;
287   {
288 <        if (!strncmp(s,FMTSTR,FMTSTRL))
126 <                formatval(cp->fs, s);
127 <        else if (cp->fp != NULL)        /* don't copy format info. */
288 >        if (!formatval(cp->fs, s) && cp->fp != NULL)
289                  fputs(s, cp->fp);
290 +        return(0);
291   }
292  
293  
294 < /*
295 < * Copymatch(pat,str) checks pat for wildcards, and
296 < * copies str into pat if there is a match (returning true).
135 < */
136 <
137 < #ifdef COPYMATCH
138 < copymatch(pat, str)
139 < char    *pat, *str;
294 > int
295 > globmatch(p, s)                 /* check for match of s against pattern p */
296 > register char   *p, *s;
297   {
298 <        int     docopy = 0;
142 <        register char   *p = pat, *s = str;
298 >        int     setmatch;
299  
300          do {
301                  switch (*p) {
302                  case '?':                       /* match any character */
303                          if (!*s++)
304                                  return(0);
149                        docopy++;
305                          break;
306                  case '*':                       /* match any string */
307                          while (p[1] == '*') p++;
308                          do
309 <                                if ( (p[1]=='?' || p[1]==*s)
310 <                                                && copymatch(p+1,s) ) {
156 <                                        strcpy(pat, str);
309 >                                if ( (p[1]=='?' || p[1]==*s) &&
310 >                                                globmatch(p+1,s) )
311                                          return(1);
158                                }
312                          while (*s++);
313                          return(0);
314 +                case '[':                       /* character set */
315 +                        setmatch = *s == *++p;
316 +                        if (!*p)
317 +                                return(0);
318 +                        while (*++p != ']') {
319 +                                if (!*p)
320 +                                        return(0);
321 +                                if (*p == '-') {
322 +                                        setmatch += p[-1] <= *s && *s <= p[1];
323 +                                        if (!*++p)
324 +                                                break;
325 +                                } else
326 +                                        setmatch += *p == *s;
327 +                        }
328 +                        if (!setmatch)
329 +                                return(0);
330 +                        s++;
331 +                        break;
332                  case '\\':                      /* literal next */
333                          p++;
334                  /* fall through */
# Line 168 | Line 339 | char   *pat, *str;
339                          break;
340                  }
341          } while (*p++);
171        if (docopy)
172                strcpy(pat, str);
342          return(1);
343   }
175 #else
176 #define copymatch(pat, s)       (!strcmp(pat, s))
177 #endif
344  
345  
346   /*
# Line 185 | Line 351 | char   *pat, *str;
351   * if any input format is found (or there is an error), and 0 otherwise.
352   * If fmt contains any '*' or '?' characters, then checkheader
353   * does wildcard expansion and copies a matching result into fmt.
354 < * Be sure that fmt is big enough to hold the match in such cases!
354 > * Be sure that fmt is big enough to hold the match in such cases,
355 > * and that it is not a static, read-only string!
356   * The input header (minus any format lines) is copied to fout
357   * if fout is not NULL.
358   */
359  
360 + int
361   checkheader(fin, fmt, fout)
362   FILE  *fin;
363   char  *fmt;
364   FILE  *fout;
365   {
366          struct check    cdat;
367 +        register char   *cp;
368  
369          cdat.fp = fout;
370          cdat.fs[0] = '\0';
371          if (getheader(fin, mycheck, &cdat) < 0)
372                  return(-1);
373 <        if (cdat.fs[0] != '\0')
374 <                return(copymatch(fmt, cdat.fs) ? 1 : -1);
375 <        return(0);
373 >        if (!cdat.fs[0])
374 >                return(0);
375 >        for (cp = fmt; *cp; cp++)               /* check for globbing */
376 >                if (*cp == '?' | *cp == '*')
377 >                        if (globmatch(fmt, cdat.fs)) {
378 >                                strcpy(fmt, cdat.fs);
379 >                                return(1);
380 >                        } else
381 >                                return(-1);
382 >        return(strcmp(fmt, cdat.fs) ? -1 : 1);  /* literal match */
383   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines