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.12 by greg, Tue Feb 25 02:47:21 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 "copyright.h"
28 +
29   #include  <stdio.h>
30 + #include  <string.h>
31 + #include  <time.h>
32   #include  <ctype.h>
33  
34   #define  MAXLINE        512
# Line 37 | Line 43 | char  HDRSTR[] = "#?";         /* information header magic nu
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;
# Line 48 | Line 59 | register FILE  *fp;
59   }
60  
61  
62 + int
63   headidval(r,s)                  /* get header id (return true if is id) */
64   register char  *r, *s;
65   {
# Line 55 | Line 67 | register char  *r, *s;
67  
68          while (*cp) if (*cp++ != *s++) return(0);
69          if (r == NULL) return(1);
70 <        while (*s) *r++ = *s++;
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   {
# Line 68 | Line 81 | char  *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 < register FILE  *fp;
145 > FILE  *fp;
146   {
147          int  quote;
148  
149          while (ac-- > 0) {
150 <                if (index(*av, ' ') != NULL) {          /* quote it */
151 <                        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);
150 >                fputword(*av++, fp);
151 >                fputc(ac ? ' ' : '\n', fp);
152          }
91        putc('\n', fp);
153   }
154  
155  
156 + int
157   formatval(r, s)                 /* get format value (return true if format) */
158   register char  *r;
159   register char  *s;
# Line 102 | Line 164 | register char  *s;
164          while (isspace(*s)) s++;
165          if (!*s) return(0);
166          if (r == NULL) return(1);
167 <        while(*s) *r++ = *s++;
168 <        while (isspace(r[-1])) r--;
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   {
# Line 116 | Line 180 | char  *s;
180   }
181  
182  
183 + void
184   fputformat(s, fp)               /* put out a format value */
185   char  *s;
186   FILE  *fp;
# Line 126 | Line 191 | FILE  *fp;
191   }
192  
193  
194 + int
195   getheader(fp, f, p)             /* get header from file */
196   FILE  *fp;
197   int  (*f)();
# Line 147 | Line 213 | char  *p;
213                          ungetc(buf[MAXLINE-2], fp);     /* prevent false end */
214                          buf[MAXLINE-2] = '\0';
215                  }
216 <                if (f != NULL)
217 <                        (*f)(buf, p);
216 >                if (f != NULL && (*f)(buf, p) < 0)
217 >                        return(-1);
218          }
219   }
220  
# Line 159 | Line 225 | struct check {
225   };
226  
227  
228 < static
228 > static int
229   mycheck(s, cp)                  /* check a header line for format info. */
230   char  *s;
231   register struct check  *cp;
232   {
233          if (!formatval(cp->fs, s) && cp->fp != NULL)
234                  fputs(s, cp->fp);
235 +        return(0);
236   }
237  
238  
239 < /*
240 < * Copymatch(pat,str) checks pat for wildcards, and
241 < * copies str into pat if there is a match (returning true).
175 < */
176 <
177 < #ifdef COPYMATCH
178 < copymatch(pat, str)
179 < char    *pat, *str;
239 > int
240 > globmatch(p, s)                 /* check for match of s against pattern p */
241 > register char   *p, *s;
242   {
243 <        int     docopy = 0;
182 <        register char   *p = pat, *s = str;
243 >        int     setmatch;
244  
245          do {
246                  switch (*p) {
247                  case '?':                       /* match any character */
248                          if (!*s++)
249                                  return(0);
189                        docopy++;
250                          break;
251                  case '*':                       /* match any string */
252                          while (p[1] == '*') p++;
253                          do
254 <                                if ( (p[1]=='?' || p[1]==*s)
255 <                                                && copymatch(p+1,s) ) {
196 <                                        strcpy(pat, str);
254 >                                if ( (p[1]=='?' || p[1]==*s) &&
255 >                                                globmatch(p+1,s) )
256                                          return(1);
198                                }
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 */
# Line 208 | Line 284 | char   *pat, *str;
284                          break;
285                  }
286          } while (*p++);
211        if (docopy)
212                strcpy(pat, str);
287          return(1);
288   }
215 #else
216 #define copymatch(pat, s)       (!strcmp(pat, s))
217 #endif
289  
290  
291   /*
# Line 225 | Line 296 | char   *pat, *str;
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!
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] != '\0')
319 <                return(copymatch(fmt, cdat.fs) ? 1 : -1);
320 <        return(0);
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