1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* header.c - routines for reading and writing information headers. |
6 |
* |
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 |
90 |
|
91 |
#ifndef BSD |
92 |
#define index strchr |
93 |
#endif |
94 |
|
95 |
extern char *index(); |
96 |
|
97 |
char HDRSTR[] = "#?"; /* information header magic number */ |
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; |
110 |
{ |
111 |
fputs(HDRSTR, fp); |
112 |
fputs(s, fp); |
113 |
putc('\n', fp); |
114 |
} |
115 |
|
116 |
|
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(headidval(NULL, 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 |
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 |
register char *cp = FMTSTR; |
217 |
|
218 |
while (*cp) if (*cp++ != *s++) return(0); |
219 |
while (isspace(*s)) s++; |
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; |
242 |
{ |
243 |
fputs(FMTSTR, fp); |
244 |
fputs(s, fp); |
245 |
putc('\n', fp); |
246 |
} |
247 |
|
248 |
|
249 |
int |
250 |
getheader(fp, f, p) /* get header from file */ |
251 |
FILE *fp; |
252 |
int (*f)(); |
253 |
char *p; |
254 |
{ |
255 |
char buf[MAXLINE]; |
256 |
|
257 |
for ( ; ; ) { |
258 |
buf[MAXLINE-2] = '\n'; |
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 && (*f)(buf, p) < 0) |
272 |
return(-1); |
273 |
} |
274 |
} |
275 |
|
276 |
|
277 |
struct check { |
278 |
FILE *fp; |
279 |
char fs[64]; |
280 |
}; |
281 |
|
282 |
|
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 |
int |
295 |
globmatch(p, s) /* check for match of s against pattern p */ |
296 |
register char *p, *s; |
297 |
{ |
298 |
int setmatch; |
299 |
|
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 |
if ( (p[1]=='?' || p[1]==*s) && |
310 |
globmatch(p+1,s) ) |
311 |
return(1); |
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 */ |
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 |
* error reading the header. If fmt is empty, then -1 is returned |
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, |
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]) |
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 |
} |