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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines