ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
Revision: 2.11
Committed: Sat Feb 22 02:07:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +155 -21 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4     /*
5     * header.c - routines for reading and writing information headers.
6     *
7 greg 2.11 * Externals declared in resolu.h
8 greg 1.3 *
9 greg 2.4 * 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 greg 2.11 * 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 greg 2.3 * printargs(ac,av,fp) print an argument list to fp, followed by '\n'
17 greg 1.3 * 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 greg 2.6 * globmatch(pat, str) check for glob match of str against pat
22 greg 1.3 * 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 greg 1.1 */
26    
27 greg 2.11 /* ====================================================================
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 greg 1.1 #include <stdio.h>
85 greg 2.11 #include <string.h>
86     #include <time.h>
87 greg 1.3 #include <ctype.h>
88 greg 1.1
89 greg 2.3 #define MAXLINE 512
90 greg 1.1
91 greg 2.2 #ifndef BSD
92 greg 2.3 #define index strchr
93 greg 2.2 #endif
94    
95     extern char *index();
96    
97 greg 2.4 char HDRSTR[] = "#?"; /* information header magic number */
98 greg 1.2
99 greg 2.4 char FMTSTR[] = "FORMAT="; /* format identifier */
100 greg 1.2
101 greg 2.11 char TMSTR[] = "CAPDATE="; /* capture date identifier */
102    
103     static int mycheck();
104 greg 2.4
105 greg 2.11
106     void
107 greg 2.4 newheader(s, fp) /* identifying line of information header */
108     char *s;
109     register FILE *fp;
110     {
111     fputs(HDRSTR, fp);
112     fputs(s, fp);
113     putc('\n', fp);
114     }
115    
116    
117 greg 2.5 int
118 greg 2.4 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 gregl 2.9 while (*s && !isspace(*s)) *r++ = *s++;
126 greg 2.4 *r = '\0';
127     return(1);
128     }
129    
130    
131 greg 2.5 int
132 greg 2.4 isheadid(s) /* check to see if line is header id */
133     char *s;
134     {
135     return(headidval(NULL, s));
136     }
137    
138    
139 greg 2.11 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 greg 1.1 printargs(ac, av, fp) /* print arguments to a file */
198     int ac;
199     char **av;
200 greg 2.11 FILE *fp;
201 greg 1.1 {
202 greg 2.2 int quote;
203    
204 greg 1.1 while (ac-- > 0) {
205 greg 2.11 fputword(*av++, fp);
206     fputc(ac ? ' ' : '\n', fp);
207 greg 1.1 }
208     }
209    
210    
211 greg 2.5 int
212 greg 2.4 formatval(r, s) /* get format value (return true if format) */
213 greg 1.2 register char *r;
214     register char *s;
215     {
216 greg 2.4 register char *cp = FMTSTR;
217    
218     while (*cp) if (*cp++ != *s++) return(0);
219 greg 1.3 while (isspace(*s)) s++;
220 greg 2.4 if (!*s) return(0);
221     if (r == NULL) return(1);
222 greg 2.5 do
223     *r++ = *s++;
224     while(*s && !isspace(*s));
225 greg 1.2 *r = '\0';
226 greg 2.4 return(1);
227 greg 1.2 }
228    
229    
230 greg 2.5 int
231 greg 2.4 isformat(s) /* is line a format line? */
232     char *s;
233     {
234     return(formatval(NULL, s));
235     }
236    
237    
238 greg 2.11 void
239 greg 1.2 fputformat(s, fp) /* put out a format value */
240     char *s;
241 greg 1.1 FILE *fp;
242 greg 1.2 {
243     fputs(FMTSTR, fp);
244     fputs(s, fp);
245     putc('\n', fp);
246     }
247    
248    
249 greg 2.5 int
250 greg 1.2 getheader(fp, f, p) /* get header from file */
251     FILE *fp;
252 greg 1.1 int (*f)();
253 greg 1.2 char *p;
254 greg 1.1 {
255     char buf[MAXLINE];
256    
257     for ( ; ; ) {
258     buf[MAXLINE-2] = '\n';
259 greg 2.3 if (fgets(buf, MAXLINE, fp) == NULL)
260 greg 1.1 return(-1);
261     if (buf[0] == '\n')
262     return(0);
263 greg 2.3 #ifdef MSDOS
264     if (buf[0] == '\r' && buf[1] == '\n')
265     return(0);
266     #endif
267 greg 1.1 if (buf[MAXLINE-2] != '\n') {
268     ungetc(buf[MAXLINE-2], fp); /* prevent false end */
269     buf[MAXLINE-2] = '\0';
270     }
271 gwlarson 2.10 if (f != NULL && (*f)(buf, p) < 0)
272     return(-1);
273 greg 1.1 }
274     }
275    
276    
277 greg 1.2 struct check {
278     FILE *fp;
279 greg 1.3 char fs[64];
280 greg 1.2 };
281 greg 1.1
282 greg 1.2
283 greg 2.11 static int
284 greg 1.2 mycheck(s, cp) /* check a header line for format info. */
285 greg 1.1 char *s;
286 greg 1.2 register struct check *cp;
287 greg 1.1 {
288 greg 2.4 if (!formatval(cp->fs, s) && cp->fp != NULL)
289 greg 1.2 fputs(s, cp->fp);
290 greg 2.11 return(0);
291 greg 1.1 }
292    
293    
294 greg 2.5 int
295 greg 2.11 globmatch(p, s) /* check for match of s against pattern p */
296     register char *p, *s;
297 greg 1.3 {
298 greg 2.11 int setmatch;
299 greg 1.3
300     do {
301     switch (*p) {
302     case '?': /* match any character */
303     if (!*s++)
304     return(0);
305     break;
306     case '*': /* match any string */
307     while (p[1] == '*') p++;
308     do
309 greg 2.6 if ( (p[1]=='?' || p[1]==*s) &&
310     globmatch(p+1,s) )
311 greg 1.3 return(1);
312     while (*s++);
313     return(0);
314 greg 2.11 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 greg 1.3 case '\\': /* literal next */
333     p++;
334     /* fall through */
335     default: /* normal character */
336     if (*p != *s)
337     return(0);
338     s++;
339     break;
340     }
341     } while (*p++);
342     return(1);
343     }
344    
345    
346     /*
347     * Checkheader(fin,fmt,fout) returns a value of 1 if the input format
348     * matches the specification in fmt, 0 if no input format was found,
349     * and -1 if the input format does not match or there is an
350 greg 1.4 * error reading the header. If fmt is empty, then -1 is returned
351 greg 1.3 * 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 greg 2.7 * 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 greg 1.3 * The input header (minus any format lines) is copied to fout
357     * if fout is not NULL.
358     */
359    
360 greg 2.5 int
361 greg 1.3 checkheader(fin, fmt, fout)
362 greg 1.2 FILE *fin;
363     char *fmt;
364     FILE *fout;
365 greg 1.1 {
366 greg 1.2 struct check cdat;
367 greg 2.7 register char *cp;
368 greg 1.2
369     cdat.fp = fout;
370     cdat.fs[0] = '\0';
371     if (getheader(fin, mycheck, &cdat) < 0)
372 greg 1.3 return(-1);
373 greg 2.6 if (!cdat.fs[0])
374     return(0);
375 greg 2.7 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 greg 1.1 }