ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
Revision: 2.17
Committed: Mon Jun 30 14:59:11 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.16: +3 -2 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: header.c,v 2.16 2003/06/27 06:53:21 greg Exp $";
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 #include "copyright.h"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32 #include <ctype.h>
33
34 #include "resolu.h"
35
36 #define MAXLINE 512
37
38 char HDRSTR[] = "#?"; /* information header magic number */
39
40 char FMTSTR[] = "FORMAT="; /* format identifier */
41
42 char TMSTR[] = "CAPDATE="; /* capture date identifier */
43
44 extern void fputword(char *s, FILE *fp);
45
46 static int mycheck();
47
48
49 void
50 newheader(s, fp) /* identifying line of information header */
51 char *s;
52 register FILE *fp;
53 {
54 fputs(HDRSTR, fp);
55 fputs(s, fp);
56 putc('\n', fp);
57 }
58
59
60 int
61 headidval(r,s) /* get header id (return true if is id) */
62 register char *r, *s;
63 {
64 register char *cp = HDRSTR;
65
66 while (*cp) if (*cp++ != *s++) return(0);
67 if (r == NULL) return(1);
68 while (*s && !isspace(*s)) *r++ = *s++;
69 *r = '\0';
70 return(1);
71 }
72
73
74 int
75 isheadid(s) /* check to see if line is header id */
76 char *s;
77 {
78 return(headidval(NULL, s));
79 }
80
81
82 int
83 dateval(tloc, s) /* get capture date value */
84 time_t *tloc;
85 char *s;
86 {
87 struct tm tms;
88 register char *cp = TMSTR;
89
90 while (*cp) if (*cp++ != *s++) return(0);
91 while (isspace(*s)) s++;
92 if (!*s) return(0);
93 if (sscanf(s, "%d:%d:%d %d:%d:%d",
94 &tms.tm_year, &tms.tm_mon, &tms.tm_mday,
95 &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6)
96 return(0);
97 if (tloc == NULL)
98 return(1);
99 tms.tm_mon--;
100 tms.tm_year -= 1900;
101 tms.tm_isdst = -1; /* ask mktime() to figure out DST */
102 *tloc = mktime(&tms);
103 return(1);
104 }
105
106
107 int
108 isdate(s) /* is the given line a capture date? */
109 char *s;
110 {
111 return(dateval(NULL, s));
112 }
113
114
115 void
116 fputdate(tv, fp) /* write out the given time value */
117 time_t tv;
118 FILE *fp;
119 {
120 struct tm *tm = localtime(&tv);
121 if (tm == NULL)
122 return;
123 fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR,
124 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
125 tm->tm_hour, tm->tm_min, tm->tm_sec);
126 }
127
128
129 void
130 fputnow(fp) /* write out the current time */
131 FILE *fp;
132 {
133 time_t tv;
134 time(&tv);
135 fputdate(tv, fp);
136 }
137
138
139 void
140 printargs(ac, av, fp) /* print arguments to a file */
141 int ac;
142 char **av;
143 FILE *fp;
144 {
145 while (ac-- > 0) {
146 fputword(*av++, fp);
147 fputc(ac ? ' ' : '\n', fp);
148 }
149 }
150
151
152 int
153 formatval(r, s) /* get format value (return true if format) */
154 register char *r;
155 register char *s;
156 {
157 register char *cp = FMTSTR;
158
159 while (*cp) if (*cp++ != *s++) return(0);
160 while (isspace(*s)) s++;
161 if (!*s) return(0);
162 if (r == NULL) return(1);
163 do
164 *r++ = *s++;
165 while(*s && !isspace(*s));
166 *r = '\0';
167 return(1);
168 }
169
170
171 int
172 isformat(s) /* is line a format line? */
173 char *s;
174 {
175 return(formatval(NULL, s));
176 }
177
178
179 void
180 fputformat(s, fp) /* put out a format value */
181 char *s;
182 FILE *fp;
183 {
184 fputs(FMTSTR, fp);
185 fputs(s, fp);
186 putc('\n', fp);
187 }
188
189
190 int
191 getheader(fp, f, p) /* get header from file */
192 FILE *fp;
193 int (*f)();
194 char *p;
195 {
196 char buf[MAXLINE];
197
198 for ( ; ; ) {
199 buf[MAXLINE-2] = '\n';
200 if (fgets(buf, MAXLINE, fp) == NULL)
201 return(-1);
202 if (buf[0] == '\n')
203 return(0);
204 #ifdef MSDOS
205 if (buf[0] == '\r' && buf[1] == '\n')
206 return(0);
207 #endif
208 if (buf[MAXLINE-2] != '\n') {
209 ungetc(buf[MAXLINE-2], fp); /* prevent false end */
210 buf[MAXLINE-2] = '\0';
211 }
212 if (f != NULL && (*f)(buf, p) < 0)
213 return(-1);
214 }
215 }
216
217
218 struct check {
219 FILE *fp;
220 char fs[64];
221 };
222
223
224 static int
225 mycheck(s, cp) /* check a header line for format info. */
226 char *s;
227 register struct check *cp;
228 {
229 if (!formatval(cp->fs, s) && cp->fp != NULL)
230 fputs(s, cp->fp);
231 return(0);
232 }
233
234
235 int
236 globmatch(p, s) /* check for match of s against pattern p */
237 register char *p, *s;
238 {
239 int setmatch;
240
241 do {
242 switch (*p) {
243 case '?': /* match any character */
244 if (!*s++)
245 return(0);
246 break;
247 case '*': /* match any string */
248 while (p[1] == '*') p++;
249 do
250 if ( (p[1]=='?' || p[1]==*s) &&
251 globmatch(p+1,s) )
252 return(1);
253 while (*s++);
254 return(0);
255 case '[': /* character set */
256 setmatch = *s == *++p;
257 if (!*p)
258 return(0);
259 while (*++p != ']') {
260 if (!*p)
261 return(0);
262 if (*p == '-') {
263 setmatch += p[-1] <= *s && *s <= p[1];
264 if (!*++p)
265 break;
266 } else
267 setmatch += *p == *s;
268 }
269 if (!setmatch)
270 return(0);
271 s++;
272 break;
273 case '\\': /* literal next */
274 p++;
275 /* fall through */
276 default: /* normal character */
277 if (*p != *s)
278 return(0);
279 s++;
280 break;
281 }
282 } while (*p++);
283 return(1);
284 }
285
286
287 /*
288 * Checkheader(fin,fmt,fout) returns a value of 1 if the input format
289 * matches the specification in fmt, 0 if no input format was found,
290 * and -1 if the input format does not match or there is an
291 * error reading the header. If fmt is empty, then -1 is returned
292 * if any input format is found (or there is an error), and 0 otherwise.
293 * If fmt contains any '*' or '?' characters, then checkheader
294 * does wildcard expansion and copies a matching result into fmt.
295 * Be sure that fmt is big enough to hold the match in such cases,
296 * and that it is not a static, read-only string!
297 * The input header (minus any format lines) is copied to fout
298 * if fout is not NULL.
299 */
300
301 int
302 checkheader(fin, fmt, fout)
303 FILE *fin;
304 char *fmt;
305 FILE *fout;
306 {
307 struct check cdat;
308 register char *cp;
309
310 cdat.fp = fout;
311 cdat.fs[0] = '\0';
312 if (getheader(fin, mycheck, (char *)&cdat) < 0)
313 return(-1);
314 if (!cdat.fs[0])
315 return(0);
316 for (cp = fmt; *cp; cp++) /* check for globbing */
317 if (*cp == '?' | *cp == '*')
318 if (globmatch(fmt, cdat.fs)) {
319 strcpy(fmt, cdat.fs);
320 return(1);
321 } else
322 return(-1);
323 return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */
324 }