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.1 by greg, Tue Nov 12 16:54:24 1991 UTC vs.
Revision 2.9 by gregl, Fri Oct 31 11:40:52 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   *
10   *      8/19/88
11   *
12 < *  printargs(ac,av,fp) print an argument list to fp, followed by '\n'
12 > *  newheader(t,fp)     start new information header identified by string t
13 > *  isheadid(s)         returns true if s is a header id line
14 > *  headidval(r,s)      copy header identifier value in s to r
15 > *  printargs(ac,av,fp) print an argument list to fp, followed by '\n'
16   *  isformat(s)         returns true if s is of the form "FORMAT=*"
17   *  formatval(r,s)      copy the format value in s to r
18   *  fputformat(s,fp)    write "FORMAT=%s" to fp
19   *  getheader(fp,f,p)   read header from fp, calling f(s,p) on each line
20 + *  globmatch(pat, str) check for glob match of str against pat
21   *  checkheader(i,p,o)  check header format from i against p and copy to o
22   *
23   *  To copy header from input to output, use getheader(fin, fputs, fout)
# Line 22 | Line 26 | static char SCCSid[] = "$SunId$ LBL";
26   #include  <stdio.h>
27   #include  <ctype.h>
28  
29 < #define  MAXLINE        512
29 > #define  MAXLINE        512
30  
31 < char  FMTSTR[] = "FORMAT=";
32 < int  FMTSTRL = 7;
31 > #ifndef BSD
32 > #define  index  strchr
33 > #endif
34  
35 + extern char  *index();
36  
37 < printargs(ac, av, fp)           /* print arguments to a file */
38 < int  ac;
39 < char  **av;
40 < FILE  *fp;
37 > char  HDRSTR[] = "#?";          /* information header magic number */
38 >
39 > char  FMTSTR[] = "FORMAT=";     /* format identifier */
40 >
41 >
42 > newheader(s, fp)                /* identifying line of information header */
43 > char  *s;
44 > register FILE  *fp;
45   {
46 <        while (ac-- > 0) {
47 <                fputs(*av++, fp);
38 <                putc(' ', fp);
39 <        }
46 >        fputs(HDRSTR, fp);
47 >        fputs(s, fp);
48          putc('\n', fp);
49   }
50  
51  
52 < isformat(s)                     /* is line a format line? */
52 > int
53 > headidval(r,s)                  /* get header id (return true if is id) */
54 > register char  *r, *s;
55 > {
56 >        register char  *cp = HDRSTR;
57 >
58 >        while (*cp) if (*cp++ != *s++) return(0);
59 >        if (r == NULL) return(1);
60 >        while (*s && !isspace(*s)) *r++ = *s++;
61 >        *r = '\0';
62 >        return(1);
63 > }
64 >
65 >
66 > int
67 > isheadid(s)                     /* check to see if line is header id */
68   char  *s;
69   {
70 <        return(!strncmp(s,FMTSTR,FMTSTRL));
70 >        return(headidval(NULL, s));
71   }
72  
73  
74 < formatval(r, s)                 /* return format value */
74 > printargs(ac, av, fp)           /* print arguments to a file */
75 > int  ac;
76 > char  **av;
77 > register FILE  *fp;
78 > {
79 >        int  quote;
80 >
81 >        while (ac-- > 0) {
82 >                if (index(*av, ' ') != NULL) {          /* quote it */
83 >                        if (index(*av, '\'') != NULL)
84 >                                quote = '"';
85 >                        else
86 >                                quote = '\'';
87 >                        putc(quote, fp);
88 >                        fputs(*av++, fp);
89 >                        putc(quote, fp);
90 >                } else
91 >                        fputs(*av++, fp);
92 >                putc(ac ? ' ' : '\n', fp);
93 >        }
94 > }
95 >
96 >
97 > int
98 > formatval(r, s)                 /* get format value (return true if format) */
99   register char  *r;
100   register char  *s;
101   {
102 <        s += FMTSTRL;
102 >        register char  *cp = FMTSTR;
103 >
104 >        while (*cp) if (*cp++ != *s++) return(0);
105          while (isspace(*s)) s++;
106 <        if (!*s) { *r = '\0'; return; }
107 <        while(*s) *r++ = *s++;
108 <        while (isspace(r[-1])) r--;
106 >        if (!*s) return(0);
107 >        if (r == NULL) return(1);
108 >        do
109 >                *r++ = *s++;
110 >        while(*s && !isspace(*s));
111          *r = '\0';
112 +        return(1);
113   }
114  
115  
116 + int
117 + isformat(s)                     /* is line a format line? */
118 + char  *s;
119 + {
120 +        return(formatval(NULL, s));
121 + }
122 +
123 +
124   fputformat(s, fp)               /* put out a format value */
125   char  *s;
126   FILE  *fp;
# Line 71 | Line 131 | FILE  *fp;
131   }
132  
133  
134 + int
135   getheader(fp, f, p)             /* get header from file */
136   FILE  *fp;
137   int  (*f)();
# Line 80 | Line 141 | char  *p;
141  
142          for ( ; ; ) {
143                  buf[MAXLINE-2] = '\n';
144 <                if (fgets(buf, sizeof(buf), fp) == NULL)
144 >                if (fgets(buf, MAXLINE, fp) == NULL)
145                          return(-1);
146                  if (buf[0] == '\n')
147                          return(0);
148 + #ifdef MSDOS
149 +                if (buf[0] == '\r' && buf[1] == '\n')
150 +                        return(0);
151 + #endif
152                  if (buf[MAXLINE-2] != '\n') {
153                          ungetc(buf[MAXLINE-2], fp);     /* prevent false end */
154                          buf[MAXLINE-2] = '\0';
# Line 105 | Line 170 | mycheck(s, cp)                 /* check a header line for format inf
170   char  *s;
171   register struct check  *cp;
172   {
173 <        if (!strncmp(s,FMTSTR,FMTSTRL))
109 <                formatval(cp->fs, s);
110 <        else if (cp->fp != NULL)        /* don't copy format info. */
173 >        if (!formatval(cp->fs, s) && cp->fp != NULL)
174                  fputs(s, cp->fp);
175   }
176  
177  
178 < /*
179 < * Copymatch(pat,str) checks pat for wildcards, and
117 < * copies str into pat if there is a match (returning true).
118 < */
119 <
120 < #ifdef COPYMATCH
121 < copymatch(pat, str)
178 > int
179 > globmatch(pat, str)             /* check for glob match of str against pat */
180   char    *pat, *str;
181   {
124        int     docopy = 0;
182          register char   *p = pat, *s = str;
183  
184          do {
# Line 129 | Line 186 | char   *pat, *str;
186                  case '?':                       /* match any character */
187                          if (!*s++)
188                                  return(0);
132                        docopy++;
189                          break;
190                  case '*':                       /* match any string */
191                          while (p[1] == '*') p++;
192                          do
193 <                                if ( (p[1]=='?' || p[1]==*s)
194 <                                                && copymatch(p+1,s) ) {
139 <                                        strcpy(pat, str);
193 >                                if ( (p[1]=='?' || p[1]==*s) &&
194 >                                                globmatch(p+1,s) )
195                                          return(1);
141                                }
196                          while (*s++);
197                          return(0);
198                  case '\\':                      /* literal next */
# Line 151 | Line 205 | char   *pat, *str;
205                          break;
206                  }
207          } while (*p++);
154        if (docopy)
155                strcpy(pat, str);
208          return(1);
209   }
158 #else
159 #define copymatch(pat, s)       (!strcmp(pat, s))
160 #endif
210  
211  
212   /*
# Line 168 | Line 217 | char   *pat, *str;
217   * if any input format is found (or there is an error), and 0 otherwise.
218   * If fmt contains any '*' or '?' characters, then checkheader
219   * does wildcard expansion and copies a matching result into fmt.
220 < * Be sure that fmt is big enough to hold the match in such cases!
220 > * Be sure that fmt is big enough to hold the match in such cases,
221 > * and that it is not a static, read-only string!
222   * The input header (minus any format lines) is copied to fout
223   * if fout is not NULL.
224   */
225  
226 + int
227   checkheader(fin, fmt, fout)
228   FILE  *fin;
229   char  *fmt;
230   FILE  *fout;
231   {
232          struct check    cdat;
233 +        register char   *cp;
234  
235          cdat.fp = fout;
236          cdat.fs[0] = '\0';
237          if (getheader(fin, mycheck, &cdat) < 0)
238                  return(-1);
239 <        if (cdat.fs[0] != '\0')
240 <                return(copymatch(fmt, cdat.fs) ? 1 : -1);
241 <        return(0);
239 >        if (!cdat.fs[0])
240 >                return(0);
241 >        for (cp = fmt; *cp; cp++)               /* check for globbing */
242 >                if (*cp == '?' | *cp == '*')
243 >                        if (globmatch(fmt, cdat.fs)) {
244 >                                strcpy(fmt, cdat.fs);
245 >                                return(1);
246 >                        } else
247 >                                return(-1);
248 >        return(strcmp(fmt, cdat.fs) ? -1 : 1);  /* literal match */
249   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines