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.4 by greg, Sun Feb 27 10:16:45 1994 UTC vs.
Revision 2.11 by greg, Sat Feb 22 02:07:22 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 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   *  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
# Line 37 | Line 98 | char  HDRSTR[] = "#?";         /* information header magic nu
98  
99   char  FMTSTR[] = "FORMAT=";     /* format identifier */
100  
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;
# Line 48 | Line 114 | register FILE  *fp;
114   }
115  
116  
117 + int
118   headidval(r,s)                  /* get header id (return true if is id) */
119   register char  *r, *s;
120   {
# Line 55 | Line 122 | register char  *r, *s;
122  
123          while (*cp) if (*cp++ != *s++) return(0);
124          if (r == NULL) return(1);
125 <        while (*s) *r++ = *s++;
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   {
# Line 68 | Line 136 | char  *s;
136   }
137  
138  
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 < register FILE  *fp;
200 > FILE  *fp;
201   {
202          int  quote;
203  
204          while (ac-- > 0) {
205 <                if (index(*av, ' ') != NULL) {          /* quote it */
206 <                        if (index(*av, '\'') != NULL)
81 <                                quote = '"';
82 <                        else
83 <                                quote = '\'';
84 <                        putc(quote, fp);
85 <                        fputs(*av++, fp);
86 <                        putc(quote, fp);
87 <                } else
88 <                        fputs(*av++, fp);
89 <                putc(' ', fp);
205 >                fputword(*av++, fp);
206 >                fputc(ac ? ' ' : '\n', fp);
207          }
91        putc('\n', fp);
208   }
209  
210  
211 + int
212   formatval(r, s)                 /* get format value (return true if format) */
213   register char  *r;
214   register char  *s;
# Line 102 | Line 219 | register char  *s;
219          while (isspace(*s)) s++;
220          if (!*s) return(0);
221          if (r == NULL) return(1);
222 <        while(*s) *r++ = *s++;
223 <        while (isspace(r[-1])) r--;
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   {
# Line 116 | Line 235 | char  *s;
235   }
236  
237  
238 + void
239   fputformat(s, fp)               /* put out a format value */
240   char  *s;
241   FILE  *fp;
# Line 126 | 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 147 | Line 268 | char  *p;
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 159 | 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 (!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).
175 < */
176 <
177 < #ifdef COPYMATCH
178 < copymatch(pat, str)
179 < 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;
182 <        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);
189                        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) ) {
196 <                                        strcpy(pat, str);
309 >                                if ( (p[1]=='?' || p[1]==*s) &&
310 >                                                globmatch(p+1,s) )
311                                          return(1);
198                                }
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 208 | Line 339 | char   *pat, *str;
339                          break;
340                  }
341          } while (*p++);
211        if (docopy)
212                strcpy(pat, str);
342          return(1);
343   }
215 #else
216 #define copymatch(pat, s)       (!strcmp(pat, s))
217 #endif
344  
345  
346   /*
# Line 225 | 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