ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/rsplit.c
(Generate patch)

Comparing ray/src/cal/rsplit.c (file contents):
Revision 1.15 by greg, Sun Apr 5 15:07:09 2020 UTC vs.
Revision 1.17 by greg, Fri Jun 7 20:26:53 2024 UTC

# Line 17 | Line 17 | static const char      RCSid[] = "$Id$";
17   #define DOHEADER        1
18   #define DORESOLU        2
19  
20 < #define MAXFILE         512             /* maximum number of files */
20 > int                     swapped = 0;    /* input is byte-swapped */
21  
22 < static int              swapped = 0;    /* input is byte-swapped */
22 > struct outstream {              /* structure to hold output stream info */
23 >        const char      *outspec;       /* output specification */
24 >        FILE            *output;        /* output stream */
25 >        int             ncomp;          /* component count */
26 >        int             bytsiz;         /* bytes/component if binary */
27 >        int             hdrflags;       /* header output flags */
28 >        int             termc;          /* data separation character */
29 >        const char      *format;        /* data format */
30 > }       *rofile = NULL;
31  
32 < static FILE             *output[MAXFILE];
25 < static int              ncomp[MAXFILE];
26 < static int              bytsiz[MAXFILE];
27 < static int              hdrflags[MAXFILE];
28 < static const char       *format[MAXFILE];
29 < static int              termc[MAXFILE];
30 < static int              nfiles = 0;
32 > int             nfiles = 0;     /* output file count */
33  
34 < static RESOLU   ourres = {PIXSTANDARD, 0, 0};
34 > RESOLU          ourres = {PIXSTANDARD, 0, 0};
35  
36 < static char     buf[16384];             /* input buffer used in scanOK() */
36 > char            buf[16384];             /* input buffer used in scanOK() */
37  
38  
39   /* process header line */
40 < static int
40 > int
41   headline(char *s, void *p)
42   {
43          extern const char       FMTSTR[];
# Line 55 | Line 57 | headline(char *s, void *p)
57          }
58          i = nfiles;
59          while (i--)                     /* else copy line to output streams */
60 <                if (hdrflags[i] & DOHEADER)
61 <                        fputs(s, output[i]);
60 >                if (rofile[i].hdrflags & DOHEADER)
61 >                        fputs(s, rofile[i].output);
62          return(1);
63   }
64  
65  
66   /* scan field into buffer up to and including terminating byte */
67 < static int
67 > int
68   scanOK(int termc)
69   {
70          int     skip_white = (termc == ' ');
# Line 102 | Line 104 | main(int argc, char *argv[])
104          int             curbytes = 0;
105          int             curflags = 0;
106          const char      *curfmt = "ascii";
105        short           outndx[MAXFILE];
107          int             i;
108  
109 <        memset(outndx, 0, sizeof(outndx));
109 >        rofile = (struct outstream *)calloc(argc-1, sizeof(struct outstream));
110 >        if (!rofile) {
111 >                fputs(argv[0], stderr);
112 >                fputs(": not enough memory\n", stderr);
113 >                return(1);
114 >        }
115          for (i = 1; i < argc; i++) {
116                  if (argv[i][0] == '-') {
117                          switch (argv[i][1]) {
# Line 187 | Line 193 | main(int argc, char *argv[])
193                          case '\0':                      /* stdout */
194                                  if (!nstdoutcomp) {     /* first use? */
195                                          needres |= (curflags & DORESOLU);
196 <                                        hdrflags[nfiles] = curflags;
196 >                                        rofile[nfiles].hdrflags = curflags;
197                                  }
198 <                                output[nfiles] = stdout;
198 >                                rofile[nfiles].output = stdout;
199                                  if (curbytes > 0)
200 <                                        SET_FILE_BINARY(output[nfiles]);
201 <                                termc[nfiles] = curterm;
202 <                                format[nfiles] = curfmt;
200 >                                        SET_FILE_BINARY(rofile[nfiles].output);
201 >                                rofile[nfiles].termc = curterm;
202 >                                rofile[nfiles].format = curfmt;
203                                  nstdoutcomp +=
204 <                                        ncomp[nfiles] = curncomp;
205 <                                bytsiz[nfiles] = curbytes;
206 <                                outndx[nfiles++] = i;
204 >                                        rofile[nfiles].ncomp = curncomp;
205 >                                rofile[nfiles].bytsiz = curbytes;
206 >                                rofile[nfiles++].outspec = argv[i];
207                                  break;
208                          badopt:;
209                          default:
# Line 206 | Line 212 | main(int argc, char *argv[])
212                                  return(1);
213                          }
214                  } else if (argv[i][0] == '.' && !argv[i][1]) {
215 <                        output[nfiles] = NULL;          /* discard data */
216 <                        termc[nfiles] = curterm;
217 <                        format[nfiles] = curfmt;
218 <                        ncomp[nfiles] = curncomp;
219 <                        bytsiz[nfiles] = curbytes;
220 <                        outndx[nfiles++] = i;
215 >                        rofile[nfiles].output = NULL;           /* discard data */
216 >                        rofile[nfiles].termc = curterm;
217 >                        rofile[nfiles].format = curfmt;
218 >                        rofile[nfiles].ncomp = curncomp;
219 >                        rofile[nfiles].bytsiz = curbytes;
220 >                        rofile[nfiles++].outspec = argv[i];
221                  } else if (argv[i][0] == '!') {
222                          needres |= (curflags & DORESOLU);
223 <                        hdrflags[nfiles] = curflags;
224 <                        termc[nfiles] = curterm;
225 <                        if ((output[nfiles] = popen(argv[i]+1, "w")) == NULL) {
223 >                        rofile[nfiles].hdrflags = curflags;
224 >                        rofile[nfiles].termc = curterm;
225 >                        if ((rofile[nfiles].output = popen(argv[i]+1, "w")) == NULL) {
226                                  fputs(argv[i], stderr);
227                                  fputs(": cannot start command\n", stderr);
228                                  return(1);
229                          }
230                          if (curbytes > 0)
231 <                                SET_FILE_BINARY(output[nfiles]);
232 <                        format[nfiles] = curfmt;
233 <                        ncomp[nfiles] = curncomp;
234 <                        bytsiz[nfiles] = curbytes;
235 <                        outndx[nfiles++] = i;
231 >                                SET_FILE_BINARY(rofile[nfiles].output);
232 >                        rofile[nfiles].format = curfmt;
233 >                        rofile[nfiles].ncomp = curncomp;
234 >                        rofile[nfiles].bytsiz = curbytes;
235 >                        rofile[nfiles++].outspec = argv[i];
236                  } else {
237                          int     j = nfiles;
238                          while (j--)                     /* check duplicates */
239 <                                if (!strcmp(argv[i], argv[outndx[j]])) {
239 >                                if (!strcmp(argv[i], rofile[j].outspec)) {
240                                          fputs(argv[0], stderr);
241                                          fputs(": duplicate output: ", stderr);
242                                          fputs(argv[i], stderr);
# Line 244 | Line 250 | main(int argc, char *argv[])
250                                  return(1);
251                          }
252                          needres |= (curflags & DORESOLU);
253 <                        hdrflags[nfiles] = curflags;
254 <                        termc[nfiles] = curterm;
253 >                        rofile[nfiles].hdrflags = curflags;
254 >                        rofile[nfiles].termc = curterm;
255                          if (!append & !force && access(argv[i], F_OK) == 0) {
256                                  fputs(argv[i], stderr);
257                                  fputs(": file exists -- use -f to overwrite\n",
258                                                  stderr);
259                                  return(1);
260                          }
261 <                        output[nfiles] = fopen(argv[i], append ? "a" : "w");
262 <                        if (output[nfiles] == NULL) {
261 >                        rofile[nfiles].output = fopen(argv[i], append ? "a" : "w");
262 >                        if (!rofile[nfiles].output) {
263                                  fputs(argv[i], stderr);
264                                  fputs(": cannot open for output\n", stderr);
265                                  return(1);
266                          }
267                          if (curbytes > 0)
268 <                                SET_FILE_BINARY(output[nfiles]);
269 <                        format[nfiles] = curfmt;
270 <                        ncomp[nfiles] = curncomp;
271 <                        bytsiz[nfiles] = curbytes;
272 <                        outndx[nfiles++] = i;
268 >                                SET_FILE_BINARY(rofile[nfiles].output);
269 >                        rofile[nfiles].format = curfmt;
270 >                        rofile[nfiles].ncomp = curncomp;
271 >                        rofile[nfiles].bytsiz = curbytes;
272 >                        rofile[nfiles++].outspec = argv[i];
273                  }
268                if (nfiles >= MAXFILE) {
269                        fputs(argv[0], stderr);
270                        fputs(": too many output streams\n", stderr);
271                        return(1);
272                }
274          }
275          if (!nfiles) {
276                  fputs(argv[0], stderr);
277                  fputs(": no output streams\n", stderr);
278                  return(1);
279 +        }                                       /* reduce array to size we need */
280 +        rofile = (struct outstream *)realloc(rofile, nfiles*sizeof(struct outstream));
281 +        if (!rofile) {
282 +                fputs(argv[0], stderr);
283 +                fputs(": realloc() failed!\n", stderr);
284 +                return(1);
285          }
286          if (bininp)                             /* binary input? */
287                  SET_FILE_BINARY(stdin);
288   #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
289          flockfile(stdin);
290          for (i = nfiles; i--; )
291 <                if (output[i] != NULL)
292 <                        ftrylockfile(output[i]);
291 >                if (rofile[i].output != NULL)
292 >                        ftrylockfile(rofile[i].output);
293   #endif
294                                                  /* load/copy header */
295          if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) {
# Line 312 | Line 319 | main(int argc, char *argv[])
319                  }
320          }
321          for (i = 0; i < nfiles; i++) {          /* complete headers */
322 <                if (hdrflags[i] & DOHEADER) {
322 >                if (rofile[i].hdrflags & DOHEADER) {
323                          if (!(inpflags & DOHEADER))
324 <                                newheader("RADIANCE", output[i]);
325 <                        printargs(argc, argv, output[i]);
326 <                        fprintf(output[i], "NCOMP=%d\n", output[i]==stdout ?
327 <                                                nstdoutcomp : ncomp[i]);
328 <                        if (format[i] != NULL) {
324 >                                newheader("RADIANCE", rofile[i].output);
325 >                        printargs(argc, argv, rofile[i].output);
326 >                        fprintf(rofile[i].output, "NCOMP=%d\n", rofile[i].output==stdout ?
327 >                                                nstdoutcomp : rofile[i].ncomp);
328 >                        if (rofile[i].format != NULL) {
329                                  extern const char  BIGEND[];
330 <                                if (bytsiz[i] > 1) {
331 <                                        fputs(BIGEND, output[i]);
330 >                                if (rofile[i].bytsiz > 1) {
331 >                                        fputs(BIGEND, rofile[i].output);
332                                          fputs(nativebigendian() ^ swapped ?
333 <                                                "1\n" : "0\n", output[i]);
333 >                                                "1\n" : "0\n", rofile[i].output);
334                                  }
335 <                                fputformat(format[i], output[i]);
335 >                                fputformat(rofile[i].format, rofile[i].output);
336                          }
337 <                        fputc('\n', output[i]);
337 >                        fputc('\n', rofile[i].output);
338                  }
339 <                if (hdrflags[i] & DORESOLU)
340 <                        fputsresolu(&ourres, output[i]);
339 >                if (rofile[i].hdrflags & DORESOLU)
340 >                        fputsresolu(&ourres, rofile[i].output);
341          }
342          do {                                    /* main loop */
343                  for (i = 0; i < nfiles; i++) {
344 <                        if (bytsiz[i] > 0) {            /* binary output */
345 <                                if (getbinary(buf, bytsiz[i], ncomp[i],
346 <                                                        stdin) < ncomp[i])
344 >                        if (rofile[i].bytsiz > 0) {             /* binary output */
345 >                                if (getbinary(buf, rofile[i].bytsiz, rofile[i].ncomp,
346 >                                                        stdin) != rofile[i].ncomp)
347                                          break;
348 <                                if (output[i] != NULL &&
349 <                                            putbinary(buf, bytsiz[i], ncomp[i],
350 <                                                        output[i]) < ncomp[i])
348 >                                if (rofile[i].output != NULL &&
349 >                                            putbinary(buf, rofile[i].bytsiz, rofile[i].ncomp,
350 >                                                        rofile[i].output) != rofile[i].ncomp)
351                                          break;
352 <                        } else if (ncomp[i] > 1) {      /* N-field output */
353 <                                int     n = ncomp[i];
352 >                        } else if (rofile[i].ncomp > 1) {       /* N-field output */
353 >                                int     n = rofile[i].ncomp;
354                                  while (n--) {
355 <                                        if (!scanOK(termc[i]))
355 >                                        if (!scanOK(rofile[i].termc))
356                                                  break;
357 <                                        if (output[i] != NULL &&
358 <                                                    fputs(buf, output[i]) == EOF)
357 >                                        if (rofile[i].output != NULL &&
358 >                                                    fputs(buf, rofile[i].output) == EOF)
359                                                  break;
360                                  }
361                                  if (n >= 0)             /* fell short? */
362                                          break;
363 <                                if ((output[i] != NULL) &  /* add EOL if none */
364 <                                                (termc[i] != '\n'))
365 <                                        fputc('\n', output[i]);
363 >                                if ((rofile[i].output != NULL) &  /* add EOL if none */
364 >                                                (rofile[i].termc != '\n'))
365 >                                        fputc('\n', rofile[i].output);
366                          } else {                        /* 1-field output */
367 <                                if (!scanOK(termc[i]))
367 >                                if (!scanOK(rofile[i].termc))
368                                          break;
369 <                                if (output[i] != NULL) {
370 <                                        if (fputs(buf, output[i]) == EOF)
369 >                                if (rofile[i].output != NULL) {
370 >                                        if (fputs(buf, rofile[i].output) == EOF)
371                                                  break;
372 <                                        if (termc[i] != '\n')   /* add EOL? */
373 <                                                fputc('\n', output[i]);
372 >                                        if (rofile[i].termc != '\n')    /* add EOL? */
373 >                                                fputc('\n', rofile[i].output);
374                                  }
375                          }
376                                                          /* skip input EOL? */
377 <                        if (!bininp && termc[nfiles-1] != '\n') {
377 >                        if (!bininp && rofile[nfiles-1].termc != '\n') {
378                                  int     c = getchar();
379                                  if ((c != '\n') & (c != EOF))
380                                          ungetc(c, stdin);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines