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 |
3544
|
34 octave_fstream::create (const std::string& nm_arg, std::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 |
3544
|
40 octave_fstream::octave_fstream |
|
41 (const std::string& nm_arg, |
|
42 std::ios::openmode md = std::ios::in|std::ios::out, |
|
43 oct_mach_info::float_format flt_fmt) |
2317
|
44 : octave_base_stream (md, flt_fmt), nm (nm_arg) |
2081
|
45 { |
|
46 // Override default protection of 0664 so that umask will appear to |
|
47 // do the right thing. |
|
48 |
|
49 fs.open (nm.c_str (), md, 0666); |
|
50 |
|
51 if (! fs) |
3531
|
52 { |
|
53 using namespace std; |
|
54 |
|
55 error (strerror (errno)); |
|
56 } |
2081
|
57 } |
|
58 |
|
59 // Position a stream at OFFSET relative to ORIGIN. |
|
60 |
|
61 int |
3546
|
62 octave_fstream::seek (std::streamoff offset, std::ios::seek_dir origin) |
2081
|
63 { |
|
64 int retval = -1; |
|
65 |
|
66 if (! fs.bad ()) |
|
67 { |
|
68 fs.clear (); |
|
69 |
|
70 filebuf *fb = fs.rdbuf (); |
|
71 |
|
72 if (fb) |
|
73 { |
|
74 fb->seekoff (offset, origin); |
|
75 retval = fs.bad () ? -1 : 0; |
|
76 } |
|
77 } |
|
78 |
|
79 return retval; |
|
80 } |
|
81 |
|
82 // Return current stream position. |
|
83 |
|
84 long |
|
85 octave_fstream::tell (void) const |
|
86 { |
|
87 long retval = -1; |
|
88 |
|
89 if (fs) |
|
90 { |
|
91 filebuf *fb = fs.rdbuf (); |
3544
|
92 retval = static_cast<long> (fb->seekoff (0, std::ios::cur)); |
2081
|
93 } |
|
94 |
|
95 return retval; |
|
96 } |
|
97 |
|
98 // Return non-zero if EOF has been reached on this stream. |
|
99 |
|
100 bool |
|
101 octave_fstream::eof (void) const |
|
102 { |
|
103 return fs.eof (); |
|
104 } |
|
105 |
3523
|
106 std::istream * |
2081
|
107 octave_fstream::input_stream (void) |
|
108 { |
3523
|
109 std::istream *retval = 0; |
2081
|
110 |
3544
|
111 if (mode () & std::ios::in) |
2081
|
112 retval = &fs; |
|
113 |
|
114 return retval; |
|
115 } |
|
116 |
3523
|
117 std::ostream * |
2081
|
118 octave_fstream::output_stream (void) |
|
119 { |
3523
|
120 std::ostream *retval = 0; |
2081
|
121 |
3544
|
122 if (mode () & std::ios::out) |
2081
|
123 retval = &fs; |
|
124 |
|
125 return retval; |
|
126 } |
|
127 |
|
128 /* |
|
129 ;;; Local Variables: *** |
|
130 ;;; mode: C++ *** |
|
131 ;;; End: *** |
|
132 */ |