GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-stdstrm.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_stdstrm_h)
24 #define octave_oct_stdstrm_h 1
25 
26 #include "octave-config.h"
27 
28 #include "oct-stream.h"
29 #include "c-file-ptr-stream.h"
30 
31 template <typename BUF_T, typename STREAM_T, typename FILE_T>
32 class
34 {
35 public:
36 
37  octave_tstdiostream (const std::string& n, FILE_T f = 0, int fid = 0,
38  std::ios::openmode m = std::ios::in | std::ios::out,
41  typename BUF_T::close_fcn cf = BUF_T::file_close)
42  : octave::base_stream (m, ff), nm (n), md (m),
43  s (f ? new STREAM_T (f, cf) : nullptr), fnum (fid)
44  { }
45 
46  // No copying!
47 
48  octave_tstdiostream (const octave_tstdiostream&) = delete;
49 
50  octave_tstdiostream& operator = (const octave_tstdiostream&) = delete;
51 
52  // Position a stream at OFFSET relative to ORIGIN.
53 
54  int seek (off_t offset, int origin)
55  { return s ? s->seek (offset, origin) : -1; }
56 
57  // Return current stream position.
58 
59  off_t tell (void) { return s ? s->tell () : -1; }
60 
61  // Return nonzero if EOF has been reached on this stream.
62 
63  bool eof (void) const { return s ? s->eof () : true; }
64 
65  // The name of the file.
66 
67  std::string name (void) const { return nm; }
68 
69  std::istream * input_stream (void) { return (md & std::ios::in) ? s : nullptr; }
70 
71  std::ostream * output_stream (void) { return (md & std::ios::out) ? s : nullptr; }
72 
73  // FIXME: should not have to cast away const here.
74  BUF_T * rdbuf (void) const
75  { return s ? (const_cast<STREAM_T *> (s))->rdbuf () : nullptr; }
76 
77  int file_number (void) const { return fnum; }
78 
79  bool bad (void) const { return s ? s->bad () : true; }
80 
81  void clear (void) { if (s) s->clear (); }
82 
83  void do_close (void) { if (s) s->stream_close (); }
84 
85 protected:
86 
88 
89  std::ios::openmode md;
90 
92 
93  // The file number associated with this file.
94  int fnum;
95 
96  ~octave_tstdiostream (void) { delete s; }
97 };
98 
99 class
102 {
103 public:
104 
105  octave_stdiostream (const std::string& n, FILE *f = nullptr,
106  std::ios::openmode m = std::ios::in | std::ios::out,
111  (n, f, f ? fileno (f) : -1, m, ff, cf) { }
112 
113  static octave::stream
114  create (const std::string& n, FILE *f = nullptr,
115  std::ios::openmode m = std::ios::in | std::ios::out,
119  {
120  return octave::stream (new octave_stdiostream (n, f, m, ff, cf));
121  }
122 
123  // No copying!
124 
125  octave_stdiostream (const octave_stdiostream&) = delete;
126 
127  octave_stdiostream& operator = (const octave_stdiostream&) = delete;
128 
129 protected:
130 
131  ~octave_stdiostream (void) = default;
132 };
133 
134 #if defined (HAVE_ZLIB)
135 
136 class
139 {
140 public:
141 
142  octave_zstdiostream (const std::string& n, gzFile f = nullptr, int fid = 0,
143  std::ios::openmode m = std::ios::in | std::ios::out,
149  (n, f, fid, m, ff, cf) { }
150 
151  static octave::stream
152  create (const std::string& n, gzFile f = nullptr, int fid = 0,
153  std::ios::openmode m = std::ios::in | std::ios::out,
157  {
158  return octave::stream (new octave_zstdiostream (n, f, fid, m, ff, cf));
159  }
160 
161  // No copying!
162 
163  octave_zstdiostream (const octave_zstdiostream&) = delete;
164 
165  octave_zstdiostream& operator = (const octave_zstdiostream&) = delete;
166 
167 protected:
168 
169  ~octave_zstdiostream (void) = default;
170 };
171 
172 #endif
173 
174 #endif
static int file_close(gzFile f)
int(* close_fcn)(gzFile)
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
int seek(off_t offset, int origin)
Definition: oct-stdstrm.h:54
off_t tell(void)
Definition: oct-stdstrm.h:59
void clear(void)
Definition: oct-stdstrm.h:81
void do_close(void)
Definition: oct-stdstrm.h:83
std::istream * input_stream(void)
Definition: oct-stdstrm.h:69
s
Definition: file-io.cc:2729
octave_stdiostream(const std::string &n, FILE *f=nullptr, std::ios::openmode m=std::ios::in|std::ios::out, octave::mach_info::float_format ff=octave::mach_info::native_float_format(), c_file_ptr_buf::close_fcn cf=c_file_ptr_buf::file_close)
Definition: oct-stdstrm.h:105
int(* close_fcn)(FILE *)
~octave_tstdiostream(void)
Definition: oct-stdstrm.h:96
bool eof(void) const
Definition: oct-stdstrm.h:63
std::ios::openmode md
Definition: oct-stdstrm.h:89
static void close_fcn(FILE *f)
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
static octave::stream create(const std::string &n, gzFile f=nullptr, int fid=0, std::ios::openmode m=std::ios::in|std::ios::out, octave::mach_info::float_format ff=octave::mach_info::native_float_format(), c_zfile_ptr_buf::close_fcn cf=c_zfile_ptr_buf::file_close)
Definition: oct-stdstrm.h:152
std::ostream * output_stream(void)
Definition: oct-stdstrm.h:71
float_format native_float_format(void)
Definition: mach-info.cc:62
std::string name(void) const
Definition: oct-stdstrm.h:67
static int file_close(FILE *f)
octave_tstdiostream(const std::string &n, FILE_T f=0, int fid=0, std::ios::openmode m=std::ios::in|std::ios::out, octave::mach_info::float_format ff=octave::mach_info::native_float_format(), typename BUF_T::close_fcn cf=BUF_T::file_close)
Definition: oct-stdstrm.h:37
std::string nm
Definition: oct-stdstrm.h:87
int file_number(void) const
Definition: oct-stdstrm.h:77
bool bad(void) const
Definition: oct-stdstrm.h:79
octave_zstdiostream(const std::string &n, gzFile f=nullptr, int fid=0, std::ios::openmode m=std::ios::in|std::ios::out, octave::mach_info::float_format ff=octave::mach_info::native_float_format(), c_zfile_ptr_buf::close_fcn cf=c_zfile_ptr_buf::file_close)
Definition: oct-stdstrm.h:142
static octave::stream create(const std::string &n, FILE *f=nullptr, std::ios::openmode m=std::ios::in|std::ios::out, octave::mach_info::float_format ff=octave::mach_info::native_float_format(), c_file_ptr_buf::close_fcn cf=c_file_ptr_buf::file_close)
Definition: oct-stdstrm.h:114
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
BUF_T * rdbuf(void) const
Definition: oct-stdstrm.h:74