ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
Revision: 2.3
Committed: Thu Nov 12 16:20:09 1992 UTC (31 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +9 -5 lines
Log Message:
Changes for 32-bit PC port

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
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 *
12 * printargs(ac,av,fp) print an argument list to fp, followed by '\n'
13 * isformat(s) returns true if s is of the form "FORMAT=*"
14 * formatval(r,s) copy the format value in s to r
15 * fputformat(s,fp) write "FORMAT=%s" to fp
16 * getheader(fp,f,p) read header from fp, calling f(s,p) on each line
17 * checkheader(i,p,o) check header format from i against p and copy to o
18 *
19 * To copy header from input to output, use getheader(fin, fputs, fout)
20 */
21
22 #include <stdio.h>
23 #include <ctype.h>
24
25 #define MAXLINE 512
26
27 #ifndef BSD
28 #define index strchr
29 #endif
30
31 extern char *index();
32
33 char FMTSTR[] = "FORMAT=";
34 int FMTSTRL = 7;
35
36
37 printargs(ac, av, fp) /* print arguments to a file */
38 int ac;
39 char **av;
40 register FILE *fp;
41 {
42 int quote;
43
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 }
57 putc('\n', fp);
58 }
59
60
61 isformat(s) /* is line a format line? */
62 char *s;
63 {
64 return(!strncmp(s,FMTSTR,FMTSTRL));
65 }
66
67
68 formatval(r, s) /* return format value */
69 register char *r;
70 register char *s;
71 {
72 s += FMTSTRL;
73 while (isspace(*s)) s++;
74 if (!*s) { *r = '\0'; return; }
75 while(*s) *r++ = *s++;
76 while (isspace(r[-1])) r--;
77 *r = '\0';
78 }
79
80
81 fputformat(s, fp) /* put out a format value */
82 char *s;
83 FILE *fp;
84 {
85 fputs(FMTSTR, fp);
86 fputs(s, fp);
87 putc('\n', fp);
88 }
89
90
91 getheader(fp, f, p) /* get header from file */
92 FILE *fp;
93 int (*f)();
94 char *p;
95 {
96 char buf[MAXLINE];
97
98 for ( ; ; ) {
99 buf[MAXLINE-2] = '\n';
100 if (fgets(buf, MAXLINE, fp) == NULL)
101 return(-1);
102 if (buf[0] == '\n')
103 return(0);
104 #ifdef MSDOS
105 if (buf[0] == '\r' && buf[1] == '\n')
106 return(0);
107 #endif
108 if (buf[MAXLINE-2] != '\n') {
109 ungetc(buf[MAXLINE-2], fp); /* prevent false end */
110 buf[MAXLINE-2] = '\0';
111 }
112 if (f != NULL)
113 (*f)(buf, p);
114 }
115 }
116
117
118 struct check {
119 FILE *fp;
120 char fs[64];
121 };
122
123
124 static
125 mycheck(s, cp) /* check a header line for format info. */
126 char *s;
127 register struct check *cp;
128 {
129 if (!strncmp(s,FMTSTR,FMTSTRL))
130 formatval(cp->fs, s);
131 else if (cp->fp != NULL) /* don't copy format info. */
132 fputs(s, cp->fp);
133 }
134
135
136 /*
137 * Copymatch(pat,str) checks pat for wildcards, and
138 * copies str into pat if there is a match (returning true).
139 */
140
141 #ifdef COPYMATCH
142 copymatch(pat, str)
143 char *pat, *str;
144 {
145 int docopy = 0;
146 register char *p = pat, *s = str;
147
148 do {
149 switch (*p) {
150 case '?': /* match any character */
151 if (!*s++)
152 return(0);
153 docopy++;
154 break;
155 case '*': /* match any string */
156 while (p[1] == '*') p++;
157 do
158 if ( (p[1]=='?' || p[1]==*s)
159 && copymatch(p+1,s) ) {
160 strcpy(pat, str);
161 return(1);
162 }
163 while (*s++);
164 return(0);
165 case '\\': /* literal next */
166 p++;
167 /* fall through */
168 default: /* normal character */
169 if (*p != *s)
170 return(0);
171 s++;
172 break;
173 }
174 } while (*p++);
175 if (docopy)
176 strcpy(pat, str);
177 return(1);
178 }
179 #else
180 #define copymatch(pat, s) (!strcmp(pat, s))
181 #endif
182
183
184 /*
185 * Checkheader(fin,fmt,fout) returns a value of 1 if the input format
186 * matches the specification in fmt, 0 if no input format was found,
187 * and -1 if the input format does not match or there is an
188 * error reading the header. If fmt is empty, then -1 is returned
189 * if any input format is found (or there is an error), and 0 otherwise.
190 * If fmt contains any '*' or '?' characters, then checkheader
191 * does wildcard expansion and copies a matching result into fmt.
192 * Be sure that fmt is big enough to hold the match in such cases!
193 * The input header (minus any format lines) is copied to fout
194 * if fout is not NULL.
195 */
196
197 checkheader(fin, fmt, fout)
198 FILE *fin;
199 char *fmt;
200 FILE *fout;
201 {
202 struct check cdat;
203
204 cdat.fp = fout;
205 cdat.fs[0] = '\0';
206 if (getheader(fin, mycheck, &cdat) < 0)
207 return(-1);
208 if (cdat.fs[0] != '\0')
209 return(copymatch(fmt, cdat.fs) ? 1 : -1);
210 return(0);
211 }