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