]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/mstream.cpp
static wxFile::Access() added
[wxWidgets.git] / src / common / mstream.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: mstream.cpp
3// Purpose: "Memory stream" classes
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "mstream.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18#include <stdlib.h>
19#include <wx/stream.h>
20#include <wx/mstream.h>
21
22#ifdef __BORLANDC__
23#pragma hdrstop
24#endif
25
26// ----------------------------------------------------------------------------
27// wxMemoryStreamBase
28// ----------------------------------------------------------------------------
29wxMemoryStreamBase::wxMemoryStreamBase()
30{
31 m_buffer = NULL;
32 m_iolimit = 0;
33 m_persistent = FALSE;
34 m_length = 0;
35}
36
37wxMemoryStreamBase::~wxMemoryStreamBase()
38{
39 if (!m_persistent && m_buffer)
40 free(m_buffer);
41}
42
43bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size)
44{
45 if (m_iolimit == 1)
46 return FALSE;
47
48 m_length = new_size;
49 if (!m_buffer)
50 m_buffer = (char *)malloc(m_length);
51 else
52 m_buffer = (char *)realloc(m_buffer, m_length);
53
54 return (m_buffer != NULL);
55}
56
57// ----------------------------------------------------------------------------
58// wxMemoryInputStream
59// ----------------------------------------------------------------------------
60
61wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len)
62{
63 m_persistent = TRUE;
64 m_length = len;
65 m_buffer = (char *)data; // It's bad.
66 m_position_i = 0;
67 m_lastread = 0;
68 m_eof = FALSE;
69 m_iolimit = 1;
70
71 m_i_streambuf->SetBufferIO(0);
72}
73
74wxMemoryInputStream::~wxMemoryInputStream()
75{
76}
77
78char wxMemoryInputStream::Peek()
79{
80 // wxStreamBuffer is disabled so just peek the current character.
81
82 return m_buffer[m_position_i];
83}
84
85size_t wxMemoryInputStream::DoRead(void *buffer, size_t size)
86{
87 if (m_iolimit == 2) {
88 m_eof = TRUE;
89 return 0;
90 }
91 if (m_position_i+size > m_length)
92 size = m_length-m_position_i;
93
94 memcpy((void *)((unsigned long)buffer+m_position_i), m_buffer, size);
95 m_position_i += size;
96
97 return size;
98}
99
100off_t wxMemoryInputStream::DoSeekInput(off_t pos, wxSeekMode mode)
101{
102 if (m_iolimit == 2)
103 return 0;
104
105 switch (mode) {
106 case wxFromStart:
107 if ((size_t)pos > m_length)
108 return m_position_i;
109 return (m_position_i = pos);
110
111 case wxFromCurrent:
112 if ((size_t)(m_position_i+pos) > m_length)
113 return m_position_i;
114
115 return (m_position_i += pos);
116
117 case wxFromEnd:
118 if ((size_t)(m_length-pos) > m_length)
119 return m_position_i;
120
121 return (m_position_i = m_length-pos);
122 }
123
124 return m_position_i;
125}
126
127// ----------------------------------------------------------------------------
128// wxMemoryOutputStream
129// ----------------------------------------------------------------------------
130
131wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len)
132{
133 m_persistent = FALSE;
134 m_buffer = data;
135 m_length = len;
136 m_position_o = 0;
137 m_lastwrite = 0;
138 m_bad = FALSE;
139 m_iolimit = 2;
140
141 m_o_streambuf->SetBufferIO(0);
142}
143
144wxMemoryOutputStream::~wxMemoryOutputStream()
145{
146 Sync();
147}
148
149size_t wxMemoryOutputStream::DoWrite(const void *buffer, size_t size)
150{
151 if (m_iolimit == 1) {
152 m_bad = TRUE;
153 return 0;
154 }
155
156 if (m_position_o+size > m_length)
157 if (!ChangeBufferSize(m_position_o+size)) {
158 m_bad = TRUE;
159 return 0;
160 }
161
162 memcpy(m_buffer+m_position_o, buffer, size);
163 m_position_o += size;
164
165 return size;
166}
167
168off_t wxMemoryOutputStream::DoSeekOutput(off_t pos, wxSeekMode mode)
169{
170 if (m_iolimit == 1)
171 return 0;
172
173 switch (mode) {
174 case wxFromStart:
175 if ((size_t)pos > m_length)
176 return m_position_o;
177 return (m_position_o = pos);
178
179 case wxFromCurrent:
180 if ((size_t)(m_position_o+pos) > m_length)
181 return m_position_o;
182
183 return (m_position_o += pos);
184
185 case wxFromEnd:
186 if ((size_t)(m_length-pos) > m_length)
187 return m_position_o;
188
189 return (m_position_o = m_length-pos);
190 }
191
192 return m_position_o;
193}
194
195// ----------------------------------------------------------------------------
196// wxMemoryStream
197// ----------------------------------------------------------------------------
198
199wxMemoryStream::wxMemoryStream(char *data, size_t len)
200 : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0)
201{
202 m_persistent = FALSE;
203 m_buffer = data;
204 m_length = len;
205 m_iolimit = 0;
206}
207
208wxMemoryStream::~wxMemoryStream()
209{
210}