26 #if ! defined (octave_oct_stream_h)
27 #define octave_oct_stream_h 1
29 #include "octave-config.h"
58 class scanf_format_elt;
59 class scanf_format_list;
61 class printf_format_elt;
62 class printf_format_list;
74 base_stream (std::ios::openmode arg_md = std::ios::in | std::ios::out,
76 const std::string&
encoding =
"utf-8")
77 : m_mode (arg_md), m_flt_fmt (ff), m_encoding (
encoding),
78 m_conv_ostream (nullptr), m_fail (false), m_open_state (true),
91 virtual
int seek (off_t offset,
int origin) = 0;
99 virtual
bool eof () const = 0;
103 virtual std::
string name () const = 0;
122 if (! m_encoding.compare (
"utf-8"))
126 return m_conv_ostream.get ();
135 = std::unique_ptr<std::wbuffer_convert<convfacet_u8, char>>
136 (
new std::wbuffer_convert<convfacet_u8, char>
137 (os->rdbuf (),
new convfacet_u8 (m_encoding)));
138 m_conv_ostream = std::unique_ptr<std::ostream>
139 (
new std::ostream (m_converter.get ()));
142 return (m_conv_ostream ? m_conv_ostream.get () :
output_stream ());
147 bool is_open ()
const {
return m_open_state; }
155 m_open_state =
false;
164 if (
name () ==
"stdin")
166 else if (
name () ==
"stdout")
168 else if (
name () ==
"stderr")
174 bool ok ()
const {
return ! m_fail; }
178 std::string
error (
bool clear,
int& err_num);
182 int mode ()
const {
return m_mode; }
186 std::string
encoding ()
const {
return m_encoding; }
190 OCTINTERP_API
void error (
const std::string& msg);
191 OCTINTERP_API
void error (
const std::string& who,
const std::string& msg);
195 OCTINTERP_API
void clear ();
211 std::string m_encoding;
214 typedef string::deletable_facet<string::codecvt_u8> convfacet_u8;
216 std::unique_ptr<std::wbuffer_convert<convfacet_u8, char>> m_converter;
221 std::unique_ptr<std::ostream> m_conv_ostream;
230 std::string m_errmsg;
235 OCTINTERP_API std::string
237 const std::string& who );
239 OCTINTERP_API std::string
241 const std::string& who );
242 OCTINTERP_API std::string
244 const std::string& who );
246 skipl (off_t count,
bool& err,
const std::string& who );
259 const std::string& who );
262 oscanf (
const std::string& fmt,
const std::string& who );
272 OCTINTERP_API
int flush ();
275 do_numeric_printf_conv (std::ostream& os,
const printf_format_elt *elt,
276 int nsa,
int sa_1,
int sa_2,
278 const std::string& who);
280 OCTINTERP_API
void field_width_error (
const std::string& who)
const;
284 const std::string& who );
288 const std::string& who );
291 puts (
const std::string& s,
const std::string& who );
297 invalid_operation (
const std::string& who,
const char *rw);
315 OCTINTERP_API
int flush ();
317 OCTINTERP_API std::string
319 const std::string& who );
321 OCTINTERP_API std::string
323 const std::string& who );
325 OCTINTERP_API std::string
327 const std::string& who );
329 OCTINTERP_API std::string
331 const std::string& who );
334 skipl (off_t count,
bool& err,
const std::string& who );
338 const std::string& who );
340 OCTINTERP_API
int seek (off_t offset,
int origin);
345 OCTINTERP_API off_t
tell ();
347 OCTINTERP_API
int rewind ();
349 OCTINTERP_API
bool is_open ()
const;
351 OCTINTERP_API
void close ();
365 OCTINTERP_API
bool write_bytes (
const void *data, std::size_t n_elts);
367 OCTINTERP_API
bool skip_bytes (std::size_t n_elts);
369 template <
typename T>
384 oscanf (
const std::string& fmt,
const std::string& who );
396 const std::string& who );
400 const std::string& who );
403 puts (
const std::string& s,
const std::string& who );
407 OCTINTERP_API
bool eof ()
const;
409 OCTINTERP_API std::string
error (
bool clear,
int& err_num);
411 std::string
error (
bool clear =
false)
414 return error (clear, err_num);
431 bool ok ()
const {
return m_rep && m_rep->ok (); }
433 operator bool ()
const {
return ok (); }
435 OCTINTERP_API std::string
name ()
const;
437 OCTINTERP_API
int mode ()
const;
445 return m_rep ? m_rep->encoding () : std::string ();
450 return m_rep ? m_rep->input_stream () :
nullptr;
455 return (m_rep ? m_rep->output_stream () :
nullptr);
461 return (m_rep ? m_rep->preferred_output_stream () :
nullptr);
464 void clearerr () {
if (m_rep) m_rep->clearerr (); }
469 std::shared_ptr<base_stream> m_rep;
471 bool stream_ok (
bool clear =
true)
const
486 void invalid_operation (
const std::string& who,
const char *rw)
489 m_rep->invalid_operation (who, rw);
493 finalize_read (std::list<void *>& input_buf_list,
514 OCTINTERP_API
int insert (
stream& os);
516 OCTINTERP_API
stream lookup (
int fid,
const std::string& who =
"")
const;
520 OCTINTERP_API
int remove (
int fid,
const std::string& who =
"");
522 const std::string& who =
"");
524 OCTINTERP_API
void clear (
bool flush =
true);
529 OCTINTERP_API std::string list_open_files ()
const;
533 OCTINTERP_API
int get_file_number (
const octave_value& fid)
const;
541 typedef std::map<int, stream> ostrl_map;
545 mutable ostrl_map::const_iterator m_lookup_cache;
552 OCTAVE_END_NAMESPACE(
octave)
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
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")
void error(const std::string &who, const std::string &msg)
mach_info::float_format float_format() const
virtual std::ostream * output_stream()
std::string encoding() const
std::string error(bool clear, int &err_num)
void error(const std::string &msg)
virtual int file_number() const
std::ostream * preferred_output_stream()
std::string error(bool clear=false)
void error(const std::string &msg)
static std::string mode_as_string(int mode)
stream & operator=(const stream &)=default
octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
std::istream * input_stream()
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
bool skip_bytes(std::size_t n_elts)
std::ostream * preferred_output_stream()
std::ostream * output_stream()
off_t skipl(off_t count, bool &err, const std::string &who)
octave_value textscan(const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, const std::string &who, octave_idx_type &count)
stream(base_stream *bs=nullptr)
int puts(const std::string &s, const std::string &who)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
void error(const char *msg)
bool write_bytes(const void *data, std::size_t n_elts)
int seek(off_t offset, int origin)
stream(const stream &)=default
octave_value_list oscanf(const std::string &fmt, const std::string &who)
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
std::string error(bool clear, int &err_num)
octave_value read(const Array< double > &size, octave_idx_type block_size, oct_data_conv::data_type input_type, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt, octave_idx_type &count)
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
float_format native_float_format()