comparison libinterp/corefcn/oct-stream.h @ 21503:20bf0ec536e2

integrate textscan more completely with octave_stream class * oct-stream.h, oct-stream.cc (octave_stream::textscan, octave_base_stream::do_textscan): New functions. * oct-stream.cc (textscan): Move class declaration here. * oct-stream.h: From here. * oct-stream.cc (textscan_format_list, textscan): New data member for error reporting. * file-io.cc (Ftextscan): Parse format and ntimes arguments here. Call octave_stream::textscan.
author John W. Eaton <jwe@octave.org>
date Sat, 19 Mar 2016 12:19:06 -0400
parents 7a19c5678f91
children 5b9868c2e212
comparison
equal deleted inserted replaced
21502:7a19c5678f91 21503:20bf0ec536e2
37 class scanf_format_list; 37 class scanf_format_list;
38 38
39 class printf_format_elt; 39 class printf_format_elt;
40 class printf_format_list; 40 class printf_format_list;
41 41
42 class delimited_stream;
43 class textscan_format_elt;
44 class textscan_format_list;
45
46 // These only appear as reference arguments or return values. 42 // These only appear as reference arguments or return values.
47 43
48 template <typename T> class Array; 44 template <typename T> class Array;
45 class Cell;
49 class octave_value_list; 46 class octave_value_list;
50 class string_vector; 47 class string_vector;
51 48
52 #include "data-conv.h" 49 #include "data-conv.h"
53 #include "mach-info.h" 50 #include "mach-info.h"
54 #include "oct-refcount.h" 51 #include "oct-refcount.h"
55 52
56 #include "Cell.h"
57 #include "ov.h" 53 #include "ov.h"
58
59 // Main class to implement textscan. Read data and parse it
60 // according to a format.
61 //
62 // The calling sequence is
63 //
64 // textscan scanner ();
65 // scanner.scan (...);
66
67 class
68 OCTINTERP_API
69 textscan
70 {
71 public:
72
73 textscan (void);
74
75 ~textscan (void) { }
76
77 octave_value scan (std::istream& isp, const octave_value_list& args);
78
79 octave_value scan (std::istream& isp, const octave_value_list& args,
80 octave_idx_type& count);
81
82 private:
83
84 friend class textscan_format_list;
85
86 std::string buf;
87
88 // Three cases for delim_table and delim_list
89 // 1. delim_table empty, delim_list empty: whitespace delimiters
90 // 2. delim_table = look-up table of delim chars, delim_list empty.
91 // 3. delim_table non-empty, delim_list = Cell array of delim strings
92
93 std::string whitespace_table;
94
95 // delim_table[i] == '\0' if i is not a delimiter.
96 std::string delim_table;
97
98 // String of delimiter characters.
99 std::string delims;
100
101 Cell comment_style;
102
103 // How far ahead to look to detect an open comment.
104 int comment_len;
105
106 // First character of open comment.
107 int comment_char;
108
109 octave_idx_type buffer_size;
110
111 std::string date_locale;
112
113 // 'inf' and 'nan' for formatted_double.
114 Cell inf_nan;
115
116 // Array of strings of delimiters.
117 Cell delim_list;
118
119 // Longest delimiter.
120 int delim_len;
121
122 octave_value empty_value;
123 std::string exp_chars;
124 int header_lines;
125 Cell treat_as_empty;
126
127 // Longest string to treat as "N/A".
128 int treat_as_empty_len;
129
130 std::string whitespace;
131
132 short eol1;
133 short eol2;
134 short return_on_error;
135
136 bool collect_output;
137 bool multiple_delims_as_one;
138 bool default_exp;
139 bool numeric_delim;
140
141 octave_idx_type lines;
142
143 octave_value do_scan (std::istream& isp, textscan_format_list& fmt_list,
144 octave_idx_type ntimes);
145
146 void parse_options (const octave_value_list& args,
147 textscan_format_list& fmt_list);
148
149 int read_format_once (delimited_stream& isp, textscan_format_list& fmt_list,
150 std::list<octave_value>& retval,
151 Array<octave_idx_type> row, int& done_after);
152
153 void scan_one (delimited_stream& is, const textscan_format_elt& fmt,
154 octave_value& ov, Array<octave_idx_type> row);
155
156 // Methods to process a particular conversion specifier.
157 double read_double (delimited_stream& is,
158 const textscan_format_elt& fmt) const;
159
160 void scan_complex (delimited_stream& is, const textscan_format_elt& fmt,
161 Complex& val) const;
162
163 int scan_bracket (delimited_stream& is, const std::string& pattern,
164 std::string& val) const;
165
166 int scan_caret (delimited_stream& is, const std::string& pattern,
167 std::string& val) const;
168
169 void scan_string (delimited_stream& is, const textscan_format_elt& fmt,
170 std::string& val) const;
171
172 void scan_cstring (delimited_stream& is, const textscan_format_elt& fmt,
173 std::string& val) const;
174
175 void scan_qstring (delimited_stream& is, const textscan_format_elt& fmt,
176 std::string& val);
177
178 // Helper methods.
179 std::string read_until (delimited_stream& is, const Cell& delimiters,
180 const std::string& ends) const;
181
182 int lookahead (delimited_stream& is, const Cell& targets, int max_len,
183 bool case_sensitive = true) const;
184
185 bool match_literal (delimited_stream& isp, const textscan_format_elt& elem);
186
187 int skip_whitespace (delimited_stream& is, bool EOLstop = false);
188
189 int skip_delim (delimited_stream& is);
190
191 bool is_delim (unsigned char ch) const
192 {
193 return ((delim_table.empty () && (isspace (ch) || ch == eol1 || ch == eol2))
194 || delim_table[ch] != '\0');
195 }
196
197 bool isspace (unsigned int ch) const { return whitespace_table[ch & 0xff]; }
198
199 // True if the only delimiter is whitespace.
200 bool whitespace_delim (void) const { return delim_table.empty (); }
201
202 // No copying!
203
204 textscan (const textscan&);
205
206 textscan& operator = (const textscan&);
207 };
208 54
209 // Provide an interface for Octave streams. 55 // Provide an interface for Octave streams.
210 56
211 class 57 class
212 OCTINTERP_API 58 OCTINTERP_API
355 const std::string& who /* = "scanf" */); 201 const std::string& who /* = "scanf" */);
356 202
357 octave_value_list oscanf (const std::string& fmt, 203 octave_value_list oscanf (const std::string& fmt,
358 const std::string& who /* = "scanf" */); 204 const std::string& who /* = "scanf" */);
359 205
206 octave_value do_textscan (const std::string& fmt, octave_idx_type ntimes,
207 const octave_value_list& options,
208 const std::string& who, octave_idx_type& count);
209
360 // Functions that are defined for all output streams (output streams 210 // Functions that are defined for all output streams (output streams
361 // are those that define os). 211 // are those that define os).
362 212
363 int flush (void); 213 int flush (void);
364 214
458 octave_value_list oscanf (const std::string& fmt, 308 octave_value_list oscanf (const std::string& fmt,
459 const std::string& who /* = "scanf" */); 309 const std::string& who /* = "scanf" */);
460 310
461 octave_value_list oscanf (const octave_value& fmt, 311 octave_value_list oscanf (const octave_value& fmt,
462 const std::string& who /* = "scanf" */); 312 const std::string& who /* = "scanf" */);
313
314 octave_value textscan (const std::string& fmt, octave_idx_type ntimes,
315 const octave_value_list& options,
316 const std::string& who, octave_idx_type& count);
463 317
464 int printf (const std::string& fmt, const octave_value_list& args, 318 int printf (const std::string& fmt, const octave_value_list& args,
465 const std::string& who /* = "printf" */); 319 const std::string& who /* = "printf" */);
466 320
467 int printf (const octave_value& fmt, const octave_value_list& args, 321 int printf (const octave_value& fmt, const octave_value_list& args,