GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-stdstrm.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
38template <typename BUF_T, typename STREAM_T, typename FILE_T>
40{
41public:
42
43 tstdiostream (const std::string& n, FILE_T f = 0, int fid = 0,
44 std::ios::openmode m = std::ios::in | std::ios::out,
45 mach_info::float_format ff = mach_info::native_float_format (),
46 const std::string& encoding = "utf-8",
47 typename BUF_T::close_fcn cf = BUF_T::file_close)
48 : base_stream (m, ff, encoding), m_name (n), m_mode (m),
49 m_stream (f ? new STREAM_T (f, cf) : nullptr), m_fnum (fid)
50 { }
51
52 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tstdiostream)
53
54 // Position a stream at OFFSET relative to ORIGIN.
55
56 int seek (off_t offset, int origin)
57 {
58 return m_stream ? m_stream->seek (offset, origin) : -1;
59 }
60
61 // Return current stream position.
62
63 off_t tell () { return m_stream ? m_stream->tell () : -1; }
64
65 // Return nonzero if EOF has been reached on this stream.
66
67 bool eof () const { return m_stream ? m_stream->eof () : true; }
68
69 // The name of the file.
70
71 std::string name () const { return m_name; }
72
73 std::istream * input_stream ()
74 {
75 return (m_mode & std::ios::in) ? m_stream : nullptr;
76 }
77
78 std::ostream * output_stream ()
79 {
80 return (m_mode & std::ios::out) ? m_stream : nullptr;
81 }
82
83 // FIXME: should not have to cast away const here.
84 BUF_T * rdbuf () const
85 {
86 return m_stream ? (const_cast<STREAM_T *> (m_stream))->rdbuf () : nullptr;
87 }
88
89 int file_number () const { return m_fnum; }
90
91 bool bad () const { return m_stream ? m_stream->bad () : true; }
92
93 void clear ()
94 {
95 if (m_stream)
96 m_stream->clear ();
97 }
98
99 void do_close ()
100 {
101 if (m_stream)
102 m_stream->stream_close ();
103 }
104
105protected:
106
107 ~tstdiostream () { delete m_stream; }
108
109 //--------
110
111 std::string m_name;
112
113 std::ios::openmode m_mode;
114
116
117 // The file number associated with this file.
119};
120
122 : public tstdiostream<c_file_ptr_buf, io_c_file_ptr_stream, FILE *>
123{
124public:
125
126 stdiostream (const std::string& n, FILE *f = nullptr,
127 std::ios::openmode m = std::ios::in | std::ios::out,
128 mach_info::float_format ff = mach_info::native_float_format (),
129 const std::string& encoding = "utf-8",
132 (n, f, f ? fileno (f) : -1, m, ff, encoding, cf) { }
133
134 static stream
135 create (const std::string& n, FILE *f = nullptr,
136 std::ios::openmode m = std::ios::in | std::ios::out,
137 mach_info::float_format ff = mach_info::native_float_format (),
138 const std::string& encoding = "utf-8",
140 {
141 return stream (new stdiostream (n, f, m, ff, encoding, cf));
142 }
143
144 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (stdiostream)
145
146protected:
147
148 ~stdiostream () = default;
149};
150
151#if defined (HAVE_ZLIB)
152
154 : public tstdiostream<c_zfile_ptr_buf, io_c_zfile_ptr_stream, gzFile>
155{
156public:
157
158 zstdiostream (const std::string& n, gzFile f = nullptr, int fid = 0,
159 std::ios::openmode m = std::ios::in | std::ios::out,
160 mach_info::float_format ff = mach_info::native_float_format (),
161 const std::string& encoding = "utf-8",
164 (n, f, fid, m, ff, encoding, cf) { }
165
166 static stream
167 create (const std::string& n, gzFile f = nullptr, int fid = 0,
168 std::ios::openmode m = std::ios::in | std::ios::out,
169 mach_info::float_format ff = mach_info::native_float_format (),
170 const std::string& encoding = "utf-8",
172 {
173 return stream (new zstdiostream (n, f, fid, m, ff, encoding, cf));
174 }
175
176 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (zstdiostream)
177
178protected:
179
180 ~zstdiostream () = default;
181};
182
183#endif
184
185OCTAVE_END_NAMESPACE(octave)
186
187#endif
std::string encoding() const
Definition oct-stream.h:184
friend class stream
Definition oct-stream.h:68
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)
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)
~stdiostream()=default
std::ios::openmode m_mode
bool bad() const
Definition oct-stdstrm.h:91
std::ostream * output_stream()
Definition oct-stdstrm.h:78
std::string m_name
int file_number() const
Definition oct-stdstrm.h:89
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:43
void do_close()
Definition oct-stdstrm.h:99
std::string name() const
Definition oct-stdstrm.h:71
STREAM_T * m_stream
off_t tell()
Definition oct-stdstrm.h:63
int seek(off_t offset, int origin)
Definition oct-stdstrm.h:56
BUF_T * rdbuf() const
Definition oct-stdstrm.h:84
std::istream * input_stream()
Definition oct-stdstrm.h:73
bool eof() const
Definition oct-stdstrm.h:67
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)
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)
~zstdiostream()=default
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE * f