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.4 by greg, Sun Feb 27 10:16:45 1994 UTC vs.
Revision 2.20 by schorsch, Sun Jul 27 22:12:01 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines