26#if ! defined (octave_oct_stream_h)
27#define octave_oct_stream_h 1
29#include "octave-config.h"
58class scanf_format_elt;
59class scanf_format_list;
61class printf_format_elt;
62class printf_format_list;
66class OCTINTERP_API wbuffer_u8_converter;
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 ()
93 virtual int seek (off_t offset,
int origin) = 0;
101 virtual bool eof ()
const = 0;
105 virtual std::string
name ()
const = 0;
124 if (! m_encoding.compare (
"utf-8"))
125 return output_stream ();
128 return m_conv_ostream.get ();
130 return create_converter_stream ();
135 bool is_open ()
const {
return m_open_state; }
143 m_open_state =
false;
152 if (name () ==
"stdin")
154 else if (name () ==
"stdout")
156 else if (name () ==
"stderr")
162 bool ok ()
const {
return ! m_fail; }
166 std::string
error (
bool clear,
int& err_num);
170 int mode ()
const {
return m_mode; }
174 std::string
encoding ()
const {
return m_encoding; }
178 OCTINTERP_API
void error (
const std::string& msg);
179 OCTINTERP_API
void error (
const std::string& who,
const std::string& msg);
183 OCTINTERP_API
void clear ();
187 OCTINTERP_API
void clearerr ();
196 mach_info::float_format m_flt_fmt;
199 std::string m_encoding;
202 wbuffer_u8_converter *m_converter;
207 std::unique_ptr<std::ostream> m_conv_ostream;
216 std::string m_errmsg;
218 OCTINTERP_API std::ostream * create_converter_stream ();
223 OCTINTERP_API std::string
225 const std::string& who );
227 OCTINTERP_API std::string
229 const std::string& who );
230 OCTINTERP_API std::string
232 const std::string& who );
234 skipl (off_t count,
bool& err,
const std::string& who );
247 const std::string& who );
250 oscanf (
const std::string& fmt,
const std::string& who );
260 OCTINTERP_API
int flush ();
263 do_numeric_printf_conv (std::ostream& os,
const printf_format_elt *elt,
264 int nsa,
int sa_1,
int sa_2,
266 const std::string& who);
268 OCTINTERP_API
void field_width_error (
const std::string& who)
const;
272 const std::string& who );
276 const std::string& who );
279 puts (
const std::string& s,
const std::string& who );
285 invalid_operation (
const std::string& who,
const char *rw);
301 OCTINTERP_API
int flush ();
303 OCTINTERP_API std::string
305 const std::string& who );
307 OCTINTERP_API std::string
309 const std::string& who );
311 OCTINTERP_API std::string
313 const std::string& who );
315 OCTINTERP_API std::string
317 const std::string& who );
320 skipl (off_t count,
bool& err,
const std::string& who );
324 const std::string& who );
326 OCTINTERP_API
int seek (off_t offset,
int origin);
331 OCTINTERP_API off_t tell ();
333 OCTINTERP_API
int rewind ();
335 OCTINTERP_API
bool is_open ()
const;
337 OCTINTERP_API
void close ();
351 OCTINTERP_API
bool write_bytes (
const void *data, std::size_t n_elts);
353 OCTINTERP_API
bool skip_bytes (std::size_t n_elts);
355 template <
typename T>
370 oscanf (
const std::string& fmt,
const std::string& who );
373 oscanf (
const octave_value& fmt,
const std::string& who );
382 const std::string& who );
386 const std::string& who );
389 puts (
const std::string& s,
const std::string& who );
393 OCTINTERP_API
bool eof ()
const;
395 OCTINTERP_API std::string
error (
bool clear,
int& err_num);
397 std::string
error (
bool clear =
false)
400 return error (clear, err_num);
417 bool ok ()
const {
return m_rep && m_rep->ok (); }
419 operator bool ()
const {
return ok (); }
421 OCTINTERP_API std::string name ()
const;
423 OCTINTERP_API
int mode ()
const;
425 OCTINTERP_API mach_info::float_format
float_format ()
const;
427 OCTINTERP_API
static std::string mode_as_string (
int mode);
431 return m_rep ? m_rep->encoding () : std::string ();
436 return m_rep ? m_rep->input_stream () :
nullptr;
441 return (m_rep ? m_rep->output_stream () :
nullptr);
447 return (m_rep ? m_rep->preferred_output_stream () :
nullptr);
450 void clearerr () {
if (m_rep) m_rep->clearerr (); }
455 std::shared_ptr<base_stream> m_rep;
457 bool stream_ok (
bool clear =
true)
const
472 void invalid_operation (
const std::string& who,
const char *rw)
475 m_rep->invalid_operation (who, rw);
479 finalize_read (std::list<void *>& input_buf_list,
485 mach_info::float_format ffmt);
498 OCTINTERP_API
int insert (
stream& os);
500 OCTINTERP_API
stream lookup (
int fid,
const std::string& who =
"")
const;
504 OCTINTERP_API
int remove (
int fid,
const std::string& who =
"");
506 const std::string& who =
"");
508 OCTINTERP_API
void clear (
bool flush =
true);
513 OCTINTERP_API std::string list_open_files ()
const;
517 OCTINTERP_API
int get_file_number (
const octave_value& fid)
const;
525 typedef std::map<int, stream> ostrl_map;
529 mutable ostrl_map::const_iterator m_lookup_cache;
536OCTAVE_END_NAMESPACE(octave)
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
N Dimensional Array with copy-on-write semantics.
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")
virtual bool eof() const =0
std::ostream * preferred_output_stream()
mach_info::float_format float_format() const
virtual int seek(off_t offset, int origin)=0
virtual std::string name() const =0
std::string encoding() const
virtual std::ostream * output_stream()
virtual std::istream * input_stream()
virtual int file_number() const
std::ostream * output_stream()
std::string error(bool clear=false)
std::ostream * preferred_output_stream()
std::istream * input_stream()
void error(const std::string &msg)
stream(base_stream *bs=nullptr)
void error(const char *msg)
stream(const stream &)=default
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)