GNU Octave  8.1.0
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-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_oct_stdstrm_h)
27 #define octave_oct_stdstrm_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iomanip>
32 
33 #include "oct-stream.h"
34 #include "c-file-ptr-stream.h"
35 
37 
38 template <typename BUF_T, typename STREAM_T, typename FILE_T>
39 class
41 {
42 public:
43 
44  tstdiostream (const std::string& n, FILE_T f = 0, int fid = 0,
45  std::ios::openmode m = std::ios::in | std::ios::out,
47  const std::string& encoding = "utf-8",
48  typename BUF_T::close_fcn cf = BUF_T::file_close)
49  : base_stream (m, ff, encoding), m_name (n), m_mode (m),
50  m_stream (f ? new STREAM_T (f, cf) : nullptr), m_fnum (fid)
51  { }
52 
53  // No copying!
54 
55  tstdiostream (const tstdiostream&) = delete;
56 
57  tstdiostream& operator = (const tstdiostream&) = delete;
58 
59  // Position a stream at OFFSET relative to ORIGIN.
60 
61  int seek (off_t offset, int origin)
62  {
63  return m_stream ? m_stream->seek (offset, origin) : -1;
64  }
65 
66  // Return current stream position.
67 
68  off_t tell (void) { return m_stream ? m_stream->tell () : -1; }
69 
70  // Return nonzero if EOF has been reached on this stream.
71 
72  bool eof (void) const { return m_stream ? m_stream->eof () : true; }
73 
74  // The name of the file.
75 
76  std::string name (void) const { return m_name; }
77 
78  std::istream * input_stream (void)
79  {
80  return (m_mode & std::ios::in) ? m_stream : nullptr;
81  }
82 
83  std::ostream * output_stream (void)
84  {
85  return (m_mode & std::ios::out) ? m_stream : nullptr;
86  }
87 
88  // FIXME: should not have to cast away const here.
89  BUF_T * rdbuf (void) const
90  {
91  return m_stream ? (const_cast<STREAM_T *> (m_stream))->rdbuf () : nullptr;
92  }
93 
94  int file_number (void) const { return m_fnum; }
95 
96  bool bad (void) const { return m_stream ? m_stream->bad () : true; }
97 
98  void clear (void)
99  {
100  if (m_stream)
101  m_stream->clear ();
102  }
103 
104  void do_close (void)
105  {
106  if (m_stream)
107  m_stream->stream_close ();
108  }
109 
110 protected:
111 
112  ~tstdiostream (void) { delete m_stream; }
113 
114  //--------
115 
116  std::string m_name;
117 
118  std::ios::openmode m_mode;
119 
121 
122  // The file number associated with this file.
123  int m_fnum;
124 };
125 
126 class
129 {
130 public:
131 
132  stdiostream (const std::string& n, FILE *f = nullptr,
133  std::ios::openmode m = std::ios::in | std::ios::out,
135  const std::string& encoding = "utf-8",
138  (n, f, f ? fileno (f) : -1, m, ff, encoding, cf) { }
139 
140  static stream
141  create (const std::string& n, FILE *f = nullptr,
142  std::ios::openmode m = std::ios::in | std::ios::out,
144  const std::string& encoding = "utf-8",
146  {
147  return stream (new stdiostream (n, f, m, ff, encoding, cf));
148  }
149 
150  // No copying!
151 
152  stdiostream (const stdiostream&) = delete;
153 
154  stdiostream& operator = (const stdiostream&) = delete;
155 
156 protected:
157 
158  ~stdiostream (void) = default;
159 };
160 
161 #if defined (HAVE_ZLIB)
162 
163 class
166 {
167 public:
168 
169  zstdiostream (const std::string& n, gzFile f = nullptr, int fid = 0,
170  std::ios::openmode m = std::ios::in | std::ios::out,
172  const std::string& encoding = "utf-8",
175  (n, f, fid, m, ff, encoding, cf) { }
176 
177  static stream
178  create (const std::string& n, gzFile f = nullptr, int fid = 0,
179  std::ios::openmode m = std::ios::in | std::ios::out,
181  const std::string& encoding = "utf-8",
183  {
184  return stream (new zstdiostream (n, f, fid, m, ff, encoding, cf));
185  }
186 
187  // No copying!
188 
189  zstdiostream (const zstdiostream&) = delete;
190 
191  zstdiostream& operator = (const zstdiostream&) = delete;
192 
193 protected:
194 
195  ~zstdiostream (void) = default;
196 };
197 
198 #endif
199 
201 
202 #if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
203 
204 OCTAVE_DEPRECATED (7, "use 'octave::stdiostream' instead")
205 typedef octave::stdiostream octave_stdiostream;
206 
207 OCTAVE_DEPRECATED (7, "use 'octave::zstdiostream' instead")
208 typedef octave::zstdiostream octave_zstdiostream;
209 
210 #endif
211 
212 #endif
OCTAVE_END_NAMESPACE(octave)
int(* close_fcn)(FILE *)
static int file_close(FILE *m_f)
static int file_close(gzFile m_f)
int(* close_fcn)(gzFile)
stdiostream(const std::string &n, FILE *f=nullptr, std::ios::openmode m=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8", c_file_ptr_buf::close_fcn cf=c_file_ptr_buf::file_close)
Definition: oct-stdstrm.h:132
stdiostream(const stdiostream &)=delete
static stream create(const std::string &n, FILE *f=nullptr, std::ios::openmode m=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8", c_file_ptr_buf::close_fcn cf=c_file_ptr_buf::file_close)
Definition: oct-stdstrm.h:141
~stdiostream(void)=default
std::istream * input_stream(void)
Definition: oct-stdstrm.h:78
bool bad(void) const
Definition: oct-stdstrm.h:96
void do_close(void)
Definition: oct-stdstrm.h:104
int file_number(void) const
Definition: oct-stdstrm.h:94
std::ios::openmode m_mode
Definition: oct-stdstrm.h:118
std::string m_name
Definition: oct-stdstrm.h:116
tstdiostream(const tstdiostream &)=delete
tstdiostream(const std::string &n, FILE_T f=0, int fid=0, std::ios::openmode m=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8", typename BUF_T::close_fcn cf=BUF_T::file_close)
Definition: oct-stdstrm.h:44
BUF_T * rdbuf(void) const
Definition: oct-stdstrm.h:89
off_t tell(void)
Definition: oct-stdstrm.h:68
STREAM_T * m_stream
Definition: oct-stdstrm.h:120
std::string name(void) const
Definition: oct-stdstrm.h:76
void clear(void)
Definition: oct-stdstrm.h:98
int seek(off_t offset, int origin)
Definition: oct-stdstrm.h:61
~tstdiostream(void)
Definition: oct-stdstrm.h:112
bool eof(void) const
Definition: oct-stdstrm.h:72
std::ostream * output_stream(void)
Definition: oct-stdstrm.h:83
~zstdiostream(void)=default
zstdiostream(const std::string &n, gzFile f=nullptr, int fid=0, std::ios::openmode m=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8", c_zfile_ptr_buf::close_fcn cf=c_zfile_ptr_buf::file_close)
Definition: oct-stdstrm.h:169
static stream create(const std::string &n, gzFile f=nullptr, int fid=0, std::ios::openmode m=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8", c_zfile_ptr_buf::close_fcn cf=c_zfile_ptr_buf::file_close)
Definition: oct-stdstrm.h:178
zstdiostream(const zstdiostream &)=delete
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE * f
float_format native_float_format(void)
Definition: mach-info.cc:65
float_format
Definition: mach-info.h:38
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753