-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathdatafile.c
More file actions
5929 lines (5188 loc) · 177 KB
/
datafile.c
File metadata and controls
5929 lines (5188 loc) · 177 KB
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GNUPLOT - datafile.c */
/*[
* Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted, but not the right to
* distribute the complete modified source code. Modifications are to
* be distributed as patches to the released version. Permission to
* distribute binaries produced by compiling modified sources is granted,
* provided you
* 1. distribute the corresponding source modifications from the
* released version in the form of a patch file along with the binaries,
* 2. add special version identification to distinguish your version
* in addition to the base release version number,
* 3. provide your name and address as the primary contact for the
* support of your modified version, and
* 4. retain our contact information in regard to use of the base
* software.
* Permission to distribute the released version of the source code along
* with corresponding source modifications in the form of a patch file is
* granted with same provisions 2 through 4 for binary distributions.
*
* This software is provided "as is" without express or implied warranty
* to the extent permitted by applicable law.
]*/
/* AUTHOR : David Denholm */
/*
* this file provides the functions to handle data-file reading..
* takes care of all the pipe / stdin / index / using worries
*/
/*{{{ notes */
/*
* every a:b:c:d:e:f - plot every a'th point from c to e,
* in every b lines from d to f
* ie for (line=d; line<=f; line+=b)
* for (point=c; point >=e; point+=a)
*
* public variables declared in this file.
* int df_no_use_specs - number of columns specified with 'using'
* int df_no_tic_specs - count of additional ticlabel columns
* int df_line_number - for error reporting
* int df_datum - increases with each data point
* int df_eof - end of file
*
* public information about the data file or format
* TBOOLEAN df_matrix - TRUE if splot matrix
* TBOOLEAN df_binary - binary data file format (maybe auto-generated)
*
* functions
* int df_open(char *file_name, int max_using, plot_header *plot)
* parses index / using on command line
* max_using is max no of 'using' columns allowed (obsolete?)
* plot_header is NULL if called from fit or set_palette code
* returns number of 'using' cols specified, or -1 on error (?)
*
* int df_readline(double vector[], int max)
* reads a line, does all the 'index' and 'using' manipulation
* deposits values into vector[]
* returns
* number of columns parsed [0 = not a blank line, but no valid data],
* DF_EOF - end of file
* DF_UNDEFINED - undefined result during eval of extended using spec
* DF_MISSING - requested column matched that of 'set missing <foo>'
* DF_FIRST_BLANK - first consecutive blank line
* DF_SECOND_BLANK - second consecutive blank line
* DF_FOUND_KEY_TITLE - only relevant to first line of data
* DF_KEY_TITLE_MISSING and only for 'set key autotitle columnhead'
* DF_STRINGDATA - not currently used by anyone
* DF_COLUMN_HEADERS - first row used as headers rather than data
*
* if a using spec was given, lines not fulfilling spec are ignored.
* we will always return exactly the number of items specified
*
* if no spec given, we return number of consecutive columns we parsed.
*
* if we are processing indexes, separated by 'n' blank lines,
* we will return n-1 blank lines before noticing the index change
*
* void df_close()
* closes a currently open file.
*
* void f_dollars(x)
* void f_column() actions for expressions using $i, column(j), etc
* void f_valid()
*
*
* Line parsing is slightly differently from previous versions of gnuplot...
* given a line containing fewer columns than asked for, gnuplot used to make
* up values... Now if I have explicitly said 'using 1:2:3', then if
* column 3 doesn't exist, I dont want this point...
*
*/
/*}}} */
/* Daniel Sebald: added general binary 2d data support. (20 August 2004)
*/
#include "datafile.h"
#include "datablock.h"
#include "alloc.h"
#include "command.h"
#include "eval.h"
#include "gp_time.h"
#include "gplocale.h"
#include "graphics.h"
#include "misc.h"
#include "parse.h"
#include "plot.h"
#include "plot2d.h" /* For reevaluate_plot_title() */
#include "readline.h"
#include "util.h"
#include "breaders.h"
#include "tabulate.h" /* For sanity check inblock != outblock */
#include "voxelgrid.h"
/* test to see if the end of an inline datafile is reached */
#define is_EOF(c) ((c) == 'e' || (c) == 'E')
/* is it a comment line? */
#define is_comment(c) ((c) && (strchr(df_commentschars, (c)) != NULL))
/* Used to skip whitespace but not cross a field boundary */
#define NOTSEP (!df_separators || !strchr(df_separators,*s))
enum COLUMN_TYPE { CT_DEFAULT, CT_STRING, CT_KEYLABEL, CT_MUST_HAVE,
CT_XTICLABEL, CT_X2TICLABEL, CT_YTICLABEL, CT_Y2TICLABEL,
CT_ZTICLABEL, CT_CBTICLABEL };
/*{{{ static fns */
static int check_missing(char *s);
static void expand_df_column(int);
static void clear_df_column_headers(void);
static char *df_gets(void);
static int df_tokenise(char *s);
static double *df_read_matrix(int *rows, int *columns);
static void plot_option_every(void);
static void plot_option_index(void);
static void plot_option_using(int);
static TBOOLEAN valid_format(const char *);
static void plot_ticlabel_using(int);
static void add_key_entry(char *temp_string, int df_datum);
static char * df_generate_pseudodata(void);
static char * df_generate_ascii_array_entry(void);
static int df_skip_bytes(off_t nbytes);
static int axcol_for_ticlabel(enum COLUMN_TYPE type, int *axis);
/*}}} */
/*{{{ variables */
/* public (exported) variables client might access */
int df_no_use_specs; /* how many using columns were specified */
int df_line_number;
int df_datum; /* suggested x value if none given */
int df_last_col = 0; /* visible to user via STATS_columns */
int df_bad_matrix_values;
AXIS_INDEX df_axis[MAXDATACOLS];
TBOOLEAN df_matrix = FALSE; /* indicates if data originated from a 2D or 3D format */
void *df_pixeldata; /* pixel data from an external library (e.g. libgd) */
/* string representing missing values in ascii datafiles */
char *missing_val = NULL;
/* input field separators, NULL if whitespace is the separator */
char *df_separators = NULL;
/* comments chars */
char *df_commentschars = 0;
/* If any 'inline data' are in use for the current plot, flag this */
TBOOLEAN plotted_data_from_stdin = FALSE;
/* This flag is controlled by 'set/unset datafile columnheaders'.
* Even if it is FALSE, columnheader processing may still be triggered
* implicitly by use of the columheader function or keyword
* in a using spec or title.
*/
TBOOLEAN df_columnheaders = FALSE;
/* Setting this allows the parser to recognize Fortran D or Q */
/* format constants in the input file. But it slows things down */
TBOOLEAN df_fortran_constants = FALSE;
/* Setting this disables re-initialization of the floating point exception */
/* handler before every expression evaluation in a using spec. */
TBOOLEAN df_nofpe_trap = FALSE;
/* private variables */
/* Bookkeeping for df_fgets() and df_gets().
* Must be initialized before any calls to either function.
*/
static char *df_line = NULL;
static size_t max_line_len = 0;
#define DATA_LINE_BUFSIZ 160
static FILE *data_fp = NULL;
#if defined(PIPES)
static TBOOLEAN df_pipe_open = FALSE;
#endif
#if defined(HAVE_FDOPEN)
static int data_fd = -2; /* only used for file redirection */
#endif
static TBOOLEAN mixed_data_fp = FALSE; /* inline data */
char *df_filename = NULL; /* name of data file */
static int df_eof = 0;
static int df_no_tic_specs; /* ticlabel columns not counted in df_no_use_specs */
#ifndef MAXINT /* should there be one already defined ? */
# define MAXINT INT_MAX /* from <limits.h> */
#endif
/* stuff for implementing index */
static int blank_count = 0; /* how many blank lines recently */
static int df_lower_index = 0; /* first mesh required */
static int df_upper_index = MAXINT;
static int df_index_step = 1; /* 'every' for indices */
static int df_current_index; /* current mesh */
static int df_last_index_read; /* last mesh we actually read data from */
/* stuff for named index support */
static char *indexname = NULL;
static TBOOLEAN index_found = FALSE;
static int df_longest_columnhead = 0;
/* stuff for every point:line */
static TBOOLEAN set_every = FALSE;
static int everypoint = 1;
static int firstpoint = 0;
static int lastpoint = MAXINT;
static int everyline = 1;
static int firstline = 0;
static int lastline = MAXINT;
static int point_count = -1; /* point counter - preincrement and test 0 */
static int line_count = 0; /* line counter */
/* for ascii file "skip" lines at head of file */
static int df_skip_at_front = 0;
/* for pseudo-data (1 if filename = '+'; 2 if filename = '++') */
static int df_pseudodata = 0;
static int df_pseudorecord = 0;
static int df_pseudospan = 0;
static double df_pseudovalue_0 = 0;
static double df_pseudovalue_1 = 0;
/* for datablocks */
static TBOOLEAN df_datablock = FALSE;
static char **df_datablock_line = NULL;
/* for arrays */
static int df_array_index = 0;
static char *df_arrayname = NULL;
/* track dimensions of input matrix/array/image */
static unsigned int df_xpixels;
static unsigned int df_ypixels;
static TBOOLEAN df_transpose;
static double df_image_origin[2];
static double df_image_deltas[2];
/* parsing stuff */
struct use_spec_s use_spec[MAXDATACOLS];
static char *df_format = NULL;
static char *df_binary_format = NULL;
TBOOLEAN evaluate_inside_using = FALSE;
TBOOLEAN df_warn_on_missing_columnheader = FALSE;
/* rather than three arrays which all grow dynamically, make one
* dynamic array of this structure
*/
typedef struct df_column_struct {
double datum;
enum DF_STATUS good;
char *position; /* points to start of this field in current line */
char *header; /* points to copy of the header for this column */
} df_column_struct;
static df_column_struct *df_column = NULL; /* we'll allocate space as needed */
static int df_max_cols = 0; /* space allocated */
static int df_no_cols; /* total number of columns found in input lines */
static int fast_columns; /* corey@cac optimization */
char *df_tokens[MAXDATACOLS]; /* filled in by df_tokenise */
static char *df_stringexpression[MAXDATACOLS]; /* filled in after evaluate_at() */
static struct curve_points *df_current_plot; /* used to process histogram labels + key entries */
struct value df_strings[MAXDATACOLS]; /* used only by TABLESTYLE */
static TBOOLEAN df_tabulate_strings = FALSE; /* used only by TABLESTYLE */
/* These control the handling of fields in the first row of a data file.
* See also parse_1st_row_as_headers.
*/
#define NO_COLUMN_HEADER (-99) /* some value that can never be a real column */
static int column_for_key_title = NO_COLUMN_HEADER;
static TBOOLEAN df_already_got_headers = FALSE;
char *df_key_title = NULL; /* filled in from column header if requested */
struct at_type *df_plot_title_at; /* used for deferred evaluation of plot title */
/* last resort mechanism to catch missing data */
static TBOOLEAN df_missing_data_in_expression = FALSE;
/* Binary *read* variables used by df_readbinary().
* There is a confusing difference between the ascii and binary "matrix" keywords.
* Ascii matrix data by default is interpreted as having an implicit uniform grid
* of x and y coords that are not actually present in the data file.
* The equivalent binary data format is called "binary general".
* In both of these cases the internal flag df_nonuniform_matrix is FALSE;
* Binary matrix data contains explicit y values in the first row, and explicit x
* values in the first column. This is signalled by "binary matrix".
* In this case the internal flag df_nonuniform_matrix is TRUE.
*
* EAM May 2011 - Add a keyword "nonuniform matrix" to indicate ascii matrix data
* in the same format as "binary matrix", i.e. with explicit x and y coordinates.
* EAM Jul 2014 - Add keywords "columnheaders" and "rowheaders" to indicate ascii
* matrix data in the uniform grid format containing labels in row 1 and column 1.
* EAM Jul 2021 - Add keyword "sparse matrix" to indicate ascii matrix data
* provided as individual entries (x y value) in any order.
*/
static TBOOLEAN df_read_binary;
static TBOOLEAN df_nonuniform_matrix;
static TBOOLEAN df_matrix_columnheaders, df_matrix_rowheaders;
static int df_plot_mode;
static int df_readascii(double [], int);
static int df_readbinary(double [], int);
static void initialize_use_spec(void);
static void initialize_plot_style(struct curve_points *);
static void initialize_binary_vars(void);
static void df_insert_scanned_use_spec(int);
static void adjust_binary_use_spec(struct curve_points *);
static void clear_binary_records(df_records_type);
static void plot_option_binary_format(char *);
static void plot_option_binary(TBOOLEAN, TBOOLEAN);
static void plot_option_array(void);
static void plot_option_sparse(void);
static TBOOLEAN rotation_matrix_2D(double R[][2], double);
static TBOOLEAN rotation_matrix_3D(double P[][3], double *);
static int token2tuple(double *, int);
static void df_determine_matrix_info(FILE *);
static void df_swap_bytes_by_endianess(char *, int, int);
typedef enum df_multivalue_type {
DF_DELTA,
DF_FLIP_AXIS,
DF_FLIP,
DF_SCAN,
DF_ORIGIN,
DF_CENTER,
DF_ROTATION,
DF_PERPENDICULAR,
DF_SKIP
} df_multivalue_type;
static void plot_option_multivalued(df_multivalue_type,int);
char *df_endian[DF_ENDIAN_TYPE_LENGTH] = {
"little",
"pdp (middle)",
"swapped pdp (dimmle)",
"big"
};
#define SUPPORT_MIDDLE_ENDIAN 1
#if SUPPORT_MIDDLE_ENDIAN
/* To generate a swap, take the bit-wise complement of the lowest two bits. */
typedef enum df_byte_read_order_type {
DF_0123,
DF_1032,
DF_2301,
DF_3210
} df_byte_read_order_type;
/* First argument, this program's endianess. Second argument, file's endianess.
* Don't use directly. Use 'byte_read_order()' function instead.*/
static char df_byte_read_order_map[4][4] = {
{DF_0123, DF_1032, DF_2301, DF_3210},
{DF_1032, DF_0123, DF_1032, DF_2301},
{DF_2301, DF_1032, DF_0123, DF_1032},
{DF_3210, DF_2301, DF_1032, DF_0123}
};
static long long_0x2468 = 0x2468;
#define TEST_BIG_PDP ( (((char *)&long_0x2468)[0] < 3) ? DF_BIG_ENDIAN : DF_PDP_ENDIAN )
#define THIS_COMPILER_ENDIAN ( (((char *)&long_0x2468)[0] < 5) ? TEST_BIG_PDP : DF_LITTLE_ENDIAN )
/* Argument is file's endianess type. */
static df_byte_read_order_type byte_read_order(df_endianess_type);
/* Logical variables indicating information about data file. */
TBOOLEAN df_binary_file;
TBOOLEAN df_matrix_file;
TBOOLEAN df_sparse_matrix;
TBOOLEAN df_voxelgrid;
static int df_M_count;
static int df_N_count;
static int df_O_count;
/* Initially set to default and then possibly altered by command line. */
df_binary_file_record_struct *df_bin_record = 0;
/* Default settings. */
df_binary_file_record_struct *df_bin_record_default = 0;
/* Settings that are transferred to default upon reset. */
df_binary_file_record_struct df_bin_record_reset = {
{-1, 0, 0},
{1, 1, 1},
{1, 1, 1},
DF_TRANSLATE_DEFAULT,
{0, 0, 0},
0,
{0, 0, 1},
{DF_SCAN_POINT, DF_SCAN_LINE, DF_SCAN_PLANE},
FALSE,
{0, 0, 0},
{0, 0, 0},
{1, 1, 1},
{0, 0, 0},
DF_TRANSLATE_DEFAULT,
{0, 0, 0},
0, 0, /* submatrix size */
NULL /* data_memory */
};
int df_max_num_bin_records = 0, df_num_bin_records, df_bin_record_count;
int df_max_num_bin_records_default = 0, df_num_bin_records_default;
/* Used to mark the location of a blank line in the original data input file */
const struct coordinate blank_data_line = {-999, -999, -999, -999, -999, -999, -999, UNDEFINED};
static void gpbin_filetype_function(void);
static void raw_filetype_function(void);
static void avs_filetype_function(void);
static void (*binary_input_function)(void); /* Will point to one of the above */
static void auto_filetype_function(void){} /* Just a placeholder for auto */
struct gen_ftable df_bin_filetype_table[] = {
{"avs", avs_filetype_function},
{"bin", raw_filetype_function},
{"edf", edf_filetype_function},
{"ehf", edf_filetype_function},
{"gif", gif_filetype_function},
{"gpbin", gpbin_filetype_function},
{"jpeg", jpeg_filetype_function},
{"jpg", jpeg_filetype_function},
{"png", png_filetype_function},
{"raw", raw_filetype_function},
{"rgb", raw_filetype_function},
{"auto", auto_filetype_function},
{NULL, NULL}
};
#define RAW_FILETYPE 1
/* Initially set to default and then possibly altered by command line. */
static int df_bin_filetype;
/* Default setting. */
static int df_bin_filetype_default;
static df_endianess_type df_bin_file_endianess_default;
/* Setting that is transferred to default upon reset. */
static int df_bin_filetype_reset = -1;
#define DF_BIN_FILE_ENDIANESS_RESET THIS_COMPILER_ENDIAN
/* This one is needed by breaders.c */
df_endianess_type df_bin_file_endianess;
typedef struct df_bin_scan_table_2D_struct {
char *string;
df_sample_scan_type scan[3];
} df_bin_scan_table_2D_struct;
df_bin_scan_table_2D_struct df_bin_scan_table_2D[] = {
{"xy", {DF_SCAN_POINT, DF_SCAN_LINE, DF_SCAN_PLANE}},
{"yx", {DF_SCAN_LINE, DF_SCAN_POINT, DF_SCAN_PLANE}},
{"tr", {DF_SCAN_POINT, DF_SCAN_LINE, DF_SCAN_PLANE}},
{"rt", {DF_SCAN_LINE, DF_SCAN_POINT, DF_SCAN_PLANE}}
};
#define TRANSPOSE_INDEX 1
typedef struct df_bin_scan_table_3D_struct {
char *string;
df_sample_scan_type scan[3];
} df_bin_scan_table_3D_struct;
df_bin_scan_table_3D_struct df_bin_scan_table_3D[] = {
{"xyz", {DF_SCAN_POINT, DF_SCAN_LINE, DF_SCAN_PLANE}},
{"zxy", {DF_SCAN_LINE, DF_SCAN_PLANE, DF_SCAN_POINT}},
{"yzx", {DF_SCAN_PLANE, DF_SCAN_POINT, DF_SCAN_LINE}},
{"yxz", {DF_SCAN_LINE, DF_SCAN_POINT, DF_SCAN_PLANE}},
{"xzy", {DF_SCAN_POINT, DF_SCAN_PLANE, DF_SCAN_LINE}},
{"zyx", {DF_SCAN_PLANE, DF_SCAN_LINE, DF_SCAN_POINT}},
{"trz", {DF_SCAN_POINT, DF_SCAN_LINE, DF_SCAN_PLANE}},
{"ztr", {DF_SCAN_LINE, DF_SCAN_PLANE, DF_SCAN_POINT}},
{"rzt", {DF_SCAN_PLANE, DF_SCAN_POINT, DF_SCAN_LINE}},
{"rtz", {DF_SCAN_LINE, DF_SCAN_POINT, DF_SCAN_PLANE}},
{"tzr", {DF_SCAN_POINT, DF_SCAN_PLANE, DF_SCAN_LINE}},
{"zrt", {DF_SCAN_PLANE, DF_SCAN_LINE, DF_SCAN_POINT}}
};
/* Names for machine dependent field sizes. */
char *ch_names[] = {"char","schar","c"};
char *uc_names[] = {"uchar"};
char *sh_names[] = {"short"};
char *us_names[] = {"ushort"};
char *in_names[] = {"int","sint","i","d"};
char *ui_names[] = {"uint","u"};
char *lo_names[] = {"long","ld"};
char *ul_names[] = {"ulong","lu"};
char *fl_names[] = {"float","f"};
char *db_names[] = {"double","lf"};
/* Machine independent names. */
char *byte_names[] = {"int8","byte"};
char *ubyte_names[] = {"uint8","ubyte"};
char *word_names[] = {"int16","word"};
char *uword_names[] = {"uint16","uword"};
char *word2_names[] = {"int32"};
char *uword2_names[] = {"uint32"};
char *word4_names[] = {"int64"};
char *uword4_names[] = {"uint64"};
char *float_names[] = {"float32"};
char *float2_names[] = {"float64"};
typedef struct df_binary_details_struct {
char **name;
unsigned short no_names;
df_binary_type_struct type;
} df_binary_details_struct;
typedef struct df_binary_tables_struct {
df_binary_details_struct *group;
unsigned short group_length;
} df_binary_tables_struct;
df_binary_details_struct df_binary_details[] = {
{ch_names,sizeof(ch_names)/sizeof(ch_names[0]),{DF_CHAR,sizeof(char)}},
{uc_names,sizeof(uc_names)/sizeof(uc_names[0]),{DF_UCHAR,sizeof(unsigned char)}},
{sh_names,sizeof(sh_names)/sizeof(sh_names[0]),{DF_SHORT,sizeof(short)}},
{us_names,sizeof(us_names)/sizeof(us_names[0]),{DF_USHORT,sizeof(unsigned short)}},
{in_names,sizeof(in_names)/sizeof(in_names[0]),{DF_INT,sizeof(int)}},
{ui_names,sizeof(ui_names)/sizeof(ui_names[0]),{DF_UINT,sizeof(unsigned int)}},
{lo_names,sizeof(lo_names)/sizeof(lo_names[0]),{DF_LONG,sizeof(long)}},
{ul_names,sizeof(ul_names)/sizeof(ul_names[0]),{DF_ULONG,sizeof(unsigned long)}},
{fl_names,sizeof(fl_names)/sizeof(fl_names[0]),{DF_FLOAT,sizeof(float)}},
{db_names,sizeof(db_names)/sizeof(db_names[0]),{DF_DOUBLE,sizeof(double)}},
{NULL,0, {DF_LONGLONG,sizeof(long long)}},
{NULL,0, {DF_ULONGLONG,sizeof(unsigned long long)}}
};
df_binary_details_struct df_binary_details_independent[] = {
{byte_names,sizeof(byte_names)/sizeof(byte_names[0]),{SIGNED_TEST(1),1}},
{ubyte_names,sizeof(ubyte_names)/sizeof(ubyte_names[0]),{UNSIGNED_TEST(1),1}},
{word_names,sizeof(word_names)/sizeof(word_names[0]),{SIGNED_TEST(2),2}},
{uword_names,sizeof(uword_names)/sizeof(uword_names[0]),{UNSIGNED_TEST(2),2}},
{word2_names,sizeof(word2_names)/sizeof(word2_names[0]),{SIGNED_TEST(4),4}},
{uword2_names,sizeof(uword2_names)/sizeof(uword2_names[0]),{UNSIGNED_TEST(4),4}},
{word4_names,sizeof(word4_names)/sizeof(word4_names[0]),{SIGNED_TEST(8),8}},
{uword4_names,sizeof(uword4_names)/sizeof(uword4_names[0]),{UNSIGNED_TEST(8),8}},
{float_names,sizeof(float_names)/sizeof(float_names[0]),{FLOAT_TEST(4),4}},
{float2_names,sizeof(float2_names)/sizeof(float2_names[0]),{FLOAT_TEST(8),8}}
};
int df_no_bin_cols; /* binary columns to read */
df_binary_tables_struct df_binary_tables[] = {
{df_binary_details,sizeof(df_binary_details)/sizeof(df_binary_details[0])},
{df_binary_details_independent,sizeof(df_binary_details_independent)/sizeof(df_binary_details_independent[0])}
};
/* Information about binary data structure, to be determined by the
* using and format options. This should be one greater than df_no_bin_cols.
*/
static df_column_bininfo_struct *df_column_bininfo = NULL; /* allocate space as needed */
static int df_max_bininfo_cols = 0; /* space allocated */
static const char *matrix_general_binary_conflict_msg
= "Conflict between some matrix binary and general binary keywords";
#endif
/*}}} */
/* Initialize input buffer used by df_gets and df_fgets. */
/* Called via reset_command() on program entry. */
void
df_init()
{
if (max_line_len < DATA_LINE_BUFSIZ) {
max_line_len = DATA_LINE_BUFSIZ;
df_line = gp_alloc(max_line_len, "datafile line buffer");
}
}
/*{{{ static char *df_gets() */
static char *
df_gets()
{
/* HBB 20000526: prompt user for inline data, if in interactive mode */
if (mixed_data_fp && interactive)
fputs("input data ('e' ends) > ", stderr);
/* Special pseudofiles '+' and '++' return coords of sample */
if (df_pseudodata)
return df_generate_pseudodata();
if (df_datablock)
return *(df_datablock_line++);
if (df_array)
return df_generate_ascii_array_entry();
return df_fgets(data_fp);
}
/*}}} */
/*{{{ char *df_gets() */
/*
* This one is shared by df_gets() and by datablock.c:datablock_command
*/
char *
df_fgets( FILE *fin )
{
int len = 0;
if (!fgets(df_line, max_line_len, fin))
return NULL;
if (mixed_data_fp)
++inline_num;
for (;;) {
len += strlen(df_line + len);
if (len > 0 && df_line[len - 1] == '\n') {
/* we have read an entire text-file line.
* Strip the trailing linefeed and return
*/
df_line[len - 1] = 0;
return df_line;
}
if ((max_line_len - len) < 32)
df_line = gp_realloc(df_line, max_line_len *= 2, "datafile line buffer");
if (!fgets(df_line + len, max_line_len - len, fin))
return df_line; /* unexpected end of file, but we have something to do */
}
/* NOTREACHED */
return NULL;
}
/*}}} */
static int
df_tokenise(char *s)
{
/* implement our own sscanf that takes 'missing' into account,
* and can understand fortran quad format
*/
TBOOLEAN in_string;
int i;
/* "here data" lines may end in \n rather than \0. */
/* DOS/Windows lines may end in \r rather than \0. */
if (s[strlen(s)-1] == '\n' || s[strlen(s)-1] == '\r')
s[strlen(s)-1] = '\0';
for (i = 0; i<MAXDATACOLS; i++)
df_tokens[i] = NULL;
df_no_cols = 0;
while (*s) {
/* We may poke at 2 new fields before coming back here - make sure there is room */
if (df_max_cols <= df_no_cols + 2)
expand_df_column((df_max_cols < 20) ? df_max_cols+20 : 2*df_max_cols);
/* have always skipped spaces at this point */
df_column[df_no_cols].position = s;
in_string = FALSE;
/* Keep pointer to start of this token if user wanted it for
* anything, particularly if it is a string */
for (i = 0; i<MAXDATACOLS; i++) {
if (df_no_cols == use_spec[i].column-1) {
df_tokens[i] = s;
if (use_spec[i].expected_type == CT_STRING)
df_column[df_no_cols].good = DF_GOOD;
}
}
/* CSV files must accept numbers inside quotes also,
* so we step past the quote */
if (*s == '"' && df_separators != NULL) {
in_string = TRUE;
df_column[df_no_cols].position = ++s;
}
if (*s == '"') {
/* treat contents of a quoted string as single column */
in_string = !in_string;
df_column[df_no_cols].good = DF_STRINGDATA;
} else if (check_missing(s)) {
df_column[df_no_cols].good = DF_MISSING;
df_column[df_no_cols].datum = not_a_number();
df_column[df_no_cols].position = NULL;
} else {
int used;
int count;
int dfncp1 = df_no_cols + 1;
/* optimizations by Corey Satten, [email protected] */
/* only scanf the field if it is mentioned in one of the using specs */
if ((fast_columns == 0)
|| (df_no_use_specs == 0)
|| ((df_no_use_specs > 0)
&& (use_spec[0].column == dfncp1
|| (df_no_use_specs > 1
&& (use_spec[1].column == dfncp1
|| (df_no_use_specs > 2
&& (use_spec[2].column == dfncp1
|| (df_no_use_specs > 3
&& (use_spec[3].column == dfncp1
|| (df_no_use_specs > 4
&& (use_spec[4].column == dfncp1
|| df_no_use_specs > 5)
)
)
)
)
)
)
)
)
)
) {
/* Use strtod() because
* - it is faster than sscanf()
* - sscanf(... %n ...) may not be portable
* - it allows error checking
* - atof() does not return a count or new position
*/
char *next;
df_column[df_no_cols].datum = strtod(s, &next);
used = next - s;
count = (used) ? 1 : 0;
} else {
/* skip any space at start of column */
while (isspace((unsigned char) *s) && NOTSEP)
++s;
count = (*s && NOTSEP) ? 1 : 0;
/* skip chars to end of column */
used = 0;
if (df_separators != NULL && in_string) {
do
++s;
while (*s && *s != '"');
in_string = FALSE;
}
while (!isspace((unsigned char) *s)
&& (*s != NUL) && NOTSEP)
++s;
}
/* it might be a fortran double or quad precision.
* 'used' is only safe if count is 1
*/
if (df_fortran_constants && count == 1 &&
(s[used] == 'd' || s[used] == 'D' ||
s[used] == 'q' || s[used] == 'Q')) {
/* HBB 20001221: avoid breaking parsing of time/date
* strings like 01Dec2000 that would be caused by
* overwriting the 'D' with an 'e'... */
char *endptr;
char save_char = s[used];
/* might be fortran double */
s[used] = 'e';
/* and try again */
df_column[df_no_cols].datum = strtod(s, &endptr);
count = (endptr == s) ? 0 : 1;
s[used] = save_char;
}
df_column[df_no_cols].good = count == 1 ? DF_GOOD : DF_BAD;
if (isnan(df_column[df_no_cols].datum)) {
df_column[df_no_cols].good = DF_UNDEFINED;
FPRINTF((stderr,"NaN in column %d\n", df_no_cols));
}
}
++df_no_cols;
/* If we are in a quoted string, skip to end of quote */
if (in_string) {
do
s++;
while (*s && (unsigned char) *s != '"');
}
/* skip to 1st character in the next field */
if (df_separators != NULL && !df_array) {
/* skip to next separator or end of line */
while ((*s != '\0') && (*s != '\n') && NOTSEP)
++s;
if ((*s == '\0') || (*s == '\n')) /* End of line; we're done */
break;
/* step over field separator */
++s;
/* skip whitespace at start of next field */
while ((*s == ' ' || *s == '\t') && NOTSEP)
++s;
if ((*s == '\0') || (*s == '\n')) { /* Last field is empty */
df_column[df_no_cols].good = DF_MISSING;
df_column[df_no_cols].datum = not_a_number();
df_column[df_no_cols].position = NULL;
++df_no_cols;
break;
}
} else {
/* skip trash chars remaining in this column */
while ((*s != '\0') && (*s != '\n') && !isspace((unsigned char) *s))
++s;
/* skip whitespace to start of next column */
while (isspace((unsigned char) *s) && *s != '\n')
++s;
}
}
return df_no_cols;
}
/*{{{ static double *df_read_matrix() */
/* Reads a matrix from a text file and stores it in allocated memory.
*
* IMPORTANT NOTE: The routine returns the memory pointer for that matrix,
* but does not retain the pointer. Maintenance of the memory is left to
* the calling code.
*/
static double *
df_read_matrix(int *rows, int *cols)
{
int max_rows = 0;
int c;
double *linearized_matrix = NULL;
char *s;
int index = 0;
df_bad_matrix_values = 0;
*rows = 0;
*cols = 0;
for (;;) {
if (!(s = df_gets())) {
df_eof = 1;
/* NULL if we have not read anything yet */
return linearized_matrix;
}
/* skip leading spaces */
while (isspace((unsigned char) *s) && NOTSEP)
++s;
/* skip blank lines and comments */
if (!*s || is_comment(*s)) {
/* except that some comments hide an index name */
if (indexname) {
while (is_comment(*s) || isspace((unsigned char)*s))
++s;
if (*s && !strncmp(s, indexname, strlen(indexname)))
index_found = TRUE;
}
/* This whole section copied with minor tweaks from df_readascii() */
if (++blank_count == 1) {
/* first blank line */
if (linearized_matrix)
return linearized_matrix;
if (indexname && !index_found)
continue;
if (df_current_index < df_lower_index)
continue;
}
if (blank_count == 2) {
/* just reached the end of a data block */
++df_current_index;
if (indexname && index_found) {
df_eof = 1;
return linearized_matrix;
}
if (df_current_index <= df_lower_index)
continue;
if (df_current_index > df_upper_index) {
df_eof = 1;
return linearized_matrix;
}
} else {
/* Ignore any blank lines beyond the 2nd */
continue;
}
}
/* get here => was not blank */
df_last_index_read = df_current_index;
/* TODO: Handle columnheaders for 2nd and subsequent data blocks?
* if (blank_count >= 2) { do something }
*/
blank_count = 0;
if (mixed_data_fp && is_EOF(*s)) {
df_eof = 1;
return linearized_matrix;
}
c = df_tokenise(s);
if (!c)
return linearized_matrix;
/* If the first row of matrix data contains column headers */
if (!df_already_got_headers && df_matrix_columnheaders && *rows == 0) {
int i;
char *temp_string;
df_already_got_headers = TRUE;
for (i = (df_matrix_rowheaders ? 1 :0); i < c; i++) {
double xpos = df_matrix_rowheaders ? (i-1) : i;
if (use_spec[0].at) {
struct value a;
df_column[0].datum = xpos;
df_column[0].good = DF_GOOD;
evaluate_inside_using = TRUE;
evaluate_at(use_spec[0].at, &a);
evaluate_inside_using = FALSE;
xpos = real(&a);
}
temp_string = df_parse_string_field(df_column[i].position);
add_tic_user(&axis_array[FIRST_X_AXIS], temp_string, xpos, -1);
free(temp_string);
}
continue;
}
if (*cols && c != *cols) {
/* it's not regular */
if (linearized_matrix)
free(linearized_matrix);
int_error(NO_CARET, "Matrix does not represent a grid");
}
*cols = c;
++*rows;
if (*rows > max_rows) {
max_rows = GPMAX(2*max_rows,1);
linearized_matrix = gp_realloc(linearized_matrix,
*cols * max_rows * sizeof(double),
"df_matrix");
}
/* store data */
{
int i;
for (i = 0; i < c; ++i) {
/* First column in "matrix rowheaders" is a ytic label */
if (df_matrix_rowheaders && i == 0) {
char *temp_string;
double ypos = *rows - 1;