ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/lam.c
Revision: 1.17
Committed: Thu Aug 18 00:52:47 2016 UTC (7 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad5R1
Changes since 1.16: +4 -3 lines
Log Message:
Switched over to more efficient fread/fwrite replacements getbinary/putbinary

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: lam.c,v 1.16 2016/03/24 19:00:54 greg Exp $";
3 #endif
4 /*
5 * lam.c - simple program to laminate files.
6 *
7 * 7/14/88 Greg Ward
8 */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <ctype.h>
14
15 #include "platform.h"
16 #include "rtprocess.h"
17 #include "rtio.h"
18
19 #define MAXFILE 512 /* maximum number of files */
20
21 #define MAXLINE 65536 /* maximum input line */
22
23 long incnt = 0; /* limit number of records? */
24
25 FILE *input[MAXFILE];
26 int bytsiz[MAXFILE];
27 char *tabc[MAXFILE];
28 int nfiles;
29
30 char buf[MAXLINE];
31
32 int
33 main(argc, argv)
34 int argc;
35 char *argv[];
36 {
37 int unbuff = 0;
38 int binout = 0;
39 int i;
40 char *curtab;
41 int curbytes;
42 int puteol;
43
44 curtab = "\t";
45 curbytes = 0;
46 nfiles = 0;
47 for (i = 1; i < argc; i++) {
48 if (argv[i][0] == '-') {
49 switch (argv[i][1]) {
50 case 't':
51 curtab = argv[i]+2;
52 break;
53 case 'u':
54 unbuff = !unbuff;
55 break;
56 case 'i':
57 switch (argv[i][2]) {
58 case 'n':
59 incnt = atol(argv[++i]);
60 break;
61 case 'f':
62 curbytes = sizeof(float);
63 break;
64 case 'd':
65 curbytes = sizeof(double);
66 break;
67 case 'i':
68 curbytes = sizeof(int);
69 break;
70 case 'w':
71 curbytes = 2;
72 break;
73 case 'b':
74 curbytes = 1;
75 break;
76 case 'a':
77 curbytes = argv[i][3] ? -1 : 0;
78 break;
79 default:
80 goto badopt;
81 }
82 if (isdigit(argv[i][3]))
83 curbytes *= atoi(argv[i]+3);
84 if (abs(curbytes) > MAXLINE) {
85 fputs(argv[0], stderr);
86 fputs(": input size too big\n", stderr);
87 exit(1);
88 }
89 if (curbytes) {
90 curtab = "";
91 ++binout;
92 }
93 break;
94 case '\0':
95 tabc[nfiles] = curtab;
96 input[nfiles] = stdin;
97 if (curbytes > 0)
98 SET_FILE_BINARY(input[nfiles]);
99 else
100 curbytes = -curbytes;
101 bytsiz[nfiles++] = curbytes;
102 break;
103 badopt:;
104 default:
105 fputs(argv[0], stderr);
106 fputs(": bad option\n", stderr);
107 exit(1);
108 }
109 } else if (argv[i][0] == '!') {
110 tabc[nfiles] = curtab;
111 if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) {
112 fputs(argv[i], stderr);
113 fputs(": cannot start command\n", stderr);
114 exit(1);
115 }
116 if (curbytes > 0)
117 SET_FILE_BINARY(input[nfiles]);
118 else
119 curbytes = -curbytes;
120 bytsiz[nfiles++] = curbytes;
121 } else {
122 tabc[nfiles] = curtab;
123 if ((input[nfiles] = fopen(argv[i], "r")) == NULL) {
124 fputs(argv[i], stderr);
125 fputs(": cannot open file\n", stderr);
126 exit(1);
127 }
128 if (curbytes > 0)
129 SET_FILE_BINARY(input[nfiles]);
130 else
131 curbytes = -curbytes;
132 bytsiz[nfiles++] = curbytes;
133 }
134 if (nfiles >= MAXFILE) {
135 fputs(argv[0], stderr);
136 fputs(": too many input streams\n", stderr);
137 exit(1);
138 }
139 }
140 if (binout) /* binary output? */
141 SET_FILE_BINARY(stdout);
142 #ifdef getc_unlocked /* avoid lock/unlock overhead */
143 for (i = nfiles; i--; )
144 flockfile(input[i]);
145 flockfile(stdout);
146 #endif
147 puteol = 0; /* any ASCII output at all? */
148 for (i = nfiles; i--; )
149 if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') {
150 puteol++;
151 break;
152 }
153 do { /* main loop */
154 for (i = 0; i < nfiles; i++) {
155 if (bytsiz[i]) { /* binary file */
156 if (getbinary(buf, bytsiz[i], 1, input[i]) < 1)
157 break;
158 if (i)
159 fputs(tabc[i], stdout);
160 putbinary(buf, bytsiz[i], 1, stdout);
161 } else {
162 if (fgets(buf, MAXLINE, input[i]) == NULL)
163 break;
164 if (i)
165 fputs(tabc[i], stdout);
166 buf[strlen(buf)-1] = '\0';
167 fputs(buf, stdout);
168 }
169 }
170 if (i < nfiles)
171 break;
172 if (puteol)
173 putchar('\n');
174 if (unbuff)
175 fflush(stdout);
176 } while (--incnt);
177 return(0);
178 }