GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-stream.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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 <ios>
29 #include <iosfwd>
30 #include <list>
31 #include <map>
32 #include <string>
33 
34 // These only appear as reference arguments or return values.
35 
36 template <typename T> class Array;
37 class Cell;
38 class octave_value;
39 class octave_value_list;
40 class string_vector;
41 
42 #include "data-conv.h"
43 #include "mach-info.h"
44 #include "oct-refcount.h"
45 
46 namespace octave
47 {
48  class interpreter;
49 
50  // These are only needed as arguments to private functions, so they
51  // are also treated as private.
52 
53  class scanf_format_elt;
54  class scanf_format_list;
55 
56  class printf_format_elt;
57  class printf_format_list;
58 
59  // Provide an interface for Octave streams.
60 
61  class
62  OCTINTERP_API
64  {
65  friend class stream;
66 
67  public:
68 
69  base_stream (std::ios::openmode arg_md = std::ios::in | std::ios::out,
71  : count (0), md (arg_md), flt_fmt (ff), fail (false), open_state (true),
72  errmsg ()
73  { }
74 
75  // No copying!
76 
77  base_stream (const base_stream&) = delete;
78 
79  base_stream& operator = (const base_stream&) = delete;
80 
81  virtual ~base_stream (void) = default;
82 
83  // The remaining functions are not specific to input or output only,
84  // and must be provided by the derived classes.
85 
86  // Position a stream at OFFSET relative to ORIGIN.
87 
88  virtual int seek (off_t offset, int origin) = 0;
89 
90  // Return current stream position.
91 
92  virtual off_t tell (void) = 0;
93 
94  // Return TRUE if EOF has been reached on this stream.
95 
96  virtual bool eof (void) const = 0;
97 
98  // The name of the file.
99 
100  virtual std::string name (void) const = 0;
101 
102  // If the derived class provides this function and it returns a
103  // pointer to a valid istream, scanf(), read(), getl(), and gets()
104  // will automatically work for this stream.
105 
106  virtual std::istream * input_stream (void) { return nullptr; }
107 
108  // If the derived class provides this function and it returns a
109  // pointer to a valid ostream, flush(), write(), and printf() will
110  // automatically work for this stream.
111 
112  virtual std::ostream * output_stream (void) { return nullptr; }
113 
114  // Return TRUE if this stream is open.
115 
116  bool is_open (void) const { return open_state; }
117 
118  virtual void do_close (void) { }
119 
120  void close (void)
121  {
122  if (is_open ())
123  {
124  open_state = false;
125  do_close ();
126  }
127  }
128 
129  virtual int file_number (void) const
130  {
131  // Kluge alert!
132 
133  if (name () == "stdin")
134  return 0;
135  else if (name () == "stdout")
136  return 1;
137  else if (name () == "stderr")
138  return 2;
139  else
140  return -1;
141  }
142 
143  bool ok (void) const { return ! fail; }
144 
145  // Return current error message for this stream.
146 
147  std::string error (bool clear, int& err_num);
148 
149  protected:
150 
151  int mode (void) const { return md; }
152 
154 
155  // Set current error state and set fail to TRUE.
156 
157  void error (const std::string& msg);
158  void error (const std::string& who, const std::string& msg);
159 
160  // Clear any error message and set fail to FALSE.
161 
162  void clear (void);
163 
164  // Clear stream state.
165 
166  void clearerr (void);
167 
168  private:
169 
170  // A reference count.
172 
173  // The permission bits for the file. Should be some combination of
174  // std::ios::open_mode bits.
175  int md;
176 
177  // Data format.
179 
180  // TRUE if an error has occurred.
181  bool fail;
182 
183  // TRUE if this stream is open.
185 
186  // Should contain error message if fail is TRUE.
188 
189  // Functions that are defined for all input streams (input streams
190  // are those that define is).
191 
192  std::string do_gets (octave_idx_type max_len, bool& err, bool strip_newline,
193  const std::string& who /* = "gets" */);
194 
195  std::string getl (octave_idx_type max_len, bool& err,
196  const std::string& who /* = "getl" */);
197  std::string gets (octave_idx_type max_len, bool& err,
198  const std::string& who /* = "gets" */);
199  off_t skipl (off_t count, bool& err, const std::string& who /* = "skipl" */);
200 
201  octave_value do_scanf (scanf_format_list& fmt_list, octave_idx_type nr,
202  octave_idx_type nc,
203  bool one_elt_size_spec, octave_idx_type& count,
204  const std::string& who /* = "scanf" */);
205 
206  octave_value scanf (const std::string& fmt, const Array<double>& size,
207  octave_idx_type& count, const std::string& who /* = "scanf" */);
208 
209  bool do_oscanf (const scanf_format_elt *elt, octave_value&,
210  const std::string& who /* = "scanf" */);
211 
212  octave_value_list oscanf (const std::string& fmt,
213  const std::string& who /* = "scanf" */);
214 
215  octave_value do_textscan (const std::string& fmt, octave_idx_type ntimes,
216  const octave_value_list& options,
217  const std::string& who, octave_idx_type& count);
218 
219  // Functions that are defined for all output streams (output streams
220  // are those that define os).
221 
222  int flush (void);
223 
224  int do_numeric_printf_conv (std::ostream& os, const printf_format_elt *elt,
225  int nsa, int sa_1, int sa_2,
226  const octave_value& val,
227  const std::string& who);
228 
229  int do_printf (printf_format_list& fmt_list, const octave_value_list& args,
230  const std::string& who /* = "printf" */);
231 
232  int printf (const std::string& fmt, const octave_value_list& args,
233  const std::string& who /* = "printf" */);
234 
235  int puts (const std::string& s, const std::string& who /* = "puts" */);
236 
237  // We can always do this in terms of seek(), so the derived class
238  // only has to provide that.
239 
240  void invalid_operation (const std::string& who, const char *rw);
241  };
242 
243  class
244  OCTINTERP_API
245  stream
246  {
247  public:
248 
249  stream (base_stream *bs = nullptr);
250 
251  ~stream (void);
252 
253  stream (const stream&);
254 
255  stream& operator = (const stream&);
256 
257  int flush (void);
258 
259  std::string getl (octave_idx_type max_len, bool& err,
260  const std::string& who /* = "getl" */);
261  std::string getl (const octave_value& max_len, bool& err,
262  const std::string& who /* = "getl" */);
263 
264  std::string gets (octave_idx_type max_len, bool& err,
265  const std::string& who /* = "gets" */);
266  std::string gets (const octave_value& max_len, bool& err,
267  const std::string& who /* = "gets" */);
268 
269  off_t skipl (off_t count, bool& err, const std::string& who /* = "skipl" */);
270  off_t skipl (const octave_value& count, bool& err,
271  const std::string& who /* = "skipl" */);
272 
273  int seek (off_t offset, int origin);
274  int seek (const octave_value& offset, const octave_value& origin);
275 
276  off_t tell (void);
277 
278  int rewind (void);
279 
280  bool is_open (void) const;
281 
282  void close (void);
283 
284  octave_value read (const Array<double>& size, octave_idx_type block_size,
285  oct_data_conv::data_type input_type,
286  oct_data_conv::data_type output_type,
288  octave_idx_type& count);
289 
290  octave_idx_type write (const octave_value& data, octave_idx_type block_size,
291  oct_data_conv::data_type output_type,
292  octave_idx_type skip,
294 
295  bool write_bytes (const void *data, size_t n_elts);
296 
297  bool skip_bytes (size_t n_elts);
298 
299  template <typename T>
300  octave_idx_type write (const Array<T>& data, octave_idx_type block_size,
301  oct_data_conv::data_type output_type,
302  octave_idx_type skip,
304 
305  octave_value scanf (const std::string& fmt, const Array<double>& size,
306  octave_idx_type& count, const std::string& who /* = "scanf" */);
307 
308  octave_value scanf (const octave_value& fmt, const Array<double>& size,
309  octave_idx_type& count, const std::string& who /* = "scanf" */);
310 
311  octave_value_list oscanf (const std::string& fmt,
312  const std::string& who /* = "scanf" */);
313 
314  octave_value_list oscanf (const octave_value& fmt,
315  const std::string& who /* = "scanf" */);
316 
317  octave_value textscan (const std::string& fmt, octave_idx_type ntimes,
318  const octave_value_list& options,
319  const std::string& who, octave_idx_type& count);
320 
321  int printf (const std::string& fmt, const octave_value_list& args,
322  const std::string& who /* = "printf" */);
323 
324  int printf (const octave_value& fmt, const octave_value_list& args,
325  const std::string& who /* = "printf" */);
326 
327  int puts (const std::string& s, const std::string& who /* = "puts" */);
328  int puts (const octave_value& s, const std::string& who /* = "puts" */);
329 
330  bool eof (void) const;
331 
332  std::string error (bool clear, int& err_num);
333 
334  std::string error (bool clear = false)
335  {
336  int err_num;
337  return error (clear, err_num);
338  }
339 
340  // Set the error message and state.
341 
342  void error (const std::string& msg)
343  {
344  if (rep)
345  rep->error (msg);
346  }
347 
348  void error (const char *msg) { error (std::string (msg)); }
349 
350  int file_number (void) { return rep ? rep->file_number () : -1; }
351 
352  bool is_valid (void) const { return (rep != nullptr); }
353 
354  bool ok (void) const { return rep && rep->ok (); }
355 
356  operator bool () const { return ok (); }
357 
358  std::string name (void) const;
359 
360  int mode (void) const;
361 
363 
364  static std::string mode_as_string (int mode);
365 
366  std::istream * input_stream (void)
367  {
368  return rep ? rep->input_stream () : nullptr;
369  }
370 
371  std::ostream * output_stream (void)
372  {
373  return rep ? rep->output_stream () : nullptr;
374  }
375 
376  void clearerr (void) { if (rep) rep->clearerr (); }
377 
378  private:
379 
380  // The actual representation of this stream.
382 
383  bool stream_ok (bool clear = true) const
384  {
385  bool retval = true;
386 
387  if (rep)
388  {
389  if (clear)
390  rep->clear ();
391  }
392  else
393  retval = false;
394 
395  return retval;
396  }
397 
398  void invalid_operation (const std::string& who, const char *rw)
399  {
400  if (rep)
401  rep->invalid_operation (who, rw);
402  }
403 
405  finalize_read (std::list<void *>& input_buf_list,
406  octave_idx_type input_buf_elts,
407  octave_idx_type elts_read,
409  oct_data_conv::data_type input_type,
410  oct_data_conv::data_type output_type,
412  };
413 
414  class
415  OCTINTERP_API
417  {
418  public:
419 
420  stream_list (interpreter& interp);
421 
422  stream_list (const stream_list&) = delete;
423  stream_list& operator = (const stream_list&) = delete;
424 
425  ~stream_list (void);
426 
427  int insert (stream& os);
428 
429  stream lookup (int fid, const std::string& who = "") const;
430  stream lookup (const octave_value& fid, const std::string& who = "") const;
431 
432  int remove (int fid, const std::string& who = "");
433  int remove (const octave_value& fid, const std::string& who = "");
434 
435  void clear (bool flush = true);
436 
437  string_vector get_info (int fid) const;
438  string_vector get_info (const octave_value& fid) const;
439 
440  std::string list_open_files (void) const;
441 
442  octave_value open_file_numbers (void) const;
443 
444  int get_file_number (const octave_value& fid) const;
445 
446  octave_value stdin_file (void) const;
447  octave_value stdout_file (void) const;
448  octave_value stderr_file (void) const;
449 
450  private:
451 
452  typedef std::map<int, stream> ostrl_map;
453 
455 
456  mutable ostrl_map::const_iterator lookup_cache;
457 
461  };
462 }
463 
464 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
465 
466 OCTAVE_DEPRECATED (4.4, "use 'octave::base_stream' instead")
467 typedef octave::base_stream octave_base_stream;
468 
469 OCTAVE_DEPRECATED (4.4, "use 'octave::stream' instead")
470 typedef octave::stream octave_stream;
471 
472 OCTAVE_DEPRECATED (4.4, "use 'octave::stream_list' instead")
473 typedef octave::stream_list octave_stream_list;
474 
475 #endif
476 
477 #endif
bool stream_ok(bool clear=true) const
Definition: oct-stream.h:383
void error(const std::string &msg)
Definition: oct-stream.h:342
Definition: Cell.h:37
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
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:6348
printf("%s\, nthargout(2, "system", "cmd"))
void error(const char *fmt,...)
Definition: error.cc:578
void invalid_operation(const std::string &who, const char *rw)
Definition: oct-stream.cc:5952
octave::mach_info::float_format flt_fmt
Definition: load-save.cc:736
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
refcount< octave_idx_type > count
Definition: oct-stream.h:171
s
Definition: file-io.cc:2729
std::string error(bool clear=false)
Definition: oct-stream.h:334
std::istream * input_stream(void)
Definition: oct-stream.h:366
std::ostream * output_stream(void)
Definition: oct-stream.h:371
mach_info::float_format flt_fmt
Definition: oct-stream.h:178
nd deftypefn *std::string name
Definition: sysdep.cc:647
void close(void)
Definition: oct-stream.h:120
int file_number(void)
Definition: oct-stream.h:350
virtual int file_number(void) const
Definition: oct-stream.h:129
bool is_valid(void) const
Definition: oct-stream.h:352
void error(const char *msg)
Definition: oct-stream.h:348
mach_info::float_format float_format(void) const
Definition: oct-stream.h:153
float_format native_float_format(void)
Definition: mach-info.cc:62
void invalid_operation(const std::string &who, const char *rw)
Definition: oct-stream.h:398
is false
Definition: cellfun.cc:400
octave_value retval
Definition: data.cc:6246
int mode(void) const
Definition: oct-stream.h:151
virtual void do_close(void)
Definition: oct-stream.h:118
virtual std::istream * input_stream(void)
Definition: oct-stream.h:106
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
virtual std::ostream * output_stream(void)
Definition: oct-stream.h:112
base_stream(std::ios::openmode arg_md=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format())
Definition: oct-stream.h:69
ostrl_map::const_iterator lookup_cache
Definition: oct-stream.h:456
base_stream * rep
Definition: oct-stream.h:381
OCTAVE_EXPORT octave_value_list only variables visible in the local scope are displayed The following are valid options
Definition: variables.cc:1862
OCTAVE_EXPORT octave_value_list error nd deftypefn *const octave_scalar_map err
Definition: error.cc:1049
std::string errmsg
Definition: oct-stream.h:187
std::map< int, stream > ostrl_map
Definition: oct-stream.h:452
bool ok(void) const
Definition: oct-stream.h:354
int fid
Definition: file-io.cc:625
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:888
bool is_open(void) const
Definition: oct-stream.h:116
bool ok(void) const
Definition: oct-stream.h:143
octave::stream os
Definition: file-io.cc:627
void clearerr(void)
Definition: oct-stream.h:376