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.12 by greg, Tue Feb 25 02:47:21 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 + #ifndef BSD
37 + #define  index  strchr
38 + #endif
39 +
40 + extern char  *index();
41 +
42 + char  HDRSTR[] = "#?";          /* information header magic number */
43 +
44 + char  FMTSTR[] = "FORMAT=";     /* format identifier */
45 +
46 + char  TMSTR[] = "CAPDATE=";     /* capture date identifier */
47 +
48 + static int mycheck();
49 +
50 +
51 + void
52 + 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 + int
63 + 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 +        while (*s && !isspace(*s)) *r++ = *s++;
71 +        *r = '\0';
72 +        return(1);
73 + }
74 +
75 +
76 + int
77 + isheadid(s)                     /* check to see if line is header id */
78 + char  *s;
79 + {
80 +        return(headidval(NULL, s));
81 + }
82 +
83 +
84 + 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   printargs(ac, av, fp)           /* print arguments to a file */
143   int  ac;
144   char  **av;
145   FILE  *fp;
146   {
147 +        int  quote;
148 +
149          while (ac-- > 0) {
150 <                fputs(*av++, fp);
151 <                putc(' ', fp);
150 >                fputword(*av++, fp);
151 >                fputc(ac ? ' ' : '\n', fp);
152          }
25        putc('\n', fp);
153   }
154  
155  
156 < #define  MAXLINE        512
156 > int
157 > formatval(r, s)                 /* get format value (return true if format) */
158 > register char  *r;
159 > register char  *s;
160 > {
161 >        register char  *cp = FMTSTR;
162  
163 < getheader(fp, f)                /* get header from file */
163 >        while (*cp) if (*cp++ != *s++) return(0);
164 >        while (isspace(*s)) s++;
165 >        if (!*s) return(0);
166 >        if (r == NULL) return(1);
167 >        do
168 >                *r++ = *s++;
169 >        while(*s && !isspace(*s));
170 >        *r = '\0';
171 >        return(1);
172 > }
173 >
174 >
175 > int
176 > isformat(s)                     /* is line a format line? */
177 > char  *s;
178 > {
179 >        return(formatval(NULL, s));
180 > }
181 >
182 >
183 > void
184 > fputformat(s, fp)               /* put out a format value */
185 > char  *s;
186   FILE  *fp;
187 + {
188 +        fputs(FMTSTR, fp);
189 +        fputs(s, fp);
190 +        putc('\n', fp);
191 + }
192 +
193 +
194 + int
195 + getheader(fp, f, p)             /* get header from file */
196 + FILE  *fp;
197   int  (*f)();
198 + char  *p;
199   {
200          char  buf[MAXLINE];
201  
202          for ( ; ; ) {
203                  buf[MAXLINE-2] = '\n';
204 <                if (fgets(buf, sizeof(buf), fp) == NULL)
204 >                if (fgets(buf, MAXLINE, fp) == NULL)
205                          return(-1);
206                  if (buf[0] == '\n')
207                          return(0);
208 + #ifdef MSDOS
209 +                if (buf[0] == '\r' && buf[1] == '\n')
210 +                        return(0);
211 + #endif
212                  if (buf[MAXLINE-2] != '\n') {
213                          ungetc(buf[MAXLINE-2], fp);     /* prevent false end */
214                          buf[MAXLINE-2] = '\0';
215                  }
216 <                if (f != NULL)
217 <                        (*f)(buf);
216 >                if (f != NULL && (*f)(buf, p) < 0)
217 >                        return(-1);
218          }
219   }
220  
221  
222 < static FILE     *outfp;
222 > struct check {
223 >        FILE    *fp;
224 >        char    fs[64];
225 > };
226  
227 < static
228 < myputs(s)
227 >
228 > static int
229 > mycheck(s, cp)                  /* check a header line for format info. */
230   char  *s;
231 + register struct check  *cp;
232   {
233 <        fputs(s, outfp);
233 >        if (!formatval(cp->fs, s) && cp->fp != NULL)
234 >                fputs(s, cp->fp);
235 >        return(0);
236   }
237  
238  
239 < copyheader(fin, fout)           /* copy file header */
240 < FILE  *fin, *fout;
239 > int
240 > globmatch(p, s)                 /* check for match of s against pattern p */
241 > register char   *p, *s;
242   {
243 <        outfp = fout;
244 <        return(getheader(fin, myputs));
243 >        int     setmatch;
244 >
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 >                                if ( (p[1]=='?' || p[1]==*s) &&
255 >                                                globmatch(p+1,s) )
256 >                                        return(1);
257 >                        while (*s++);
258 >                        return(0);
259 >                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 >                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 > * error reading the header.  If fmt is empty, then -1 is returned
296 > * 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 > * 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 > * The input header (minus any format lines) is copied to fout
302 > * if fout is not NULL.
303 > */
304 >
305 > int
306 > checkheader(fin, fmt, fout)
307 > FILE  *fin;
308 > char  *fmt;
309 > FILE  *fout;
310 > {
311 >        struct check    cdat;
312 >        register char   *cp;
313 >
314 >        cdat.fp = fout;
315 >        cdat.fs[0] = '\0';
316 >        if (getheader(fin, mycheck, &cdat) < 0)
317 >                return(-1);
318 >        if (!cdat.fs[0])
319 >                return(0);
320 >        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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines