ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.14
Committed: Tue Aug 30 06:10:12 2016 UTC (7 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R1
Changes since 2.13: +15 -1 lines
Log Message:
Added -a option to getinfo for adding lines to header

File Contents

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