ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
Revision: 2.12
Committed: Tue Feb 25 02:47:21 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

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