double.c 23 KB

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