1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
|
/* This program, double, is a filter for converting, scaling, joining, and
cutting data sets that are in the format accepted by the `spline' and
`graph' utilities. The data sets may be in binary (either double or
single precision floating point format, or integer format), or in ascii.
Copyright (C) 1989-1999 Free Software Foundation, Inc.
The `I', `O', and `q' options (i.e. `--input-type', `--output-type', and
`--precision') are similar to those accepted by `spline' and `graph'.
For example, `-I a' specifies that the input is in ascii format, and
`-O i' that the output should be in binary integer format.
The length of each record in the input is specified with the `-R' option.
For example, `-R 2' would be appropriate if the input consists of
pairs of numbers (x,y). The default record length is 1.
By default, `double' copies all fields of each input record to a
corresponding output record. Since `-R 1' is the default, without
additional options it will simply copy an input stream of numbers to an
output stream. You can use the `-f' option to specify which fields you
want copied. For example, `-R 3 -f 2 0' would interpret the input as
being made of size-3 records, and would produce output consisting of
size-2 records, each of which would be made from field #2 and field #0
of the corresponding input record. (Fields are numbered starting with
zero.) In the `-f' specification, fields may be repeated.
You can use the `-t' option to multiply all the input fields by a
constant, and `-p' to add a constant to each of the input fields.
`double' can also join streams of records together. The `-j' and `-J'
options are used to specify the names of files consisting of size-1
records, called a `pre-file' and a `post-file'. If you use `-j
filename' in the above example, the output records will be of size 3
rather than size 2, and the first field of each output record will be
taken from the corresponding record in `filename'. So the `-j' option
`prepends' a component to each record. Similarly, `-J' will append a
component. You can also use `-T' and `-P' to specify a `times file'
and a `plus file', which will respectively multiply each record
by a number taken from the times file, and add to the components of
each record a number taken from the plus file.
The `-d' (--dataset-limits) option takes three args: min, max, and
spacing. It specifies a linear progression through each dataset
that is processed, and is useful for `thinning out' large datasets.
`double' does not require that its input file(s) consist of only a
single dataset. However, the lengths of the corresponding datasets
should be equal. */
#include "sys-defines.h"
#include "getopt.h"
/* type of data in input and output streams */
typedef enum
{
T_ASCII, T_SINGLE, T_DOUBLE, T_INTEGER
}
data_type;
data_type input_type = T_ASCII;
data_type output_type = T_ASCII;
const char *progname = "double"; /* name of this program */
const char *usage_appendage = " [FILE]...\n\
With no FILE, or when FILE is -, read standard input.\n";
int precision = 8; /* default no. of digits after decimal pt. */
#define ARG_NONE 0
#define ARG_REQUIRED 1
#define ARG_OPTIONAL 2
struct option long_options[] =
{
/* string arg */
{"input-type", ARG_REQUIRED, NULL, 'I'},
{"output-type", ARG_REQUIRED, NULL, 'O'},
{"precision", ARG_REQUIRED, NULL, 'q'},
/* file name arg */
{"times-file", ARG_REQUIRED, NULL, 'T'},
{"plus-file", ARG_REQUIRED, NULL, 'P'},
{"pre-join-file", ARG_REQUIRED, NULL, 'j'},
{"post-join-file", ARG_REQUIRED, NULL, 'J'},
/* floating point arg */
{"times", ARG_REQUIRED, NULL, 't'},
{"plus", ARG_REQUIRED, NULL, 'p'},
/* integer arg */
{"record-length", ARG_REQUIRED, NULL, 'R'},
{"fields", ARG_OPTIONAL, NULL, 'f'}, /* 0,1,2, or ... */
{"dataset-limits", ARG_OPTIONAL, NULL, 'd'}, /* 0,1,2,3 args*/
/* flags */
{"version", ARG_NONE, NULL, 'V' << 8},
{"help", ARG_NONE, NULL, 'h' << 8},
{NULL, 0, 0, 0}
};
/* null-terminated list of options that we don't show to the user */
int hidden_options[] = { 0 };
/* forward references */
bool mung_dataset ____P ((FILE *input, int record_length, int *field_array, int field_array_len, double scale, double baseline, FILE *add_fp, FILE *mult_fp, FILE *pre_join_fp, FILE *post_join_fp, int precision, bool suppress));
bool read_float ____P ((FILE *input, double *dptr));
bool skip_whitespace ____P ((FILE *stream));
bool write_float ____P ((double data, int precision));
int get_record ____P ((FILE *input, double *record, int record_length));
void maybe_emit_oob_warning ____P ((void));
void open_file ____P ((char *name, FILE **fpp));
void output_dataset_separator ____P ((void));
void set_format_type ____P ((char *s, data_type *typep));
/* from libcommon */
extern void display_usage ____P((const char *progname, const int *omit_vals, const char *appendage, bool fonts));
extern void display_version ____P((const char *progname));
extern voidptr_t xcalloc ____P ((size_t nmemb, size_t size));
extern voidptr_t xmalloc ____P ((size_t length));
extern voidptr_t xrealloc ____P ((voidptr_t p, size_t length));
extern char *xstrdup ____P ((const char *s));
int
#ifdef _HAVE_PROTOS
main (int argc, char **argv)
#else
main (argc, argv)
int argc;
char **argv;
#endif
{
int option; /* for option parsing */
int opt_index;
int errcnt = 0; /* errors encountered in parsing */
int i;
bool show_version = false; /* remember to show version message */
bool show_usage = false; /* remember to output usage message */
char *add_file = NULL, *mult_file = NULL;
char *pre_join_file = NULL, *post_join_file = NULL;
FILE *add_fp = NULL, *mult_fp = NULL;
FILE *pre_join_fp = NULL, *post_join_fp = NULL;
double scale = 1.0, baseline = 0.0; /* mult., additive constants */
int record_length = 1; /* default record length */
int dataset_min = 0, dataset_max = INT_MAX, dataset_spacing = 1;
int local_dataset_min, local_dataset_max, local_dataset_spacing;
int *field_array = NULL; /* array of indices we'll extract */
int field_array_len = 0; /* initial size of field_array[] */
int dataset_index = 0; /* running count */
bool more_points, dataset_printed = false;
for ( ; ; )
{
option = getopt_long (argc, argv, "I:O:q:T:P:j:J:t:p:R:f::d::", long_options, &opt_index);
if (option == 0)
option = long_options[opt_index].val;
switch (option)
{
/* ----------- options with no argument --------------*/
case 'V' << 8: /* display version */
show_version = true;
break;
case 'h' << 8: /* help */
show_usage = true;
break;
/* ----------- options with a single argument --------------*/
case 'I':
set_format_type (optarg, &input_type);
break;
case 'O':
set_format_type (optarg, &output_type);
break;
case 'T': /* Times file name, ARG REQUIRED */
mult_file = xstrdup (optarg);
break;
case 'P': /* Plus file name, ARG REQUIRED */
add_file = xstrdup (optarg);
break;
case 'j': /* Pre-join file name, ARG REQUIRED */
pre_join_file = xstrdup (optarg);
break;
case 'J': /* Post-join file name, ARG REQUIRED */
post_join_file = xstrdup (optarg);
break;
case 't': /* Times (mult. constant), ARG REQUIRED */
if (sscanf (optarg, "%lf", &scale) <= 0)
{
fprintf (stderr,
"%s: error: bad multiplicative constant `%s'\n",
progname, optarg);
return EXIT_FAILURE;
}
break;
case 'p': /* Plus (add. constant), ARG REQUIRED */
if (sscanf (optarg, "%lf", &baseline) <= 0)
{
fprintf (stderr,
"%s: error: bad additive constant `%s'\n",
progname, optarg);
return EXIT_FAILURE;
}
break;
case 'q': /* Precision, ARG REQUIRED */
if ((sscanf (optarg, "%d", &precision) <= 0)
|| (precision < 1))
{
fprintf (stderr,
"%s: error: bad precision `%s', must be integer >= 1\n",
progname, optarg);
return EXIT_FAILURE;
}
break;
case 'R': /* Number of data per record, ARG REQUIRED */
if ((sscanf (optarg, "%d", &record_length) <= 0)
|| (record_length < 1))
{
fprintf (stderr,
"%s: error: bad record length `%s', must be integer >= 1\n",
progname, optarg);
return EXIT_FAILURE;
}
break;
/* ----- Options with a variable number of arguments ----- */
case 'd': /* Dataset limits, ARG OPTIONAL [0,1,2,3] */
if (optind >= argc)
break;
if (sscanf (argv[optind], "%d", &local_dataset_min) <= 0)
break;
dataset_min = local_dataset_min;
optind++; /* tell getopt we recognized dataset_min */
if (optind >= argc)
break;
if (sscanf (argv [optind], "%d", &local_dataset_max) <= 0)
break;
dataset_max = local_dataset_max;
optind++; /* tell getopt we recognized dataset_max */
if (optind >= argc)
break;
if (sscanf (argv [optind], "%d", &local_dataset_spacing) <= 0)
break;
dataset_spacing = local_dataset_spacing;
optind++; /* tell getopt we recognized dataset_spacing */
break;
case 'f':
for ( ; ; )
{
int field_index;
if (optind >= argc)
break;
if (sscanf (argv[optind], "%d", &field_index) <= 0)
break;
if (field_index < 0)
{
fprintf (stderr, "%s: error: bad field index `%d', must be >= 0\n",
progname, field_index);
return EXIT_FAILURE;
}
if (field_array_len == 0)
field_array =
(int *)xmalloc ((++field_array_len) * sizeof(int));
else
field_array =
(int *)xrealloc (field_array,
(++field_array_len) * sizeof(int));
field_array[field_array_len - 1] = field_index;
optind++; /* tell getopt we recognized field index */
}
break;
/*---------------- End of options ----------------*/
default: /* Default, unknown option */
errcnt++;
break;
} /* endswitch */
if ((option == EOF))
{
errcnt--;
break; /* break out of option processing */
}
}
/* endwhile */
if (errcnt > 0)
{
fprintf (stderr, "Try `%s --help' for more information\n", progname);
return EXIT_FAILURE;
}
if (show_version)
{
display_version (progname);
return EXIT_SUCCESS;
}
if (show_usage)
{
display_usage (progname, hidden_options, usage_appendage, false);
return EXIT_SUCCESS;
}
/* Sanity checks on user-supplied options */
if (dataset_spacing < 1)
{
fprintf (stderr, "%s: error: bad dataset spacing `%d' (must be > 0)\n",
progname, dataset_spacing);
return EXIT_FAILURE;
}
for (i = 0; i < field_array_len; i++)
if (field_array[i] >= record_length)
{
fprintf (stderr,
"%s: error: at least one field index out of bounds\n", progname);
return EXIT_FAILURE;
}
/* default if no `-R' option seen: extract all fields of each record */
if (field_array_len == 0)
{
field_array =
(int *)xmalloc ((record_length) * sizeof(int));
field_array_len = record_length;
for (i = 0; i < field_array_len; i++)
field_array[i] = i;
}
/* open additive/multiplicative/join files. */
if (add_file)
open_file (add_file, &add_fp);
if (mult_file)
open_file (mult_file, &mult_fp);
if (pre_join_file)
open_file (pre_join_file, &pre_join_fp);
if (post_join_file)
open_file (post_join_file, &post_join_fp);
if (optind < argc)
{
/* call mung_dataset() on all datasets contained in
each file specified on command line */
for (; optind < argc; optind++)
{
FILE *data_fp;
/* open file, treat "-" as stdin */
if (strcmp (argv[optind], "-") == 0)
data_fp = stdin;
else
open_file (argv[optind], &data_fp);
/* loop through datasets in file (may be more than one) */
do
{
bool dataset_ok;
dataset_ok = ((dataset_index >= dataset_min)
&& (dataset_index <= dataset_max)
&& ((dataset_index - dataset_min)
% dataset_spacing == 0)) ? true : false;
/* output a separator between successive datasets */
if (dataset_printed && dataset_ok)
output_dataset_separator();
more_points = mung_dataset (data_fp,
record_length,
field_array, field_array_len,
scale, baseline,
add_fp, mult_fp,
pre_join_fp, post_join_fp,
precision, dataset_ok ? false : true);
if (dataset_ok)
dataset_printed = true;
dataset_index++;
}
while (more_points);
/* close file (but don't close stdin) */
if (data_fp != stdin && fclose (data_fp) < 0)
{
fprintf (stderr, "%s: error: couldn't close input file\n",
progname);
return EXIT_FAILURE;
}
}
}
else /* no files spec'd, read stdin instead */
/* loop through datasets (may be more than one) */
do
{
bool dataset_ok;
dataset_ok = ((dataset_index >= dataset_min)
&& (dataset_index <= dataset_max)
&& ((dataset_index - dataset_min)
% dataset_spacing == 0)) ? true : false;
/* output a separator between successive datasets */
if (dataset_printed && dataset_ok)
output_dataset_separator();
more_points = mung_dataset (stdin,
record_length,
field_array, field_array_len,
scale, baseline,
add_fp, mult_fp,
pre_join_fp, post_join_fp,
precision, dataset_ok ? false : true);
if (dataset_ok)
dataset_printed = true;
dataset_index++;
}
while (more_points); /* keep going if no EOF yet */
return EXIT_SUCCESS;
}
/* read_float reads a single floating point quantity from an input file
(in either ascii or double format). Return value indicates whether it
was read successfully. */
bool
#ifdef _HAVE_PROTOS
read_float (FILE *input, double *dptr)
#else
read_float (input, dptr)
FILE *input;
double *dptr;
#endif
{
int num_read;
double dval;
float fval;
int ival;
switch (input_type)
{
case T_ASCII:
default:
num_read = fscanf (input, "%lf", &dval);
break;
case T_SINGLE:
num_read = fread ((voidptr_t) &fval, sizeof (fval), 1, input);
dval = fval;
break;
case T_DOUBLE:
num_read = fread ((voidptr_t) &dval, sizeof (dval), 1, input);
break;
case T_INTEGER:
num_read = fread ((voidptr_t) &ival, sizeof (ival), 1, input);
dval = ival;
break;
}
if (num_read <= 0)
return false;
if (dval != dval)
{
fprintf (stderr, "%s: encountered a NaN (not-a-number) in binary input file\n",
progname);
return false;
}
else
{
*dptr = dval;
return true;
}
}
/* get_record() attempts to read a record (a sequence of record_length
data, i.e., floating-point quantities) from an input file. Return
value is 0 if a record was successfully read, 1 if no record could be
read (i.e. EOF or garbage in stream). A return value of 2 is special:
it indicates that an explicit end-of-dataset indicator was seen in the
input file. For an ascii stream this is two newlines in succession;
for a stream of doubles it is a DBL_MAX appearing at what would
otherwise have been the beginning of the record, etc. */
int
#ifdef _HAVE_PROTOS
get_record (FILE *input, double *record, int record_length)
#else
get_record (input, record, record_length)
FILE *input;
double *record;
int record_length;
#endif
{
bool success;
int i, items_read, lookahead;
head:
if (input_type == T_ASCII)
{
bool two_newlines;
/* skip whitespace, up to but not including 2nd newline */
two_newlines = skip_whitespace (input);
if (two_newlines)
/* end-of-dataset indicator */
return 2;
}
if (feof (input))
return 1; /* EOF */
if (input_type == T_ASCII)
{
lookahead = getc (input);
ungetc (lookahead, input);
if (lookahead == (int)'#') /* comment line */
{
char c;
do
{
items_read = fread (&c, sizeof (c), 1, input);
if (items_read <= 0)
return 1; /* EOF */
}
while (c != '\n');
ungetc ((int)'\n', input); /* push back \n at the end of # line */
goto head;
}
}
for (i = 0; i < record_length; i++)
{
double val;
success = read_float (input, &val);
if (i == 0 &&
((input_type == T_DOUBLE && val == DBL_MAX)
|| (input_type == T_SINGLE && val == (double)FLT_MAX)
|| (input_type == T_INTEGER && val == (double)INT_MAX)))
/* end-of-dataset indicator */
return 2;
if (!success) /* EOF or garbage */
{
if (i > 0)
fprintf (stderr, "%s: input file terminated prematurely\n",
progname);
return 1;
}
record[i] = val;
}
return 0;
}
/* Emit a double, in specified output representation. Be sure to inform
user if any of the emitted values were out-of-bounds for
single-precision or integer format. */
bool
#ifdef _HAVE_PROTOS
write_float (double x, int precision)
#else
write_float (x, precision)
double x;
int precision;
#endif
{
int num_written = 0;
float fx;
int ix;
switch (output_type)
{
case T_ASCII:
default:
num_written = printf ("%.*g ", precision, x);
break;
case T_SINGLE:
fx = FROUND(x);
if (fx == FLT_MAX || fx == -(FLT_MAX))
{
maybe_emit_oob_warning();
if (fx == FLT_MAX)
fx *= 0.99999; /* kludge */
}
num_written = fwrite ((voidptr_t) &fx, sizeof (fx), 1, stdout);
break;
case T_DOUBLE:
num_written = fwrite ((voidptr_t) &x, sizeof (x), 1, stdout);
break;
case T_INTEGER:
ix = IROUND(x);
if (ix == INT_MAX || ix == -(INT_MAX))
{
maybe_emit_oob_warning();
if (ix == INT_MAX)
ix--;
}
num_written = fwrite ((voidptr_t) &ix, sizeof (ix), 1, stdout);
break;
}
if (num_written < 0)
return false;
else
return true;
}
void
#ifdef _HAVE_PROTOS
open_file (char *name, FILE **fpp)
#else
open_file (name, fpp)
char *name;
FILE **fpp;
#endif
{
FILE *fp;
fp = fopen (name, "r");
if (fp == NULL)
{
fprintf (stderr, "%s: %s: %s\n", progname, name, strerror(errno));
exit (EXIT_FAILURE);
}
*fpp = fp;
}
void
#ifdef _HAVE_PROTOS
set_format_type (char *s, data_type *typep)
#else
set_format_type (s, typep)
char *s;
data_type *typep;
#endif
{
switch (s[0])
{
case 'a':
case 'A':
/* ASCII format: records and fields within records are separated by
whitespace, and datasets are separated by a pair of newlines. */
*typep = T_ASCII;
break;
case 'f':
case 'F':
/* Binary single precision: records and fields within records are
contiguous, and datasets are separated by a FLT_MAX. */
*typep = T_SINGLE;
break;
case 'd':
case 'D':
/* Binary double precision: records and fields within records are
contiguous, and datasets are separated by a DBL_MAX. */
*typep = T_DOUBLE;
break;
case 'i':
case 'I':
/* Binary integer: records and fields within records are contiguous,
and datasets are separated by an occurrence of INT_MAX. */
*typep = T_INTEGER;
break;
default:
{
fprintf (stderr, "%s: error: invalid data format type `%s'\n",
progname, s);
exit (EXIT_FAILURE);
}
break;
}
}
/* mung_dataset() is the main routine for extracting fields from records in
a dataset, and munging them. Its return value indicates whether the
records in the input file ended with an explicit end-of-dataset
indicator, i.e., whether another dataset is expected to follow. An
end-of-dataset indicator is two newlines in succession for an ascii
stream, and a DBL_MAX for a stream of doubles, etc. */
bool
#ifdef _HAVE_PROTOS
mung_dataset (FILE *input, int record_length,
int *field_array, int field_array_len,
double scale, double baseline, FILE *add_fp, FILE *mult_fp,
FILE *pre_join_fp, FILE *post_join_fp, int precision,
bool suppress)
#else
mung_dataset (input, record_length, field_array, field_array_len, scale, baseline, add_fp, mult_fp, pre_join_fp, post_join_fp, precision, suppress)
FILE *input;
int record_length; /* number of fields per record in dataset */
int *field_array; /* array of fields we'll extract */
int field_array_len; /* length of this array */
double scale, baseline;
FILE *add_fp, *mult_fp, *pre_join_fp, *post_join_fp;
int precision;
bool suppress; /* suppress output for this dataset */
#endif
{
double *record = (double *)xmalloc (record_length * sizeof(double));
bool in_trouble = suppress; /* once in trouble, we never get out */
if (!in_trouble)
{
/* rewind all fp's */
if (add_fp)
fseek(add_fp, 0L, 0);
if (mult_fp)
fseek(mult_fp, 0L, 0);
if (pre_join_fp)
fseek(pre_join_fp, 0L, 0);
if (post_join_fp)
fseek(post_join_fp, 0L, 0);
}
for ( ; ; )
{
int i;
int success;
double add_data, mult_data, pre_join_data, post_join_data;
if (!in_trouble && add_fp && read_float (add_fp, &add_data) == false)
in_trouble = true;
if (!in_trouble && mult_fp && read_float (mult_fp, &mult_data) == false)
in_trouble = true;
if (!in_trouble && pre_join_fp
&& read_float (pre_join_fp, &pre_join_data) == false)
in_trouble = true;
if (!in_trouble && post_join_fp
&& read_float (post_join_fp, &post_join_data) == false)
in_trouble = true;
success = get_record (input, record, record_length);
switch (success)
{
case 0: /* good record */
if (in_trouble) /* if in trouble, do nought till dataset end */
continue;
if (pre_join_fp)
write_float (pre_join_data, precision);
for (i = 0; i < field_array_len; i++)
{
double datum;
datum = record[field_array[i]];
if (mult_fp)
datum *= mult_data;
if (add_fp)
datum += add_data;
datum *= scale;
datum += baseline;
/* output the munged datum */
write_float (datum, precision);
}
if (post_join_fp)
write_float (post_join_data, precision);
if (output_type == T_ASCII) /* end each record with a newline */
printf ("\n");
break;
case 1: /* no more records, EOF seen */
return false;
case 2: /* end of dataset, but input continues */
return true;
}
}
}
/* skip_whitespace() skips whitespace in an ascii-format input file,
up to but not including a second newline. Return value indicates
whether or not two newlines were in fact seen. (For ascii-format
input files, two newlines signals an end-of-dataset.) */
bool
#ifdef _HAVE_PROTOS
skip_whitespace (FILE *stream)
#else
skip_whitespace (stream)
FILE *stream;
#endif
{
int lookahead;
int nlcount = 0;
do
{
lookahead = getc (stream);
if (lookahead == (int)'\n')
nlcount++;
}
while (lookahead != EOF
&& isspace((unsigned char)lookahead)
&& nlcount < 2);
if (lookahead == EOF)
return false;
ungetc (lookahead, stream);
return (nlcount == 2 ? true : false);
}
/* Output a separator between datasets. For ascii-format output streams
this is an extra newline (after the one that the spline ended with,
yielding two newlines in succession). For double-format output streams
this is a DBL_MAX, etc. */
void
#ifdef _HAVE_PROTOS
output_dataset_separator(void)
#else
output_dataset_separator()
#endif
{
double ddummy;
float fdummy;
int idummy;
switch (output_type)
{
case T_ASCII:
default:
printf ("\n");
break;
case T_DOUBLE:
ddummy = DBL_MAX;
fwrite ((voidptr_t) &ddummy, sizeof(ddummy), 1, stdout);
break;
case T_SINGLE:
fdummy = FLT_MAX;
fwrite ((voidptr_t) &fdummy, sizeof(fdummy), 1, stdout);
break;
case T_INTEGER:
idummy = INT_MAX;
fwrite ((voidptr_t) &idummy, sizeof(idummy), 1, stdout);
break;
}
}
void
#ifdef _HAVE_PROTOS
maybe_emit_oob_warning (void)
#else
maybe_emit_oob_warning ()
#endif
{
static bool warning_written = false;
if (!warning_written)
{
fprintf (stderr, "%s: approximating one or more out-of-bounds output values\n", progname);
warning_written = true;
}
}
|