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
38 octave_tstdiostream : public octave::base_stream
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,
44                        octave::mach_info::float_format ff
45                          = octave::mach_info::native_float_format (),
46                        const std::string& encoding = "utf-8",
47                        typename BUF_T::close_fcn cf = BUF_T::file_close)
base_stream(m,ff,encoding)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 
54   octave_tstdiostream (const octave_tstdiostream&) = delete;
55 
56   octave_tstdiostream& operator = (const octave_tstdiostream&) = delete;
57 
58   // Position a stream at OFFSET relative to ORIGIN.
59 
seek(off_t offset,int origin)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 
tell(void)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 
eof(void)71   bool eof (void) const { return m_stream ? m_stream->eof () : true; }
72 
73   // The name of the file.
74 
name(void)75   std::string name (void) const { return m_name; }
76 
input_stream(void)77   std::istream * input_stream (void)
78   {
79     return (m_mode & std::ios::in) ? m_stream : nullptr;
80   }
81 
output_stream(void)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.
rdbuf(void)88   BUF_T * rdbuf (void) const
89   {
90     return m_stream ? (const_cast<STREAM_T *> (m_stream))->rdbuf () : nullptr;
91   }
92 
file_number(void)93   int file_number (void) const { return fnum; }
94 
bad(void)95   bool bad (void) const { return m_stream ? m_stream->bad () : true; }
96 
clear(void)97   void clear (void)
98   {
99     if (m_stream)
100       m_stream->clear ();
101   }
102 
do_close(void)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 
115   STREAM_T *m_stream;
116 
117   // The file number associated with this file.
118   int fnum;
119 
~octave_tstdiostream(void)120   ~octave_tstdiostream (void) { delete m_stream; }
121 };
122 
123 class
124 octave_stdiostream
125   : public octave_tstdiostream<c_file_ptr_buf, io_c_file_ptr_stream, FILE *>
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,
131                       octave::mach_info::float_format ff
132                         = octave::mach_info::native_float_format (),
133                       const std::string& encoding = "utf-8",
134                       c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::file_close)
135     : octave_tstdiostream<c_file_ptr_buf, io_c_file_ptr_stream, FILE *>
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,
141           octave::mach_info::float_format ff
142             = octave::mach_info::native_float_format (),
143           const std::string& encoding = "utf-8",
144           c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::file_close)
145   {
146     return octave::stream (new octave_stdiostream (n, f, m, ff, encoding, cf));
147   }
148 
149   // No copying!
150 
151   octave_stdiostream (const octave_stdiostream&) = delete;
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
163 octave_zstdiostream
164   : public octave_tstdiostream<c_zfile_ptr_buf, io_c_zfile_ptr_stream, gzFile>
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,
170                        octave::mach_info::float_format ff
171                          = octave::mach_info::native_float_format (),
172                        const std::string& encoding = "utf-8",
173                        c_zfile_ptr_buf::close_fcn cf
174                          = c_zfile_ptr_buf::file_close)
175     : octave_tstdiostream<c_zfile_ptr_buf, io_c_zfile_ptr_stream, gzFile>
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,
181           octave::mach_info::float_format ff
182             = octave::mach_info::native_float_format (),
183           const std::string& encoding = "utf-8",
184           c_zfile_ptr_buf::close_fcn cf = c_zfile_ptr_buf::file_close)
185   {
186     return octave::stream (new octave_zstdiostream (n, f, fid, m, ff, encoding,
187                                                     cf));
188   }
189 
190   // No copying!
191 
192   octave_zstdiostream (const octave_zstdiostream&) = delete;
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
204