| 20 |  | #include <sys/mman.h> | 
| 21 |  | #endif | 
| 22 |  |  | 
| 23 | + | #define MAXLEVELS       16      /* max RxC.. block pairs */ | 
| 24 | + |  | 
| 25 |  | typedef struct { | 
| 26 |  | void    *mapped;        /* memory-mapped pointer */ | 
| 27 |  | void    *base;          /* pointer to base memory */ | 
| 30 |  |  | 
| 31 |  | typedef struct { | 
| 32 |  | int     nw_rec;         /* number of words per record */ | 
| 33 | < | int     nrecs;          /* number of records we found */ | 
| 33 | > | long    nrecs;          /* number of records we found */ | 
| 34 |  | char    *rec[1];        /* record array (extends struct) */ | 
| 35 |  | } RECINDEX; | 
| 36 |  |  | 
| 219 |  |  | 
| 220 |  | /* copy nth record from index to stdout */ | 
| 221 |  | static int | 
| 222 | < | print_record(const RECINDEX *rp, int n) | 
| 222 | > | print_record(const RECINDEX *rp, long n) | 
| 223 |  | { | 
| 224 |  | int     words2go = rp->nw_rec; | 
| 225 |  | char    *scp; | 
| 292 |  | int             transpose = 0;                  /* transpose rows & cols? */ | 
| 293 |  | int             i_header = 1;                   /* input header? */ | 
| 294 |  | int             o_header = 1;                   /* output header? */ | 
| 295 | + | int             outArray[MAXLEVELS][2];         /* output block nesting */ | 
| 296 | + | int             outLevels = 0;                  /* number of blocking levels */ | 
| 297 |  |  | 
| 298 | + | /* parse RxCx... string */ | 
| 299 | + | static int | 
| 300 | + | get_array(const char *spec, int blklvl[][2], int nlvls) | 
| 301 | + | { | 
| 302 | + | int     n; | 
| 303 | + |  | 
| 304 | + | if (nlvls <= 0) { | 
| 305 | + | fputs("Too many block levels!\n", stderr); | 
| 306 | + | exit(1); | 
| 307 | + | } | 
| 308 | + | if (sscanf(spec, "%dx%d", &blklvl[0][0], &blklvl[0][1]) != 2) { | 
| 309 | + | fputs("Bad block specification!\n", stderr); | 
| 310 | + | exit(1); | 
| 311 | + | } | 
| 312 | + | while (isdigit(*spec)) | 
| 313 | + | spec++; | 
| 314 | + | spec++;         /* 'x' */ | 
| 315 | + | while (isdigit(*spec)) | 
| 316 | + | spec++; | 
| 317 | + | if ((*spec != 'x') & (*spec != 'X')) { | 
| 318 | + | if (*spec) { | 
| 319 | + | fputs("Blocks must be separated by 'x' or 'X'\n", stderr); | 
| 320 | + | exit(1); | 
| 321 | + | } | 
| 322 | + | return(1); | 
| 323 | + | } | 
| 324 | + | spec++; | 
| 325 | + | n = get_array(spec, blklvl+1, nlvls-1); | 
| 326 | + | if (!n) | 
| 327 | + | return(0); | 
| 328 | + | blklvl[0][0] *= blklvl[1][0]; | 
| 329 | + | blklvl[0][1] *= blklvl[1][1]; | 
| 330 | + | return(n+1); | 
| 331 | + | } | 
| 332 | + |  | 
| 333 |  | /* check settings and assign defaults */ | 
| 334 |  | static int | 
| 335 |  | check_sizes() | 
| 359 |  | return(1); | 
| 360 |  | } | 
| 361 |  |  | 
| 362 | < | /* output transposed ASCII or binary data from memory */ | 
| 362 | > | /* call to compute block input position */ | 
| 363 | > | static long | 
| 364 | > | get_block_pos(int r, int c, int blklvl[][2], int nlvls) | 
| 365 | > | { | 
| 366 | > | long    n = 0; | 
| 367 | > |  | 
| 368 | > | while (nlvls > 1) { | 
| 369 | > | int     sr = r/blklvl[1][0]; | 
| 370 | > | int     sc = c/blklvl[1][1]; | 
| 371 | > | r -= sr*blklvl[1][0]; | 
| 372 | > | c -= sc*blklvl[1][1]; | 
| 373 | > | n += sr*blklvl[1][0]*blklvl[0][1] + sc*blklvl[1][0]*blklvl[1][1]; | 
| 374 | > | blklvl++; | 
| 375 | > | nlvls--; | 
| 376 | > | } | 
| 377 | > | n += r*blklvl[0][1] + c; | 
| 378 | > | return(n); | 
| 379 | > | } | 
| 380 | > |  | 
| 381 | > | /* return input offset based on array ordering and transpose option */ | 
| 382 | > | static long | 
| 383 | > | get_input_pos(int r, int c) | 
| 384 | > | { | 
| 385 | > | long    n; | 
| 386 | > |  | 
| 387 | > | if (outLevels > 1) {            /* block reordering */ | 
| 388 | > | n = get_block_pos(r, c, outArray, outLevels); | 
| 389 | > | if (transpose) { | 
| 390 | > | r = n/no_columns; | 
| 391 | > | c = n - r*no_columns; | 
| 392 | > | n = (long)r*ni_columns + c; | 
| 393 | > | } | 
| 394 | > | } else if (transpose)           /* transpose only */ | 
| 395 | > | n = (long)c*ni_columns + r; | 
| 396 | > | else                            /* XXX should never happen! */ | 
| 397 | > | n = (long)r*ni_columns + c; | 
| 398 | > | return(n); | 
| 399 | > | } | 
| 400 | > |  | 
| 401 | > | /* output reordered ASCII or binary data from memory */ | 
| 402 |  | static int | 
| 403 | < | do_transpose(const MEMLOAD *mp) | 
| 403 | > | do_reorder(const MEMLOAD *mp) | 
| 404 |  | { | 
| 405 |  | static const char       tabEOL[2] = {'\t','\n'}; | 
| 406 |  | RECINDEX                *rp = NULL; | 
| 434 |  | ni_columns = nrecords/ni_rows; | 
| 435 |  | if (nrecords != ni_rows*ni_columns) | 
| 436 |  | goto badspec; | 
| 437 | < | if (no_columns <= 0) | 
| 438 | < | no_columns = ni_rows; | 
| 439 | < | if (no_rows <= 0) | 
| 440 | < | no_rows = ni_columns; | 
| 441 | < | if ((no_rows != ni_columns) | (no_columns != ni_rows)) | 
| 442 | < | goto badspec; | 
| 443 | < | /* transpose records */ | 
| 437 | > | if (transpose) { | 
| 438 | > | if (no_columns <= 0) | 
| 439 | > | no_columns = ni_rows; | 
| 440 | > | if (no_rows <= 0) | 
| 441 | > | no_rows = ni_columns; | 
| 442 | > | if ((no_rows != ni_columns) | (no_columns != ni_rows)) | 
| 443 | > | goto badspec; | 
| 444 | > | } else { | 
| 445 | > | if (no_columns <= 0) | 
| 446 | > | no_columns = ni_columns; | 
| 447 | > | if (no_rows <= 0) | 
| 448 | > | no_rows = ni_rows; | 
| 449 | > | if ((no_rows != ni_rows) | (no_columns != ni_columns)) | 
| 450 | > | goto badspec; | 
| 451 | > | } | 
| 452 | > | /* reorder records */ | 
| 453 |  | for (i = 0; i < no_rows; i++) { | 
| 454 | < | for (j = 0; j < no_columns; j++) | 
| 454 | > | for (j = 0; j < no_columns; j++) { | 
| 455 | > | long    n = get_input_pos(i, j); | 
| 456 |  | if (rp != NULL) {               /* ASCII output */ | 
| 457 | < | print_record(rp, j*ni_columns + i); | 
| 457 | > | print_record(rp, n); | 
| 458 |  | putc(tabEOL[j >= no_columns-1], stdout); | 
| 459 |  | } else {                        /* binary output */ | 
| 460 | < | putbinary((char *)mp->base + | 
| 373 | < | (size_t)(n_comp*comp_size)*(j*ni_columns + i), | 
| 460 | > | putbinary((char *)mp->base + (n_comp*comp_size)*n, | 
| 461 |  | comp_size, n_comp, stdout); | 
| 462 |  | } | 
| 463 | + | } | 
| 464 |  | if (ferror(stdout)) { | 
| 465 |  | fprintf(stderr, "Error writing to stdout\n"); | 
| 466 |  | return(0); | 
| 470 |  | free_records(rp); | 
| 471 |  | return(1); | 
| 472 |  | badspec: | 
| 473 | < | fprintf(stderr, "Bad transpose specification -- check dimension(s)\n"); | 
| 473 | > | fprintf(stderr, "Bad dimension(s)\n"); | 
| 474 |  | return(0); | 
| 475 |  | } | 
| 476 |  |  | 
| 602 |  | no_columns = atoi(argv[++a]); | 
| 603 |  | else if (argv[a][2] == 'r') | 
| 604 |  | no_rows = atoi(argv[++a]); | 
| 605 | < | else | 
| 605 | > | else if (argv[a][2] || | 
| 606 | > | !(outLevels=get_array(argv[++a], outArray, MAXLEVELS))) | 
| 607 |  | goto userr; | 
| 608 |  | break; | 
| 609 |  | case 'h':                       /* turn off header */ | 
| 664 |  | } | 
| 665 |  | if (a < argc-1)                         /* arg count OK? */ | 
| 666 |  | goto userr; | 
| 667 | + | if (outLevels) {                        /* should check consistency? */ | 
| 668 | + | no_rows = outArray[0][0]; | 
| 669 | + | no_columns = outArray[0][1]; | 
| 670 | + | } | 
| 671 |  | /* open input file? */ | 
| 672 |  | if (a == argc-1 && freopen(argv[a], "r", stdin) == NULL) { | 
| 673 |  | fprintf(stderr, "%s: cannot open for reading\n", argv[a]); | 
| 678 |  | SET_FILE_BINARY(stdout); | 
| 679 |  | } | 
| 680 |  | /* check for no-op */ | 
| 681 | < | if (!transpose & (i_header == o_header) && | 
| 681 | > | if (!transpose & (outLevels <= 1) & (i_header == o_header) && | 
| 682 |  | (no_columns == ni_columns) & (no_rows == ni_rows)) { | 
| 683 |  | if (warnings) | 
| 684 |  | fprintf(stderr, "%s: no-op -- copying input verbatim\n", | 
| 710 |  | fputformat(fmtid, stdout); | 
| 711 |  | fputc('\n', stdout);            /* finish new header */ | 
| 712 |  | } | 
| 713 | < | if (transpose) {                        /* transposing rows & columns? */ | 
| 713 | > | if (transpose | (outLevels > 1)) {      /* moving stuff around? */ | 
| 714 |  | MEMLOAD myMem;                  /* need to map into memory */ | 
| 715 |  | if (a == argc-1) { | 
| 716 |  | if (load_file(&myMem, stdin) <= 0) { | 
| 723 |  | argv[0]); | 
| 724 |  | return(1); | 
| 725 |  | } | 
| 726 | < | if (!do_transpose(&myMem)) | 
| 726 | > | if (!do_reorder(&myMem)) | 
| 727 |  | return(1); | 
| 728 |  | /* free_load(&myMem);   about to exit, so don't bother */ | 
| 729 |  | } else if (!do_resize(stdin))           /* reshaping input */ | 
| 731 |  | return(0); | 
| 732 |  | userr: | 
| 733 |  | fprintf(stderr, | 
| 734 | < | "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row] [input.dat]\n", | 
| 734 | > | "Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", | 
| 735 |  | argv[0]); | 
| 736 |  | return(1); | 
| 737 |  | } |