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.3 by greg, Fri Jul 5 02:26:16 2019 UTC vs.
Revision 1.9 by greg, Tue Aug 13 16:31:35 2019 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7   *      7/4/19          Greg Ward
8   */
9  
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
10   #include <ctype.h>
11  
12 + #include "rtio.h"
13   #include "platform.h"
14 + #include "paths.h"
15   #include "resolu.h"
17 #include "rtio.h"
16  
17   #define DOHEADER        1
18   #define DORESOLU        2
19  
20   #define MAXFILE         512             /* maximum number of files */
21  
22 < FILE            *output[MAXFILE];
23 < int             bytsiz[MAXFILE];
24 < short           hdrflags[MAXFILE];
25 < const char      *format[MAXFILE];
26 < int             termc[MAXFILE];
27 < int             nfiles = 0;
22 > static FILE             *output[MAXFILE];
23 > static int              bytsiz[MAXFILE];
24 > static short            hdrflags[MAXFILE];
25 > static const char       *format[MAXFILE];
26 > static int              termc[MAXFILE];
27 > static int              nfiles = 0;
28  
29 < int             outheader = 0;          /* output header to each stream? */
29 > static RESOLU   ourres = {PIXSTANDARD, 0, 0};
30  
31 < RESOLU          ourres = {PIXSTANDARD, 0, 0};
31 > static char     buf[16384];             /* input buffer used in scanOK() */
32  
35 char            buf[16384];
33  
37
34   /* process header line */
35   static int
36   headline(char *s, void *p)
# Line 80 | Line 76 | scanOK(int termc)
76   int
77   main(int argc, char *argv[])
78   {
79 <        int             inpheader = 0;
80 <        int             inpres = 0;
85 <        int             outres = 0;
79 >        int             inpflags = 0;
80 >        int             needres = 0;
81          int             force = 0;
82          int             append = 0;
83          long            outcnt = 0;
# Line 98 | Line 93 | main(int argc, char *argv[])
93                          switch (argv[i][1]) {
94                          case 't':
95                                  curterm = argv[i][2];
96 +                                if (!curterm) curterm = '\n';
97                                  break;
98                          case 'i':
99                                  switch (argv[i][2]) {
100                                  case 'h':
101 <                                        inpheader = !inpheader;
101 >                                        inpflags ^= DOHEADER;
102                                          break;
103                                  case 'H':
104 <                                        inpres = !inpres;
104 >                                        inpflags ^= DORESOLU;
105                                          break;
106                                  default:
107                                          goto badopt;
108                                  }
109                                  break;
110                          case 'f':
111 <                                ++force;
116 <                                append = 0;
111 >                                force = !force;
112                                  break;
113                          case 'a':
114 <                                if (outheader | outres) {
120 <                                        fputs(argv[0], stderr);
121 <                                        fputs(": -a option incompatible with -oh and -oH\n",
122 <                                                        stderr);
123 <                                        return(1);
124 <                                }
125 <                                append = 1;
114 >                                append = !append;
115                                  break;
116                          case 'x':
117                                  ourres.xr = atoi(argv[++i]);
129                                outres = 1;
118                                  break;
119                          case 'y':
120                                  ourres.yr = atoi(argv[++i]);
133                                outres = 1;
121                                  break;
122                          case 'o':
123                                  switch (argv[i][2]) {
# Line 165 | Line 152 | main(int argc, char *argv[])
152                                          break;
153                                  case 'a':
154                                          curfmt = "ascii";
155 <                                        curbytes = argv[i][3] ? -1 : 0;
155 >                                        curbytes = -1;
156                                          break;
157                                  default:
158                                          goto badopt;
# Line 178 | Line 165 | main(int argc, char *argv[])
165                                          fputs(": output size too big\n", stderr);
166                                          return(1);
167                                  }
168 <                                if (curbytes > 0) {
182 <                                        curterm = '\0';
183 <                                        ++bininp;
184 <                                }
168 >                                bininp += (curbytes > 0);
169                                  break;
170                          case '\0':
171 <                                outres |= (curflags & DORESOLU);
171 >                                needres |= (curflags & DORESOLU);
172                                  termc[nfiles] = curterm;
173                                  hdrflags[nfiles] = curflags;
174                                  output[nfiles] = stdout;
# Line 200 | Line 184 | main(int argc, char *argv[])
184                                  return(1);
185                          }
186                  } else if (argv[i][0] == '!') {
187 <                        outres |= (curflags & DORESOLU);
187 >                        needres |= (curflags & DORESOLU);
188                          termc[nfiles] = curterm;
189                          hdrflags[nfiles] = curflags;
190                          if ((output[nfiles] = popen(argv[i]+1, "w")) == NULL) {
# Line 213 | Line 197 | main(int argc, char *argv[])
197                          format[nfiles] = curfmt;
198                          bytsiz[nfiles++] = curbytes;
199                  } else {
200 <                        outres |= (curflags & DORESOLU);
200 >                        if (append & (curflags != 0)) {
201 >                                fputs(argv[0], stderr);
202 >                                fputs(": -a option incompatible with -oh and -oH\n",
203 >                                                stderr);
204 >                                return(1);
205 >                        }
206 >                        needres |= (curflags & DORESOLU);
207                          termc[nfiles] = curterm;
208                          hdrflags[nfiles] = curflags;
209                          if (!append & !force && access(argv[i], F_OK) == 0) {
# Line 252 | Line 242 | main(int argc, char *argv[])
242                  flockfile(output[i]);
243   #endif
244                                                  /* load/copy header */
245 <        if (inpheader && getheader(stdin, headline, NULL) < 0) {
245 >        if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) {
246                  fputs(argv[0], stderr);
247                  fputs(": cannot get header from standard input\n",
248                                  stderr);
249                  return(1);
250          }
251                                                  /* handle resolution string */
252 <        if (inpres && !fgetsresolu(&ourres, stdin)) {
252 >        if (inpflags & DORESOLU && !fgetsresolu(&ourres, stdin)) {
253                  fputs(argv[0], stderr);
254                  fputs(": cannot get resolution string from standard input\n",
255                                  stderr);
256                  return(1);
257          }
258 <        if (outres && (ourres.xr <= 0) | (ourres.yr <= 0)) {
258 >        if (needres && (ourres.xr <= 0) | (ourres.yr <= 0)) {
259                  fputs(argv[0], stderr);
260                  fputs(": -oH option requires -iH or -x and -y options\n", stderr);
261                  return(1);
# Line 281 | Line 271 | main(int argc, char *argv[])
271          }
272          for (i = 0; i < nfiles; i++) {          /* complete headers */
273                  if (hdrflags[i] & DOHEADER) {
274 <                        if (!inpheader)
274 >                        if (!(inpflags & DOHEADER))
275                                  newheader("RADIANCE", output[i]);
276                          printargs(argc, argv, output[i]);
277                          if (format[i] != NULL)
# Line 296 | Line 286 | main(int argc, char *argv[])
286                          if (bytsiz[i] > 0) {            /* binary output */
287                                  if (getbinary(buf, bytsiz[i], 1, stdin) < 1)
288                                          break;
289 <                                putbinary(buf, bytsiz[i], 1, output[i]);
289 >                                if (putbinary(buf, bytsiz[i], 1, output[i]) < 1)
290 >                                        break;
291                          } else if (bytsiz[i] < 0) {     /* N-field output */
292                                  int     n = -bytsiz[i];
293                                  while (n--) {
294                                          if (!scanOK(termc[i]))
295                                                  break;
296 <                                        fputs(buf, output[i]);
296 >                                        if (fputs(buf, output[i]) == EOF)
297 >                                                break;
298                                  }
299                                  if (n >= 0)             /* fell short? */
300                                          break;
301 +                                if (termc[i] != '\n')   /* add EOL if none */
302 +                                        fputc('\n', output[i]);
303                          } else {                        /* 1-field output */
304                                  if (!scanOK(termc[i]))
305                                          break;
306 <                                fputs(buf, output[i]);
306 >                                if (fputs(buf, output[i]) == EOF)
307 >                                        break;
308 >                                if (termc[i] != '\n')   /* add EOL if none */
309 >                                        fputc('\n', output[i]);
310                          }
311 +                                                        /* skip input EOL? */
312 +                        if (!bininp && termc[nfiles-1] != '\n') {
313 +                                int     c = getchar();
314 +                                if ((c != '\n') & (c != EOF))
315 +                                        ungetc(c, stdin);
316 +                        }
317                  }
318                  if (i < nfiles)
319                          break;
320          } while (--outcnt);
321                                                          /* check ending */
319        if (outcnt > 0) {
320                fputs(argv[0], stderr);
321                fputs(": warning: premature EOD\n", stderr);
322        }
322          if (fflush(NULL) == EOF) {
323                  fputs(argv[0], stderr);
324                  fputs(": write error on one or more outputs\n", stderr);
325                  return(1);
326 +        }
327 +        if (outcnt > 0) {
328 +                fputs(argv[0], stderr);
329 +                fputs(": warning: premature EOD\n", stderr);
330          }
331          return(0);
332   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines