]> git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
* New wxStreams (to be documented), new classes: wxBufferedStreams,
[wxWidgets.git] / src / common / wfstream.cpp
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__
13 #pragma implementation "wfstream.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
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
33 // ----------------------------------------------------------------------------
34 // wxFileInputStream
35 // ----------------------------------------------------------------------------
36
37 wxFileInputStream::wxFileInputStream(const wxString& fileName)
38 : wxInputStream()
39 {
40 m_file = new wxFile(fileName, wxFile::read);
41 m_file_destroy = TRUE;
42 }
43
44 wxFileInputStream::wxFileInputStream()
45 : wxInputStream()
46 {
47 m_file_destroy = FALSE;
48 m_file = NULL;
49 }
50
51 wxFileInputStream::wxFileInputStream(wxFile& file)
52 {
53 m_file = &file;
54 m_file_destroy = FALSE;
55 }
56
57 wxFileInputStream::wxFileInputStream(int fd)
58 {
59 m_file = new wxFile(fd);
60 m_file_destroy = TRUE;
61 }
62
63 wxFileInputStream::~wxFileInputStream()
64 {
65 if (m_file_destroy)
66 delete m_file;
67 }
68
69 char wxFileInputStream::Peek()
70 {
71 return 0;
72 }
73
74 size_t wxFileInputStream::StreamSize() const
75 {
76 return m_file->Length();
77 }
78
79 size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
80 {
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;
93 }
94
95 off_t wxFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
96 {
97 return m_file->Seek(pos, mode);
98 }
99
100 off_t wxFileInputStream::OnSysTell() const
101 {
102 return m_file->Tell();
103 }
104
105 // ----------------------------------------------------------------------------
106 // wxFileOutputStream
107 // ----------------------------------------------------------------------------
108
109 wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
110 {
111 m_file = new wxFile(fileName, wxFile::write);
112 m_file_destroy = TRUE;
113 }
114
115 wxFileOutputStream::wxFileOutputStream(wxFile& file)
116 {
117 m_file = &file;
118 m_file_destroy = FALSE;
119 }
120
121 wxFileOutputStream::wxFileOutputStream()
122 : wxOutputStream()
123 {
124 m_file_destroy = FALSE;
125 m_file = NULL;
126 }
127
128 wxFileOutputStream::wxFileOutputStream(int fd)
129 {
130 m_file = new wxFile(fd);
131 m_file_destroy = TRUE;
132 }
133
134 wxFileOutputStream::~wxFileOutputStream()
135 {
136 if (m_file_destroy) {
137 Sync();
138 delete m_file;
139 }
140 }
141
142 size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
143 {
144 size_t ret = m_file->Write(buffer, size);
145 m_lasterror = wxStream_EOF; // TODO
146 return ret;
147 }
148
149 off_t wxFileOutputStream::OnSysTell() const
150 {
151 return m_file->Tell();
152 }
153
154 off_t wxFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
155 {
156 return m_file->Seek(pos, mode);
157 }
158
159 void wxFileOutputStream::Sync()
160 {
161 wxOutputStream::Sync();
162 m_file->Flush();
163 }
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 }
177
178 #endif
179 // wxUSE_STREAMS && wxUSE_FILE