--- ray/src/util/radcompare.c 2018/10/16 16:23:17 2.8 +++ ray/src/util/radcompare.c 2018/10/19 23:50:32 2.13 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: radcompare.c,v 2.8 2018/10/16 16:23:17 greg Exp $"; +static const char RCSid[] = "$Id: radcompare.c,v 2.13 2018/10/19 23:50:32 greg Exp $"; #endif /* * Compare Radiance files for significant differences @@ -74,6 +74,16 @@ LUTAB hdr2 = LU_SINIT(free,free); #define adv_linecnt(htp) (lin1cnt += (htp == &hdr1), \ lin2cnt += (htp == &hdr2)) +typedef struct { /* dynamic line buffer */ + char *ptr; + int len; + int siz; +} LINEBUF; + +#define init_line(bp) ((bp)->ptr = NULL, (bp)->siz = 0) + /* 100 MByte limit on line buffer */ +#define MAXBUF (100L<<20) + /* input files */ char *progname = NULL; const char stdin_name[] = ""; @@ -92,9 +102,49 @@ usage() fputs(progname, stderr); fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n", stderr); - exit(1); + exit(2); } +/* Read a text line, increasing buffer size as necessary */ +static int +read_line(LINEBUF *bp, FILE *fp) +{ + bp->len = 0; + if (!bp->ptr) { + bp->ptr = (char *)malloc(bp->siz = 512); + if (!bp->ptr) + goto memerr; + } + while (fgets(bp->ptr + bp->len, bp->siz - bp->len, fp)) { + bp->len += strlen(bp->ptr + bp->len); + if (bp->ptr[bp->len-1] == '\n') + break; /* found EOL */ + if (bp->len < bp->siz - 4) + continue; /* at EOF? */ + if (bp->siz >= MAXBUF) + break; /* don't go to extremes */ + if ((bp->siz += bp->siz/2) > MAXBUF) + bp->siz = MAXBUF; + bp->ptr = (char *)realloc(bp->ptr, bp->siz); + if (!bp->ptr) + goto memerr; + } + return(bp->len); +memerr: + fprintf(stderr, + "%s: out of memory in read_line() allocating %d byte buffer\n", + progname, bp->siz); + exit(2); +} + +/* Free line buffer */ +static void +free_line(LINEBUF *bp) +{ + if (bp->ptr) free(bp->ptr); + init_line(bp); +} + /* Get type ID from name (or 0 if not found) */ static int xlate_type(const char *nm) @@ -291,13 +341,13 @@ match_val(const LUENT *ep1, void *p2) /* Compare two sets of header variables */ static int -headers_match(LUTAB *hp1, LUTAB *hp2) +headers_match() { - int ne = lu_doall(hp1, match_val, hp2); + int ne = lu_doall(&hdr1, match_val, &hdr2); if (ne < 0) return(0); /* something didn't match! */ /* non-fatal if second header has extra */ - if (report >= REP_WARN && (ne = lu_doall(hp2, NULL, NULL) - ne)) + if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne)) printf("%s: warning - '%s' has %d extra header setting(s)\n", progname, f2name, ne); return(1); /* good match */ @@ -445,56 +495,63 @@ compare_binary() static int compare_text() { - char l1buf[4096], l2buf[4096]; + LINEBUF l1buf, l2buf; if (report >= REP_VERBOSE) { fputs(progname, stdout); fputs(": comparing inputs as ASCII text\n", stdout); } - /* compare a line at a time */ - while (fgets(l1buf, sizeof(l1buf), f1in)) { + init_line(&l1buf); init_line(&l2buf); /* compare a line at a time */ + while (read_line(&l1buf, f1in)) { lin1cnt++; - if (!*sskip2(l1buf,0)) + if (!*sskip2(l1buf.ptr,0)) continue; /* ignore empty lines */ - while (fgets(l2buf, sizeof(l2buf), f2in)) { + + while (read_line(&l2buf, f2in)) { lin2cnt++; - if (*sskip2(l2buf,0)) + if (*sskip2(l2buf.ptr,0)) break; /* found other non-empty line */ } - if (feof(f2in)) { + if (!l2buf.len) { /* input 2 EOF? */ if (report != REP_QUIET) { fputs(f2name, stdout); fputs(": unexpected end-of-file\n", stdout); } + free_line(&l1buf); free_line(&l2buf); return(0); } /* compare non-empty lines */ - if (!equiv_string(l1buf, l2buf)) { + if (!equiv_string(l1buf.ptr, l2buf.ptr)) { if (report != REP_QUIET) { printf("%s: inputs '%s' and '%s' differ at line %d|%d\n", progname, f1name, f2name, lin1cnt, lin2cnt); - if (report >= REP_VERBOSE) { + if ( report >= REP_VERBOSE && + (l1buf.len < 256) & + (l2buf.len < 256) ) { fputs("------------- Mismatch -------------\n", stdout); printf("%s@%d:\t%s", f1name, - lin1cnt, l1buf); + lin1cnt, l1buf.ptr); printf("%s@%d:\t%s", f2name, - lin2cnt, l2buf); + lin2cnt, l2buf.ptr); } } + free_line(&l1buf); free_line(&l2buf); return(0); } } - /* check for EOF on input 2 */ - while (fgets(l2buf, sizeof(l2buf), f2in)) { - if (!*sskip2(l2buf,0)) + free_line(&l1buf); /* check for EOF on input 2 */ + while (read_line(&l2buf, f2in)) { + if (!*sskip2(l2buf.ptr,0)) continue; if (report != REP_QUIET) { fputs(f1name, stdout); fputs(": unexpected end-of-file\n", stdout); } + free_line(&l2buf); return(0); } + free_line(&l2buf); return(good_RMS()); /* final check for reals */ } @@ -687,20 +744,20 @@ main(int argc, char *argv[]) SET_FILE_BINARY(stdin); /* in case we're using it */ if (!f1in && !(f1in = fopen(f1name, "rb"))) { fprintf(stderr, "%s: cannot open for reading\n", f1name); - return(1); + return(2); } if (!strcmp(f2name, "-")) { f2in = stdin; f2name = stdin_name; } else if (!(f2in = fopen(f2name, "rb"))) { fprintf(stderr, "%s: cannot open for reading\n", f2name); - return(1); + return(2); } /* load headers */ if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0) - return(1); + return(2); if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0) - return(1); + return(2); if (typ1 != typ2) { if (report != REP_QUIET) printf("%s: '%s' is %s and '%s' is %s\n", @@ -709,7 +766,7 @@ main(int argc, char *argv[]) return(1); } ign_header |= !has_header(typ1); /* check headers if indicated */ - if (!ign_header && !headers_match(&hdr1, &hdr2)) + if (!ign_header && !headers_match()) return(1); lu_done(&hdr1); lu_done(&hdr2); if (!ign_header & (report >= REP_WARN)) {