GNU Octave  6.2.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-2021 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 
36 template <typename BUF_T, typename STREAM_T, typename FILE_T>
37 class
39 {
40 public:
41 
42  octave_tstdiostream (const std::string& n, FILE_T f = 0, int fid = 0,
43  std::ios::openmode m = std::ios::in | std::ios::out,
46  const std::string& encoding = "utf-8",
47  typename BUF_T::close_fcn cf = BUF_T::file_close)
48  : octave::base_stream (m, ff, encoding), m_name (n), m_mode (m),
49  m_stream (f ? new STREAM_T (f, cf) : nullptr), fnum (fid)
50  { }
51 
52  // No copying!
53 
55 
56  octave_tstdiostream& operator = (const octave_tstdiostream&) = delete;
57 
58  // Position a stream at OFFSET relative to ORIGIN.
59 
60  int seek (off_t offset, int origin)
61  {
62  return m_stream ? m_stream->seek (offset, origin) : -1;
63  }
64 
65  // Return current stream position.
66 
67  off_t tell (void) { return m_stream ? m_stream->tell () : -1; }
68 
69  // Return nonzero if EOF has been reached on this stream.
70 
71  bool eof (void) const { return m_stream ? m_stream->eof () : true; }
72 
73  // The name of the file.
74 
75  std::string name (void) const { return m_name; }
76 
77  std::istream * input_stream (void)
78  {
79  return (m_mode & std::ios::in) ? m_stream : nullptr;
80  }
81 
82  std::ostream * output_stream (void)
83  {
84  return (m_mode & std::ios::out) ? m_stream : nullptr;
85  }
86 
87  // FIXME: should not have to cast away const here.
88  BUF_T * rdbuf (void) const
89  {
90  return m_stream ? (const_cast<STREAM_T *> (m_stream))->rdbuf () : nullptr;
91  }
92 
93  int file_number (void) const { return fnum; }
94 
95  bool bad (void) const { return m_stream ? m_stream->bad () : true; }
96 
97  void clear (void)
98  {
99  if (m_stream)
100  m_stream->clear ();
101  }
102 
103  void do_close (void)
104  {
105  if (m_stream)
106  m_stream->stream_close ();
107  }
108 
109 protected:
110 
111  std::string m_name;
112 
113  std::ios::openmode m_mode;
114 
116 
117  // The file number associated with this file.
118  int fnum;
119 
120  ~octave_tstdiostream (void) { delete m_stream; }
121 };
122 
123 class
126 {
127 public:
128 
129  octave_stdiostream (const std::string& n, FILE *f = nullptr,
130  std::ios::openmode m = std::ios::in | std::ios::out,
133  const std::string& encoding = "utf-8",
136  (n, f, f ? fileno (f) : -1, m, ff, encoding, cf) { }
137 
138  static octave::stream
139  create (const std::string& n, FILE *f = nullptr,
140  std::ios::openmode m = std::ios::in | std::ios::out,
143  const std::string& encoding = "utf-8",
145  {
146  return octave::stream (new octave_stdiostream (n, f, m, ff, encoding, cf));
147  }
148 
149  // No copying!
150 
152 
153  octave_stdiostream& operator = (const octave_stdiostream&) = delete;
154 
155 protected:
156 
157  ~octave_stdiostream (void) = default;
158 };
159 
160 #if defined (HAVE_ZLIB)
161 
162 class
165 {
166 public:
167 
168  octave_zstdiostream (const std::string& n, gzFile f = nullptr, int fid = 0,
169  std::ios::openmode m = std::ios::in | std::ios::out,
172  const std::string& encoding = "utf-8",
176  (n, f, fid, m, ff, encoding, cf) { }
177 
178  static octave::stream
179  create (const std::string& n, gzFile f = nullptr, int fid = 0,
180  std::ios::openmode m = std::ios::in | std::ios::out,
183  const std::string& encoding = "utf-8",
185  {
186  return octave::stream (new octave_zstdiostream (n, f, fid, m, ff, encoding,
187  cf));
188  }
189 
190  // No copying!
191 
193 
194  octave_zstdiostream& operator = (const octave_zstdiostream&) = delete;
195 
196 protected:
197 
198  ~octave_zstdiostream (void) = default;
199 };
200 
201 #endif
202 
203 #endif
static void close_fcn(FILE *f)
int(* close_fcn)(FILE *)
static int file_close(FILE *f)
int(* close_fcn)(gzFile)
static int file_close(gzFile f)
octave_stdiostream(const octave_stdiostream &)=delete
~octave_stdiostream(void)=default
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(), const std::string &encoding="utf-8", c_file_ptr_buf::close_fcn cf=c_file_ptr_buf::file_close)
Definition: oct-stdstrm.h:139
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(), const std::string &encoding="utf-8", c_file_ptr_buf::close_fcn cf=c_file_ptr_buf::file_close)
Definition: oct-stdstrm.h:129
off_t tell(void)
Definition: oct-stdstrm.h:67
octave_tstdiostream(const octave_tstdiostream &)=delete
std::ostream * output_stream(void)
Definition: oct-stdstrm.h:82
bool eof(void) const
Definition: oct-stdstrm.h:71
bool bad(void) const
Definition: oct-stdstrm.h:95
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(), const std::string &encoding="utf-8", typename BUF_T::close_fcn cf=BUF_T::file_close)
Definition: oct-stdstrm.h:42
int file_number(void) const
Definition: oct-stdstrm.h:93
std::ios::openmode m_mode
Definition: oct-stdstrm.h:113
std::string name(void) const
Definition: oct-stdstrm.h:75
BUF_T * rdbuf(void) const
Definition: oct-stdstrm.h:88
std::string m_name
Definition: oct-stdstrm.h:111
std::istream * input_stream(void)
Definition: oct-stdstrm.h:77
void do_close(void)
Definition: oct-stdstrm.h:103
void clear(void)
Definition: oct-stdstrm.h:97
STREAM_T * m_stream
Definition: oct-stdstrm.h:115
int seek(off_t offset, int origin)
Definition: oct-stdstrm.h:60
~octave_zstdiostream(void)=default
octave_zstdiostream(const octave_zstdiostream &)=delete
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(), const std::string &encoding="utf-8", c_zfile_ptr_buf::close_fcn cf=c_zfile_ptr_buf::file_close)
Definition: oct-stdstrm.h:179
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(), const std::string &encoding="utf-8", c_zfile_ptr_buf::close_fcn cf=c_zfile_ptr_buf::file_close)
Definition: oct-stdstrm.h:168
F77_RET_T const F77_DBLE const F77_DBLE * f
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
float_format native_float_format(void)
Definition: mach-info.cc:65