2117
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_octave_stream_h) |
|
24 #define octave_octave_stream_h 1 |
|
25 |
|
26 #include <string> |
|
27 |
|
28 #include <iostream.h> |
|
29 #include <strstream.h> |
|
30 |
|
31 #include "Array.h" |
2317
|
32 #include "data-conv.h" |
|
33 #include "mach-info.h" |
2117
|
34 |
|
35 #include "oct-obj.h" |
|
36 #include "str-vec.h" |
|
37 |
|
38 struct |
|
39 scanf_format_elt |
|
40 { |
2215
|
41 scanf_format_elt (const char *txt = 0, int w = 0, bool d = false, |
2117
|
42 char typ = '\0', char mod = '\0') |
2215
|
43 : text (txt), width (w), discard (d), type (typ), modifier (mod) { } |
2117
|
44 |
|
45 ~scanf_format_elt (void) { delete text; } |
|
46 |
|
47 const char *text; |
2215
|
48 int width; |
2117
|
49 bool discard; |
|
50 char type; |
|
51 char modifier; |
|
52 }; |
|
53 |
|
54 class |
|
55 scanf_format_list |
|
56 { |
|
57 public: |
|
58 |
|
59 scanf_format_list (const string& fmt = string ()); |
|
60 |
|
61 ~scanf_format_list (void); |
|
62 |
|
63 int num_conversions (void) { return nconv; } |
|
64 |
2215
|
65 // The length can be different than the number of conversions. |
|
66 // For example, "x %d y %d z" has 2 conversions but the length of |
|
67 // the list is 3 because of the characters that appear after the |
|
68 // last conversion. |
|
69 |
|
70 int length (void) { return list.length (); } |
|
71 |
2117
|
72 const scanf_format_elt *first (void) |
|
73 { |
|
74 curr_idx = 0; |
|
75 return current (); |
|
76 } |
|
77 |
|
78 const scanf_format_elt *current (void) const |
|
79 { return list.length () > 0 ? list.elem (curr_idx) : 0; } |
|
80 |
|
81 const scanf_format_elt *next (void) |
|
82 { |
|
83 curr_idx++; |
|
84 if (curr_idx >= list.length ()) |
|
85 curr_idx = 0; |
|
86 return current (); |
|
87 } |
|
88 |
|
89 void printme (void) const; |
|
90 |
|
91 bool ok (void) const { return (nconv >= 0); } |
|
92 |
2800
|
93 operator void* () const |
|
94 { |
|
95 return ok () |
|
96 ? static_cast<void *> (-1) : static_cast<void *> (0); |
|
97 } |
2117
|
98 |
|
99 bool all_character_conversions (void); |
|
100 |
|
101 bool all_numeric_conversions (void); |
|
102 |
|
103 private: |
|
104 |
|
105 // Number of conversions specified by this format string, or -1 if |
|
106 // invalid conversions have been found. |
|
107 int nconv; |
|
108 |
|
109 // Index to current element; |
|
110 int curr_idx; |
|
111 |
|
112 // List of format elements. |
|
113 Array<scanf_format_elt*> list; |
|
114 |
|
115 // Temporary buffer. |
|
116 ostrstream *buf; |
|
117 |
2215
|
118 void add_elt_to_list (int width, bool discard, char type, char modifier, |
2117
|
119 int& num_elts); |
|
120 |
2215
|
121 void process_conversion (const string& s, int& i, int n, int& width, |
|
122 bool& discard, char& type, char& modifier, |
|
123 int& num_elts); |
2117
|
124 |
2215
|
125 int finish_conversion (const string& s, int& i, int n, int& width, |
|
126 bool discard, char& type, char modifier, |
|
127 int& num_elts); |
2117
|
128 // No copying! |
|
129 |
|
130 scanf_format_list (const scanf_format_list&); |
|
131 |
|
132 scanf_format_list& operator = (const scanf_format_list&); |
|
133 }; |
|
134 |
|
135 struct |
|
136 printf_format_elt |
|
137 { |
|
138 printf_format_elt (const char *txt = 0, int n = 0, char typ = '\0', |
|
139 char mod = '\0') |
|
140 : text (txt), args (n), type (typ), modifier (mod) { } |
|
141 |
|
142 ~printf_format_elt (void) { delete text; } |
|
143 |
|
144 const char *text; |
|
145 int args; |
|
146 char type; |
|
147 char modifier; |
|
148 }; |
|
149 |
|
150 class |
|
151 printf_format_list |
|
152 { |
|
153 public: |
|
154 |
|
155 printf_format_list (const string& fmt = string ()); |
|
156 |
|
157 ~printf_format_list (void); |
|
158 |
|
159 int num_conversions (void) { return nconv; } |
|
160 |
|
161 const printf_format_elt *first (void) |
|
162 { |
|
163 curr_idx = 0; |
|
164 return current (); |
|
165 } |
|
166 |
|
167 const printf_format_elt *current (void) const |
|
168 { return list.length () > 0 ? list.elem (curr_idx) : 0; } |
|
169 |
|
170 const printf_format_elt *next (void) |
|
171 { |
|
172 curr_idx++; |
|
173 if (curr_idx >= list.length ()) |
|
174 curr_idx = 0; |
|
175 return current (); |
|
176 } |
|
177 |
|
178 void printme (void) const; |
|
179 |
|
180 bool ok (void) const { return (nconv >= 0); } |
|
181 |
2800
|
182 operator void* () const |
|
183 { |
|
184 return ok () |
|
185 ? static_cast<void *> (-1) : static_cast<void *> (0); |
|
186 } |
2117
|
187 |
|
188 private: |
|
189 |
|
190 // Number of conversions specified by this format string, or -1 if |
|
191 // invalid conversions have been found. |
|
192 int nconv; |
|
193 |
|
194 // Index to current element; |
|
195 int curr_idx; |
|
196 |
|
197 // List of format elements. |
|
198 Array<printf_format_elt*> list; |
|
199 |
|
200 // Temporary buffer. |
|
201 ostrstream *buf; |
|
202 |
|
203 void add_elt_to_list (int args, char type, char modifier, |
|
204 int& num_elts); |
|
205 |
|
206 void process_conversion (const string& s, int& i, int n, int& args, |
|
207 char& modifier, char& type, int& num_elts); |
|
208 |
|
209 void finish_conversion (const string& s, int& i, int args, |
|
210 char modifier, char& type, int& num_elts); |
|
211 |
|
212 // No copying! |
|
213 |
|
214 printf_format_list (const printf_format_list&); |
|
215 |
|
216 printf_format_list& operator = (const printf_format_list&); |
|
217 }; |
|
218 |
|
219 // Provide an interface for Octave streams. |
|
220 |
|
221 class |
|
222 octave_base_stream |
|
223 { |
|
224 friend class octave_stream; |
|
225 |
|
226 public: |
|
227 |
|
228 octave_base_stream (ios::openmode arg_md = ios::in|ios::out, |
2317
|
229 oct_mach_info::float_format ff = oct_mach_info::native) |
|
230 : md (arg_md), flt_fmt (ff), fail (false) { } |
2117
|
231 |
|
232 virtual ~octave_base_stream (void) { } |
|
233 |
|
234 // The remaining functions are not specific to input or output only, |
|
235 // and must be provided by the derived classes. |
|
236 |
|
237 // Position a stream at OFFSET relative to ORIGIN. |
|
238 |
2610
|
239 virtual int seek (streamoff offset, ios::seek_dir origin) = 0; |
2117
|
240 |
|
241 // Return current stream position. |
|
242 |
|
243 virtual long tell (void) const = 0; |
|
244 |
|
245 // Return non-zero if EOF has been reached on this stream. |
|
246 |
|
247 virtual bool eof (void) const = 0; |
|
248 |
|
249 // The name of the file. |
|
250 |
|
251 virtual string name (void) = 0; |
|
252 |
|
253 // If the derived class provides this function and it returns a |
|
254 // pointer to a valid istream, scanf(), read(), getl(), and gets() |
|
255 // will automatically work for this stream. |
|
256 |
|
257 virtual istream *input_stream (void) { return 0; } |
|
258 |
|
259 // If the derived class provides this function and it returns a |
|
260 // pointer to a valid ostream, flush(), write(), and printf() will |
|
261 // automatically work for this stream. |
|
262 |
|
263 virtual ostream *output_stream (void) { return 0; } |
|
264 |
|
265 bool ok (void) const { return ! fail; } |
|
266 |
|
267 // Return current error message for this stream. |
|
268 |
2435
|
269 string error (bool clear, int& err_num); |
2117
|
270 |
|
271 protected: |
|
272 |
|
273 int mode (void) { return md; } |
|
274 |
2317
|
275 oct_mach_info::float_format float_format (void) { return flt_fmt; } |
2117
|
276 |
|
277 // Set current error state and set fail to TRUE. |
|
278 |
|
279 void error (const string& msg); |
|
280 |
|
281 // Clear any error message and set fail to FALSE. |
|
282 |
|
283 void clear (void); |
|
284 |
|
285 private: |
|
286 |
|
287 // The permission bits for the file. Should be some combination of |
|
288 // ios::open_mode bits. |
|
289 int md; |
|
290 |
|
291 // Data format. |
2317
|
292 oct_mach_info::float_format flt_fmt; |
2117
|
293 |
|
294 // TRUE if an error has occurred. |
|
295 bool fail; |
|
296 |
|
297 // Should contain error message if fail is TRUE. |
|
298 string errmsg; |
|
299 |
|
300 // Functions that are defined for all input streams (input streams |
|
301 // are those that define is). |
|
302 |
2317
|
303 string do_gets (int max_len, bool& err, bool strip_newline, |
|
304 const char *fcn); |
2117
|
305 |
|
306 string getl (int max_len, bool& err); |
|
307 string gets (int max_len, bool& err); |
|
308 |
2317
|
309 octave_value do_read (int nr, int nc, oct_data_conv::data_type dt, |
|
310 int skip, oct_mach_info::float_format flt_fmt, |
|
311 int& count); |
2117
|
312 |
2317
|
313 octave_value read (const Matrix& size, oct_data_conv::data_type dt, |
|
314 int skip, oct_mach_info::float_format flt_fmt, |
|
315 int& count); |
2117
|
316 |
|
317 octave_value do_char_scanf (scanf_format_list& fmt_list, |
|
318 int nr, int nc, int& count); |
|
319 |
|
320 octave_value do_real_scanf (scanf_format_list& fmt_list, |
|
321 int nr, int nc, int& count); |
|
322 |
|
323 octave_value do_scanf (scanf_format_list& fmt_list, int nr, int nc, |
|
324 int& count); |
|
325 |
|
326 octave_value scanf (const string& fmt, const Matrix& size, int& count); |
|
327 |
2712
|
328 bool do_oscanf (const scanf_format_elt *elt, octave_value&); |
2215
|
329 |
|
330 octave_value_list oscanf (const string& fmt); |
|
331 |
2117
|
332 // Functions that are defined for all output streams (output streams |
|
333 // are those that define os). |
|
334 |
|
335 int flush (void); |
|
336 |
2317
|
337 int do_write (const Matrix& m, oct_data_conv::data_type dt, int skip, |
|
338 oct_mach_info::float_format flt_fmt); |
2117
|
339 |
2317
|
340 int write (const octave_value& data, oct_data_conv::data_type dt, |
|
341 int skip, oct_mach_info::float_format flt_fmt); |
2117
|
342 |
|
343 int do_printf (printf_format_list& fmt_list, const octave_value_list& args); |
|
344 |
|
345 int printf (const string& fmt, const octave_value_list& args); |
|
346 |
|
347 int puts (const string& s); |
|
348 |
|
349 // We can always do this in terms of seek(), so the derived class |
|
350 // only has to provide that. |
|
351 |
|
352 int rewind (void); |
|
353 |
|
354 void invalid_operation (const char *op, const char *rw); |
|
355 |
|
356 // No copying! |
|
357 |
|
358 octave_base_stream (const octave_base_stream&); |
|
359 |
|
360 octave_base_stream& operator = (const octave_base_stream&); |
|
361 }; |
|
362 |
|
363 class |
|
364 octave_stream |
|
365 { |
|
366 public: |
|
367 |
2148
|
368 octave_stream (octave_base_stream *bs = 0, bool pf = false) |
|
369 : rep (bs), preserve (pf) { } |
2117
|
370 |
2148
|
371 ~octave_stream (void) |
|
372 { |
|
373 if (! preserve) |
|
374 delete rep; |
|
375 } |
2117
|
376 |
|
377 int flush (void); |
|
378 |
|
379 string getl (int max_len, bool& err); |
|
380 string getl (const octave_value& max_len, bool& err); |
|
381 |
|
382 string gets (int max_len, bool& err); |
|
383 string gets (const octave_value& max_len, bool& err); |
|
384 |
2610
|
385 int seek (streamoff offset, ios::seek_dir origin); |
2117
|
386 int seek (const octave_value& offset, const octave_value& origin); |
|
387 |
|
388 long tell (void) const; |
|
389 |
|
390 int rewind (void); |
|
391 |
2317
|
392 octave_value read (const Matrix& size, oct_data_conv::data_type dt, |
|
393 int skip, oct_mach_info::float_format flt_fmt, |
2117
|
394 int& count); |
|
395 |
2317
|
396 int write (const octave_value& data, oct_data_conv::data_type dt, |
|
397 int skip, oct_mach_info::float_format flt_fmt); |
2117
|
398 |
|
399 octave_value scanf (const string& fmt, const Matrix& size, int& count); |
|
400 |
2215
|
401 octave_value_list oscanf (const string& fmt); |
|
402 |
2117
|
403 int printf (const string& fmt, const octave_value_list& args); |
|
404 |
|
405 int puts (const string& s); |
|
406 int puts (const octave_value& s); |
|
407 |
|
408 bool eof (void) const; |
|
409 |
2435
|
410 string error (bool clear, int& err_num); |
2117
|
411 |
|
412 string error (bool clear = false) |
|
413 { |
2435
|
414 int err_num; |
|
415 return error (clear, err_num); |
2117
|
416 } |
|
417 |
|
418 bool ok (void) const { return rep && rep->ok (); } |
|
419 |
2800
|
420 operator void* () const |
|
421 { |
|
422 return ok () |
|
423 ? static_cast<void *> (-1) : static_cast<void *> (0); |
|
424 } |
2117
|
425 |
|
426 string name (void); |
|
427 |
|
428 int mode (void); |
|
429 |
2317
|
430 oct_mach_info::float_format float_format (void); |
2117
|
431 |
|
432 static string mode_as_string (int mode); |
|
433 |
|
434 private: |
|
435 |
|
436 // The actual representation of this stream. |
|
437 octave_base_stream *rep; |
|
438 |
2225
|
439 // If true, do not delete rep. |
2148
|
440 bool preserve; |
|
441 |
2117
|
442 void invalid_stream_error (const char *op) const; |
|
443 |
|
444 bool stream_ok (const char *op, bool clear = true) const |
|
445 { |
|
446 bool retval = true; |
|
447 |
|
448 if (rep) |
|
449 { |
|
450 if (clear) |
|
451 rep->clear (); |
|
452 } |
|
453 else |
|
454 { |
|
455 retval = false; |
|
456 invalid_stream_error (op); |
|
457 } |
|
458 |
|
459 return retval; |
|
460 } |
|
461 |
|
462 void error (const string& msg) |
|
463 { |
|
464 if (rep) |
|
465 rep->error (msg); |
|
466 } |
|
467 |
|
468 // Must create named streams. |
|
469 |
|
470 octave_stream (void); |
|
471 |
|
472 // No copying! |
|
473 |
|
474 octave_stream (const octave_stream&); |
|
475 |
|
476 octave_stream& operator = (const octave_stream&); |
|
477 }; |
|
478 |
|
479 class |
|
480 octave_stream_list |
|
481 { |
|
482 protected: |
|
483 |
|
484 octave_stream_list (void) : list (32), curr_len (0) { } |
|
485 |
|
486 public: |
|
487 |
|
488 ~octave_stream_list (void) { } |
|
489 |
|
490 static int insert (octave_base_stream *obs); |
|
491 |
|
492 static octave_stream *lookup (int fid); |
|
493 static octave_stream *lookup (const octave_value& fid); |
|
494 |
|
495 static int remove (int fid); |
|
496 static int remove (const octave_value& fid); |
|
497 |
|
498 static void clear (void); |
|
499 |
|
500 static string_vector get_info (int fid); |
|
501 static string_vector get_info (const octave_value& fid); |
|
502 |
|
503 static string list_open_files (void); |
|
504 |
|
505 static octave_value open_file_numbers (void); |
|
506 |
2609
|
507 static int get_file_number (const octave_value& fid); |
|
508 |
2117
|
509 private: |
|
510 |
|
511 Array<octave_stream*> list; |
|
512 |
|
513 int curr_len; |
|
514 |
|
515 static octave_stream_list *instance; |
|
516 |
|
517 int do_insert (octave_base_stream *obs); |
|
518 |
|
519 octave_stream *do_lookup (int fid) const; |
|
520 octave_stream *do_lookup (const octave_value& fid) const; |
|
521 |
|
522 int do_remove (int fid); |
|
523 int do_remove (const octave_value& fid); |
|
524 |
|
525 void do_clear (void); |
|
526 |
|
527 string_vector do_get_info (int fid) const; |
|
528 string_vector do_get_info (const octave_value& fid) const; |
|
529 |
|
530 string do_list_open_files (void) const; |
|
531 |
|
532 octave_value do_open_file_numbers (void) const; |
|
533 |
2609
|
534 int do_get_file_number (const octave_value& fid) const; |
2117
|
535 }; |
|
536 |
|
537 #endif |
|
538 |
|
539 /* |
|
540 ;;; Local Variables: *** |
|
541 ;;; mode: C++ *** |
|
542 ;;; End: *** |
|
543 */ |