ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
Revision: 2.21
Committed: Wed Jul 30 10:11:06 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.20: +2 -2 lines
Log Message:
Added prototypes submitted by Randolph Fritz.

File Contents

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