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