--- ray/src/cal/rsplit.c 2019/08/13 16:31:35 1.9 +++ ray/src/cal/rsplit.c 2020/03/31 16:39:01 1.12 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rsplit.c,v 1.9 2019/08/13 16:31:35 greg Exp $"; +static const char RCSid[] = "$Id: rsplit.c,v 1.12 2020/03/31 16:39:01 greg Exp $"; #endif /* * rsplit.c - split input into multiple output streams @@ -19,9 +19,12 @@ static const char RCSid[] = "$Id: rsplit.c,v 1.9 2019/ #define MAXFILE 512 /* maximum number of files */ +static int swapped = 0; /* input is byte-swapped */ + static FILE *output[MAXFILE]; +static int ncomp[MAXFILE]; static int bytsiz[MAXFILE]; -static short hdrflags[MAXFILE]; +static int hdrflags[MAXFILE]; static const char *format[MAXFILE]; static int termc[MAXFILE]; static int nfiles = 0; @@ -36,7 +39,7 @@ static int headline(char *s, void *p) { extern const char FMTSTR[]; - int i = nfiles; + int i; if (strstr(s, FMTSTR) == s) return(0); /* don't copy format */ @@ -46,6 +49,11 @@ headline(char *s, void *p) return(0); if (!strncmp(s, "NCOMP=", 6)) return(0); + if ((i = isbigendian(s)) >= 0) { + swapped = (nativebigendian() != i); + return(0); + } + i = nfiles; while (i--) /* else copy line to output streams */ if (hdrflags[i] & DOHEADER) fputs(s, output[i]); @@ -57,14 +65,20 @@ headline(char *s, void *p) static int scanOK(int termc) { + int skip_white = (termc == ' '); char *cp = buf; int c; while ((c = getchar()) != EOF) { + if (skip_white && isspace(c)) + continue; + skip_white = 0; + if (c == '\n' && isspace(termc)) + c = termc; /* forgiving assumption */ *cp++ = c; if (cp-buf >= sizeof(buf)) break; - if (c == termc) { + if ((termc == ' ') ? isspace(c) : (c == termc)) { *cp = '\0'; return(cp-buf); } @@ -83,6 +97,7 @@ main(int argc, char *argv[]) long outcnt = 0; int bininp = 0; int curterm = '\n'; + int curncomp = 1; int curbytes = 0; int curflags = 0; const char *curfmt = "ascii"; @@ -152,15 +167,14 @@ main(int argc, char *argv[]) break; case 'a': curfmt = "ascii"; - curbytes = -1; + curbytes = 0; break; default: goto badopt; } - if (isdigit(argv[i][3])) - curbytes *= atoi(argv[i]+3); - curbytes += (curbytes == -1); - if (curbytes > (int)sizeof(buf)) { + curncomp = isdigit(argv[i][3]) ? + atoi(argv[i]+3) : 1 ; + if (curbytes*curncomp > (int)sizeof(buf)) { fputs(argv[0], stderr); fputs(": output size too big\n", stderr); return(1); @@ -175,6 +189,7 @@ main(int argc, char *argv[]) if (curbytes > 0) SET_FILE_BINARY(output[nfiles]); format[nfiles] = curfmt; + ncomp[nfiles] = curncomp; bytsiz[nfiles++] = curbytes; break; badopt:; @@ -195,6 +210,7 @@ main(int argc, char *argv[]) if (curbytes > 0) SET_FILE_BINARY(output[nfiles]); format[nfiles] = curfmt; + ncomp[nfiles] = curncomp; bytsiz[nfiles++] = curbytes; } else { if (append & (curflags != 0)) { @@ -221,6 +237,7 @@ main(int argc, char *argv[]) if (curbytes > 0) SET_FILE_BINARY(output[nfiles]); format[nfiles] = curfmt; + ncomp[nfiles] = curncomp; bytsiz[nfiles++] = curbytes; } if (nfiles >= MAXFILE) { @@ -274,8 +291,17 @@ main(int argc, char *argv[]) if (!(inpflags & DOHEADER)) newheader("RADIANCE", output[i]); printargs(argc, argv, output[i]); - if (format[i] != NULL) + fprintf(output[i], "NCOMP=%d\n", ncomp[i]); + if (format[i] != NULL) { + extern const char BIGEND[]; + if ((format[i][0] == 'f') | + (format[i][0] == 'd')) { + fputs(BIGEND, output[i]); + fputs(nativebigendian() ^ swapped ? + "1\n" : "0\n", output[i]); + } fputformat(format[i], output[i]); + } fputc('\n', output[i]); } if (hdrflags[i] & DORESOLU) @@ -284,12 +310,14 @@ main(int argc, char *argv[]) do { /* main loop */ for (i = 0; i < nfiles; i++) { if (bytsiz[i] > 0) { /* binary output */ - if (getbinary(buf, bytsiz[i], 1, stdin) < 1) + if (getbinary(buf, bytsiz[i], ncomp[i], + stdin) < ncomp[i]) break; - if (putbinary(buf, bytsiz[i], 1, output[i]) < 1) + if (putbinary(buf, bytsiz[i], ncomp[i], + output[i]) < ncomp[i]) break; - } else if (bytsiz[i] < 0) { /* N-field output */ - int n = -bytsiz[i]; + } else if (ncomp[i] > 1) { /* N-field output */ + int n = ncomp[i]; while (n--) { if (!scanOK(termc[i])) break;