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