GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-stream.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_oct_stream_h)
27#define octave_oct_stream_h 1
28
29#include "octave-config.h"
30
31#include <ios>
32#include <iosfwd>
33#include <list>
34#include <map>
35#include <memory>
36#include <string>
37
38#include "oct-string.h"
39
40// These only appear as reference arguments or return values.
41
42class Cell;
43class octave_value;
45class string_vector;
46
47#include "Array.h"
48#include "data-conv.h"
49#include "mach-info.h"
50
52
53class interpreter;
54
55// These are only needed as arguments to private functions, so they
56// are also treated as private.
57
58class scanf_format_elt;
59class scanf_format_list;
60
61class printf_format_elt;
62class printf_format_list;
63
64// This is a class that inherits from with std::wbuffer_convert which is
65// deprecated in C++17.
66class OCTINTERP_API wbuffer_u8_converter;
67
68// Provide an interface for Octave streams.
69
70class OCTINTERP_API base_stream
71{
72 friend class stream;
73
74public:
75
76 base_stream (std::ios::openmode arg_md = std::ios::in | std::ios::out,
77 mach_info::float_format ff = mach_info::native_float_format (),
78 const std::string& encoding = "utf-8")
79 : m_mode (arg_md), m_flt_fmt (ff), m_encoding (encoding),
80 m_converter (nullptr), m_conv_ostream (nullptr), m_fail (false),
81 m_open_state (true), m_errmsg ()
82 { }
83
84 OCTAVE_DISABLE_COPY_MOVE (base_stream)
85
86 virtual OCTINTERP_API ~base_stream ();
87
88 // The remaining functions are not specific to input or output only,
89 // and must be provided by the derived classes.
90
91 // Position a stream at OFFSET relative to ORIGIN.
92
93 virtual int seek (off_t offset, int origin) = 0;
94
95 // Return current stream position.
96
97 virtual off_t tell () = 0;
98
99 // Return TRUE if EOF has been reached on this stream.
100
101 virtual bool eof () const = 0;
102
103 // The name of the file.
104
105 virtual std::string name () const = 0;
106
107 // If the derived class provides this function and it returns a
108 // pointer to a valid istream, scanf(), read(), getl(), and gets()
109 // will automatically work for this stream.
110
111 virtual std::istream * input_stream () { return nullptr; }
112
113 // If the derived class provides this function and it returns a
114 // pointer to a valid ostream, flush(), write(), and printf() will
115 // automatically work for this stream.
116
117 virtual std::ostream * output_stream () { return nullptr; }
118
119 // Return either the original output stream or one wrapped with the
120 // encoding facet.
121
122 std::ostream * preferred_output_stream ()
123 {
124 if (! m_encoding.compare ("utf-8"))
125 return output_stream ();
126
127 if (m_conv_ostream)
128 return m_conv_ostream.get ();
129
130 return create_converter_stream ();
131 }
132
133 // Return TRUE if this stream is open.
134
135 bool is_open () const { return m_open_state; }
136
137 virtual void do_close () { }
138
139 void close ()
140 {
141 if (is_open ())
142 {
143 m_open_state = false;
144 do_close ();
145 }
146 }
147
148 virtual int file_number () const
149 {
150 // Kluge alert!
151
152 if (name () == "stdin")
153 return 0;
154 else if (name () == "stdout")
155 return 1;
156 else if (name () == "stderr")
157 return 2;
158 else
159 return -1;
160 }
161
162 bool ok () const { return ! m_fail; }
163
164 // Return current error message for this stream.
165
166 std::string error (bool clear, int& err_num);
167
168protected:
169
170 int mode () const { return m_mode; }
171
172 mach_info::float_format float_format () const { return m_flt_fmt; }
173
174 std::string encoding () const { return m_encoding; }
175
176 // Set current error state and set fail to TRUE.
177
178 OCTINTERP_API void error (const std::string& msg);
179 OCTINTERP_API void error (const std::string& who, const std::string& msg);
180
181 // Clear any error message and set fail to FALSE.
182
183 OCTINTERP_API void clear ();
184
185 // Clear stream state.
186
187 OCTINTERP_API void clearerr ();
188
189private:
190
191 // The permission bits for the file. Should be some combination of
192 // std::ios::open_mode bits.
193 int m_mode;
194
195 // Data format.
196 mach_info::float_format m_flt_fmt;
197
198 // Code page
199 std::string m_encoding;
200
201 // encoding conversion facet
202 wbuffer_u8_converter *m_converter;
203
204 // wrappers for encoding conversion
205 // std::unique_ptr<std::istream> m_conv_istream;
206
207 std::unique_ptr<std::ostream> m_conv_ostream;
208
209 // TRUE if an error has occurred.
210 bool m_fail;
211
212 // TRUE if this stream is open.
213 bool m_open_state;
214
215 // Should contain error message if fail is TRUE.
216 std::string m_errmsg;
217
218 OCTINTERP_API std::ostream * create_converter_stream ();
219
220 // Functions that are defined for all input streams (input streams
221 // are those that define is).
222
223 OCTINTERP_API std::string
224 do_gets (octave_idx_type max_len, bool& err, bool strip_newline,
225 const std::string& who /* = "gets" */);
226
227 OCTINTERP_API std::string
228 getl (octave_idx_type max_len, bool& err,
229 const std::string& who /* = "getl" */);
230 OCTINTERP_API std::string
231 gets (octave_idx_type max_len, bool& err,
232 const std::string& who /* = "gets" */);
233 OCTINTERP_API off_t
234 skipl (off_t count, bool& err, const std::string& who /* = "skipl" */);
235
236 OCTINTERP_API octave_value
237 do_scanf (scanf_format_list& fmt_list, octave_idx_type nr,
238 octave_idx_type nc, bool one_elt_size_spec,
239 octave_idx_type& count, const std::string& who /* = "scanf" */);
240
241 OCTINTERP_API octave_value
242 scanf (const std::string& fmt, const Array<double>& size,
243 octave_idx_type& count, const std::string& who /* = "scanf" */);
244
245 OCTINTERP_API bool
246 do_oscanf (const scanf_format_elt *elt, octave_value&,
247 const std::string& who /* = "scanf" */);
248
249 OCTINTERP_API octave_value_list
250 oscanf (const std::string& fmt, const std::string& who /* = "scanf" */);
251
252 OCTINTERP_API octave_value
253 do_textscan (const std::string& fmt, octave_idx_type ntimes,
254 const octave_value_list& options,
255 const std::string& who, octave_idx_type& count);
256
257 // Functions that are defined for all output streams (output streams
258 // are those that define os).
259
260 OCTINTERP_API int flush ();
261
262 OCTINTERP_API int
263 do_numeric_printf_conv (std::ostream& os, const printf_format_elt *elt,
264 int nsa, int sa_1, int sa_2,
265 const octave_value& val,
266 const std::string& who);
267
268 OCTINTERP_API void field_width_error (const std::string& who) const;
269
270 OCTINTERP_API int
271 do_printf (printf_format_list& fmt_list, const octave_value_list& args,
272 const std::string& who /* = "printf" */);
273
274 OCTINTERP_API int
275 printf (const std::string& fmt, const octave_value_list& args,
276 const std::string& who /* = "printf" */);
277
278 OCTINTERP_API int
279 puts (const std::string& s, const std::string& who /* = "puts" */);
280
281 // We can always do this in terms of seek(), so the derived class
282 // only has to provide that.
283
284 OCTINTERP_API void
285 invalid_operation (const std::string& who, const char *rw);
286};
287
288class OCTINTERP_API stream
289{
290public:
291
292 // BS must be allocated with new or nullptr.
293 stream (base_stream *bs = nullptr) : m_rep (bs) { }
294
295 stream (const stream&) = default;
296
297 stream& operator = (const stream&) = default;
298
299 ~stream () = default;
300
301 OCTINTERP_API int flush ();
302
303 OCTINTERP_API std::string
304 getl (octave_idx_type max_len, bool& err,
305 const std::string& who /* = "getl" */);
306
307 OCTINTERP_API std::string
308 getl (const octave_value& max_len, bool& err,
309 const std::string& who /* = "getl" */);
310
311 OCTINTERP_API std::string
312 gets (octave_idx_type max_len, bool& err,
313 const std::string& who /* = "gets" */);
314
315 OCTINTERP_API std::string
316 gets (const octave_value& max_len, bool& err,
317 const std::string& who /* = "gets" */);
318
319 OCTINTERP_API off_t
320 skipl (off_t count, bool& err, const std::string& who /* = "skipl" */);
321
322 OCTINTERP_API off_t
323 skipl (const octave_value& count, bool& err,
324 const std::string& who /* = "skipl" */);
325
326 OCTINTERP_API int seek (off_t offset, int origin);
327
328 OCTINTERP_API int
329 seek (const octave_value& offset, const octave_value& origin);
330
331 OCTINTERP_API off_t tell ();
332
333 OCTINTERP_API int rewind ();
334
335 OCTINTERP_API bool is_open () const;
336
337 OCTINTERP_API void close ();
338
339 OCTINTERP_API octave_value
340 read (const Array<double>& size, octave_idx_type block_size,
341 oct_data_conv::data_type input_type,
342 oct_data_conv::data_type output_type,
343 octave_idx_type skip, mach_info::float_format flt_fmt,
344 octave_idx_type& count);
345
346 OCTINTERP_API octave_idx_type
347 write (const octave_value& data, octave_idx_type block_size,
348 oct_data_conv::data_type output_type,
349 octave_idx_type skip, mach_info::float_format flt_fmt);
350
351 OCTINTERP_API bool write_bytes (const void *data, std::size_t n_elts);
352
353 OCTINTERP_API bool skip_bytes (std::size_t n_elts);
354
355 template <typename T>
356 OCTINTERP_API octave_idx_type
357 write (const Array<T>& data, octave_idx_type block_size,
358 oct_data_conv::data_type output_type,
359 octave_idx_type skip, mach_info::float_format flt_fmt);
360
361 OCTINTERP_API octave_value
362 scanf (const std::string& fmt, const Array<double>& size,
363 octave_idx_type& count, const std::string& who /* = "scanf" */);
364
365 OCTINTERP_API octave_value
366 scanf (const octave_value& fmt, const Array<double>& size,
367 octave_idx_type& count, const std::string& who /* = "scanf" */);
368
369 OCTINTERP_API octave_value_list
370 oscanf (const std::string& fmt, const std::string& who /* = "scanf" */);
371
372 OCTINTERP_API octave_value_list
373 oscanf (const octave_value& fmt, const std::string& who /* = "scanf" */);
374
375 OCTINTERP_API octave_value
376 textscan (const std::string& fmt, octave_idx_type ntimes,
377 const octave_value_list& options,
378 const std::string& who, octave_idx_type& count);
379
380 OCTINTERP_API int
381 printf (const std::string& fmt, const octave_value_list& args,
382 const std::string& who /* = "printf" */);
383
384 OCTINTERP_API int
385 printf (const octave_value& fmt, const octave_value_list& args,
386 const std::string& who /* = "printf" */);
387
388 OCTINTERP_API int
389 puts (const std::string& s, const std::string& who /* = "puts" */);
390 OCTINTERP_API int
391 puts (const octave_value& s, const std::string& who /* = "puts" */);
392
393 OCTINTERP_API bool eof () const;
394
395 OCTINTERP_API std::string error (bool clear, int& err_num);
396
397 std::string error (bool clear = false)
398 {
399 int err_num;
400 return error (clear, err_num);
401 }
402
403 // Set the error message and state.
404
405 void error (const std::string& msg)
406 {
407 if (m_rep)
408 m_rep->error (msg);
409 }
410
411 void error (const char *msg) { error (std::string (msg)); }
412
413 int file_number () { return m_rep ? m_rep->file_number () : -1; }
414
415 bool is_valid () const { return bool (m_rep); }
416
417 bool ok () const { return m_rep && m_rep->ok (); }
418
419 operator bool () const { return ok (); }
420
421 OCTINTERP_API std::string name () const;
422
423 OCTINTERP_API int mode () const;
424
425 OCTINTERP_API mach_info::float_format float_format () const;
426
427 OCTINTERP_API static std::string mode_as_string (int mode);
428
429 std::string encoding ()
430 {
431 return m_rep ? m_rep->encoding () : std::string ();
432 }
433
434 std::istream * input_stream ()
435 {
436 return m_rep ? m_rep->input_stream () : nullptr;
437 }
438
439 std::ostream * output_stream ()
440 {
441 return (m_rep ? m_rep->output_stream () : nullptr);
442 }
443
444 std::ostream * preferred_output_stream ()
445 {
446 // return output stream with encoding facet if applicable
447 return (m_rep ? m_rep->preferred_output_stream () : nullptr);
448 }
449
450 void clearerr () { if (m_rep) m_rep->clearerr (); }
451
452private:
453
454 // The actual representation of this stream.
455 std::shared_ptr<base_stream> m_rep;
456
457 bool stream_ok (bool clear = true) const
458 {
459 bool retval = true;
460
461 if (m_rep)
462 {
463 if (clear)
464 m_rep->clear ();
465 }
466 else
467 retval = false;
468
469 return retval;
470 }
471
472 void invalid_operation (const std::string& who, const char *rw)
473 {
474 if (m_rep)
475 m_rep->invalid_operation (who, rw);
476 }
477
478 OCTINTERP_API octave_value
479 finalize_read (std::list<void *>& input_buf_list,
480 octave_idx_type input_buf_elts,
481 octave_idx_type elts_read,
483 oct_data_conv::data_type input_type,
484 oct_data_conv::data_type output_type,
485 mach_info::float_format ffmt);
486};
487
488class OCTINTERP_API stream_list
489{
490public:
491
492 OCTINTERP_API stream_list (interpreter& interp);
493
494 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (stream_list)
495
496 OCTINTERP_API ~stream_list ();
497
498 OCTINTERP_API int insert (stream& os);
499
500 OCTINTERP_API stream lookup (int fid, const std::string& who = "") const;
501 OCTINTERP_API stream
502 lookup (const octave_value& fid, const std::string& who = "") const;
503
504 OCTINTERP_API int remove (int fid, const std::string& who = "");
505 OCTINTERP_API int remove (const octave_value& fid,
506 const std::string& who = "");
507
508 OCTINTERP_API void clear (bool flush = true);
509
510 OCTINTERP_API string_vector get_info (int fid) const;
511 OCTINTERP_API string_vector get_info (const octave_value& fid) const;
512
513 OCTINTERP_API std::string list_open_files () const;
514
515 OCTINTERP_API octave_value open_file_numbers () const;
516
517 OCTINTERP_API int get_file_number (const octave_value& fid) const;
518
519 OCTINTERP_API octave_value stdin_file () const;
520 OCTINTERP_API octave_value stdout_file () const;
521 OCTINTERP_API octave_value stderr_file () const;
522
523private:
524
525 typedef std::map<int, stream> ostrl_map;
526
527 ostrl_map m_list;
528
529 mutable ostrl_map::const_iterator m_lookup_cache;
530
531 int m_stdin_file;
532 int m_stdout_file;
533 int m_stderr_file;
534};
535
536OCTAVE_END_NAMESPACE(octave)
537
538#endif
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
N Dimensional Array with copy-on-write semantics.
Definition Array-base.h:130
Definition Cell.h:41
base_stream(std::ios::openmode arg_md=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8")
Definition oct-stream.h:76
bool is_open() const
Definition oct-stream.h:135
virtual bool eof() const =0
std::ostream * preferred_output_stream()
Definition oct-stream.h:122
mach_info::float_format float_format() const
Definition oct-stream.h:172
bool ok() const
Definition oct-stream.h:162
virtual int seek(off_t offset, int origin)=0
void close()
Definition oct-stream.h:139
virtual std::string name() const =0
virtual void do_close()
Definition oct-stream.h:137
std::string encoding() const
Definition oct-stream.h:174
virtual std::ostream * output_stream()
Definition oct-stream.h:117
virtual off_t tell()=0
virtual std::istream * input_stream()
Definition oct-stream.h:111
virtual int file_number() const
Definition oct-stream.h:148
int mode() const
Definition oct-stream.h:170
std::ostream * output_stream()
Definition oct-stream.h:439
std::string error(bool clear=false)
Definition oct-stream.h:397
int file_number()
Definition oct-stream.h:413
bool is_valid() const
Definition oct-stream.h:415
std::ostream * preferred_output_stream()
Definition oct-stream.h:444
std::istream * input_stream()
Definition oct-stream.h:434
void error(const std::string &msg)
Definition oct-stream.h:405
~stream()=default
void clearerr()
Definition oct-stream.h:450
stream(base_stream *bs=nullptr)
Definition oct-stream.h:293
void error(const char *msg)
Definition oct-stream.h:411
stream(const stream &)=default
std::string encoding()
Definition oct-stream.h:429
bool ok() const
Definition oct-stream.h:417
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition error.cc:1008