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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines