GNU Octave  9.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-2024 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  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tstdiostream)
54 
55  // Position a stream at OFFSET relative to ORIGIN.
56 
57  int seek (off_t offset, int origin)
58  {
59  return m_stream ? m_stream->seek (offset, origin) : -1;
60  }
61 
62  // Return current stream position.
63 
64  off_t tell () { return m_stream ? m_stream->tell () : -1; }
65 
66  // Return nonzero if EOF has been reached on this stream.
67 
68  bool eof () const { return m_stream ? m_stream->eof () : true; }
69 
70  // The name of the file.
71 
72  std::string name () const { return m_name; }
73 
74  std::istream * input_stream ()
75  {
76  return (m_mode & std::ios::in) ? m_stream : nullptr;
77  }
78 
79  std::ostream * output_stream ()
80  {
81  return (m_mode & std::ios::out) ? m_stream : nullptr;
82  }
83 
84  // FIXME: should not have to cast away const here.
85  BUF_T * rdbuf () const
86  {
87  return m_stream ? (const_cast<STREAM_T *> (m_stream))->rdbuf () : nullptr;
88  }
89 
90  int file_number () const { return m_fnum; }
91 
92  bool bad () const { return m_stream ? m_stream->bad () : true; }
93 
94  void clear ()
95  {
96  if (m_stream)
97  m_stream->clear ();
98  }
99 
100  void do_close ()
101  {
102  if (m_stream)
103  m_stream->stream_close ();
104  }
105 
106 protected:
107 
108  ~tstdiostream () { delete m_stream; }
109 
110  //--------
111 
112  std::string m_name;
113 
114  std::ios::openmode m_mode;
115 
117 
118  // The file number associated with this file.
119  int m_fnum;
120 };
121 
122 class
125 {
126 public:
127 
128  stdiostream (const std::string& n, FILE *f = nullptr,
129  std::ios::openmode m = std::ios::in | std::ios::out,
131  const std::string& encoding = "utf-8",
134  (n, f, f ? fileno (f) : -1, m, ff, encoding, cf) { }
135 
136  static stream
137  create (const std::string& n, FILE *f = nullptr,
138  std::ios::openmode m = std::ios::in | std::ios::out,
140  const std::string& encoding = "utf-8",
142  {
143  return stream (new stdiostream (n, f, m, ff, encoding, cf));
144  }
145 
146  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (stdiostream)
147 
148 protected:
149 
150  ~stdiostream () = default;
151 };
152 
153 #if defined (HAVE_ZLIB)
154 
155 class
158 {
159 public:
160 
161  zstdiostream (const std::string& n, gzFile f = nullptr, int fid = 0,
162  std::ios::openmode m = std::ios::in | std::ios::out,
164  const std::string& encoding = "utf-8",
167  (n, f, fid, m, ff, encoding, cf) { }
168 
169  static stream
170  create (const std::string& n, gzFile f = nullptr, int fid = 0,
171  std::ios::openmode m = std::ios::in | std::ios::out,
173  const std::string& encoding = "utf-8",
175  {
176  return stream (new zstdiostream (n, f, fid, m, ff, encoding, cf));
177  }
178 
179  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (zstdiostream)
180 
181 protected:
182 
183  ~zstdiostream () = default;
184 };
185 
186 #endif
187 
188 OCTAVE_END_NAMESPACE(octave)
189 
190 #endif
virtual int seek(off_t offset, int origin)=0
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:128
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:137
~stdiostream()=default
std::ios::openmode m_mode
Definition: oct-stdstrm.h:114
BUF_T * rdbuf() const
Definition: oct-stdstrm.h:85
bool bad() const
Definition: oct-stdstrm.h:92
std::string m_name
Definition: oct-stdstrm.h:112
int file_number() const
Definition: oct-stdstrm.h:90
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
std::istream * input_stream()
Definition: oct-stdstrm.h:74
void do_close()
Definition: oct-stdstrm.h:100
std::string name() const
Definition: oct-stdstrm.h:72
STREAM_T * m_stream
Definition: oct-stdstrm.h:116
off_t tell()
Definition: oct-stdstrm.h:64
void clear()
Definition: oct-stdstrm.h:94
std::ostream * output_stream()
Definition: oct-stdstrm.h:79
bool eof() const
Definition: oct-stdstrm.h:68
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:161
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:170
~zstdiostream()=default
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE * f
float_format native_float_format()
Definition: mach-info.cc:67
float_format
Definition: mach-info.h:38
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761