ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/lam.c
Revision: 1.28
Committed: Thu Mar 6 20:19:03 2025 UTC (2 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.27: +4 -2 lines
Log Message:
perf(rlam): Increased maximum input line width to 1 MByte

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 1.28 static const char RCSid[] = "$Id: lam.c,v 1.27 2025/02/06 19:52:05 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * lam.c - simple program to laminate files.
6     *
7     * 7/14/88 Greg Ward
8     */
9    
10 greg 1.7 #include <ctype.h>
11 schorsch 1.2
12 greg 1.25 #include "rtio.h"
13 schorsch 1.2 #include "platform.h"
14 greg 1.25 #include "paths.h"
15 greg 1.1
16 greg 1.28 #ifndef MAXLINE
17     #define MAXLINE (1L<<20) /* maximum input line per stream */
18     #endif
19 greg 1.1
20 greg 1.26 struct instream { /* structure to hold input stream info */
21     FILE *input; /* input stream */
22     int bytsiz; /* bytes/component if binary */
23     char *tabc; /* data separation string */
24     } *rifile = NULL;
25 greg 1.1
26 greg 1.20 int nfiles = 0;
27 greg 1.1
28     char buf[MAXLINE];
29    
30 schorsch 1.4 int
31 greg 1.20 main(int argc, char *argv[])
32 greg 1.1 {
33 greg 1.18 long incnt = 0;
34 greg 1.11 int unbuff = 0;
35 greg 1.14 int binout = 0;
36 greg 1.18 char *curtab = "\t";
37     int curbytes = 0;
38     int puteol;
39 greg 1.11 int i;
40 greg 1.1
41 greg 1.26 rifile = (struct instream *)calloc(argc-1, sizeof(struct instream));
42     if (!rifile) {
43     fputs(argv[0], stderr);
44     fputs(": not enough memory\n", stderr);
45     return(1);
46     }
47 greg 1.1 for (i = 1; i < argc; i++) {
48     if (argv[i][0] == '-') {
49     switch (argv[i][1]) {
50     case 't':
51 greg 1.6 curtab = argv[i]+2;
52 greg 1.23 if (!*curtab) curtab = "\n";
53 greg 1.27 continue;
54 greg 1.11 case 'u':
55     unbuff = !unbuff;
56 greg 1.27 continue;
57 greg 1.7 case 'i':
58     switch (argv[i][2]) {
59 greg 1.15 case 'n':
60     incnt = atol(argv[++i]);
61 greg 1.27 continue;
62 greg 1.7 case 'f':
63 greg 1.24 case 'F':
64 greg 1.7 curbytes = sizeof(float);
65     break;
66     case 'd':
67 greg 1.24 case 'D':
68 greg 1.7 curbytes = sizeof(double);
69     break;
70 greg 1.10 case 'i':
71 greg 1.24 case 'I':
72 greg 1.10 curbytes = sizeof(int);
73     break;
74 greg 1.7 case 'w':
75 greg 1.24 case 'W':
76 greg 1.10 curbytes = 2;
77     break;
78     case 'b':
79     curbytes = 1;
80 greg 1.7 break;
81     case 'a':
82 greg 1.12 curbytes = argv[i][3] ? -1 : 0;
83 greg 1.7 break;
84     default:
85     goto badopt;
86     }
87     if (isdigit(argv[i][3]))
88     curbytes *= atoi(argv[i]+3);
89 greg 1.19 curbytes += (curbytes == -1);
90     if (curbytes > MAXLINE) {
91 greg 1.7 fputs(argv[0], stderr);
92 greg 1.12 fputs(": input size too big\n", stderr);
93 greg 1.19 return(1);
94 greg 1.7 }
95 greg 1.19 if (curbytes > 0) {
96 greg 1.8 curtab = "";
97 greg 1.19 ++binout;
98 greg 1.14 }
99 greg 1.27 continue;
100 greg 1.1 case '\0':
101 greg 1.26 rifile[nfiles].tabc = curtab;
102     rifile[nfiles].input = stdin;
103 greg 1.12 if (curbytes > 0)
104 greg 1.26 SET_FILE_BINARY(rifile[nfiles].input);
105     rifile[nfiles++].bytsiz = curbytes;
106 greg 1.27 continue;
107 greg 1.7 badopt:;
108 greg 1.1 default:
109     fputs(argv[0], stderr);
110 greg 1.27 fputs(": unknown option '", stderr);
111     fputs(argv[i], stderr);
112     fputs("'\n", stderr);
113 greg 1.19 return(1);
114 greg 1.1 }
115 greg 1.27 }
116     if (argv[i][0] == '!') {
117 greg 1.26 rifile[nfiles].tabc = curtab;
118     if ((rifile[nfiles].input = popen(argv[i]+1, "r")) == NULL) {
119 greg 1.1 fputs(argv[i], stderr);
120     fputs(": cannot start command\n", stderr);
121 greg 1.19 return(1);
122 greg 1.1 }
123 greg 1.12 if (curbytes > 0)
124 greg 1.26 SET_FILE_BINARY(rifile[nfiles].input);
125     rifile[nfiles++].bytsiz = curbytes;
126 greg 1.1 } else {
127 greg 1.26 rifile[nfiles].tabc = curtab;
128     if ((rifile[nfiles].input = fopen(argv[i], "r")) == NULL) {
129 greg 1.1 fputs(argv[i], stderr);
130     fputs(": cannot open file\n", stderr);
131 greg 1.19 return(1);
132 greg 1.1 }
133 greg 1.12 if (curbytes > 0)
134 greg 1.26 SET_FILE_BINARY(rifile[nfiles].input);
135     rifile[nfiles++].bytsiz = curbytes;
136 greg 1.1 }
137     }
138 greg 1.19 if (!nfiles) {
139     fputs(argv[0], stderr);
140     fputs(": no input streams\n", stderr);
141     return(1);
142 greg 1.26 } /* reduce array to size we need */
143     rifile = (struct instream *)realloc(rifile, nfiles*sizeof(struct instream));
144     if (!rifile) {
145     fputs(argv[0], stderr);
146     fputs(": realloc() failed!\n", stderr);
147     return(1);
148 greg 1.19 }
149 greg 1.14 if (binout) /* binary output? */
150     SET_FILE_BINARY(stdout);
151     #ifdef getc_unlocked /* avoid lock/unlock overhead */
152     for (i = nfiles; i--; )
153 greg 1.26 flockfile(rifile[i].input);
154 greg 1.14 flockfile(stdout);
155     #endif
156 greg 1.15 puteol = 0; /* any ASCII output at all? */
157 greg 1.7 for (i = nfiles; i--; )
158 greg 1.26 puteol += (rifile[i].bytsiz <= 0);
159 greg 1.16 do { /* main loop */
160 greg 1.1 for (i = 0; i < nfiles; i++) {
161 greg 1.26 if (rifile[i].bytsiz > 0) { /* binary input */
162     if (getbinary(buf, rifile[i].bytsiz, 1, rifile[i].input) < 1)
163 greg 1.11 break;
164 greg 1.26 if (putbinary(buf, rifile[i].bytsiz, 1, stdout) != 1)
165 greg 1.22 break;
166 greg 1.26 } else if (rifile[i].bytsiz < 0) { /* multi-line input */
167     int n = -rifile[i].bytsiz;
168 greg 1.19 while (n--) {
169 greg 1.26 if (fgets(buf, MAXLINE, rifile[i].input) == NULL)
170 greg 1.19 break;
171 greg 1.26 if ((i > 0) | (n < -rifile[i].bytsiz-1))
172     fputs(rifile[i].tabc, stdout);
173 greg 1.19 buf[strlen(buf)-1] = '\0';
174 greg 1.22 if (fputs(buf, stdout) == EOF)
175     break;
176 greg 1.19 }
177     if (n >= 0) /* fell short? */
178     break;
179     } else { /* single-line input */
180 greg 1.26 if (fgets(buf, MAXLINE, rifile[i].input) == NULL)
181 greg 1.11 break;
182 greg 1.1 if (i)
183 greg 1.26 fputs(rifile[i].tabc, stdout);
184 greg 1.1 buf[strlen(buf)-1] = '\0';
185 greg 1.22 if (fputs(buf, stdout) == EOF)
186     break;
187 greg 1.1 }
188     }
189 greg 1.11 if (i < nfiles)
190     break;
191     if (puteol)
192 greg 1.1 putchar('\n');
193 greg 1.11 if (unbuff)
194     fflush(stdout);
195 greg 1.16 } while (--incnt);
196 greg 1.21 /* check ending */
197 greg 1.22 if (fflush(stdout) == EOF) {
198     fputs(argv[0], stderr);
199     fputs(": write error on standard output\n", stderr);
200     return(1);
201     }
202 greg 1.21 if (incnt > 0) {
203     fputs(argv[0], stderr);
204     fputs(": warning: premature EOD\n", stderr);
205     }
206 greg 1.11 return(0);
207 greg 1.1 }