ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.18
Committed: Sat Jul 6 14:08:07 2019 UTC (4 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.17: +22 -6 lines
Log Message:
Made getinfo -d option useful for header-skip ('-') and command ('-c') options

File Contents

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