--- ray/src/common/idmap.c 2019/07/26 16:18:06 2.1 +++ ray/src/common/idmap.c 2022/08/24 19:55:58 2.3 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: idmap.c,v 2.1 2019/07/26 16:18:06 greg Exp $"; +static const char RCSid[] = "$Id: idmap.c,v 2.3 2022/08/24 19:55:58 greg Exp $"; #endif /* * Routines for loading identifier maps @@ -30,8 +30,10 @@ idmap_ropen(const char *fname, int hflags) idinit.finp = fopen(fname, "rb"); if (!idinit.finp) { - fputs(fname, stderr); - fputs(": cannot open for reading\n", stderr); + if (hflags & HF_STDERR) { + fputs(fname, stderr); + fputs(": cannot open for reading\n", stderr); + } return NULL; } SET_FILE_BINARY(idinit.finp); @@ -42,8 +44,10 @@ idmap_ropen(const char *fname, int hflags) gotfmt = checkheader(idinit.finp, infmt, (hflags & HF_HEADOUT) ? stdout : (FILE *)NULL); if (gotfmt <= 0) { - fputs(fname, stderr); - fputs(": bad or missing header format\n", stderr); + if (hflags & HF_STDERR) { + fputs(fname, stderr); + fputs(": bad or missing header format\n", stderr); + } fclose(idinit.finp); return NULL; } @@ -62,17 +66,21 @@ idmap_ropen(const char *fname, int hflags) idinit.bytespi = 3; break; default: - fputs(fname, stderr); - fputs(": unsupported index size: ", stderr); - fputs(infmt, stderr); - fputc('\n', stderr); + if (hflags & HF_STDERR) { + fputs(fname, stderr); + fputs(": unsupported index size: ", stderr); + fputs(infmt, stderr); + fputc('\n', stderr); + } fclose(idinit.finp); return NULL; } /* get/copy resolution string */ if (!fgetsresolu(&idinit.res, idinit.finp)) { - fputs(fname, stderr); - fputs(": missing map resolution\n", stderr); + if (hflags & HF_STDERR) { + fputs(fname, stderr); + fputs(": missing map resolution\n", stderr); + } fclose(idinit.finp); return NULL; } @@ -85,15 +93,18 @@ idmap_ropen(const char *fname, int hflags) tablength = ftell(idinit.finp) - idinit.dstart - (long)idinit.res.xr*idinit.res.yr*idinit.bytespi; if (tablength < 2) { - fputs(fname, stderr); - fputs(": truncated file\n", stderr); + if (hflags & HF_STDERR) { + fputs(fname, stderr); + fputs(": truncated file\n", stderr); + } fclose(idinit.finp); return NULL; } /* allocate and load table */ idmp = (IDMAP *)malloc(sizeof(IDMAP)+tablength); if (idmp == NULL) { - fputs("Out of memory allocating ID table!\n", stderr); + if (hflags & HF_STDERR) + fputs("Out of memory allocating ID table!\n", stderr); fclose(idinit.finp); return NULL; } @@ -102,19 +113,22 @@ idmap_ropen(const char *fname, int hflags) if (fseek(idmp->finp, -tablength, SEEK_END) < 0) goto seekerr; if (fread(idmp->idtable, 1, tablength, idmp->finp) != tablength) { - fputs(fname, stderr); - fputs(": error reading identifier table\n", stderr); + if (hflags & HF_STDERR) { + fputs(fname, stderr); + fputs(": error reading identifier table\n", stderr); + } fclose(idmp->finp); free(idmp); return NULL; } - if (fseek(idmp->finp, idmp->curpos = idmp->dstart, SEEK_SET) < 0) + if (fseek(idmp->finp, idmp->dstart, SEEK_SET) < 0) goto seekerr; while (tablength > 0) /* create index */ idmp->nids += !idmp->idtable[--tablength]; idmp->idoffset = (int *)malloc(sizeof(int)*idmp->nids); if (!idmp->idoffset) { - fputs("Out of memory in idmap_ropen!\n", stderr); + if (hflags & HF_STDERR) + fputs("Out of memory in idmap_ropen!\n", stderr); fclose(idmp->finp); free(idmp); return NULL; @@ -126,8 +140,10 @@ idmap_ropen(const char *fname, int hflags) } return idmp; seekerr: - fputs(fname, stderr); - fputs(": cannot seek on file\n", stderr); + if (hflags & HF_STDERR) { + fputs(fname, stderr); + fputs(": cannot seek on file\n", stderr); + } fclose(idinit.finp); if (idmp) free(idmp); return NULL; @@ -143,8 +159,6 @@ idmap_next_i(IDMAP *idmp) if (ndx == EOF && feof(idmp->finp)) return -1; - idmp->curpos += idmp->bytespi; - ndx &= (1<<(idmp->bytespi<<3)) - 1; /* undo sign extension */ return ndx; @@ -168,18 +182,13 @@ idmap_next(IDMAP *idmp) int idmap_seek(IDMAP *idmp, int x, int y) { - long seekto; - if ((x < 0) | (y < 0) | (x >= idmp->res.xr) | (y >= idmp->res.yr)) return 0; - seekto = idmp->dstart + ((long)y*idmp->res.xr + x)*idmp->bytespi; - - if (seekto != idmp->curpos && fseek(idmp->finp, seekto, SEEK_SET) < 0) + if (fseek(idmp->finp, idmp->dstart + ((long)y*idmp->res.xr + x)*idmp->bytespi, + SEEK_SET) < 0) return -1; - idmp->curpos = seekto; - return 1; }