| 1 |
greg |
2.1 |
#ifndef lint |
| 2 |
greg |
2.43 |
static const char RCSid[] = "$Id: rcollate.c,v 2.42 2022/03/16 19:25:25 greg Exp $"; |
| 3 |
greg |
2.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Utility to re-order records in a binary or ASCII data file (matrix) |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
#include <stdlib.h> |
| 9 |
|
|
#include <ctype.h> |
| 10 |
|
|
#include "platform.h" |
| 11 |
|
|
#include "rtio.h" |
| 12 |
|
|
#include "resolu.h" |
| 13 |
schorsch |
2.23 |
#if defined(_WIN32) || defined(_WIN64) |
| 14 |
|
|
#undef ftello |
| 15 |
|
|
#define ftello ftell |
| 16 |
|
|
#undef ssize_t |
| 17 |
|
|
#define ssize_t size_t |
| 18 |
greg |
2.7 |
#else |
| 19 |
schorsch |
2.23 |
#include <sys/mman.h> |
| 20 |
greg |
2.1 |
#endif |
| 21 |
|
|
|
| 22 |
greg |
2.31 |
#define MAXLEVELS 16 /* max RxC.. block pairs */ |
| 23 |
|
|
|
| 24 |
greg |
2.1 |
typedef struct { |
| 25 |
greg |
2.29 |
void *mapped; /* memory-mapped pointer */ |
| 26 |
greg |
2.1 |
void *base; /* pointer to base memory */ |
| 27 |
|
|
size_t len; /* allocated memory length */ |
| 28 |
|
|
} MEMLOAD; /* file loaded/mapped into memory */ |
| 29 |
|
|
|
| 30 |
|
|
typedef struct { |
| 31 |
|
|
int nw_rec; /* number of words per record */ |
| 32 |
greg |
2.42 |
size_t nrecs; /* number of records we found */ |
| 33 |
greg |
2.1 |
char *rec[1]; /* record array (extends struct) */ |
| 34 |
|
|
} RECINDEX; |
| 35 |
|
|
|
| 36 |
greg |
2.40 |
int warnings = 1; /* report warnings? */ |
| 37 |
|
|
|
| 38 |
|
|
char *fmtid = NULL; /* format id */ |
| 39 |
|
|
int comp_size = 0; /* binary bytes/channel */ |
| 40 |
|
|
int n_comp = 0; /* components/record */ |
| 41 |
|
|
int ni_columns = 0; /* number of input columns */ |
| 42 |
|
|
int ni_rows = 0; /* number of input rows */ |
| 43 |
|
|
int no_columns = 0; /* number of output columns */ |
| 44 |
|
|
int no_rows = 0; /* number of output rows */ |
| 45 |
|
|
int transpose = 0; /* transpose rows & cols? */ |
| 46 |
|
|
int i_header = 1; /* input header? */ |
| 47 |
|
|
int o_header = 1; /* output header? */ |
| 48 |
|
|
int outArray[MAXLEVELS][2]; /* output block nesting */ |
| 49 |
|
|
int outLevels = 0; /* number of blocking levels */ |
| 50 |
|
|
int check = 0; /* force data check? */ |
| 51 |
greg |
2.4 |
|
| 52 |
greg |
2.1 |
/* free loaded file */ |
| 53 |
|
|
static void |
| 54 |
|
|
free_load(MEMLOAD *mp) |
| 55 |
|
|
{ |
| 56 |
|
|
if (mp == NULL || (mp->base == NULL) | (mp->len <= 0)) |
| 57 |
|
|
return; |
| 58 |
|
|
#ifdef MAP_FILE |
| 59 |
|
|
if (mp->mapped) |
| 60 |
greg |
2.29 |
munmap(mp->mapped, mp->len); |
| 61 |
greg |
2.1 |
else |
| 62 |
|
|
#endif |
| 63 |
|
|
free(mp->base); |
| 64 |
greg |
2.29 |
mp->mapped = NULL; |
| 65 |
greg |
2.1 |
mp->base = NULL; |
| 66 |
|
|
mp->len = 0; |
| 67 |
|
|
} |
| 68 |
|
|
|
| 69 |
greg |
2.20 |
/* load memory from an input stream, starting from current position */ |
| 70 |
|
|
static int |
| 71 |
|
|
load_stream(MEMLOAD *mp, FILE *fp) |
| 72 |
|
|
{ |
| 73 |
|
|
size_t alloced = 0; |
| 74 |
|
|
char buf[8192]; |
| 75 |
|
|
size_t nr; |
| 76 |
|
|
|
| 77 |
|
|
if (mp == NULL) |
| 78 |
|
|
return(-1); |
| 79 |
greg |
2.29 |
mp->mapped = NULL; |
| 80 |
greg |
2.20 |
mp->base = NULL; |
| 81 |
|
|
mp->len = 0; |
| 82 |
|
|
if (fp == NULL) |
| 83 |
|
|
return(-1); |
| 84 |
|
|
while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 85 |
|
|
if (!alloced) |
| 86 |
greg |
2.21 |
mp->base = malloc(alloced = nr); |
| 87 |
greg |
2.20 |
else if (mp->len+nr > alloced) |
| 88 |
|
|
mp->base = realloc(mp->base, |
| 89 |
|
|
alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); |
| 90 |
|
|
if (mp->base == NULL) |
| 91 |
|
|
return(-1); |
| 92 |
|
|
memcpy((char *)mp->base + mp->len, buf, nr); |
| 93 |
|
|
mp->len += nr; |
| 94 |
|
|
} |
| 95 |
|
|
if (ferror(fp)) { |
| 96 |
|
|
free_load(mp); |
| 97 |
|
|
return(-1); |
| 98 |
|
|
} |
| 99 |
|
|
if (alloced > mp->len*5/4) /* don't waste too much space */ |
| 100 |
|
|
mp->base = realloc(mp->base, mp->len); |
| 101 |
|
|
return(mp->len > 0); |
| 102 |
|
|
} |
| 103 |
|
|
|
| 104 |
greg |
2.38 |
#if defined(_WIN32) || defined(_WIN64) |
| 105 |
|
|
/* too difficult to fix this */ |
| 106 |
|
|
#define load_file load_stream |
| 107 |
|
|
#else |
| 108 |
greg |
2.1 |
/* load a file into memory */ |
| 109 |
|
|
static int |
| 110 |
|
|
load_file(MEMLOAD *mp, FILE *fp) |
| 111 |
|
|
{ |
| 112 |
|
|
int fd; |
| 113 |
greg |
2.28 |
off_t skip, flen, fpos; |
| 114 |
greg |
2.1 |
if (mp == NULL) |
| 115 |
|
|
return(-1); |
| 116 |
greg |
2.29 |
mp->mapped = NULL; |
| 117 |
greg |
2.1 |
mp->base = NULL; |
| 118 |
|
|
mp->len = 0; |
| 119 |
|
|
if (fp == NULL) |
| 120 |
|
|
return(-1); |
| 121 |
|
|
fd = fileno(fp); |
| 122 |
|
|
skip = ftello(fp); |
| 123 |
|
|
flen = lseek(fd, 0, SEEK_END); |
| 124 |
|
|
if (flen <= skip) |
| 125 |
|
|
return((int)(flen - skip)); |
| 126 |
|
|
mp->len = (size_t)(flen - skip); |
| 127 |
|
|
#ifdef MAP_FILE |
| 128 |
greg |
2.38 |
if (mp->len >= 1L<<20) { /* map file if >= 1 MByte */ |
| 129 |
greg |
2.29 |
mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); |
| 130 |
|
|
if (mp->mapped != MAP_FAILED) { |
| 131 |
|
|
mp->base = (char *)mp->mapped + skip; |
| 132 |
greg |
2.1 |
return(1); /* mmap() success */ |
| 133 |
|
|
} |
| 134 |
greg |
2.29 |
mp->mapped = NULL; /* else fall back to reading it in... */ |
| 135 |
greg |
2.1 |
} |
| 136 |
|
|
#endif |
| 137 |
|
|
if (lseek(fd, skip, SEEK_SET) != skip || |
| 138 |
|
|
(mp->base = malloc(mp->len)) == NULL) { |
| 139 |
|
|
mp->len = 0; |
| 140 |
|
|
return(-1); |
| 141 |
|
|
} |
| 142 |
greg |
2.28 |
fpos = skip; |
| 143 |
|
|
while (fpos < flen) { /* read() fails if n > 2 GBytes */ |
| 144 |
|
|
ssize_t nread = read(fd, (char *)mp->base+(fpos-skip), |
| 145 |
|
|
(flen-fpos < 1L<<24) ? flen-fpos : 1L<<24); |
| 146 |
greg |
2.27 |
if (nread <= 0) { |
| 147 |
|
|
free_load(mp); |
| 148 |
|
|
return(-1); |
| 149 |
|
|
} |
| 150 |
greg |
2.28 |
fpos += nread; |
| 151 |
greg |
2.1 |
} |
| 152 |
|
|
return(1); |
| 153 |
|
|
} |
| 154 |
greg |
2.38 |
#endif |
| 155 |
greg |
2.1 |
|
| 156 |
|
|
/* free a record index */ |
| 157 |
|
|
#define free_records(rp) free(rp) |
| 158 |
|
|
|
| 159 |
|
|
/* compute record index */ |
| 160 |
|
|
static RECINDEX * |
| 161 |
|
|
index_records(const MEMLOAD *mp, int nw_rec) |
| 162 |
|
|
{ |
| 163 |
greg |
2.24 |
int nall = 0; |
| 164 |
greg |
2.1 |
RECINDEX *rp; |
| 165 |
|
|
char *cp, *mend; |
| 166 |
|
|
int n; |
| 167 |
|
|
|
| 168 |
|
|
if (mp == NULL || (mp->base == NULL) | (mp->len <= 0)) |
| 169 |
|
|
return(NULL); |
| 170 |
|
|
if (nw_rec <= 0) |
| 171 |
|
|
return(NULL); |
| 172 |
greg |
2.24 |
nall = 1000; |
| 173 |
|
|
rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *)); |
| 174 |
greg |
2.1 |
if (rp == NULL) |
| 175 |
|
|
return(NULL); |
| 176 |
|
|
rp->nw_rec = nw_rec; |
| 177 |
|
|
rp->nrecs = 0; |
| 178 |
|
|
cp = (char *)mp->base; |
| 179 |
|
|
mend = cp + mp->len; |
| 180 |
|
|
for ( ; ; ) { /* whitespace-separated words */ |
| 181 |
|
|
while (cp < mend && !*cp | isspace(*cp)) |
| 182 |
|
|
++cp; |
| 183 |
|
|
if (cp >= mend) |
| 184 |
|
|
break; |
| 185 |
greg |
2.24 |
if (rp->nrecs >= nall) { |
| 186 |
|
|
nall += nall>>1; /* get more record space */ |
| 187 |
|
|
rp = (RECINDEX *)realloc(rp, |
| 188 |
|
|
sizeof(RECINDEX) + nall*sizeof(char *)); |
| 189 |
|
|
if (rp == NULL) |
| 190 |
|
|
return(NULL); |
| 191 |
|
|
} |
| 192 |
greg |
2.1 |
rp->rec[rp->nrecs++] = cp; /* point to first non-white */ |
| 193 |
|
|
n = rp->nw_rec; |
| 194 |
|
|
while (++cp < mend) /* find end of record */ |
| 195 |
|
|
if (!*cp | isspace(*cp)) { |
| 196 |
|
|
if (--n <= 0) |
| 197 |
|
|
break; /* got requisite # words */ |
| 198 |
|
|
do { /* else find next word */ |
| 199 |
|
|
if (*cp == '\n') { |
| 200 |
greg |
2.40 |
fputs("Unexpected EOL in record!\n", stderr); |
| 201 |
greg |
2.1 |
free_records(rp); |
| 202 |
|
|
return(NULL); |
| 203 |
|
|
} |
| 204 |
|
|
if (++cp >= mend) |
| 205 |
|
|
break; |
| 206 |
|
|
} while (!*cp | isspace(*cp)); |
| 207 |
|
|
} |
| 208 |
|
|
} |
| 209 |
|
|
rp->rec[rp->nrecs] = mend; /* reallocate to save space */ |
| 210 |
|
|
rp = (RECINDEX *)realloc(rp, |
| 211 |
|
|
sizeof(RECINDEX) + rp->nrecs*sizeof(char *)); |
| 212 |
|
|
return(rp); |
| 213 |
|
|
} |
| 214 |
|
|
|
| 215 |
|
|
/* count number of columns based on first EOL */ |
| 216 |
|
|
static int |
| 217 |
|
|
count_columns(const RECINDEX *rp) |
| 218 |
|
|
{ |
| 219 |
|
|
char *cp = rp->rec[0]; |
| 220 |
|
|
char *mend = rp->rec[rp->nrecs]; |
| 221 |
|
|
int i; |
| 222 |
|
|
|
| 223 |
|
|
while (*cp != '\n') |
| 224 |
|
|
if (++cp >= mend) |
| 225 |
|
|
return(0); |
| 226 |
|
|
for (i = 0; i < rp->nrecs; i++) |
| 227 |
|
|
if (rp->rec[i] >= cp) |
| 228 |
|
|
break; |
| 229 |
|
|
return(i); |
| 230 |
|
|
} |
| 231 |
|
|
|
| 232 |
|
|
/* copy nth record from index to stdout */ |
| 233 |
|
|
static int |
| 234 |
greg |
2.42 |
print_record(const RECINDEX *rp, size_t n) |
| 235 |
greg |
2.1 |
{ |
| 236 |
greg |
2.41 |
static char delims[] = " \t\n\r\f"; |
| 237 |
|
|
int words2go = rp->nw_rec; |
| 238 |
|
|
char *scp; |
| 239 |
greg |
2.1 |
|
| 240 |
greg |
2.42 |
if (n >= rp->nrecs) |
| 241 |
greg |
2.1 |
return(0); |
| 242 |
|
|
scp = rp->rec[n]; |
| 243 |
greg |
2.40 |
|
| 244 |
|
|
if (check && !isfltd(scp, delims)) |
| 245 |
|
|
goto formerr; |
| 246 |
greg |
2.1 |
do { |
| 247 |
|
|
putc(*scp++, stdout); |
| 248 |
|
|
if (!*scp | isspace(*scp)) { |
| 249 |
|
|
if (--words2go <= 0) |
| 250 |
|
|
break; |
| 251 |
|
|
putc(' ', stdout); /* single space btwn. words */ |
| 252 |
|
|
do |
| 253 |
|
|
if (++scp >= rp->rec[n+1]) |
| 254 |
|
|
break; |
| 255 |
|
|
while (!*scp | isspace(*scp)); |
| 256 |
greg |
2.40 |
|
| 257 |
|
|
if (check && !isfltd(scp, delims)) |
| 258 |
|
|
goto formerr; |
| 259 |
greg |
2.1 |
} |
| 260 |
|
|
} while (scp < rp->rec[n+1]); |
| 261 |
|
|
/* caller adds record sep. */ |
| 262 |
|
|
return(1); |
| 263 |
greg |
2.40 |
formerr: |
| 264 |
|
|
fputs("Badly formed number: ", stderr); |
| 265 |
|
|
while (*scp && !isspace(*scp)) |
| 266 |
|
|
fputc(*scp++, stderr); |
| 267 |
|
|
fputc('\n', stderr); |
| 268 |
|
|
return(0); |
| 269 |
greg |
2.1 |
} |
| 270 |
|
|
|
| 271 |
greg |
2.42 |
/* copy a stream to stdout, return bytes written or 0 on error */ |
| 272 |
|
|
static size_t |
| 273 |
greg |
2.1 |
output_stream(FILE *fp) |
| 274 |
|
|
{ |
| 275 |
greg |
2.42 |
size_t ntot = 0; |
| 276 |
greg |
2.1 |
char buf[8192]; |
| 277 |
greg |
2.42 |
size_t n; |
| 278 |
greg |
2.1 |
|
| 279 |
|
|
if (fp == NULL) |
| 280 |
|
|
return(0); |
| 281 |
greg |
2.13 |
fflush(stdout); |
| 282 |
greg |
2.42 |
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 283 |
greg |
2.1 |
if (write(fileno(stdout), buf, n) != n) |
| 284 |
|
|
return(0); |
| 285 |
greg |
2.42 |
ntot += n; |
| 286 |
|
|
} |
| 287 |
|
|
return(!ferror(fp) * ntot); |
| 288 |
greg |
2.1 |
} |
| 289 |
|
|
|
| 290 |
|
|
/* get next word from stream, leaving stream on EOL or start of next word */ |
| 291 |
|
|
static char * |
| 292 |
|
|
fget_word(char buf[256], FILE *fp) |
| 293 |
|
|
{ |
| 294 |
|
|
int c; |
| 295 |
|
|
char *cp; |
| 296 |
|
|
/* skip nul's and white space */ |
| 297 |
|
|
while (!(c = getc(fp)) || isspace(c)) |
| 298 |
|
|
; |
| 299 |
|
|
if (c == EOF) |
| 300 |
|
|
return(NULL); |
| 301 |
|
|
cp = buf; |
| 302 |
|
|
do |
| 303 |
|
|
*cp++ = c; |
| 304 |
|
|
while ((c = getc(fp)) != EOF && !isspace(c) && cp < buf+255); |
| 305 |
|
|
*cp = '\0'; |
| 306 |
|
|
while (isspace(c) & (c != '\n')) |
| 307 |
|
|
c = getc(fp); |
| 308 |
|
|
if (c != EOF) |
| 309 |
|
|
ungetc(c, fp); |
| 310 |
|
|
return(buf); |
| 311 |
|
|
} |
| 312 |
|
|
|
| 313 |
greg |
2.31 |
/* parse RxCx... string */ |
| 314 |
|
|
static int |
| 315 |
|
|
get_array(const char *spec, int blklvl[][2], int nlvls) |
| 316 |
|
|
{ |
| 317 |
|
|
int n; |
| 318 |
|
|
|
| 319 |
|
|
if (nlvls <= 0) { |
| 320 |
|
|
fputs("Too many block levels!\n", stderr); |
| 321 |
|
|
exit(1); |
| 322 |
|
|
} |
| 323 |
|
|
if (sscanf(spec, "%dx%d", &blklvl[0][0], &blklvl[0][1]) != 2) { |
| 324 |
|
|
fputs("Bad block specification!\n", stderr); |
| 325 |
|
|
exit(1); |
| 326 |
|
|
} |
| 327 |
|
|
while (isdigit(*spec)) |
| 328 |
|
|
spec++; |
| 329 |
|
|
spec++; /* 'x' */ |
| 330 |
|
|
while (isdigit(*spec)) |
| 331 |
|
|
spec++; |
| 332 |
|
|
if ((*spec != 'x') & (*spec != 'X')) { |
| 333 |
|
|
if (*spec) { |
| 334 |
|
|
fputs("Blocks must be separated by 'x' or 'X'\n", stderr); |
| 335 |
|
|
exit(1); |
| 336 |
|
|
} |
| 337 |
|
|
return(1); |
| 338 |
|
|
} |
| 339 |
|
|
spec++; |
| 340 |
|
|
n = get_array(spec, blklvl+1, nlvls-1); |
| 341 |
|
|
if (!n) |
| 342 |
|
|
return(0); |
| 343 |
|
|
blklvl[0][0] *= blklvl[1][0]; |
| 344 |
|
|
blklvl[0][1] *= blklvl[1][1]; |
| 345 |
|
|
return(n+1); |
| 346 |
|
|
} |
| 347 |
greg |
2.1 |
|
| 348 |
greg |
2.9 |
/* check settings and assign defaults */ |
| 349 |
greg |
2.11 |
static int |
| 350 |
greg |
2.9 |
check_sizes() |
| 351 |
|
|
{ |
| 352 |
|
|
if (fmtid == NULL) { |
| 353 |
|
|
fmtid = "ascii"; |
| 354 |
|
|
} else if (!comp_size) { |
| 355 |
|
|
if (!strcmp(fmtid, "float")) |
| 356 |
|
|
comp_size = sizeof(float); |
| 357 |
|
|
else if (!strcmp(fmtid, "double")) |
| 358 |
|
|
comp_size = sizeof(double); |
| 359 |
|
|
else if (!strcmp(fmtid, "byte")) |
| 360 |
|
|
comp_size = 1; |
| 361 |
greg |
2.14 |
else if (strcmp(fmtid, "ascii")) { |
| 362 |
greg |
2.11 |
fprintf(stderr, "Unsupported format: %s\n", fmtid); |
| 363 |
|
|
return(0); |
| 364 |
|
|
} |
| 365 |
greg |
2.9 |
} |
| 366 |
greg |
2.16 |
if (transpose && (no_rows <= 0) & (no_columns <= 0)) { |
| 367 |
|
|
if (ni_rows > 0) no_columns = ni_rows; |
| 368 |
|
|
if (ni_columns > 0) no_rows = ni_columns; |
| 369 |
greg |
2.42 |
} else { |
| 370 |
|
|
if (no_columns <= 0) |
| 371 |
|
|
no_columns = ni_columns; |
| 372 |
|
|
if ((no_rows <= 0) & (no_columns > 0) && |
| 373 |
|
|
!((ni_rows*ni_columns) % no_columns)) |
| 374 |
|
|
no_rows = ni_rows*ni_columns/no_columns; |
| 375 |
|
|
} |
| 376 |
greg |
2.9 |
if (n_comp <= 0) |
| 377 |
|
|
n_comp = 3; |
| 378 |
greg |
2.11 |
return(1); |
| 379 |
greg |
2.9 |
} |
| 380 |
|
|
|
| 381 |
greg |
2.31 |
/* call to compute block input position */ |
| 382 |
greg |
2.42 |
static size_t |
| 383 |
greg |
2.31 |
get_block_pos(int r, int c, int blklvl[][2], int nlvls) |
| 384 |
|
|
{ |
| 385 |
greg |
2.42 |
size_t n = 0; |
| 386 |
greg |
2.31 |
|
| 387 |
|
|
while (nlvls > 1) { |
| 388 |
|
|
int sr = r/blklvl[1][0]; |
| 389 |
|
|
int sc = c/blklvl[1][1]; |
| 390 |
|
|
r -= sr*blklvl[1][0]; |
| 391 |
|
|
c -= sc*blklvl[1][1]; |
| 392 |
|
|
n += sr*blklvl[1][0]*blklvl[0][1] + sc*blklvl[1][0]*blklvl[1][1]; |
| 393 |
|
|
blklvl++; |
| 394 |
|
|
nlvls--; |
| 395 |
|
|
} |
| 396 |
|
|
n += r*blklvl[0][1] + c; |
| 397 |
|
|
return(n); |
| 398 |
|
|
} |
| 399 |
|
|
|
| 400 |
|
|
/* return input offset based on array ordering and transpose option */ |
| 401 |
greg |
2.42 |
static size_t |
| 402 |
greg |
2.31 |
get_input_pos(int r, int c) |
| 403 |
|
|
{ |
| 404 |
greg |
2.42 |
size_t n; |
| 405 |
greg |
2.31 |
|
| 406 |
|
|
if (outLevels > 1) { /* block reordering */ |
| 407 |
|
|
n = get_block_pos(r, c, outArray, outLevels); |
| 408 |
|
|
if (transpose) { |
| 409 |
greg |
2.34 |
r = n/ni_rows; |
| 410 |
|
|
c = n - r*ni_rows; |
| 411 |
greg |
2.42 |
n = (size_t)c*ni_columns + r; |
| 412 |
greg |
2.31 |
} |
| 413 |
|
|
} else if (transpose) /* transpose only */ |
| 414 |
greg |
2.42 |
n = (size_t)c*ni_columns + r; |
| 415 |
greg |
2.31 |
else /* XXX should never happen! */ |
| 416 |
greg |
2.42 |
n = (size_t)r*no_columns + c; |
| 417 |
greg |
2.31 |
return(n); |
| 418 |
|
|
} |
| 419 |
|
|
|
| 420 |
|
|
/* output reordered ASCII or binary data from memory */ |
| 421 |
greg |
2.1 |
static int |
| 422 |
greg |
2.31 |
do_reorder(const MEMLOAD *mp) |
| 423 |
greg |
2.1 |
{ |
| 424 |
|
|
static const char tabEOL[2] = {'\t','\n'}; |
| 425 |
|
|
RECINDEX *rp = NULL; |
| 426 |
greg |
2.42 |
size_t nrecords; |
| 427 |
greg |
2.1 |
int i, j; |
| 428 |
greg |
2.5 |
/* propogate sizes */ |
| 429 |
|
|
if (ni_rows <= 0) |
| 430 |
greg |
2.36 |
ni_rows = transpose ? no_columns : no_rows; |
| 431 |
greg |
2.5 |
if (ni_columns <= 0) |
| 432 |
greg |
2.36 |
ni_columns = transpose ? no_rows : no_columns; |
| 433 |
greg |
2.1 |
/* get # records (& index) */ |
| 434 |
greg |
2.9 |
if (!comp_size) { |
| 435 |
|
|
if ((rp = index_records(mp, n_comp)) == NULL) |
| 436 |
greg |
2.1 |
return(0); |
| 437 |
|
|
if (ni_columns <= 0) |
| 438 |
|
|
ni_columns = count_columns(rp); |
| 439 |
|
|
nrecords = rp->nrecs; |
| 440 |
greg |
2.3 |
} else if ((ni_rows > 0) & (ni_columns > 0)) { |
| 441 |
greg |
2.42 |
nrecords = (size_t)ni_rows*ni_columns; |
| 442 |
greg |
2.9 |
if (nrecords > mp->len/(n_comp*comp_size)) { |
| 443 |
greg |
2.33 |
fputs("Input too small for specified size and type\n", |
| 444 |
|
|
stderr); |
| 445 |
greg |
2.3 |
return(0); |
| 446 |
|
|
} |
| 447 |
|
|
} else |
| 448 |
greg |
2.9 |
nrecords = mp->len/(n_comp*comp_size); |
| 449 |
greg |
2.1 |
/* check sizes */ |
| 450 |
|
|
if ((ni_rows <= 0) & (ni_columns > 0)) |
| 451 |
|
|
ni_rows = nrecords/ni_columns; |
| 452 |
greg |
2.36 |
else if ((ni_columns <= 0) & (ni_rows > 0)) |
| 453 |
greg |
2.1 |
ni_columns = nrecords/ni_rows; |
| 454 |
greg |
2.42 |
if (nrecords != (size_t)ni_rows*ni_columns) |
| 455 |
greg |
2.1 |
goto badspec; |
| 456 |
greg |
2.31 |
if (transpose) { |
| 457 |
|
|
if (no_columns <= 0) |
| 458 |
|
|
no_columns = ni_rows; |
| 459 |
|
|
if (no_rows <= 0) |
| 460 |
|
|
no_rows = ni_columns; |
| 461 |
greg |
2.34 |
if (outLevels <= 1 && |
| 462 |
|
|
(no_rows != ni_columns) | (no_columns != ni_rows)) |
| 463 |
greg |
2.31 |
goto badspec; |
| 464 |
greg |
2.33 |
} else { |
| 465 |
|
|
if (no_columns <= 0) |
| 466 |
|
|
no_columns = ni_columns; |
| 467 |
|
|
if (no_rows <= 0) |
| 468 |
|
|
no_rows = ni_rows; |
| 469 |
|
|
} |
| 470 |
|
|
if (ni_rows*ni_columns != no_rows*no_columns) { |
| 471 |
|
|
fputs("Number of input and output records do not match\n", |
| 472 |
|
|
stderr); |
| 473 |
|
|
return(0); |
| 474 |
greg |
2.31 |
} |
| 475 |
greg |
2.36 |
if (o_header) { /* finish header? */ |
| 476 |
|
|
printf("NROWS=%d\n", no_rows); |
| 477 |
|
|
printf("NCOLS=%d\n", no_columns); |
| 478 |
greg |
2.39 |
fputformat(fmtid, stdout); |
| 479 |
greg |
2.36 |
fputc('\n', stdout); |
| 480 |
|
|
} |
| 481 |
greg |
2.31 |
/* reorder records */ |
| 482 |
greg |
2.1 |
for (i = 0; i < no_rows; i++) { |
| 483 |
greg |
2.31 |
for (j = 0; j < no_columns; j++) { |
| 484 |
greg |
2.42 |
size_t n = get_input_pos(i, j); |
| 485 |
greg |
2.33 |
if (n >= nrecords) { |
| 486 |
|
|
fputs("Index past end-of-file\n", stderr); |
| 487 |
|
|
return(0); |
| 488 |
|
|
} |
| 489 |
greg |
2.1 |
if (rp != NULL) { /* ASCII output */ |
| 490 |
greg |
2.40 |
if (!print_record(rp, n)) |
| 491 |
|
|
return(0); |
| 492 |
greg |
2.1 |
putc(tabEOL[j >= no_columns-1], stdout); |
| 493 |
|
|
} else { /* binary output */ |
| 494 |
greg |
2.31 |
putbinary((char *)mp->base + (n_comp*comp_size)*n, |
| 495 |
greg |
2.25 |
comp_size, n_comp, stdout); |
| 496 |
greg |
2.1 |
} |
| 497 |
greg |
2.31 |
} |
| 498 |
greg |
2.1 |
if (ferror(stdout)) { |
| 499 |
greg |
2.40 |
fputs("Error writing to stdout\n", stderr); |
| 500 |
greg |
2.1 |
return(0); |
| 501 |
|
|
} |
| 502 |
|
|
} |
| 503 |
|
|
if (rp != NULL) |
| 504 |
|
|
free_records(rp); |
| 505 |
|
|
return(1); |
| 506 |
|
|
badspec: |
| 507 |
greg |
2.40 |
fputs("Bad dimension(s)\n", stderr); |
| 508 |
greg |
2.1 |
return(0); |
| 509 |
|
|
} |
| 510 |
|
|
|
| 511 |
greg |
2.41 |
/* resize stream input by ignoring EOLs between ASCII records */ |
| 512 |
greg |
2.1 |
static int |
| 513 |
|
|
do_resize(FILE *fp) |
| 514 |
|
|
{ |
| 515 |
greg |
2.42 |
size_t records2go = ni_rows*ni_columns; |
| 516 |
greg |
2.1 |
int columns2go = no_columns; |
| 517 |
|
|
char word[256]; |
| 518 |
greg |
2.36 |
|
| 519 |
|
|
if (o_header) { /* finish header? */ |
| 520 |
|
|
if (no_rows > 0) |
| 521 |
|
|
printf("NROWS=%d\n", no_rows); |
| 522 |
|
|
if (no_columns > 0) |
| 523 |
|
|
printf("NCOLS=%d\n", no_columns); |
| 524 |
greg |
2.39 |
fputformat(fmtid, stdout); |
| 525 |
greg |
2.36 |
fputc('\n', stdout); |
| 526 |
|
|
} |
| 527 |
greg |
2.42 |
if (comp_size) { /* just copy binary data? */ |
| 528 |
|
|
size_t nwritten = output_stream(fp); |
| 529 |
|
|
if ((no_rows > 0) & (no_columns > 0) && |
| 530 |
|
|
nwritten != (size_t)no_rows*no_columns*n_comp*comp_size) { |
| 531 |
|
|
if (check) { |
| 532 |
|
|
fputs("Incorrect binary file size\n", stderr); |
| 533 |
|
|
return(0); /* fatal if check is true */ |
| 534 |
|
|
} |
| 535 |
|
|
if (warnings) |
| 536 |
|
|
fputs("Warning -- unexpected binary file size\n", stderr); |
| 537 |
|
|
} |
| 538 |
|
|
return(nwritten > 0); |
| 539 |
|
|
} /* else straight ASCII data copy? */ |
| 540 |
|
|
if (!check & (no_columns == ni_columns) & (no_rows == ni_rows)) |
| 541 |
|
|
return(output_stream(fp) > 0); |
| 542 |
|
|
/* need to reshape (& check?) input */ |
| 543 |
greg |
2.1 |
if (no_columns <= 0) { |
| 544 |
greg |
2.40 |
fputs("Missing -oc specification\n", stderr); |
| 545 |
greg |
2.1 |
return(0); |
| 546 |
|
|
} |
| 547 |
|
|
if ((records2go <= 0) & (no_rows > 0)) |
| 548 |
|
|
records2go = no_rows*no_columns; |
| 549 |
|
|
else if (no_rows*no_columns != records2go) { |
| 550 |
|
|
fprintf(stderr, |
| 551 |
greg |
2.36 |
"Number of input and output records disagree (%dx%d != %dx%d)\n", |
| 552 |
greg |
2.1 |
ni_rows, ni_columns, no_rows, no_columns); |
| 553 |
|
|
return(0); |
| 554 |
|
|
} |
| 555 |
|
|
do { /* reshape records */ |
| 556 |
greg |
2.42 |
int n = n_comp; |
| 557 |
|
|
while (n--) { |
| 558 |
greg |
2.1 |
if (fget_word(word, fp) == NULL) { |
| 559 |
greg |
2.42 |
if ((records2go > 0) | (n < n_comp-1)) |
| 560 |
greg |
2.1 |
break; |
| 561 |
|
|
goto done; /* normal EOD */ |
| 562 |
|
|
} |
| 563 |
greg |
2.41 |
if (check && !isflt(word)) { |
| 564 |
greg |
2.40 |
fputs("Badly formed number: ", stderr); |
| 565 |
|
|
fputs(word, stderr); |
| 566 |
|
|
fputc('\n', stderr); |
| 567 |
|
|
return(0); |
| 568 |
|
|
} |
| 569 |
greg |
2.1 |
fputs(word, stdout); |
| 570 |
|
|
if (n) { /* mid-record? */ |
| 571 |
|
|
int c = getc(fp); |
| 572 |
|
|
if ((c == '\n') | (c == EOF)) |
| 573 |
|
|
break; |
| 574 |
|
|
ungetc(c, fp); |
| 575 |
|
|
putc(' ', stdout); |
| 576 |
|
|
} |
| 577 |
|
|
} |
| 578 |
|
|
if (n >= 0) { |
| 579 |
greg |
2.40 |
fputs("Incomplete record / unexpected EOF\n", stderr); |
| 580 |
greg |
2.1 |
return(0); |
| 581 |
|
|
} |
| 582 |
|
|
if (--columns2go <= 0) { /* time to end output row? */ |
| 583 |
|
|
putc('\n', stdout); |
| 584 |
|
|
columns2go = no_columns; |
| 585 |
|
|
} else /* else separate records */ |
| 586 |
|
|
putc('\t', stdout); |
| 587 |
|
|
} while (--records2go); /* expected EOD? */ |
| 588 |
|
|
done: |
| 589 |
greg |
2.42 |
if (columns2go != no_columns) { |
| 590 |
|
|
if (check) { |
| 591 |
|
|
fputs("Incomplete final row\n", stderr); |
| 592 |
|
|
return(0); |
| 593 |
|
|
} |
| 594 |
|
|
if (warnings) |
| 595 |
|
|
fputs("Warning -- incomplete final row\n", stderr); |
| 596 |
|
|
} |
| 597 |
|
|
if (fget_word(word, fp) != NULL) { |
| 598 |
|
|
if (check) { |
| 599 |
|
|
fputs("Characters beyond expected EOD\n", stderr); |
| 600 |
|
|
return(0); |
| 601 |
|
|
} |
| 602 |
|
|
if (warnings) |
| 603 |
|
|
fputs("Warning -- characters beyond expected EOD\n", stderr); |
| 604 |
|
|
} |
| 605 |
greg |
2.1 |
return(1); |
| 606 |
|
|
} |
| 607 |
|
|
|
| 608 |
|
|
/* process a header line and copy to stdout */ |
| 609 |
|
|
static int |
| 610 |
|
|
headline(char *s, void *p) |
| 611 |
|
|
{ |
| 612 |
greg |
2.26 |
static char fmt[MAXFMTLEN]; |
| 613 |
greg |
2.9 |
int n; |
| 614 |
greg |
2.1 |
|
| 615 |
|
|
if (formatval(fmt, s)) { |
| 616 |
greg |
2.9 |
if (fmtid == NULL) { |
| 617 |
|
|
fmtid = fmt; |
| 618 |
|
|
return(0); |
| 619 |
|
|
} |
| 620 |
greg |
2.1 |
if (!strcmp(fmt, fmtid)) |
| 621 |
|
|
return(0); |
| 622 |
|
|
fprintf(stderr, "Input format '%s' != '%s'\n", fmt, fmtid); |
| 623 |
|
|
return(-1); |
| 624 |
|
|
} |
| 625 |
greg |
2.9 |
if (!strncmp(s, "NROWS=", 6)) { |
| 626 |
|
|
n = atoi(s+6); |
| 627 |
|
|
if ((ni_rows > 0) & (n != ni_rows)) { |
| 628 |
|
|
fputs("Incorrect input row count\n", stderr); |
| 629 |
|
|
return(-1); |
| 630 |
|
|
} |
| 631 |
|
|
ni_rows = n; |
| 632 |
|
|
return(0); |
| 633 |
|
|
} |
| 634 |
|
|
if (!strncmp(s, "NCOLS=", 6)) { |
| 635 |
|
|
n = atoi(s+6); |
| 636 |
|
|
if ((ni_columns > 0) & (n != ni_columns)) { |
| 637 |
|
|
fputs("Incorrect input column count\n", stderr); |
| 638 |
|
|
return(-1); |
| 639 |
|
|
} |
| 640 |
|
|
ni_columns = n; |
| 641 |
|
|
return(0); |
| 642 |
|
|
} |
| 643 |
|
|
if (!strncmp(s, "NCOMP=", 6)) { |
| 644 |
|
|
n = atoi(s+6); |
| 645 |
|
|
if ((n_comp > 0) & (n != n_comp)) { |
| 646 |
greg |
2.14 |
fputs("Incorrect number of components\n", stderr); |
| 647 |
greg |
2.9 |
return(-1); |
| 648 |
|
|
} |
| 649 |
|
|
n_comp = n; |
| 650 |
|
|
return(0); |
| 651 |
|
|
} |
| 652 |
greg |
2.16 |
if (o_header) |
| 653 |
|
|
fputs(s, stdout); /* copy header info. */ |
| 654 |
greg |
2.1 |
return(0); |
| 655 |
|
|
} |
| 656 |
|
|
|
| 657 |
|
|
/* main routine for converting rows/columns in data file */ |
| 658 |
|
|
int |
| 659 |
|
|
main(int argc, char *argv[]) |
| 660 |
|
|
{ |
| 661 |
greg |
2.14 |
int a; |
| 662 |
greg |
2.1 |
|
| 663 |
greg |
2.14 |
for (a = 1; a < argc && argv[a][0] == '-'; a++) |
| 664 |
|
|
switch (argv[a][1]) { |
| 665 |
greg |
2.1 |
case 'i': /* input */ |
| 666 |
greg |
2.14 |
if (argv[a][2] == 'c') /* columns */ |
| 667 |
|
|
ni_columns = atoi(argv[++a]); |
| 668 |
|
|
else if (argv[a][2] == 'r') |
| 669 |
|
|
ni_rows = atoi(argv[++a]); |
| 670 |
greg |
2.1 |
else |
| 671 |
|
|
goto userr; |
| 672 |
|
|
break; |
| 673 |
|
|
case 'o': /* output */ |
| 674 |
greg |
2.14 |
if (argv[a][2] == 'c') /* columns */ |
| 675 |
|
|
no_columns = atoi(argv[++a]); |
| 676 |
|
|
else if (argv[a][2] == 'r') |
| 677 |
|
|
no_rows = atoi(argv[++a]); |
| 678 |
greg |
2.31 |
else if (argv[a][2] || |
| 679 |
|
|
!(outLevels=get_array(argv[++a], outArray, MAXLEVELS))) |
| 680 |
greg |
2.1 |
goto userr; |
| 681 |
|
|
break; |
| 682 |
greg |
2.10 |
case 'h': /* turn off header */ |
| 683 |
greg |
2.14 |
switch (argv[a][2]) { |
| 684 |
greg |
2.10 |
case 'i': |
| 685 |
|
|
i_header = 0; |
| 686 |
|
|
break; |
| 687 |
|
|
case 'o': |
| 688 |
|
|
o_header = 0; |
| 689 |
|
|
break; |
| 690 |
|
|
case '\0': |
| 691 |
|
|
i_header = o_header = 0; |
| 692 |
|
|
break; |
| 693 |
|
|
default: |
| 694 |
|
|
goto userr; |
| 695 |
|
|
} |
| 696 |
greg |
2.1 |
break; |
| 697 |
|
|
case 't': /* transpose on/off */ |
| 698 |
|
|
transpose = !transpose; |
| 699 |
|
|
break; |
| 700 |
|
|
case 'f': /* format */ |
| 701 |
greg |
2.14 |
switch (argv[a][2]) { |
| 702 |
greg |
2.1 |
case 'a': /* ASCII */ |
| 703 |
|
|
case 'A': |
| 704 |
|
|
fmtid = "ascii"; |
| 705 |
greg |
2.9 |
comp_size = 0; |
| 706 |
greg |
2.1 |
break; |
| 707 |
|
|
case 'f': /* float */ |
| 708 |
|
|
case 'F': |
| 709 |
|
|
fmtid = "float"; |
| 710 |
greg |
2.9 |
comp_size = sizeof(float); |
| 711 |
greg |
2.1 |
break; |
| 712 |
|
|
case 'd': /* double */ |
| 713 |
|
|
case 'D': |
| 714 |
|
|
fmtid = "double"; |
| 715 |
greg |
2.9 |
comp_size = sizeof(double); |
| 716 |
greg |
2.1 |
break; |
| 717 |
|
|
case 'b': /* binary (bytes) */ |
| 718 |
|
|
case 'B': |
| 719 |
|
|
fmtid = "byte"; |
| 720 |
greg |
2.9 |
comp_size = 1; |
| 721 |
greg |
2.1 |
break; |
| 722 |
|
|
default: |
| 723 |
|
|
goto userr; |
| 724 |
|
|
} |
| 725 |
greg |
2.14 |
if (argv[a][3]) { |
| 726 |
|
|
if (!isdigit(argv[a][3])) |
| 727 |
greg |
2.1 |
goto userr; |
| 728 |
greg |
2.14 |
n_comp = atoi(argv[a]+3); |
| 729 |
|
|
} else |
| 730 |
|
|
n_comp = 1; |
| 731 |
greg |
2.1 |
break; |
| 732 |
greg |
2.4 |
case 'w': /* warnings on/off */ |
| 733 |
|
|
warnings = !warnings; |
| 734 |
|
|
break; |
| 735 |
greg |
2.36 |
case 'c': /* force check operation */ |
| 736 |
|
|
check = 1; |
| 737 |
|
|
break; |
| 738 |
greg |
2.1 |
default: |
| 739 |
|
|
goto userr; |
| 740 |
|
|
} |
| 741 |
greg |
2.14 |
if (a < argc-1) /* arg count OK? */ |
| 742 |
greg |
2.1 |
goto userr; |
| 743 |
greg |
2.31 |
if (outLevels) { /* should check consistency? */ |
| 744 |
|
|
no_rows = outArray[0][0]; |
| 745 |
|
|
no_columns = outArray[0][1]; |
| 746 |
|
|
} |
| 747 |
greg |
2.1 |
/* open input file? */ |
| 748 |
greg |
2.42 |
if (a == argc-1 && freopen(argv[a], "rb", stdin) == NULL) { |
| 749 |
greg |
2.14 |
fprintf(stderr, "%s: cannot open for reading\n", argv[a]); |
| 750 |
greg |
2.1 |
return(1); |
| 751 |
greg |
2.42 |
} else |
| 752 |
greg |
2.1 |
SET_FILE_BINARY(stdin); |
| 753 |
greg |
2.42 |
SET_FILE_BINARY(stdout); |
| 754 |
greg |
2.40 |
#ifdef getc_unlocked /* avoid stupid semaphores */ |
| 755 |
|
|
flockfile(stdin); |
| 756 |
|
|
flockfile(stdout); |
| 757 |
|
|
#endif |
| 758 |
greg |
2.1 |
/* check for no-op */ |
| 759 |
greg |
2.36 |
if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) && |
| 760 |
greg |
2.19 |
(no_columns == ni_columns) & (no_rows == ni_rows)) { |
| 761 |
greg |
2.4 |
if (warnings) |
| 762 |
|
|
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
| 763 |
greg |
2.1 |
argv[0]); |
| 764 |
greg |
2.42 |
return(!output_stream(stdin)); |
| 765 |
greg |
2.1 |
} |
| 766 |
greg |
2.43 |
/* read input header? */ |
| 767 |
|
|
if (i_header && getheader(stdin, headline, NULL) < 0) |
| 768 |
|
|
return(1); |
| 769 |
|
|
if (!check_sizes()) /* adjust sizes */ |
| 770 |
greg |
2.11 |
return(1); |
| 771 |
greg |
2.43 |
if (o_header) { /* add to output header? */ |
| 772 |
greg |
2.30 |
if (!i_header) |
| 773 |
|
|
newheader("RADIANCE", stdout); |
| 774 |
greg |
2.14 |
printargs(a, argv, stdout); |
| 775 |
greg |
2.9 |
printf("NCOMP=%d\n", n_comp); |
| 776 |
greg |
2.10 |
} |
| 777 |
greg |
2.43 |
if (!comp_size) { /* a little late, here... */ |
| 778 |
greg |
2.42 |
SET_FILE_TEXT(stdin); |
| 779 |
|
|
SET_FILE_TEXT(stdout); |
| 780 |
|
|
} |
| 781 |
|
|
if (transpose | (outLevels > 1) || (o_header && no_rows <= 0)) { |
| 782 |
greg |
2.19 |
MEMLOAD myMem; /* need to map into memory */ |
| 783 |
greg |
2.14 |
if (a == argc-1) { |
| 784 |
greg |
2.1 |
if (load_file(&myMem, stdin) <= 0) { |
| 785 |
|
|
fprintf(stderr, "%s: error loading file into memory\n", |
| 786 |
greg |
2.14 |
argv[a]); |
| 787 |
greg |
2.1 |
return(1); |
| 788 |
|
|
} |
| 789 |
|
|
} else if (load_stream(&myMem, stdin) <= 0) { |
| 790 |
|
|
fprintf(stderr, "%s: error loading stdin into memory\n", |
| 791 |
|
|
argv[0]); |
| 792 |
|
|
return(1); |
| 793 |
|
|
} |
| 794 |
greg |
2.31 |
if (!do_reorder(&myMem)) |
| 795 |
greg |
2.1 |
return(1); |
| 796 |
greg |
2.17 |
/* free_load(&myMem); about to exit, so don't bother */ |
| 797 |
greg |
2.36 |
} else if (!do_resize(stdin)) /* just reshaping input */ |
| 798 |
greg |
2.1 |
return(1); |
| 799 |
|
|
return(0); |
| 800 |
|
|
userr: |
| 801 |
|
|
fprintf(stderr, |
| 802 |
greg |
2.36 |
"Usage: %s [-h[io]][-w][-c][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", |
| 803 |
greg |
2.1 |
argv[0]); |
| 804 |
|
|
return(1); |
| 805 |
|
|
} |