ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
Revision: 2.5
Committed: Sun Oct 15 14:07:58 1995 UTC (28 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +11 -6 lines
Log Message:
made changes for new 32-bit_rle_xyze picture type

File Contents

# User Rev Content
1 greg 2.4 /* Copyright (c) 1994 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * header.c - routines for reading and writing information headers.
9     *
10     * 8/19/88
11 greg 1.3 *
12 greg 2.4 * 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 greg 2.3 * printargs(ac,av,fp) print an argument list to fp, followed by '\n'
16 greg 1.3 * 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 greg 2.5 * copymatch(pat, str) copy str into pat if glob match
21 greg 1.3 * 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)
24 greg 1.1 */
25    
26     #include <stdio.h>
27 greg 1.3 #include <ctype.h>
28 greg 1.1
29 greg 2.3 #define MAXLINE 512
30 greg 1.1
31 greg 2.2 #ifndef BSD
32 greg 2.3 #define index strchr
33 greg 2.2 #endif
34    
35     extern char *index();
36    
37 greg 2.4 char HDRSTR[] = "#?"; /* information header magic number */
38 greg 1.2
39 greg 2.4 char FMTSTR[] = "FORMAT="; /* format identifier */
40 greg 1.2
41 greg 2.4
42     newheader(s, fp) /* identifying line of information header */
43     char *s;
44     register FILE *fp;
45     {
46     fputs(HDRSTR, fp);
47     fputs(s, fp);
48     putc('\n', fp);
49     }
50    
51    
52 greg 2.5 int
53 greg 2.4 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) *r++ = *s++;
61     *r = '\0';
62     return(1);
63     }
64    
65    
66 greg 2.5 int
67 greg 2.4 isheadid(s) /* check to see if line is header id */
68     char *s;
69     {
70     return(headidval(NULL, s));
71     }
72    
73    
74 greg 1.1 printargs(ac, av, fp) /* print arguments to a file */
75     int ac;
76     char **av;
77 greg 2.2 register FILE *fp;
78 greg 1.1 {
79 greg 2.2 int quote;
80    
81 greg 1.1 while (ac-- > 0) {
82 greg 2.2 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 greg 1.1 putc(' ', fp);
93     }
94     putc('\n', fp);
95     }
96    
97    
98 greg 2.5 int
99 greg 2.4 formatval(r, s) /* get format value (return true if format) */
100 greg 1.2 register char *r;
101     register char *s;
102     {
103 greg 2.4 register char *cp = FMTSTR;
104    
105     while (*cp) if (*cp++ != *s++) return(0);
106 greg 1.3 while (isspace(*s)) s++;
107 greg 2.4 if (!*s) return(0);
108     if (r == NULL) return(1);
109 greg 2.5 do
110     *r++ = *s++;
111     while(*s && !isspace(*s));
112 greg 1.2 *r = '\0';
113 greg 2.4 return(1);
114 greg 1.2 }
115    
116    
117 greg 2.5 int
118 greg 2.4 isformat(s) /* is line a format line? */
119     char *s;
120     {
121     return(formatval(NULL, s));
122     }
123    
124    
125 greg 1.2 fputformat(s, fp) /* put out a format value */
126     char *s;
127 greg 1.1 FILE *fp;
128 greg 1.2 {
129     fputs(FMTSTR, fp);
130     fputs(s, fp);
131     putc('\n', fp);
132     }
133    
134    
135 greg 2.5 int
136 greg 1.2 getheader(fp, f, p) /* get header from file */
137     FILE *fp;
138 greg 1.1 int (*f)();
139 greg 1.2 char *p;
140 greg 1.1 {
141     char buf[MAXLINE];
142    
143     for ( ; ; ) {
144     buf[MAXLINE-2] = '\n';
145 greg 2.3 if (fgets(buf, MAXLINE, fp) == NULL)
146 greg 1.1 return(-1);
147     if (buf[0] == '\n')
148     return(0);
149 greg 2.3 #ifdef MSDOS
150     if (buf[0] == '\r' && buf[1] == '\n')
151     return(0);
152     #endif
153 greg 1.1 if (buf[MAXLINE-2] != '\n') {
154     ungetc(buf[MAXLINE-2], fp); /* prevent false end */
155     buf[MAXLINE-2] = '\0';
156     }
157     if (f != NULL)
158 greg 1.2 (*f)(buf, p);
159 greg 1.1 }
160     }
161    
162    
163 greg 1.2 struct check {
164     FILE *fp;
165 greg 1.3 char fs[64];
166 greg 1.2 };
167 greg 1.1
168 greg 1.2
169 greg 1.1 static
170 greg 1.2 mycheck(s, cp) /* check a header line for format info. */
171 greg 1.1 char *s;
172 greg 1.2 register struct check *cp;
173 greg 1.1 {
174 greg 2.4 if (!formatval(cp->fs, s) && cp->fp != NULL)
175 greg 1.2 fputs(s, cp->fp);
176 greg 1.1 }
177    
178    
179 greg 1.3 /*
180     * Copymatch(pat,str) checks pat for wildcards, and
181     * copies str into pat if there is a match (returning true).
182     */
183    
184 greg 2.5 int
185 greg 1.3 copymatch(pat, str)
186     char *pat, *str;
187     {
188     int docopy = 0;
189     register char *p = pat, *s = str;
190    
191     do {
192     switch (*p) {
193     case '?': /* match any character */
194     if (!*s++)
195     return(0);
196     docopy++;
197     break;
198     case '*': /* match any string */
199     while (p[1] == '*') p++;
200     do
201 greg 1.4 if ( (p[1]=='?' || p[1]==*s)
202 greg 1.3 && copymatch(p+1,s) ) {
203     strcpy(pat, str);
204     return(1);
205     }
206     while (*s++);
207     return(0);
208     case '\\': /* literal next */
209     p++;
210     /* fall through */
211     default: /* normal character */
212     if (*p != *s)
213     return(0);
214     s++;
215     break;
216     }
217     } while (*p++);
218     if (docopy)
219     strcpy(pat, str);
220     return(1);
221     }
222    
223    
224     /*
225     * Checkheader(fin,fmt,fout) returns a value of 1 if the input format
226     * matches the specification in fmt, 0 if no input format was found,
227     * and -1 if the input format does not match or there is an
228 greg 1.4 * error reading the header. If fmt is empty, then -1 is returned
229 greg 1.3 * if any input format is found (or there is an error), and 0 otherwise.
230     * If fmt contains any '*' or '?' characters, then checkheader
231     * does wildcard expansion and copies a matching result into fmt.
232     * Be sure that fmt is big enough to hold the match in such cases!
233     * The input header (minus any format lines) is copied to fout
234     * if fout is not NULL.
235     */
236    
237 greg 2.5 int
238 greg 1.3 checkheader(fin, fmt, fout)
239 greg 1.2 FILE *fin;
240     char *fmt;
241     FILE *fout;
242 greg 1.1 {
243 greg 1.2 struct check cdat;
244    
245     cdat.fp = fout;
246     cdat.fs[0] = '\0';
247     if (getheader(fin, mycheck, &cdat) < 0)
248 greg 1.3 return(-1);
249     if (cdat.fs[0] != '\0')
250     return(copymatch(fmt, cdat.fs) ? 1 : -1);
251 greg 1.2 return(0);
252 greg 1.1 }