ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/lam.c
Revision: 1.18
Committed: Thu Jul 4 16:51:03 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.17: +7 -10 lines
Log Message:
Minor bug fix for Windoz -- was setting fixed-width ASCII input to binary output

File Contents

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