2081
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2081
|
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 2, or (at your option) any |
|
10 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, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cerrno> |
|
28 #include <cstring> |
|
29 |
|
30 #include "error.h" |
|
31 #include "oct-fstrm.h" |
|
32 |
3340
|
33 octave_stream |
3523
|
34 octave_fstream::create (const std::string& nm_arg, ios::openmode md, |
3340
|
35 oct_mach_info::float_format flt_fmt) |
|
36 { |
|
37 return octave_stream (new octave_fstream (nm_arg, md, flt_fmt)); |
|
38 } |
|
39 |
3523
|
40 octave_fstream::octave_fstream (const std::string& nm_arg, |
2081
|
41 ios::openmode md = ios::in|ios::out, |
2317
|
42 oct_mach_info::float_format flt_fmt) |
|
43 : octave_base_stream (md, flt_fmt), nm (nm_arg) |
2081
|
44 { |
|
45 // Override default protection of 0664 so that umask will appear to |
|
46 // do the right thing. |
|
47 |
|
48 fs.open (nm.c_str (), md, 0666); |
|
49 |
|
50 if (! fs) |
|
51 error (strerror (errno)); |
|
52 } |
|
53 |
|
54 // Position a stream at OFFSET relative to ORIGIN. |
|
55 |
|
56 int |
|
57 octave_fstream::seek (streamoff offset, ios::seek_dir origin) |
|
58 { |
|
59 int retval = -1; |
|
60 |
|
61 if (! fs.bad ()) |
|
62 { |
|
63 fs.clear (); |
|
64 |
|
65 filebuf *fb = fs.rdbuf (); |
|
66 |
|
67 if (fb) |
|
68 { |
|
69 fb->seekoff (offset, origin); |
|
70 retval = fs.bad () ? -1 : 0; |
|
71 } |
|
72 } |
|
73 |
|
74 return retval; |
|
75 } |
|
76 |
|
77 // Return current stream position. |
|
78 |
|
79 long |
|
80 octave_fstream::tell (void) const |
|
81 { |
|
82 long retval = -1; |
|
83 |
|
84 if (fs) |
|
85 { |
|
86 filebuf *fb = fs.rdbuf (); |
2800
|
87 retval = static_cast<long> (fb->seekoff (0, ios::cur)); |
2081
|
88 } |
|
89 |
|
90 return retval; |
|
91 } |
|
92 |
|
93 // Return non-zero if EOF has been reached on this stream. |
|
94 |
|
95 bool |
|
96 octave_fstream::eof (void) const |
|
97 { |
|
98 return fs.eof (); |
|
99 } |
|
100 |
3523
|
101 std::istream * |
2081
|
102 octave_fstream::input_stream (void) |
|
103 { |
3523
|
104 std::istream *retval = 0; |
2081
|
105 |
|
106 if (mode () & ios::in) |
|
107 retval = &fs; |
|
108 |
|
109 return retval; |
|
110 } |
|
111 |
3523
|
112 std::ostream * |
2081
|
113 octave_fstream::output_stream (void) |
|
114 { |
3523
|
115 std::ostream *retval = 0; |
2081
|
116 |
|
117 if (mode () & ios::out) |
|
118 retval = &fs; |
|
119 |
|
120 return retval; |
|
121 } |
|
122 |
|
123 /* |
|
124 ;;; Local Variables: *** |
|
125 ;;; mode: C++ *** |
|
126 ;;; End: *** |
|
127 */ |