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