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 2.2 by greg, Thu Feb 20 11:40:36 1992 UTC vs.
Revision 2.17 by schorsch, Mon Jun 30 14:59:11 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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 +
29   #include  <stdio.h>
30 + #include  <string.h>
31 + #include  <time.h>
32   #include  <ctype.h>
33  
34 < #define  MAXLINE        512
34 > #include "resolu.h"
35  
36 < #ifndef BSD
28 < #define  index  strchr
29 < #endif
36 > #define  MAXLINE        512
37  
38 < extern char  *index();
38 > char  HDRSTR[] = "#?";          /* information header magic number */
39  
40 < char  FMTSTR[] = "FORMAT=";
34 < int  FMTSTRL = 7;
40 > char  FMTSTR[] = "FORMAT=";     /* format identifier */
41  
42 + char  TMSTR[] = "CAPDATE=";     /* capture date identifier */
43  
44 < printargs(ac, av, fp)           /* print arguments to a file */
45 < int  ac;
46 < char  **av;
44 > extern void     fputword(char *s, FILE *fp);
45 >
46 > static int mycheck();
47 >
48 >
49 > void
50 > newheader(s, fp)                /* identifying line of information header */
51 > char  *s;
52   register FILE  *fp;
53   {
54 <        int  quote;
55 <
44 <        while (ac-- > 0) {
45 <                if (index(*av, ' ') != NULL) {          /* quote it */
46 <                        if (index(*av, '\'') != NULL)
47 <                                quote = '"';
48 <                        else
49 <                                quote = '\'';
50 <                        putc(quote, fp);
51 <                        fputs(*av++, fp);
52 <                        putc(quote, fp);
53 <                } else
54 <                        fputs(*av++, fp);
55 <                putc(' ', fp);
56 <        }
54 >        fputs(HDRSTR, fp);
55 >        fputs(s, fp);
56          putc('\n', fp);
57   }
58  
59  
60 < isformat(s)                     /* is line a format line? */
60 > int
61 > 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 >        while (*s && !isspace(*s)) *r++ = *s++;
69 >        *r = '\0';
70 >        return(1);
71 > }
72 >
73 >
74 > int
75 > isheadid(s)                     /* check to see if line is header id */
76   char  *s;
77   {
78 <        return(!strncmp(s,FMTSTR,FMTSTRL));
78 >        return(headidval(NULL, s));
79   }
80  
81  
82 < formatval(r, s)                 /* return format value */
82 > 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 > printargs(ac, av, fp)           /* print arguments to a file */
141 > int  ac;
142 > char  **av;
143 > FILE  *fp;
144 > {
145 >        while (ac-- > 0) {
146 >                fputword(*av++, fp);
147 >                fputc(ac ? ' ' : '\n', fp);
148 >        }
149 > }
150 >
151 >
152 > int
153 > formatval(r, s)                 /* get format value (return true if format) */
154   register char  *r;
155   register char  *s;
156   {
157 <        s += FMTSTRL;
157 >        register char  *cp = FMTSTR;
158 >
159 >        while (*cp) if (*cp++ != *s++) return(0);
160          while (isspace(*s)) s++;
161 <        if (!*s) { *r = '\0'; return; }
162 <        while(*s) *r++ = *s++;
163 <        while (isspace(r[-1])) r--;
161 >        if (!*s) return(0);
162 >        if (r == NULL) return(1);
163 >        do
164 >                *r++ = *s++;
165 >        while(*s && !isspace(*s));
166          *r = '\0';
167 +        return(1);
168   }
169  
170  
171 + int
172 + isformat(s)                     /* is line a format line? */
173 + char  *s;
174 + {
175 +        return(formatval(NULL, s));
176 + }
177 +
178 +
179 + void
180   fputformat(s, fp)               /* put out a format value */
181   char  *s;
182   FILE  *fp;
# Line 88 | Line 187 | FILE  *fp;
187   }
188  
189  
190 + int
191   getheader(fp, f, p)             /* get header from file */
192   FILE  *fp;
193   int  (*f)();
# Line 97 | Line 197 | char  *p;
197  
198          for ( ; ; ) {
199                  buf[MAXLINE-2] = '\n';
200 <                if (fgets(buf, sizeof(buf), fp) == NULL)
200 >                if (fgets(buf, MAXLINE, fp) == NULL)
201                          return(-1);
202                  if (buf[0] == '\n')
203                          return(0);
204 + #ifdef MSDOS
205 +                if (buf[0] == '\r' && buf[1] == '\n')
206 +                        return(0);
207 + #endif
208                  if (buf[MAXLINE-2] != '\n') {
209                          ungetc(buf[MAXLINE-2], fp);     /* prevent false end */
210                          buf[MAXLINE-2] = '\0';
211                  }
212 <                if (f != NULL)
213 <                        (*f)(buf, p);
212 >                if (f != NULL && (*f)(buf, p) < 0)
213 >                        return(-1);
214          }
215   }
216  
# Line 117 | Line 221 | struct check {
221   };
222  
223  
224 < static
224 > static int
225   mycheck(s, cp)                  /* check a header line for format info. */
226   char  *s;
227   register struct check  *cp;
228   {
229 <        if (!strncmp(s,FMTSTR,FMTSTRL))
126 <                formatval(cp->fs, s);
127 <        else if (cp->fp != NULL)        /* don't copy format info. */
229 >        if (!formatval(cp->fs, s) && cp->fp != NULL)
230                  fputs(s, cp->fp);
231 +        return(0);
232   }
233  
234  
235 < /*
236 < * Copymatch(pat,str) checks pat for wildcards, and
237 < * copies str into pat if there is a match (returning true).
135 < */
136 <
137 < #ifdef COPYMATCH
138 < copymatch(pat, str)
139 < char    *pat, *str;
235 > int
236 > globmatch(p, s)                 /* check for match of s against pattern p */
237 > register char   *p, *s;
238   {
239 <        int     docopy = 0;
142 <        register char   *p = pat, *s = str;
239 >        int     setmatch;
240  
241          do {
242                  switch (*p) {
243                  case '?':                       /* match any character */
244                          if (!*s++)
245                                  return(0);
149                        docopy++;
246                          break;
247                  case '*':                       /* match any string */
248                          while (p[1] == '*') p++;
249                          do
250 <                                if ( (p[1]=='?' || p[1]==*s)
251 <                                                && copymatch(p+1,s) ) {
156 <                                        strcpy(pat, str);
250 >                                if ( (p[1]=='?' || p[1]==*s) &&
251 >                                                globmatch(p+1,s) )
252                                          return(1);
158                                }
253                          while (*s++);
254                          return(0);
255 +                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                  case '\\':                      /* literal next */
274                          p++;
275                  /* fall through */
# Line 168 | Line 280 | char   *pat, *str;
280                          break;
281                  }
282          } while (*p++);
171        if (docopy)
172                strcpy(pat, str);
283          return(1);
284   }
175 #else
176 #define copymatch(pat, s)       (!strcmp(pat, s))
177 #endif
285  
286  
287   /*
# Line 185 | Line 292 | char   *pat, *str;
292   * 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 < * Be sure that fmt is big enough to hold the match in such cases!
295 > * 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   * The input header (minus any format lines) is copied to fout
298   * if fout is not NULL.
299   */
300  
301 + int
302   checkheader(fin, fmt, fout)
303   FILE  *fin;
304   char  *fmt;
305   FILE  *fout;
306   {
307          struct check    cdat;
308 +        register char   *cp;
309  
310          cdat.fp = fout;
311          cdat.fs[0] = '\0';
312 <        if (getheader(fin, mycheck, &cdat) < 0)
312 >        if (getheader(fin, mycheck, (char *)&cdat) < 0)
313                  return(-1);
314 <        if (cdat.fs[0] != '\0')
315 <                return(copymatch(fmt, cdat.fs) ? 1 : -1);
316 <        return(0);
314 >        if (!cdat.fs[0])
315 >                return(0);
316 >        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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines