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

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.17 static const char RCSid[] = "$Id: header.c,v 2.16 2003/06/27 06:53:21 greg 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 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 schorsch 2.17
34     #include "resolu.h"
35 schorsch 2.14
36 greg 2.3 #define MAXLINE 512
37 greg 2.2
38 greg 2.4 char HDRSTR[] = "#?"; /* information header magic number */
39 greg 1.2
40 greg 2.4 char FMTSTR[] = "FORMAT="; /* format identifier */
41 greg 1.2
42 greg 2.11 char TMSTR[] = "CAPDATE="; /* capture date identifier */
43    
44 greg 2.16 extern void fputword(char *s, FILE *fp);
45    
46 greg 2.11 static int mycheck();
47 greg 2.4
48 greg 2.11
49     void
50 greg 2.4 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 greg 2.5 int
61 greg 2.4 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 gregl 2.9 while (*s && !isspace(*s)) *r++ = *s++;
69 greg 2.4 *r = '\0';
70     return(1);
71     }
72    
73    
74 greg 2.5 int
75 greg 2.4 isheadid(s) /* check to see if line is header id */
76     char *s;
77     {
78     return(headidval(NULL, s));
79     }
80    
81    
82 greg 2.11 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 greg 1.1 printargs(ac, av, fp) /* print arguments to a file */
141     int ac;
142     char **av;
143 greg 2.11 FILE *fp;
144 greg 1.1 {
145     while (ac-- > 0) {
146 greg 2.11 fputword(*av++, fp);
147     fputc(ac ? ' ' : '\n', fp);
148 greg 1.1 }
149     }
150    
151    
152 greg 2.5 int
153 greg 2.4 formatval(r, s) /* get format value (return true if format) */
154 greg 1.2 register char *r;
155     register char *s;
156     {
157 greg 2.4 register char *cp = FMTSTR;
158    
159     while (*cp) if (*cp++ != *s++) return(0);
160 greg 1.3 while (isspace(*s)) s++;
161 greg 2.4 if (!*s) return(0);
162     if (r == NULL) return(1);
163 greg 2.5 do
164     *r++ = *s++;
165     while(*s && !isspace(*s));
166 greg 1.2 *r = '\0';
167 greg 2.4 return(1);
168 greg 1.2 }
169    
170    
171 greg 2.5 int
172 greg 2.4 isformat(s) /* is line a format line? */
173     char *s;
174     {
175     return(formatval(NULL, s));
176     }
177    
178    
179 greg 2.11 void
180 greg 1.2 fputformat(s, fp) /* put out a format value */
181     char *s;
182 greg 1.1 FILE *fp;
183 greg 1.2 {
184     fputs(FMTSTR, fp);
185     fputs(s, fp);
186     putc('\n', fp);
187     }
188    
189    
190 greg 2.5 int
191 greg 1.2 getheader(fp, f, p) /* get header from file */
192     FILE *fp;
193 greg 1.1 int (*f)();
194 greg 1.2 char *p;
195 greg 1.1 {
196     char buf[MAXLINE];
197    
198     for ( ; ; ) {
199     buf[MAXLINE-2] = '\n';
200 greg 2.3 if (fgets(buf, MAXLINE, fp) == NULL)
201 greg 1.1 return(-1);
202     if (buf[0] == '\n')
203     return(0);
204 greg 2.3 #ifdef MSDOS
205     if (buf[0] == '\r' && buf[1] == '\n')
206     return(0);
207     #endif
208 greg 1.1 if (buf[MAXLINE-2] != '\n') {
209     ungetc(buf[MAXLINE-2], fp); /* prevent false end */
210     buf[MAXLINE-2] = '\0';
211     }
212 gwlarson 2.10 if (f != NULL && (*f)(buf, p) < 0)
213     return(-1);
214 greg 1.1 }
215     }
216    
217    
218 greg 1.2 struct check {
219     FILE *fp;
220 greg 1.3 char fs[64];
221 greg 1.2 };
222 greg 1.1
223 greg 1.2
224 greg 2.11 static int
225 greg 1.2 mycheck(s, cp) /* check a header line for format info. */
226 greg 1.1 char *s;
227 greg 1.2 register struct check *cp;
228 greg 1.1 {
229 greg 2.4 if (!formatval(cp->fs, s) && cp->fp != NULL)
230 greg 1.2 fputs(s, cp->fp);
231 greg 2.11 return(0);
232 greg 1.1 }
233    
234    
235 greg 2.5 int
236 greg 2.11 globmatch(p, s) /* check for match of s against pattern p */
237     register char *p, *s;
238 greg 1.3 {
239 greg 2.11 int setmatch;
240 greg 1.3
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 greg 2.6 if ( (p[1]=='?' || p[1]==*s) &&
251     globmatch(p+1,s) )
252 greg 1.3 return(1);
253     while (*s++);
254     return(0);
255 greg 2.11 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 greg 1.3 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 greg 1.4 * error reading the header. If fmt is empty, then -1 is returned
292 greg 1.3 * 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 greg 2.7 * 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 greg 1.3 * The input header (minus any format lines) is copied to fout
298     * if fout is not NULL.
299     */
300    
301 greg 2.5 int
302 greg 1.3 checkheader(fin, fmt, fout)
303 greg 1.2 FILE *fin;
304     char *fmt;
305     FILE *fout;
306 greg 1.1 {
307 greg 1.2 struct check cdat;
308 greg 2.7 register char *cp;
309 greg 1.2
310     cdat.fp = fout;
311     cdat.fs[0] = '\0';
312 greg 2.16 if (getheader(fin, mycheck, (char *)&cdat) < 0)
313 greg 1.3 return(-1);
314 greg 2.6 if (!cdat.fs[0])
315     return(0);
316 greg 2.7 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 greg 1.1 }