GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-stdstrm.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_oct_stdstrm_h)
24 #define octave_oct_stdstrm_h 1
25 
26 #include "oct-stream.h"
27 #include "c-file-ptr-stream.h"
28 
29 template <typename BUF_T, typename STREAM_T, typename FILE_T>
30 class
32 {
33 public:
34 
35  octave_tstdiostream (const std::string& n, FILE_T f = 0, int fid = 0,
36  std::ios::openmode m = std::ios::in|std::ios::out,
39  typename BUF_T::close_fcn cf = BUF_T::file_close)
40  : octave_base_stream (m, ff), nm (n), md (m),
41  s (f ? new STREAM_T (f, cf) : 0), fnum (fid)
42  { }
43 
44  // Position a stream at OFFSET relative to ORIGIN.
45 
46  int seek (off_t offset, int origin)
47  { return s ? s->seek (offset, origin) : -1; }
48 
49  // Return current stream position.
50 
51  off_t tell (void) { return s ? s->tell () : -1; }
52 
53  // Return non-zero if EOF has been reached on this stream.
54 
55  bool eof (void) const { return s ? s->eof () : true; }
56 
57  // The name of the file.
58 
59  std::string name (void) const { return nm; }
60 
61  std::istream *input_stream (void) { return (md & std::ios::in) ? s : 0; }
62 
63  std::ostream *output_stream (void) { return (md & std::ios::out) ? s : 0; }
64 
65  // FIXME: should not have to cast away const here.
66  BUF_T *rdbuf (void) const
67  { return s ? (const_cast<STREAM_T *> (s))->rdbuf () : 0; }
68 
69  int file_number (void) const { return fnum; }
70 
71  bool bad (void) const { return s ? s->bad () : true; }
72 
73  void clear (void) { if (s) s->clear (); }
74 
75  void do_close (void) { if (s) s->stream_close (); }
76 
77 protected:
78 
79  std::string nm;
80 
81  std::ios::openmode md;
82 
84 
85  // The file number associated with this file.
86  int fnum;
87 
88  ~octave_tstdiostream (void) { delete s; }
89 
90 private:
91 
92  // No copying!
93 
95 
96  octave_tstdiostream& operator = (const octave_tstdiostream&);
97 };
98 
99 class
102 {
103 public:
104 
105  octave_stdiostream (const std::string& n, FILE *f = 0,
106  std::ios::openmode m = std::ios::in|std::ios::out,
111  (n, f, f ? fileno (f) : -1, m, ff, cf) { }
112 
113  static octave_stream
114  create (const std::string& n, FILE *f = 0,
115  std::ios::openmode m = std::ios::in|std::ios::out,
119  {
120  return octave_stream (new octave_stdiostream (n, f, m, ff, cf));
121  }
122 
123 protected:
124 
126 
127 private:
128 
129  // No copying!
130 
132 
133  octave_stdiostream& operator = (const octave_stdiostream&);
134 };
135 
136 #ifdef HAVE_ZLIB
137 
138 class
141 {
142 public:
143 
144  octave_zstdiostream (const std::string& n, gzFile f = 0, int fid = 0,
145  std::ios::openmode m = std::ios::in|std::ios::out,
151  (n, f, fid, m, ff, cf) { }
152 
153  static octave_stream
154  create (const std::string& n, gzFile f = 0, int fid = 0,
155  std::ios::openmode m = std::ios::in|std::ios::out,
159  {
160  return octave_stream (new octave_zstdiostream (n, f, fid, m, ff, cf));
161  }
162 
163 protected:
164 
166 
167 private:
168 
169  // No copying!
170 
172 
173  octave_zstdiostream& operator = (const octave_zstdiostream&);
174 };
175 
176 #endif
177 
178 #endif