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