reader.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1989,
  2. 1990, 1991, 1995, 1996, 1997, 1998, 1999, 2000, 2005, 2008, 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 file is the point-reader half of GNU graph. Included here are
  17. routines that will read data points from an input stream. The input
  18. file may be in ascii format (a sequence of floating-point numbers,
  19. separated by whitespace), or in binary format (e.g., a sequence of
  20. doubles). Gnuplot table format is also supported.
  21. A `point' is a structure. Each point structure contains the following
  22. fields:
  23. x and y coordinates of the point
  24. a `have_x_errorbar' flag (true or false)
  25. a `have_y_errorbar' flag (true or false)
  26. xmin and xmax (meaningful only if have_x_errorbar is set)
  27. ymin and ymax (meaningful only if have_y_errorbar is set)
  28. a `pendown' flag
  29. a symbol type (a small integer, interpreted as a marker type)
  30. a symbol size (a fraction of the size of the plotting area)
  31. a symbol font name (relevant only for symbol types >= 32, see plotter.c)
  32. a linemode (a small integer)
  33. a linewidth (a fraction of the size of the display device)
  34. a polyline fill-fraction (in the interval [0,1], <0 means no fill)
  35. a use_color flag (true or false)
  36. An array of points defines a polyline, or a sequence of polylines.
  37. pendown=true means that a polyline is being drawn; pendown=false means
  38. that a polyline has just ended, and that the point (x,y), which begins a
  39. new polyline, should be moved to without drawing a line segment. By
  40. convention, the final seven fields, and have_?_errorbar, are the same
  41. for each point in a polyline. We use the term `dataset' to refer to the
  42. sequence of points in an input file that gives rise to a single
  43. polyline.
  44. If the input stream is in ascii format, two \n's in succession serves as
  45. a separator between datasets. If, instead, the input stream is in
  46. binary format (e.g., a sequence of doubles), then a single infinite
  47. quantity, such as DBL_MAX, serves as a dataset separator. If the input
  48. stream is in gnuplot `table' format, then two \n's in succession serves
  49. as a separator. But there are always two \n's before EOF in gnuplot
  50. format; this is different from ascii format.
  51. This file exports four functions (point-reader methods, basically).
  52. They are declared in extern.h. They are:
  53. new_reader ()
  54. read_file ()
  55. alter_reader_parameters ()
  56. delete_reader ()
  57. Before any points are read, the point-reader must first be created and
  58. initialized by a call to the constructor new_reader(). It takes as
  59. arguments certain reader parameters: e.g., the initial linemode and
  60. symbol type, the input stream, and the format of the input stream.
  61. read_file() reads datasets in succession from the input stream, until
  62. EOF is reached. After each dataset, besides breaking the polyline, it
  63. increments the linemode by unity (provided the reader's `autobump' flag
  64. is set). After finishing with an input stream, and before switching to
  65. another input stream as a source of points, alter_reader_parameters()
  66. should be called. It allows parameters of the reader that may differ
  67. from stream to stream to be changed.
  68. Directives in the input stream, specifying a change of linemode / symbol
  69. type, are supported. Any such directive automatically terminates a
  70. dataset and begins a new one. This is in agreement with the convention
  71. that every point in a polyline has the same linemode and the same
  72. plotting symbol. During the reading of an ascii-format input stream, a
  73. string of the form "#m=%d,S=%d" will be interpreted as a directive to
  74. change to a specified linemode / symbol type. Here the two %d's are the
  75. new linemode and symbol type, respectively. There is currently no way
  76. of changing to a specific linemode / symbol type during the reading of a
  77. input stream that is in binary format.
  78. The function `read_and_plot_file' is also exported. It is the same as
  79. `read_file', but it uses the plot_point() method of a Multigrapher (see
  80. plotter.c) to plot each point as it is read. */
  81. #include "sys-defines.h"
  82. #include "libcommon.h"
  83. #include "extern.h"
  84. /* New (larger) length of a Point array, as function of the old; used when
  85. reallocating due to exhaustion of storage. */
  86. #define NEW_POINT_ARRAY_LENGTH(old_len) \
  87. ((old_len)*sizeof(Point) < 10000000 ? 2 * (old_len) : (old_len) + 10000000/sizeof(Point))
  88. struct ReaderStruct /* point reader datatype */
  89. {
  90. /* parameters which are constant over the lifetime of a Reader, and which
  91. affect the computation of the returned point structures */
  92. bool transpose_axes; /* x <-> y ? */
  93. int log_axis; /* x,y axes are logarithmic? (portmanteau) */
  94. /* Reader parameters that are constant for the duration of each file */
  95. FILE *input; /* input stream */
  96. data_type format_type; /* stream format (T_ASCII, T_DOUBLE, etc.) */
  97. bool auto_abscissa; /* auto-generate x values? */
  98. double delta_x; /* increment for x value, if auto-generated */
  99. double initial_abscissa; /* initial value for x, if auto-generated */
  100. bool auto_bump; /* bump linemode when starting next polyline?*/
  101. /* Reader parameters that are constant for the duration of each dataset */
  102. int symbol; /* symbol type */
  103. double symbol_size; /* symbol size (in `box coordinates') */
  104. const char *symbol_font_name; /* font used for marker types >= 32 */
  105. const char *line_color; /* explicitly replace color in linemode */
  106. int linemode; /* linemode */
  107. double line_width; /* line width (as frac. of display size) */
  108. double fill_fraction; /* in interval [0,1], <0 means no filling */
  109. bool use_color; /* color/monochrome interp. of linemode */
  110. /* state variables, updated during Reader operation */
  111. bool need_break; /* draw next point with pen up ? */
  112. double abscissa; /* x value, if auto-generated */
  113. };
  114. /* Internal status codes: return values for read_dataset() and
  115. read_and_plot_dataset(), and also for the lower-level functions
  116. read_point() and read_point_{ascii,ascii_errorbar,binary,gnuplot}().
  117. IN_PROGRESS means the dataset being read is still in progress.
  118. ENDED_BY_EOF means it terminated with an EOF, and
  119. ENDED_BY_DATASET_TERMINATOR means it terminated with an explicit
  120. end-of-dataset marker. An end-of-dataset marker is two newlines in
  121. succession for an ascii stream, and a DBL_MAX for a stream of doubles.
  122. ENDED_BY_MODE_CHANGE signals that a `set linemode / symbol type'
  123. directive was seen. By convention, we interpret such a directive as
  124. ending a dataset. */
  125. typedef enum { IN_PROGRESS, ENDED_BY_EOF, ENDED_BY_DATASET_TERMINATOR, ENDED_BY_MODE_CHANGE } dataset_status_t;
  126. /* forward references */
  127. static bool skip_some_whitespace (FILE *stream);
  128. static dataset_status_t read_and_plot_dataset (Reader *reader, Multigrapher *multigrapher, int *no_of_points, Point * point);
  129. static dataset_status_t read_dataset (Reader *reader, Point **p, int *length, int *no_of_points);
  130. static dataset_status_t read_point (Reader *reader, Point *point);
  131. static dataset_status_t read_point_ascii (Reader *reader, Point *point);
  132. static dataset_status_t read_point_ascii_errorbar (Reader *reader, Point *point);
  133. static dataset_status_t read_point_binary (Reader *reader, Point *point);
  134. static dataset_status_t read_point_gnuplot (Reader *reader, Point *point);
  135. static void reset_reader (Reader *reader);
  136. static void skip_all_whitespace (FILE *stream);
  137. /* ARGS: format_type = double, or ascii, etc.
  138. symbol_size = symbol size for markers
  139. symbol_font_name = name for markers >= 32
  140. line_width = fraction of display size
  141. fill_fraction = number in range [0,1], <0 means unfilled (transparent)*/
  142. Reader *
  143. new_reader (FILE *input, data_type format_type, bool auto_abscissa, double delta_x, double abscissa, bool transpose_axes, int log_axis, bool auto_bump, int symbol, double symbol_size, const char *symbol_font_name, const char *line_color, int linemode, double line_width, double fill_fraction, bool use_color)
  144. {
  145. Reader *reader;
  146. reader = (Reader *)xmalloc (sizeof (Reader));
  147. reader->need_break = true; /* next point will have pen up */
  148. reader->input = input;
  149. reader->format_type = format_type;
  150. reader->auto_abscissa = auto_abscissa;
  151. reader->delta_x = delta_x;
  152. reader->initial_abscissa = abscissa;
  153. reader->abscissa = reader->initial_abscissa;
  154. reader->transpose_axes = transpose_axes;
  155. reader->log_axis = log_axis;
  156. reader->auto_bump = auto_bump;
  157. reader->symbol = symbol;
  158. reader->symbol_size = symbol_size;
  159. reader->symbol_font_name = symbol_font_name;
  160. reader->line_color = line_color;
  161. reader->linemode = linemode;
  162. reader->line_width = line_width;
  163. reader->fill_fraction = fill_fraction;
  164. reader->use_color = use_color;
  165. return reader;
  166. }
  167. void
  168. delete_reader (Reader *reader)
  169. {
  170. free (reader);
  171. return;
  172. }
  173. /* alter_reader_parameters() would be called just before reading datapoints
  174. from the second stream, the third stream,... It breaks the polyline
  175. under construction, if any. It also sets the stream and stream format,
  176. resets the abscissa (if auto-abscissa is in effect), and updates the
  177. linemode, symbol type, etc., if requested. (In GNU graph, we use the
  178. last feature to permit command-line specification of linemode/symbol
  179. type on a per-file basis.) */
  180. /* ARGS: note that the final new_* args make up a mask */
  181. void
  182. alter_reader_parameters (Reader *reader, FILE *input, data_type format_type, bool auto_abscissa, double delta_x, double abscissa, int symbol, double symbol_size, const char *symbol_font_name, const char *line_color, int linemode, double line_width, double fill_fraction, bool use_color, bool new_symbol, bool new_symbol_size, bool new_symbol_font_name, bool new_line_color, bool new_linemode, bool new_line_width, bool new_fill_fraction, bool new_use_color)
  183. {
  184. reader->need_break = true; /* force break in polyline */
  185. reader->input = input;
  186. reader->format_type = format_type;
  187. reader->auto_abscissa = auto_abscissa;
  188. reader->delta_x = delta_x;
  189. reader->initial_abscissa = abscissa;
  190. reader->abscissa = reader->initial_abscissa;
  191. /* test bits in mask to determine which polyline attributes need updating */
  192. if (new_symbol)
  193. reader->symbol = symbol;
  194. if (new_symbol_size)
  195. reader->symbol_size = symbol_size;
  196. if (new_symbol_font_name)
  197. reader->symbol_font_name = symbol_font_name;
  198. if (new_line_color)
  199. reader->line_color = line_color;
  200. if (new_linemode)
  201. reader->linemode = linemode;
  202. if (new_line_width)
  203. reader->line_width = line_width;
  204. if (new_fill_fraction)
  205. reader->fill_fraction = fill_fraction;
  206. if (new_use_color)
  207. reader->use_color = use_color;
  208. return;
  209. }
  210. /* read_point() calls read_point_ascii(), read_point_ascii_errorbar(),
  211. read_point_binary(), or read_point_gnuplot() to do the actual reading.
  212. It returns a status code (either IN_PROGRESS or ENDED_*, describing how
  213. the dataset in progress ended, if it did). */
  214. static dataset_status_t
  215. read_point (Reader *reader, Point *point)
  216. {
  217. dataset_status_t status;
  218. /* following fields are constant throughout each polyline */
  219. point->symbol = reader->symbol;
  220. point->symbol_size = reader->symbol_size;
  221. point->symbol_font_name = reader->symbol_font_name;
  222. point->line_color = reader->line_color;
  223. point->linemode = reader->linemode;
  224. point->line_width = reader->line_width;
  225. point->fill_fraction = reader->fill_fraction;
  226. point->use_color = reader->use_color;
  227. point->have_x_errorbar = false; /* not supported yet */
  228. point->have_y_errorbar = (reader->format_type == T_ASCII_ERRORBAR ? true : false);
  229. head:
  230. switch (reader->format_type)
  231. {
  232. case T_ASCII:
  233. default:
  234. status = read_point_ascii (reader, point);
  235. break;
  236. case T_SINGLE:
  237. case T_DOUBLE:
  238. case T_INTEGER:
  239. status = read_point_binary (reader, point);
  240. break;
  241. case T_ASCII_ERRORBAR:
  242. status = read_point_ascii_errorbar (reader, point);
  243. break;
  244. case T_GNUPLOT: /* gnuplot `table' format */
  245. status = read_point_gnuplot (reader, point);
  246. break;
  247. }
  248. if (status == IN_PROGRESS)
  249. /* got a point; if not, we just pass back the return code */
  250. {
  251. bool bad_point = false;
  252. /* If we have log axes, the values we work with ALL refer to the log10
  253. values of the data. A nonpositive value generates a warning, and a
  254. break in the polyline. */
  255. if (reader->log_axis & X_AXIS)
  256. {
  257. if (point->x > 0.0)
  258. point->x = log10 (point->x);
  259. else
  260. bad_point = true;
  261. if (point->have_x_errorbar)
  262. {
  263. if (point->xmin > 0.0)
  264. point->xmin = log10 (point->xmin);
  265. else
  266. bad_point = true;
  267. if (point->xmax > 0.0)
  268. point->xmax = log10 (point->xmax);
  269. else
  270. bad_point = true;
  271. }
  272. if (bad_point)
  273. {
  274. fprintf (stderr, "%s: the inappropriate point (%g,%g) is dropped, as this is a log plot\n",
  275. progname, point->x, point->y);
  276. reader->need_break = true;
  277. goto head; /* on to next point */
  278. }
  279. }
  280. if (reader->log_axis & Y_AXIS)
  281. {
  282. if (point->y > 0.0)
  283. point->y = log10 (point->y);
  284. else
  285. bad_point = true;
  286. if (point->have_y_errorbar)
  287. {
  288. if (point->ymin > 0.0)
  289. point->ymin = log10 (point->ymin);
  290. else
  291. bad_point = true;
  292. if (point->ymax > 0.0)
  293. point->ymax = log10 (point->ymax);
  294. else
  295. bad_point = true;
  296. }
  297. if (bad_point)
  298. {
  299. fprintf (stderr, "%s: the inappropriate point (%g,%g) is dropped, as this is a log plot\n",
  300. progname, point->x, point->y);
  301. reader->need_break = true;
  302. goto head; /* on to next point */
  303. }
  304. }
  305. if (isnan(point->x) || isnan(point->y))
  306. {
  307. reader->need_break = true;
  308. goto head; /* on to next point */
  309. }
  310. if (reader->transpose_axes)
  311. {
  312. double tmp;
  313. bool tmp_bool;
  314. tmp = point->x;
  315. point->x = point->y;
  316. point->y = tmp;
  317. tmp = point->xmin;
  318. point->xmin = point->ymin;
  319. point->ymin = tmp;
  320. tmp = point->xmax;
  321. point->xmax = point->ymax;
  322. point->ymax = tmp;
  323. tmp_bool = point->have_x_errorbar;
  324. point->have_x_errorbar = point->have_y_errorbar;
  325. point->have_y_errorbar = tmp_bool;
  326. }
  327. /* we have a point, but we may need to break the polyline before it */
  328. if (reader->need_break)
  329. point->pendown = false;
  330. else
  331. point->pendown = true;
  332. /* reset break-polyline flag */
  333. reader->need_break = false;
  334. }
  335. return status;
  336. }
  337. static dataset_status_t
  338. read_point_ascii (Reader *reader, Point *point)
  339. {
  340. int items_read, lookahead;
  341. bool two_newlines;
  342. FILE *input = reader->input;
  343. head:
  344. /* skip whitespace, up to but not including 2nd newline if any */
  345. two_newlines = skip_some_whitespace (input);
  346. if (two_newlines)
  347. return ENDED_BY_DATASET_TERMINATOR;
  348. if (feof (input))
  349. return ENDED_BY_EOF;
  350. /* process linemode / symbol type directive */
  351. lookahead = getc (input);
  352. ungetc (lookahead, input);
  353. if (lookahead == (int)'#')
  354. {
  355. int new_symbol, new_linemode;
  356. int items_read;
  357. items_read = fscanf (input,
  358. "# m = %d, S = %d", &new_linemode, &new_symbol);
  359. if (items_read == 2) /* insist on matching both */
  360. {
  361. reader->linemode = new_linemode;
  362. reader->symbol = new_symbol;
  363. return ENDED_BY_MODE_CHANGE;
  364. }
  365. else /* unknown comment line, ignore it */
  366. {
  367. char c;
  368. do
  369. {
  370. items_read = fread (&c, sizeof (c), 1, input);
  371. if (items_read <= 0)
  372. return ENDED_BY_EOF;
  373. }
  374. while (c != '\n');
  375. ungetc ((int)'\n', input); /* push back \n at the end of # line */
  376. goto head;
  377. }
  378. }
  379. /* read coordinate(s) */
  380. if (reader->auto_abscissa)
  381. {
  382. point->x = reader->abscissa;
  383. reader->abscissa += reader->delta_x;
  384. }
  385. else
  386. {
  387. items_read = fscanf (input, "%lf", &(point->x));
  388. if (items_read != 1)
  389. return ENDED_BY_EOF; /* presumably */
  390. }
  391. items_read = fscanf (input, "%lf", &(point->y));
  392. if (items_read == 1)
  393. return IN_PROGRESS; /* got a pair of floats */
  394. else
  395. {
  396. if (!reader->auto_abscissa)
  397. fprintf (stderr, "%s: an input file terminated prematurely\n", progname);
  398. return ENDED_BY_EOF; /* couldn't get y coor, effectively EOF */
  399. }
  400. }
  401. static dataset_status_t
  402. read_point_ascii_errorbar (Reader *reader, Point *point)
  403. {
  404. int items_read, lookahead;
  405. bool two_newlines;
  406. double error_size;
  407. FILE *input = reader->input;
  408. head:
  409. /* skip whitespace, up to but not including 2nd newline if any */
  410. two_newlines = skip_some_whitespace (input);
  411. if (two_newlines)
  412. return ENDED_BY_DATASET_TERMINATOR;
  413. if (feof (input))
  414. return ENDED_BY_EOF;
  415. /* process linemode / symbol type directive */
  416. lookahead = getc (input);
  417. ungetc (lookahead, input);
  418. if (lookahead == (int)'#')
  419. {
  420. int new_symbol, new_linemode;
  421. int items_read;
  422. items_read = fscanf (input,
  423. "# m = %d, S = %d", &new_linemode, &new_symbol);
  424. if (items_read == 2) /* insist on matching both */
  425. {
  426. reader->linemode = new_linemode;
  427. reader->symbol = new_symbol;
  428. return ENDED_BY_MODE_CHANGE;
  429. }
  430. else /* unknown comment line, ignore it */
  431. {
  432. char c;
  433. do
  434. {
  435. items_read = fread (&c, sizeof (c), 1, input);
  436. if (items_read <= 0)
  437. return ENDED_BY_EOF;
  438. }
  439. while (c != '\n');
  440. ungetc ((int)'\n', input); /* push back \n at the end of # line */
  441. goto head;
  442. }
  443. }
  444. /* read coordinate(s) */
  445. if (reader->auto_abscissa)
  446. {
  447. point->x = reader->abscissa;
  448. reader->abscissa += reader->delta_x;
  449. }
  450. else
  451. {
  452. items_read = fscanf (input, "%lf", &(point->x));
  453. if (items_read != 1)
  454. return ENDED_BY_EOF; /* presumably */
  455. }
  456. items_read = fscanf (input, "%lf", &(point->y));
  457. if (items_read != 1)
  458. {
  459. if (!reader->auto_abscissa)
  460. fprintf (stderr, "%s: an input file (in errorbar format) terminated prematurely\n", progname);
  461. return ENDED_BY_EOF; /* couldn't get y coor, effectively EOF */
  462. }
  463. items_read = fscanf (input, "%lf", &error_size);
  464. if (items_read != 1)
  465. {
  466. fprintf (stderr, "%s: an input file (in errorbar format) terminated prematurely\n", progname);
  467. return ENDED_BY_EOF; /* couldn't get y coor, effectively EOF */
  468. }
  469. point->ymin = point->y - error_size;
  470. point->ymax = point->y + error_size;
  471. /* don't support reading of x errorbars yet */
  472. point->xmin = 0.0;
  473. point->xmax = 0.0;
  474. return IN_PROGRESS;
  475. }
  476. static dataset_status_t
  477. read_point_binary (Reader *reader, Point *point)
  478. {
  479. int items_read;
  480. data_type format_type = reader->format_type;
  481. FILE *input = reader->input;
  482. /* read coordinate(s) */
  483. if (reader->auto_abscissa)
  484. {
  485. point->x = reader->abscissa;
  486. reader->abscissa += reader->delta_x;
  487. }
  488. else
  489. {
  490. switch (format_type)
  491. {
  492. case T_DOUBLE:
  493. default:
  494. items_read =
  495. fread ((void *) &(point->x), sizeof (double), 1, input);
  496. break;
  497. case T_SINGLE:
  498. {
  499. float fx;
  500. items_read =
  501. fread ((void *) &fx, sizeof (fx), 1, input);
  502. point->x = fx;
  503. }
  504. break;
  505. case T_INTEGER:
  506. {
  507. int ix;
  508. items_read =
  509. fread ((void *) &ix, sizeof (ix), 1, input);
  510. point->x = ix;
  511. }
  512. break;
  513. }
  514. if (items_read <= 0)
  515. return ENDED_BY_EOF; /* presumably */
  516. }
  517. if ((format_type == T_DOUBLE && point->x == DBL_MAX)
  518. || (format_type == T_SINGLE && point->x == (double)FLT_MAX)
  519. || (format_type == T_INTEGER && point->x == (double)INT_MAX))
  520. return ENDED_BY_DATASET_TERMINATOR;
  521. switch (format_type)
  522. {
  523. case T_DOUBLE:
  524. default:
  525. items_read =
  526. fread ((void *) &(point->y), sizeof (double), 1, input);
  527. break;
  528. case T_SINGLE:
  529. {
  530. float fy;
  531. items_read =
  532. fread ((void *) &fy, sizeof (fy), 1, input);
  533. point->y = fy;
  534. }
  535. break;
  536. case T_INTEGER:
  537. {
  538. int iy;
  539. items_read =
  540. fread ((void *) &iy, sizeof (iy), 1, input);
  541. point->y = iy;
  542. }
  543. break;
  544. }
  545. if (items_read != 1) /* didn't get a pair of floats */
  546. {
  547. if (!reader->auto_abscissa)
  548. fprintf (stderr, "%s: an input file (in binary format) terminated prematurely\n", progname);
  549. return ENDED_BY_EOF; /* effectively */
  550. }
  551. else if (point->x != point->x || point->y != point->y)
  552. {
  553. fprintf (stderr, "%s: a NaN (not-a-number) was encountered in a binary input file\n",
  554. progname);
  555. return ENDED_BY_EOF; /* effectively */
  556. }
  557. else
  558. return IN_PROGRESS; /* got a pair of floats */
  559. }
  560. /* Read a point from a file in gnuplot `table' format. There are two kinds
  561. of table format we can read: the old style (from early gnuplot 3.5 and
  562. before) and a more modern style (from later gnuplot 3.5, circa 1997). */
  563. static dataset_status_t
  564. read_point_gnuplot (Reader *reader, Point *point)
  565. {
  566. int lookahead, items_read;
  567. char directive, c;
  568. bool two_newlines;
  569. double x, y;
  570. FILE *input = reader->input;
  571. head:
  572. /* skip whitespace, up to but not including 2nd newline */
  573. two_newlines = skip_some_whitespace (input);
  574. if (two_newlines)
  575. /* end of dataset */
  576. {
  577. skip_all_whitespace (input);
  578. if (feof (input))
  579. return ENDED_BY_EOF; /* no dataset follows */
  580. else
  581. return ENDED_BY_DATASET_TERMINATOR; /* dataset presumably follows */
  582. }
  583. lookahead = getc (input);
  584. ungetc (lookahead, input);
  585. switch (lookahead)
  586. {
  587. case 'C': /* old-style `Curve' line, discard it */
  588. case '#': /* modern-style comment line, discard it */
  589. do
  590. {
  591. items_read = fread (&c, sizeof (c), 1, input);
  592. if (items_read <= 0)
  593. return ENDED_BY_EOF; /* effectively */
  594. }
  595. while (c != '\n');
  596. ungetc ((int)'\n', input); /* push back \n at the end of line */
  597. goto head;
  598. case 'i': /* old-style directive-first line (in-range) */
  599. case 'o': /* old-style directive-first line (out-of-range) */
  600. /* read coordinates */
  601. items_read = fscanf (input,
  602. "%c x=%lf y=%lf",
  603. &directive, &x, &y);
  604. if (items_read == 3) /* must match all */
  605. {
  606. point->x = x;
  607. point->y = y;
  608. return IN_PROGRESS; /* got a pair of floats */
  609. }
  610. else
  611. {
  612. fprintf (stderr,
  613. "%s: an input file in gnuplot format could not be parsed\n",
  614. progname);
  615. return ENDED_BY_EOF; /* effectively */
  616. }
  617. case 'u': /* old-style directive-first line */
  618. /* `undefined', next point begins new polyline (same line mode) */
  619. do
  620. {
  621. items_read = fread (&c, sizeof (c), 1, input);
  622. if (items_read <= 0)
  623. {
  624. fprintf (stderr,
  625. "%s: an input file in gnuplot format could not be parsed\n",
  626. progname);
  627. return ENDED_BY_EOF; /* effectively */
  628. }
  629. }
  630. while (c != '\n');
  631. /* break the polyline here in a soft way (i.e. don't bump line mode) */
  632. reader->need_break = true;
  633. goto head;
  634. default: /* parse as a new-style directive-last line */
  635. items_read = fscanf (input,
  636. "%lf %lf %c",
  637. &x, &y, &directive);
  638. if (items_read == 3
  639. && (directive == 'i' || directive == 'o' || directive == 'u'))
  640. {
  641. if (directive == 'u')
  642. {
  643. /* drop point; break the polyline here in a soft way
  644. (i.e. don't bump line mode) */
  645. reader->need_break = true;
  646. goto head;
  647. }
  648. else
  649. {
  650. point->x = x;
  651. point->y = y;
  652. return IN_PROGRESS;
  653. }
  654. }
  655. else
  656. {
  657. fprintf (stderr,
  658. "%s: an input file in gnuplot format could not be parsed\n",
  659. progname);
  660. return ENDED_BY_EOF; /* effectively */
  661. }
  662. }
  663. }
  664. /* read_dataset() reads an entire dataset (a sequence of points) from an
  665. input file, and stores the resulting array of points in a block that has
  666. been allocated on the heap. The length of the block in which the points
  667. are stored, and the number of points, are passed back. */
  668. /* ARGS: length = buffer length in bytes, should begin >0 */
  669. static dataset_status_t
  670. read_dataset (Reader *reader, Point **p_addr, int *length, int *no_of_points)
  671. {
  672. Point *p = *p_addr;
  673. dataset_status_t status;
  674. for ( ; ; )
  675. {
  676. /*
  677. * Grow the buffer if needed
  678. */
  679. if (*no_of_points >= *length)
  680. {
  681. int old_length, new_length;
  682. old_length = *length;
  683. new_length = NEW_POINT_ARRAY_LENGTH(old_length);
  684. p = (Point *)xrealloc (p, new_length * sizeof (Point));
  685. *length = new_length;
  686. }
  687. status = read_point (reader, &(p[*no_of_points]));
  688. if (status != IN_PROGRESS)
  689. /* we didn't get a point, i.e. dataset ended */
  690. break;
  691. (*no_of_points)++;
  692. }
  693. *p_addr = p; /* update beginning of array if needed */
  694. return status;
  695. }
  696. /* read_file() reads all datasets from an input file, and stores the
  697. resulting array of points in a block that has been allocated on the
  698. heap. The length of the block in which the data points are stored, and
  699. the number of points, are passed back. */
  700. /* ARGS: length = buffer length in bytes, should begin >0 */
  701. void
  702. read_file (Reader *reader, Point **p_addr, int *length, int *no_of_points)
  703. {
  704. dataset_status_t status;
  705. do
  706. {
  707. status = read_dataset (reader, p_addr, length, no_of_points);
  708. /* After each dataset, reset reader: force break in polyline, bump
  709. linemode (if auto-bump is in effect), and reset abscissa (if
  710. auto-abscissa is in effect). But if dataset ended with an
  711. explicit set linemode / symbol style directive, don't bump the
  712. linemode. */
  713. if (status == ENDED_BY_MODE_CHANGE)
  714. {
  715. bool saved_auto_bump;
  716. saved_auto_bump = reader->auto_bump;
  717. reader->auto_bump = false;
  718. reset_reader (reader);
  719. reader->auto_bump = saved_auto_bump;
  720. }
  721. else
  722. reset_reader (reader);
  723. }
  724. while (status != ENDED_BY_EOF);
  725. }
  726. /* reset_reader() is called after each dataset. A new polyline will be
  727. begun, the linemode will be bumped if auto-bumping is in effect, and the
  728. abscissa will be reset if auto-abscissa is in effect. */
  729. static void
  730. reset_reader (Reader *reader)
  731. {
  732. reader->need_break = true; /* force break in polyline */
  733. /* bump linemode if appropriate */
  734. if (reader->auto_bump)
  735. reader->linemode += ((reader->linemode > 0) ? 1 : -1);
  736. /* reset abscissa if auto-abscissa is in effect */
  737. if (reader->auto_abscissa)
  738. reader->abscissa = reader->initial_abscissa;
  739. return;
  740. }
  741. /* Skip whitespace in an ascii-format or gnuplot-format input file, up to
  742. but not including a second newline. Return value indicates whether or
  743. not two newlines were in fact seen. (Two newlines signals
  744. end-of-dataset.) */
  745. static bool
  746. skip_some_whitespace (FILE *stream)
  747. {
  748. int lookahead;
  749. int nlcount = 0;
  750. do
  751. {
  752. lookahead = getc (stream);
  753. if (lookahead == (int)'\n')
  754. nlcount++;
  755. }
  756. while (lookahead != EOF
  757. && isspace((unsigned char)lookahead)
  758. && nlcount < 2);
  759. if (lookahead == EOF)
  760. return false;
  761. ungetc (lookahead, stream);
  762. return (nlcount == 2 ? true : false);
  763. }
  764. /* Skip all whitespace; used for discarding whitespace in gnuplot-format
  765. input files. Old-style gnuplot table format follows each dataset by two
  766. newlines, but new-style format uses three newlines. */
  767. static void
  768. skip_all_whitespace (FILE *stream)
  769. {
  770. int lookahead;
  771. do
  772. lookahead = getc (stream);
  773. while (lookahead != EOF
  774. && isspace((unsigned char)lookahead));
  775. if (lookahead == EOF)
  776. return;
  777. else
  778. ungetc (lookahead, stream);
  779. }
  780. /**********************************************************************/
  781. /* read_and_plot_dataset() reads an entire dataset (a sequence of points)
  782. from an input file, and calls a Multigrapher's plot_point() method on
  783. each point as it is read. So plotting is accomplished in real time (the
  784. points are not stored). */
  785. static dataset_status_t
  786. read_and_plot_dataset (Reader *reader, Multigrapher *multigrapher, int *no_of_points, Point * point)
  787. {
  788. dataset_status_t status;
  789. for (; ; ++(*no_of_points))
  790. {
  791. status = read_point (reader, point);
  792. if (status != IN_PROGRESS)
  793. /* we didn't get a point, i.e. dataset ended */
  794. break;
  795. else
  796. plot_point (multigrapher, point);
  797. }
  798. return status;
  799. }
  800. /* read_and_plot_file() reads a sequence of datasets from a stream, and
  801. calls a Multigrapher's plot_point() method on them as they are read. So
  802. plotting is accomplished in real time (the points are not stored). */
  803. void
  804. read_and_plot_file (Reader *reader, Multigrapher *multigrapher, int *no_of_points,
  805. bool *new_legend_label, Legend **legends, int *no_of_legends, char const *legend_label)
  806. {
  807. dataset_status_t status;
  808. do
  809. {
  810. Point point;
  811. int old_no_of_points = *no_of_points;
  812. status = read_and_plot_dataset (reader, multigrapher, no_of_points, &point);
  813. if (*new_legend_label && *no_of_points>old_no_of_points)
  814. add_legend (legends, no_of_legends, &point, legend_label);
  815. *new_legend_label = false;
  816. /* After each dataset, reset reader: force break in polyline, bump
  817. linemode (if auto-bump is in effect), and reset abscissa (if
  818. auto-abscissa is in effect). If dataset ended with an explicit
  819. set linemode / symbol style directive, don't bump the linemode. */
  820. if (status == ENDED_BY_MODE_CHANGE)
  821. {
  822. bool saved_auto_bump;
  823. saved_auto_bump = reader->auto_bump;
  824. reader->auto_bump = false;
  825. reset_reader (reader);
  826. reader->auto_bump = saved_auto_bump;
  827. }
  828. else
  829. reset_reader (reader);
  830. /* after each dataset, flush the constructed polyline to the display
  831. device by invoking a special Multigrapher method; this ensures
  832. real-time performance */
  833. end_polyline_and_flush (multigrapher);
  834. }
  835. while (status != ENDED_BY_EOF);
  836. }
  837. /*
  838. Local Variables:
  839. c-file-style: "gnu"
  840. tab-width: 8
  841. End:
  842. */