]>
Commit | Line | Data |
---|---|---|
3d4c6a21 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: fstream.cpp | |
3 | // Purpose: "File stream" classes | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 11/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
593d4b0d | 13 | #pragma implementation "wfstream.h" |
3d4c6a21 GL |
14 | #endif |
15 | ||
79c3e0e1 GL |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
3d4c6a21 | 18 | |
79c3e0e1 | 19 | #ifdef __BORLANDC__ |
ce4169a4 | 20 | #pragma hdrstop |
79c3e0e1 GL |
21 | #endif |
22 | ||
ce4169a4 RR |
23 | #ifndef WX_PRECOMP |
24 | #include "wx/defs.h" | |
25 | #endif | |
26 | ||
27 | #if wxUSE_STREAMS && wxUSE_FILE | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <wx/stream.h> | |
31 | #include <wx/wfstream.h> | |
32 | ||
79c3e0e1 GL |
33 | // ---------------------------------------------------------------------------- |
34 | // wxFileInputStream | |
35 | // ---------------------------------------------------------------------------- | |
36 | ||
37 | wxFileInputStream::wxFileInputStream(const wxString& fileName) | |
25c70b07 | 38 | : wxInputStream() |
3d4c6a21 | 39 | { |
25c70b07 GL |
40 | m_file = new wxFile(fileName, wxFile::read); |
41 | m_file_destroy = TRUE; | |
3d4c6a21 GL |
42 | } |
43 | ||
25c70b07 GL |
44 | wxFileInputStream::wxFileInputStream() |
45 | : wxInputStream() | |
46 | { | |
47 | m_file_destroy = FALSE; | |
48 | m_file = NULL; | |
49 | } | |
50 | ||
0aca1ded GL |
51 | wxFileInputStream::wxFileInputStream(wxFile& file) |
52 | { | |
53 | m_file = &file; | |
54 | m_file_destroy = FALSE; | |
0aca1ded GL |
55 | } |
56 | ||
57 | wxFileInputStream::wxFileInputStream(int fd) | |
58 | { | |
59 | m_file = new wxFile(fd); | |
60 | m_file_destroy = TRUE; | |
0aca1ded GL |
61 | } |
62 | ||
79c3e0e1 | 63 | wxFileInputStream::~wxFileInputStream() |
3d4c6a21 | 64 | { |
25c70b07 GL |
65 | if (m_file_destroy) |
66 | delete m_file; | |
3d4c6a21 GL |
67 | } |
68 | ||
6d44bf31 | 69 | char wxFileInputStream::Peek() |
3d4c6a21 | 70 | { |
6d44bf31 | 71 | return 0; |
3d4c6a21 GL |
72 | } |
73 | ||
84b46c35 GL |
74 | size_t wxFileInputStream::StreamSize() const |
75 | { | |
76 | return m_file->Length(); | |
77 | } | |
78 | ||
75ed1d15 | 79 | size_t wxFileInputStream::OnSysRead(void *buffer, size_t size) |
6d44bf31 | 80 | { |
fae05df5 GL |
81 | off_t ret; |
82 | ||
83 | ret = m_file->Read(buffer, size); | |
84 | ||
85 | if (m_file->Eof()) | |
86 | m_lasterror = wxStream_EOF; | |
87 | if (ret == wxInvalidOffset) { | |
88 | m_lasterror = wxStream_READ_ERR; | |
89 | ret = 0; | |
90 | } | |
91 | ||
92 | return ret; | |
6d44bf31 GL |
93 | } |
94 | ||
75ed1d15 | 95 | off_t wxFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode) |
3d4c6a21 | 96 | { |
25c70b07 | 97 | return m_file->Seek(pos, mode); |
79c3e0e1 GL |
98 | } |
99 | ||
75ed1d15 | 100 | off_t wxFileInputStream::OnSysTell() const |
79c3e0e1 | 101 | { |
25c70b07 | 102 | return m_file->Tell(); |
79c3e0e1 GL |
103 | } |
104 | ||
105 | // ---------------------------------------------------------------------------- | |
106 | // wxFileOutputStream | |
107 | // ---------------------------------------------------------------------------- | |
108 | ||
109 | wxFileOutputStream::wxFileOutputStream(const wxString& fileName) | |
25c70b07 GL |
110 | { |
111 | m_file = new wxFile(fileName, wxFile::write); | |
112 | m_file_destroy = TRUE; | |
25c70b07 GL |
113 | } |
114 | ||
84b46c35 GL |
115 | wxFileOutputStream::wxFileOutputStream(wxFile& file) |
116 | { | |
117 | m_file = &file; | |
118 | m_file_destroy = FALSE; | |
84b46c35 GL |
119 | } |
120 | ||
25c70b07 GL |
121 | wxFileOutputStream::wxFileOutputStream() |
122 | : wxOutputStream() | |
79c3e0e1 | 123 | { |
25c70b07 GL |
124 | m_file_destroy = FALSE; |
125 | m_file = NULL; | |
3d4c6a21 GL |
126 | } |
127 | ||
0aca1ded GL |
128 | wxFileOutputStream::wxFileOutputStream(int fd) |
129 | { | |
130 | m_file = new wxFile(fd); | |
131 | m_file_destroy = TRUE; | |
0aca1ded GL |
132 | } |
133 | ||
79c3e0e1 | 134 | wxFileOutputStream::~wxFileOutputStream() |
3d4c6a21 | 135 | { |
25c70b07 GL |
136 | if (m_file_destroy) { |
137 | Sync(); | |
138 | delete m_file; | |
139 | } | |
79c3e0e1 | 140 | } |
3d4c6a21 | 141 | |
75ed1d15 | 142 | size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size) |
79c3e0e1 | 143 | { |
25c70b07 | 144 | size_t ret = m_file->Write(buffer, size); |
75ed1d15 | 145 | m_lasterror = wxStream_EOF; // TODO |
6d44bf31 | 146 | return ret; |
79c3e0e1 | 147 | } |
3d4c6a21 | 148 | |
75ed1d15 | 149 | off_t wxFileOutputStream::OnSysTell() const |
79c3e0e1 | 150 | { |
25c70b07 | 151 | return m_file->Tell(); |
3d4c6a21 GL |
152 | } |
153 | ||
75ed1d15 | 154 | off_t wxFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) |
3d4c6a21 | 155 | { |
25c70b07 | 156 | return m_file->Seek(pos, mode); |
3d4c6a21 GL |
157 | } |
158 | ||
79c3e0e1 | 159 | void wxFileOutputStream::Sync() |
3d4c6a21 | 160 | { |
6d44bf31 | 161 | wxOutputStream::Sync(); |
25c70b07 | 162 | m_file->Flush(); |
3d4c6a21 | 163 | } |
84b46c35 GL |
164 | |
165 | size_t wxFileOutputStream::StreamSize() const | |
166 | { | |
167 | return m_file->Length(); | |
168 | } | |
169 | ||
170 | // ---------------------------------------------------------------------------- | |
171 | // wxFileStream | |
172 | // ---------------------------------------------------------------------------- | |
173 | wxFileStream::wxFileStream(const wxString& fileName) | |
174 | : wxFileInputStream(fileName), wxFileOutputStream(*wxFileInputStream::m_file) | |
175 | { | |
176 | } | |
ce4169a4 RR |
177 | |
178 | #endif | |
179 | // wxUSE_STREAMS && wxUSE_FILE | |
cc985fac | 180 |