gui: For text data import, use the same parser for preview as for import.
[pspp] / src / language / data-io / data-parser.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2011, 2016 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef LANGUAGE_DATA_IO_DATA_PARSER_H
18 #define LANGUAGE_DATA_IO_DATA_PARSER_H
19
20 /* Abstraction of a DATA LIST or GET DATA TYPE=TXT data parser. */
21
22 #include <stdbool.h>
23 #include "data/case.h"
24 #include "libpspp/str.h"
25
26 struct dataset;
27 struct dfm_reader;
28 struct dictionary;
29 struct file_handle;
30 struct fmt_spec;
31 struct string_array;
32 struct substring;
33
34 /* Type of data read by a data parser. */
35 enum data_parser_type
36   {
37     DP_FIXED,                   /* Fields in fixed column positions. */
38     DP_DELIMITED                /* Fields delimited by e.g. commas. */
39   };
40
41 /* Creating and configuring any parser. */
42 struct data_parser *data_parser_create (void);
43 void data_parser_destroy (struct data_parser *);
44
45 enum data_parser_type data_parser_get_type (const struct data_parser *);
46 void data_parser_set_type (struct data_parser *, enum data_parser_type);
47
48 void data_parser_set_skip (struct data_parser *, int initial_records_to_skip);
49
50 /* For configuring delimited parsers only. */
51 bool data_parser_get_span (const struct data_parser *);
52 void data_parser_set_span (struct data_parser *, bool may_cases_span_records);
53
54 void data_parser_set_empty_line_has_field (struct data_parser *,
55                                            bool empty_line_has_field);
56 void data_parser_set_warn_missing_fields (struct data_parser *parser,
57                                           bool warn_missing_fields);
58
59 void data_parser_set_quotes (struct data_parser *, struct substring);
60 void data_parser_set_quote_escape (struct data_parser *, bool escape);
61 void data_parser_set_soft_delimiters (struct data_parser *, struct substring);
62 void data_parser_set_hard_delimiters (struct data_parser *, struct substring);
63
64 /* For configuring fixed parsers only. */
65 int data_parser_get_records (const struct data_parser *);
66 void data_parser_set_records (struct data_parser *, int records_per_case);
67
68 /* Field setup and parsing. */
69 void data_parser_add_delimited_field (struct data_parser *,
70                                       const struct fmt_spec *, int fv,
71                                       const char *name);
72 void data_parser_add_fixed_field (struct data_parser *,
73                                   const struct fmt_spec *, int fv,
74                                   const char *name,
75                                   int record, int first_column);
76 bool data_parser_any_fields (const struct data_parser *);
77 bool data_parser_parse (struct data_parser *, struct dfm_reader *,
78                         struct dictionary *, struct ccase *);
79 size_t data_parser_split (const struct data_parser *, struct substring line,
80                           struct string_array *);
81
82 /* Uses for a configured parser. */
83 void data_parser_output_description (struct data_parser *,
84                                      const struct file_handle *);
85 struct casereader;
86 void data_parser_make_active_file (struct data_parser *, struct dataset *,
87                                    struct dfm_reader *, struct dictionary *,
88                                    struct casereader* (*func)(struct casereader *,
89                                                               const struct dictionary *,
90                                                               void *),
91                                    void *ud);
92
93
94 #endif /* language/data-io/data-parser.h */