ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
(Generate patch)

Comparing ray/src/common/header.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:34:33 1989 UTC vs.
Revision 2.15 by greg, Fri Jun 20 00:25:49 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines