ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.22
Committed: Thu Mar 3 22:54:49 2022 UTC (8 months, 4 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +9 -3 lines
Log Message:
feat(getinfo): Now preserves byte alignment with -a and -c options

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.22 static const char RCSid[] = "$Id: getinfo.c,v 2.21 2019/07/24 17:27:54 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * getinfo.c - program to read info. header from file.
6     *
7     * 1/3/86
8     */
9    
10 greg 2.20 #include "rtio.h"
11 schorsch 2.6 #include "platform.h"
12 greg 2.15 #include "rtprocess.h"
13 schorsch 2.8 #include "resolu.h"
14 greg 2.2
15 greg 2.9 #ifdef getc_unlocked /* avoid nasty file-locking overhead */
16 greg 2.16 #undef getc
17 greg 2.9 #undef getchar
18     #undef putchar
19 greg 2.16 #define getc getc_unlocked
20 greg 2.9 #define getchar getchar_unlocked
21     #define putchar putchar_unlocked
22     #endif
23    
24 schorsch 2.8 static gethfunc tabstr;
25 greg 2.10 static void getdim(FILE *fp);
26 schorsch 2.8 static void copycat(void);
27 greg 1.1
28 greg 1.3
29 schorsch 2.8 static int
30     tabstr( /* put out line followed by tab */
31 greg 2.10 char *s,
32 schorsch 2.8 void *p
33     )
34 greg 1.1 {
35     while (*s) {
36     putchar(*s);
37     s++;
38     }
39     if (*--s == '\n')
40     putchar('\t');
41 gwlarson 2.4 return(0);
42 greg 1.1 }
43    
44    
45 schorsch 2.8 int
46     main(
47     int argc,
48     char **argv
49     )
50 greg 1.1 {
51     int dim = 0;
52 greg 2.22 char fmt[MAXFMTLEN];
53 greg 1.1 FILE *fp;
54     int i;
55    
56 greg 2.19 if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') &&
57     argv[1][1] == 'd') {
58     dim = 1 - 2*(argv[1][0] == '-');
59 greg 1.1 argc--; argv++;
60 greg 2.18 }
61     #ifdef getc_unlocked /* avoid lock/unlock overhead */
62     flockfile(stdin);
63     #endif
64     SET_FILE_BINARY(stdin);
65 greg 2.22 fmt[0] = '*'; fmt[1] = '\0';
66 greg 2.18 if (argc > 2 && !strcmp(argv[1], "-c")) {
67 greg 2.10 SET_FILE_BINARY(stdout);
68 greg 2.12 setvbuf(stdin, NULL, _IONBF, 2);
69 greg 2.22 if (checkheader(stdin, fmt, stdout) < 0) {
70 greg 2.21 fputs("Bad header!\n", stderr);
71 greg 2.17 return 1;
72 greg 2.21 }
73 greg 2.10 printargs(argc-2, argv+2, stdout);
74 greg 2.22 if (fmt[0] != '*') /* better be the same! */
75     fputformat(fmt, stdout);
76 greg 2.10 fputc('\n', stdout);
77 greg 2.18 if (dim) { /* copy resolution string? */
78     RESOLU rs;
79     if (!fgetsresolu(&rs, stdin)) {
80 greg 2.21 fputs("No resolution string!\n", stderr);
81 greg 2.18 return 1;
82     }
83 greg 2.19 if (dim > 0)
84     fputsresolu(&rs, stdout);
85 greg 2.18 }
86 greg 2.10 fflush(stdout);
87     execvp(argv[2], argv+2);
88     perror(argv[2]);
89     return 1;
90 greg 2.14 } else if (argc > 2 && !strcmp(argv[1], "-a")) {
91     SET_FILE_BINARY(stdout);
92 greg 2.22 if (checkheader(stdin, fmt, stdout) < 0) {
93 greg 2.21 fputs("Bad header!\n", stderr);
94 greg 2.17 return 1;
95 greg 2.21 }
96 greg 2.14 for (i = 2; i < argc; i++) {
97     int len = strlen(argv[i]);
98     if (!len) continue;
99     fputs(argv[i], stdout);
100     if (argv[i][len-1] != '\n')
101     fputc('\n', stdout);
102     }
103 greg 2.22 if (fmt[0] != '*')
104     fputformat(fmt, stdout);
105 greg 2.14 fputc('\n', stdout);
106     copycat();
107     return 0;
108 greg 1.2 } else if (argc == 2 && !strcmp(argv[1], "-")) {
109 schorsch 2.6 SET_FILE_BINARY(stdout);
110 greg 2.21 if (getheader(stdin, NULL, NULL) < 0) {
111     fputs("Bad header!\n", stderr);
112 greg 2.17 return 1;
113 greg 2.21 }
114 greg 2.19 if (dim < 0) { /* skip resolution string? */
115 greg 2.18 RESOLU rs;
116     if (!fgetsresolu(&rs, stdin)) {
117 greg 2.21 fputs("No resolution string!\n", stderr);
118 greg 2.18 return 1;
119     }
120     }
121 greg 1.2 copycat();
122 schorsch 2.8 return 0;
123 greg 1.1 }
124     for (i = 1; i < argc; i++) {
125     fputs(argv[i], stdout);
126     if ((fp = fopen(argv[i], "r")) == NULL)
127     fputs(": cannot open\n", stdout);
128     else {
129 greg 2.19 if (dim < 0) { /* dimensions only */
130     if (getheader(fp, NULL, NULL) < 0) {
131 greg 2.21 fputs("bad header!\n", stdout);
132 greg 2.19 continue;
133     }
134 greg 1.1 fputs(": ", stdout);
135     getdim(fp);
136     } else {
137 schorsch 2.8 tabstr(":\n", NULL);
138 greg 2.21 if (getheader(fp, tabstr, NULL) < 0) {
139     fputs(argv[i], stderr);
140     fputs(": bad header!\n", stderr);
141 greg 2.19 return 1;
142 greg 2.21 }
143 greg 2.10 fputc('\n', stdout);
144 greg 2.19 if (dim > 0) {
145     fputc('\t', stdout);
146     getdim(fp);
147     }
148 greg 1.1 }
149     fclose(fp);
150     }
151     }
152 schorsch 2.7 if (argc == 1) {
153 greg 2.19 if (dim < 0) {
154 greg 2.21 if (getheader(stdin, NULL, NULL) < 0) {
155     fputs("Bad header!\n", stderr);
156 greg 2.19 return 1;
157 greg 2.21 }
158 greg 1.1 getdim(stdin);
159     } else {
160 greg 2.21 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) {
161     fputs("Bad header!\n", stderr);
162 greg 2.17 return 1;
163 greg 2.21 }
164 greg 2.10 fputc('\n', stdout);
165 greg 2.19 if (dim > 0)
166     getdim(stdin);
167 greg 1.1 }
168 schorsch 2.7 }
169 schorsch 2.8 return 0;
170 greg 1.1 }
171    
172    
173 schorsch 2.8 static void
174     getdim( /* get dimensions from file */
175 greg 2.10 FILE *fp
176 schorsch 2.8 )
177 greg 1.1 {
178     int j;
179 greg 2.10 int c;
180 greg 2.19
181 greg 1.1 switch (c = getc(fp)) {
182     case '+': /* picture */
183     case '-':
184     do
185     putchar(c);
186     while (c != '\n' && (c = getc(fp)) != EOF);
187     break;
188     case 1: /* octree */
189     getc(fp);
190     j = 0;
191     while ((c = getc(fp)) != EOF)
192 greg 2.10 if (c == 0) {
193 greg 1.1 if (++j >= 4)
194     break;
195 greg 2.10 putchar(' ');
196     } else {
197 greg 1.1 putchar(c);
198 greg 2.10 }
199 greg 1.1 putchar('\n');
200     break;
201     default: /* ??? */
202     fputs("unknown file type\n", stdout);
203     break;
204     }
205 greg 1.2 }
206    
207    
208 schorsch 2.8 static void
209     copycat(void) /* copy input to output */
210 greg 1.2 {
211 greg 2.10 char buf[8192];
212 greg 2.11 int n;
213 greg 2.3
214 greg 2.10 fflush(stdout);
215     while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0)
216 greg 2.15 if (writebuf(fileno(stdout), buf, n) != n)
217 greg 2.10 break;
218 greg 1.1 }