GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-stream.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_oct_stream_h)
24 #define octave_oct_stream_h 1
25 
26 #include "octave-config.h"
27 
28 #include <iosfwd>
29 #include <list>
30 #include <map>
31 #include <string>
32 
33 // These are only needed as arguments to private functions, so they
34 // are also treated as private.
35 
36 class scanf_format_elt;
37 class scanf_format_list;
38 
39 class printf_format_elt;
40 class printf_format_list;
41 
42 // These only appear as reference arguments or return values.
43 
44 template <typename T> class Array;
45 class Cell;
46 class octave_value_list;
47 class string_vector;
48 
49 #include "data-conv.h"
50 #include "mach-info.h"
51 #include "oct-refcount.h"
52 
53 #include "ov.h"
54 
55 // Provide an interface for Octave streams.
56 
57 class
60 {
61  friend class octave_stream;
62 
63 public:
64 
65  octave_base_stream (std::ios::openmode arg_md = std::ios::in | std::ios::out,
68  : count (0), md (arg_md), flt_fmt (ff), fail (false), open_state (true),
69  errmsg ()
70  { }
71 
72  virtual ~octave_base_stream (void) { }
73 
74  // The remaining functions are not specific to input or output only,
75  // and must be provided by the derived classes.
76 
77  // Position a stream at OFFSET relative to ORIGIN.
78 
79  virtual int seek (off_t offset, int origin) = 0;
80 
81  // Return current stream position.
82 
83  virtual off_t tell (void) = 0;
84 
85  // Return TRUE if EOF has been reached on this stream.
86 
87  virtual bool eof (void) const = 0;
88 
89  // The name of the file.
90 
91  virtual std::string name (void) const = 0;
92 
93  // If the derived class provides this function and it returns a
94  // pointer to a valid istream, scanf(), read(), getl(), and gets()
95  // will automatically work for this stream.
96 
97  virtual std::istream *input_stream (void) { return 0; }
98 
99  // If the derived class provides this function and it returns a
100  // pointer to a valid ostream, flush(), write(), and printf() will
101  // automatically work for this stream.
102 
103  virtual std::ostream *output_stream (void) { return 0; }
104 
105  // Return TRUE if this stream is open.
106 
107  bool is_open (void) const { return open_state; }
108 
109  virtual void do_close (void) { }
110 
111  void close (void)
112  {
113  if (is_open ())
114  {
115  open_state = false;
116  do_close ();
117  }
118  }
119 
120  virtual int file_number (void) const
121  {
122  // Kluge alert!
123 
124  if (name () == "stdin")
125  return 0;
126  else if (name () == "stdout")
127  return 1;
128  else if (name () == "stderr")
129  return 2;
130  else
131  return -1;
132  }
133 
134  bool ok (void) const { return ! fail; }
135 
136  // Return current error message for this stream.
137 
138  std::string error (bool clear, int& err_num);
139 
140 protected:
141 
142  int mode (void) const { return md; }
143 
145 
146  // Set current error state and set fail to TRUE.
147 
148  void error (const std::string& msg);
149  void error (const std::string& who, const std::string& msg);
150 
151  // Clear any error message and set fail to FALSE.
152 
153  void clear (void);
154 
155  // Clear stream state.
156 
157  void clearerr (void);
158 
159 private:
160 
161  // A reference count.
163 
164  // The permission bits for the file. Should be some combination of
165  // std::ios::open_mode bits.
166  int md;
167 
168  // Data format.
170 
171  // TRUE if an error has occurred.
172  bool fail;
173 
174  // TRUE if this stream is open.
176 
177  // Should contain error message if fail is TRUE.
179 
180  // Functions that are defined for all input streams (input streams
181  // are those that define is).
182 
183  std::string do_gets (octave_idx_type max_len, bool& err, bool strip_newline,
184  const std::string& who /* = "gets" */);
185 
186  std::string getl (octave_idx_type max_len, bool& err,
187  const std::string& who /* = "getl" */);
188  std::string gets (octave_idx_type max_len, bool& err,
189  const std::string& who /* = "gets" */);
190  off_t skipl (off_t count, bool& err, const std::string& who /* = "skipl" */);
191 
192  octave_value do_scanf (scanf_format_list& fmt_list, octave_idx_type nr,
193  octave_idx_type nc,
194  bool one_elt_size_spec, octave_idx_type& count,
195  const std::string& who /* = "scanf" */);
196 
197  octave_value scanf (const std::string& fmt, const Array<double>& size,
198  octave_idx_type& count, const std::string& who /* = "scanf" */);
199 
200  bool do_oscanf (const scanf_format_elt *elt, octave_value&,
201  const std::string& who /* = "scanf" */);
202 
204  const std::string& who /* = "scanf" */);
205 
206  octave_value do_textscan (const std::string& fmt, octave_idx_type ntimes,
207  const octave_value_list& options,
208  const std::string& who, octave_idx_type& count);
209 
210  // Functions that are defined for all output streams (output streams
211  // are those that define os).
212 
213  int flush (void);
214 
215  int do_numeric_printf_conv (std::ostream& os, const printf_format_elt *elt,
216  int nsa, int sa_1, int sa_2,
217  const octave_value& val,
218  const std::string& who);
219 
220  int do_printf (printf_format_list& fmt_list, const octave_value_list& args,
221  const std::string& who /* = "printf" */);
222 
223  int printf (const std::string& fmt, const octave_value_list& args,
224  const std::string& who /* = "printf" */);
225 
226  int puts (const std::string& s, const std::string& who /* = "puts" */);
227 
228  // We can always do this in terms of seek(), so the derived class
229  // only has to provide that.
230 
231  void invalid_operation (const std::string& who, const char *rw);
232 
233  // No copying!
234 
236 
238 };
239 
240 class
243 {
244 public:
245 
247 
248  ~octave_stream (void);
249 
250  octave_stream (const octave_stream&);
251 
253 
254  int flush (void);
255 
256  std::string getl (octave_idx_type max_len, bool& err,
257  const std::string& who /* = "getl" */);
258  std::string getl (const octave_value& max_len, bool& err,
259  const std::string& who /* = "getl" */);
260 
261  std::string gets (octave_idx_type max_len, bool& err,
262  const std::string& who /* = "gets" */);
263  std::string gets (const octave_value& max_len, bool& err,
264  const std::string& who /* = "gets" */);
265 
266  off_t skipl (off_t count, bool& err, const std::string& who /* = "skipl" */);
267  off_t skipl (const octave_value& count, bool& err,
268  const std::string& who /* = "skipl" */);
269 
270  int seek (off_t offset, int origin);
271  int seek (const octave_value& offset, const octave_value& origin);
272 
273  off_t tell (void);
274 
275  int rewind (void);
276 
277  bool is_open (void) const;
278 
279  void close (void);
280 
281  octave_value read (const Array<double>& size, octave_idx_type block_size,
282  oct_data_conv::data_type input_type,
283  oct_data_conv::data_type output_type,
285  octave_idx_type& count);
286 
287  octave_idx_type write (const octave_value& data, octave_idx_type block_size,
288  oct_data_conv::data_type output_type,
289  octave_idx_type skip,
291 
292  bool write_bytes (const void *data, size_t n_elts);
293 
294  bool skip_bytes (size_t n_elts);
295 
296  template <typename T>
297  octave_idx_type write (const Array<T>& data, octave_idx_type block_size,
298  oct_data_conv::data_type output_type,
299  octave_idx_type skip,
301 
302  octave_value scanf (const std::string& fmt, const Array<double>& size,
303  octave_idx_type& count, const std::string& who /* = "scanf" */);
304 
305  octave_value scanf (const octave_value& fmt, const Array<double>& size,
306  octave_idx_type& count, const std::string& who /* = "scanf" */);
307 
309  const std::string& who /* = "scanf" */);
310 
312  const std::string& who /* = "scanf" */);
313 
314  octave_value textscan (const std::string& fmt, octave_idx_type ntimes,
315  const octave_value_list& options,
316  const std::string& who, octave_idx_type& count);
317 
318  int printf (const std::string& fmt, const octave_value_list& args,
319  const std::string& who /* = "printf" */);
320 
321  int printf (const octave_value& fmt, const octave_value_list& args,
322  const std::string& who /* = "printf" */);
323 
324  int puts (const std::string& s, const std::string& who /* = "puts" */);
325  int puts (const octave_value& s, const std::string& who /* = "puts" */);
326 
327  bool eof (void) const;
328 
329  std::string error (bool clear, int& err_num);
330 
331  std::string error (bool clear = false)
332  {
333  int err_num;
334  return error (clear, err_num);
335  }
336 
337  // Set the error message and state.
338 
339  void error (const std::string& msg)
340  {
341  if (rep)
342  rep->error (msg);
343  }
344 
345  void error (const char *msg) { error (std::string (msg)); }
346 
347  int file_number (void) { return rep ? rep->file_number () : -1; }
348 
349  bool is_valid (void) const { return (rep != 0); }
350 
351  bool ok (void) const { return rep && rep->ok (); }
352 
353  operator bool () const { return ok (); }
354 
355  std::string name (void) const;
356 
357  int mode (void) const;
358 
360 
361  static std::string mode_as_string (int mode);
362 
363  std::istream *input_stream (void)
364  {
365  return rep ? rep->input_stream () : 0;
366  }
367 
368  std::ostream *output_stream (void)
369  {
370  return rep ? rep->output_stream () : 0;
371  }
372 
373  void clearerr (void) { if (rep) rep->clearerr (); }
374 
375 private:
376 
377  // The actual representation of this stream.
379 
380  bool stream_ok (bool clear = true) const
381  {
382  bool retval = true;
383 
384  if (rep)
385  {
386  if (clear)
387  rep->clear ();
388  }
389  else
390  retval = false;
391 
392  return retval;
393  }
394 
395  void invalid_operation (const std::string& who, const char *rw)
396  {
397  if (rep)
398  rep->invalid_operation (who, rw);
399  }
400 
402  finalize_read (std::list<void *>& input_buf_list,
403  octave_idx_type input_buf_elts,
404  octave_idx_type elts_read,
406  oct_data_conv::data_type input_type,
407  oct_data_conv::data_type output_type,
409 };
410 
411 class
414 {
415 protected:
416 
417  octave_stream_list (void) : list (), lookup_cache (list.end ()) { }
418 
419 public:
420 
422 
423  static bool instance_ok (void);
424 
425  static int insert (octave_stream& os);
426 
427  static octave_stream
428  lookup (int fid, const std::string& who = "");
429 
430  static octave_stream
431  lookup (const octave_value& fid, const std::string& who = "");
432 
433  static int remove (int fid, const std::string& who = "");
434  static int remove (const octave_value& fid,
435  const std::string& who = "");
436 
437  static void clear (bool flush = true);
438 
439  static string_vector get_info (int fid);
440  static string_vector get_info (const octave_value& fid);
441 
442  static std::string list_open_files (void);
443 
444  static octave_value open_file_numbers (void);
445 
446  static int get_file_number (const octave_value& fid);
447 
448 private:
449 
450  typedef std::map<int, octave_stream> ostrl_map;
451 
452  ostrl_map list;
453 
454  mutable ostrl_map::const_iterator lookup_cache;
455 
457 
458  static void cleanup_instance (void) { delete instance; instance = 0; }
459 
460  int do_insert (octave_stream& os);
461 
462  octave_stream do_lookup (int fid,
463  const std::string& who = "") const;
464  octave_stream do_lookup (const octave_value& fid,
465  const std::string& who = "") const;
466 
467  int do_remove (int fid, const std::string& who = "");
468  int do_remove (const octave_value& fid,
469  const std::string& who = "");
470 
471  void do_clear (bool flush = true);
472 
473  string_vector do_get_info (int fid) const;
474  string_vector do_get_info (const octave_value& fid) const;
475 
476  std::string do_list_open_files (void) const;
477 
478  octave_value do_open_file_numbers (void) const;
479 
480  int do_get_file_number (const octave_value& fid) const;
481 };
482 
483 #endif
void invalid_operation(const std::string &who, const char *rw)
Definition: oct-stream.h:395
virtual ~octave_base_stream(void)
Definition: oct-stream.h:72
bool skip_bytes(size_t n_elts)
Definition: oct-stream.cc:6843
Definition: Cell.h:37
bool is_open(void) const
Definition: oct-stream.h:107
int rewind(void)
Definition: oct-stream.cc:6189
The value of lines which begin with a space character are not saved in the history list A value of all commands are saved on the history list
Definition: oct-hist.cc:728
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, octave::mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6647
octave::mach_info::float_format float_format(void) const
Definition: oct-stream.cc:7181
nd group nd example oindent or xample printf("%s\n", nthargout(2,"system","cmd"))
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
static octave_stream_list * instance
Definition: oct-stream.h:456
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6386
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
Definition: oct-stream.cc:5985
bool is_open(void) const
Definition: oct-stream.cc:6195
int seek(off_t offset, int origin)
Definition: oct-stream.cc:6061
virtual std::istream * input_stream(void)
Definition: oct-stream.h:97
octave_base_stream * rep
Definition: oct-stream.h:378
octave_stream_list(void)
Definition: oct-stream.h:417
void error(const char *fmt,...)
Definition: error.cc:570
off_t skipl(off_t count, bool &err, const std::string &who)
Definition: oct-stream.cc:6020
bool ok(void) const
Definition: oct-stream.h:134
octave::mach_info::float_format flt_fmt
Definition: load-save.cc:723
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
void error(const char *msg)
Definition: oct-stream.h:345
s
Definition: file-io.cc:2682
virtual void do_close(void)
Definition: oct-stream.h:109
std::istream * input_stream(void)
Definition: oct-stream.h:363
octave::mach_info::float_format flt_fmt
Definition: oct-stream.h:169
virtual std::ostream * output_stream(void)
Definition: oct-stream.h:103
JNIEnv void * args
Definition: ov-java.cc:67
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
#define OCTINTERP_API
Definition: mexproto.h:69
bool ok(void) const
Definition: oct-stream.h:351
octave::mach_info::float_format float_format(void) const
Definition: oct-stream.h:144
std::string errmsg
Definition: oct-stream.h:178
bool write_bytes(const void *data, size_t n_elts)
Definition: oct-stream.cc:6820
octave_value finalize_read(std::list< void * > &input_buf_list, octave_idx_type input_buf_elts, octave_idx_type elts_read, octave_idx_type nr, octave_idx_type nc, oct_data_conv::data_type input_type, oct_data_conv::data_type output_type, octave::mach_info::float_format ffmt)
Definition: oct-stream.cc:6362
int mode(void) const
Definition: oct-stream.h:142
is false
Definition: cellfun.cc:398
int puts(const std::string &s, const std::string &who)
Definition: oct-stream.cc:7105
octave_value retval
Definition: data.cc:6294
int flush(void)
Definition: oct-stream.cc:5939
std::ostream * output_stream(void)
Definition: oct-stream.h:368
virtual int file_number(void) const
Definition: oct-stream.h:120
static void cleanup_instance(void)
Definition: oct-stream.h:458
octave_value_list oscanf(const std::string &fmt, const std::string &who)
Definition: oct-stream.cc:7025
octave_value textscan(const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, const std::string &who, octave_idx_type &count)
Definition: oct-stream.cc:7059
octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
Definition: oct-stream.cc:6989
off_t tell(void)
Definition: oct-stream.cc:6178
std::string error(bool clear, int &err_num)
Definition: oct-stream.cc:5882
std::string error(bool clear=false)
Definition: oct-stream.h:331
bool stream_ok(bool clear=true) const
Definition: oct-stream.h:380
~octave_stream(void)
Definition: oct-stream.cc:5908
static float_format native_float_format(void)
Definition: mach-info.cc:162
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, octave::mach_info::float_format flt_fmt, octave_idx_type &count)
Definition: oct-stream.cc:6450
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:126
octave_stream(octave_base_stream *bs=0)
Definition: oct-stream.cc:5901
bool is_valid(void) const
Definition: oct-stream.h:349
octave_stream & operator=(const octave_stream &)
Definition: oct-stream.cc:5922
bool eof(void) const
Definition: oct-stream.cc:7137
octave_base_stream(std::ios::openmode arg_md=std::ios::in|std::ios::out, octave::mach_info::float_format ff=octave::mach_info::native_float_format())
Definition: oct-stream.h:65
OCTAVE_EXPORT octave_value_list only variables visible in the local scope are displayed The following are valid options
Definition: variables.cc:1859
octave_refcount< octave_idx_type > count
Definition: oct-stream.h:162
ostrl_map::const_iterator lookup_cache
Definition: oct-stream.h:454
void close(void)
Definition: oct-stream.cc:6206
~octave_stream_list(void)
Definition: oct-stream.h:421
int file_number(void)
Definition: oct-stream.h:347
void clearerr(void)
Definition: oct-stream.cc:3989
OCTAVE_EXPORT octave_value_list error nd deftypefn *const octave_scalar_map err
Definition: error.cc:1036
void error(const std::string &msg)
Definition: oct-stream.h:339
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable or any other valid Octave code The number of return their size
Definition: input.cc:871
void invalid_operation(const std::string &who, const char *rw)
Definition: oct-stream.cc:5895
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
Definition: oct-stream.cc:5950
void close(void)
Definition: oct-stream.h:111
static void clear(octave::dynamic_library &oct_file)
Definition: dynamic-ld.cc:230
static std::string mode_as_string(int mode)
Definition: oct-stream.cc:7192
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
std::map< int, octave_stream > ostrl_map
Definition: oct-stream.h:450
void clearerr(void)
Definition: oct-stream.h:373