26#if ! defined (octave_oct_stream_h)
27#define octave_oct_stream_h 1
29#include "octave-config.h"
56 class scanf_format_elt;
57 class scanf_format_list;
59 class printf_format_elt;
60 class printf_format_list;
72 base_stream (std::ios::openmode arg_md = std::ios::in | std::ios::out,
74 const std::string& encoding =
"utf-8")
75 : m_mode (arg_md), m_flt_fmt (ff), m_encoding (encoding),
76 m_fail (false), m_open_state (true), m_errmsg ()
92 virtual int seek (off_t offset,
int origin) = 0;
96 virtual off_t
tell (
void) = 0;
100 virtual bool eof (
void)
const = 0;
104 virtual std::string
name (
void)
const = 0;
120 bool is_open (
void)
const {
return m_open_state; }
128 m_open_state =
false;
137 if (
name () ==
"stdin")
139 else if (
name () ==
"stdout")
141 else if (
name () ==
"stderr")
147 bool ok (
void)
const {
return ! m_fail; }
151 std::string
error (
bool clear,
int& err_num);
155 int mode (
void)
const {
return m_mode; }
159 std::string
encoding (
void)
const {
return m_encoding; }
163 OCTINTERP_API
void error (
const std::string& msg);
164 OCTINTERP_API
void error (
const std::string& who,
const std::string& msg);
168 OCTINTERP_API
void clear (
void);
172 OCTINTERP_API
void clearerr (
void);
198 OCTINTERP_API std::string
200 const std::string& who );
202 OCTINTERP_API std::string
204 const std::string& who );
205 OCTINTERP_API std::string
207 const std::string& who );
209 skipl (off_t count,
bool& err,
const std::string& who );
222 const std::string& who );
225 oscanf (
const std::string& fmt,
const std::string& who );
235 OCTINTERP_API
int flush (
void);
239 int nsa,
int sa_1,
int sa_2,
241 const std::string& who);
243 OCTINTERP_API
void field_width_error (
const std::string& who)
const;
247 const std::string& who );
251 const std::string& who );
254 puts (
const std::string& s,
const std::string& who );
260 invalid_operation (
const std::string& who,
const char *rw);
278 OCTINTERP_API
int flush (
void);
280 OCTINTERP_API std::string
282 const std::string& who );
284 OCTINTERP_API std::string
286 const std::string& who );
288 OCTINTERP_API std::string
290 const std::string& who );
292 OCTINTERP_API std::string
294 const std::string& who );
297 skipl (off_t count,
bool& err,
const std::string& who );
301 const std::string& who );
303 OCTINTERP_API
int seek (off_t offset,
int origin);
308 OCTINTERP_API off_t tell (
void);
310 OCTINTERP_API
int rewind (
void);
312 OCTINTERP_API
bool is_open (
void)
const;
314 OCTINTERP_API
void close (
void);
328 OCTINTERP_API
bool write_bytes (
const void *data, std::size_t n_elts);
330 OCTINTERP_API
bool skip_bytes (std::size_t n_elts);
332 template <
typename T>
347 oscanf (
const std::string& fmt,
const std::string& who );
350 oscanf (
const octave_value& fmt,
const std::string& who );
359 const std::string& who );
363 const std::string& who );
366 puts (
const std::string& s,
const std::string& who );
370 OCTINTERP_API
bool eof (
void)
const;
372 OCTINTERP_API std::string
error (
bool clear,
int& err_num);
374 std::string
error (
bool clear =
false)
377 return error (clear, err_num);
390 int file_number (
void) {
return m_rep ? m_rep->file_number () : -1; }
394 bool ok (
void)
const {
return m_rep && m_rep->ok (); }
396 operator bool ()
const {
return ok (); }
398 OCTINTERP_API std::string
name (
void)
const;
400 OCTINTERP_API
int mode (
void)
const;
404 OCTINTERP_API
static std::string mode_as_string (
int mode);
408 return m_rep ? m_rep->encoding () : std::string ();
413 return m_rep ? m_rep->input_stream () :
nullptr;
418 return m_rep ? m_rep->output_stream () :
nullptr;
421 void clearerr (
void) {
if (m_rep) m_rep->clearerr (); }
446 m_rep->invalid_operation (who, rw);
450 finalize_read (std::list<void *>& input_buf_list,
472 OCTINTERP_API
int insert (
stream& os);
474 OCTINTERP_API
stream lookup (
int fid,
const std::string& who =
"")
const;
478 OCTINTERP_API
int remove (
int fid,
const std::string& who =
"");
479 OCTINTERP_API
int remove (
const octave_value& fid,
const std::string& who =
"");
481 OCTINTERP_API
void clear (
bool flush =
true);
486 OCTINTERP_API std::string list_open_files (
void)
const;
488 OCTINTERP_API
octave_value open_file_numbers (
void)
const;
490 OCTINTERP_API
int get_file_number (
const octave_value& fid)
const;
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
virtual bool eof(void) const =0
virtual void do_close(void)
virtual int seek(off_t offset, int origin)=0
mach_info::float_format float_format(void) const
mach_info::float_format m_flt_fmt
base_stream(const base_stream &)=delete
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 std::istream * input_stream(void)
virtual std::ostream * output_stream(void)
virtual std::string name(void) const =0
std::string encoding(void) const
virtual ~base_stream(void)=default
virtual off_t tell(void)=0
virtual int file_number(void) const
ostrl_map::const_iterator m_lookup_cache
std::map< int, stream > ostrl_map
stream_list(const stream_list &)=delete
void error(const std::string &msg)
void invalid_operation(const std::string &who, const char *rw)
std::istream * input_stream(void)
std::ostream * output_stream(void)
bool stream_ok(bool clear=true) const
OCTINTERP_API octave_idx_type write(const Array< T > &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
std::shared_ptr< base_stream > m_rep
stream(base_stream *bs=nullptr)
bool is_valid(void) const
std::string encoding(void)
void error(const char *msg)
stream(const stream &)=default
std::string error(bool clear=false)
void error(const char *fmt,...)
float_format native_float_format(void)