]> git.saurik.com Git - wxWidgets.git/blame - src/common/mstream.cpp
More Motif stuff, minor stubs correction
[wxWidgets.git] / src / common / mstream.cpp
CommitLineData
3d4c6a21 1/////////////////////////////////////////////////////////////////////////////
32fc4afb 2// Name: mstream.cpp
3d4c6a21
GL
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__
32fc4afb 13#pragma implementation "mstream.h"
3d4c6a21
GL
14#endif
15
79c3e0e1
GL
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
3d4c6a21 18#include <stdlib.h>
79c3e0e1
GL
19#include <wx/stream.h>
20#include <wx/mstream.h>
21
22#ifdef __BORLANDC__
23#pragma hdrstop
24#endif
3d4c6a21 25
79c3e0e1
GL
26// ----------------------------------------------------------------------------
27// wxMemoryStreamBase
28// ----------------------------------------------------------------------------
29wxMemoryStreamBase::wxMemoryStreamBase()
3d4c6a21 30{
79c3e0e1
GL
31 m_buffer = NULL;
32 m_iolimit = 0;
3d4c6a21 33 m_persistent = FALSE;
79c3e0e1 34 m_length = 0;
3d4c6a21
GL
35}
36
37wxMemoryStreamBase::~wxMemoryStreamBase()
38{
79c3e0e1
GL
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;
6d44bf31
GL
70
71 m_i_streambuf->SetBufferIO(0);
3d4c6a21
GL
72}
73
0cd9bfe8
GL
74wxMemoryInputStream::~wxMemoryInputStream()
75{
76}
77
32a4b1d5
GL
78char wxMemoryInputStream::Peek()
79{
80 // wxStreamBuffer is disabled so just peek the current character.
81
82 return m_buffer[m_position_i];
83}
84
6d44bf31 85size_t wxMemoryInputStream::DoRead(void *buffer, size_t size)
3d4c6a21
GL
86{
87 if (m_iolimit == 2) {
88 m_eof = TRUE;
6d44bf31 89 return 0;
3d4c6a21
GL
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;
3d4c6a21 96
6d44bf31 97 return size;
3d4c6a21
GL
98}
99
6d44bf31 100off_t wxMemoryInputStream::DoSeekInput(off_t pos, wxSeekMode mode)
3d4c6a21
GL
101{
102 if (m_iolimit == 2)
103 return 0;
104
79c3e0e1
GL
105 switch (mode) {
106 case wxFromStart:
3d4c6a21
GL
107 if ((size_t)pos > m_length)
108 return m_position_i;
109 return (m_position_i = pos);
110
79c3e0e1 111 case wxFromCurrent:
3d4c6a21
GL
112 if ((size_t)(m_position_i+pos) > m_length)
113 return m_position_i;
114
115 return (m_position_i += pos);
116
79c3e0e1 117 case wxFromEnd:
3d4c6a21
GL
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
79c3e0e1
GL
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;
6d44bf31
GL
140
141 m_o_streambuf->SetBufferIO(0);
79c3e0e1
GL
142}
143
0cd9bfe8
GL
144wxMemoryOutputStream::~wxMemoryOutputStream()
145{
6d44bf31 146 Sync();
0cd9bfe8
GL
147}
148
6d44bf31 149size_t wxMemoryOutputStream::DoWrite(const void *buffer, size_t size)
3d4c6a21
GL
150{
151 if (m_iolimit == 1) {
152 m_bad = TRUE;
6d44bf31 153 return 0;
3d4c6a21
GL
154 }
155
156 if (m_position_o+size > m_length)
157 if (!ChangeBufferSize(m_position_o+size)) {
158 m_bad = TRUE;
6d44bf31 159 return 0;
3d4c6a21
GL
160 }
161
162 memcpy(m_buffer+m_position_o, buffer, size);
163 m_position_o += size;
3d4c6a21 164
6d44bf31 165 return size;
3d4c6a21
GL
166}
167
6d44bf31 168off_t wxMemoryOutputStream::DoSeekOutput(off_t pos, wxSeekMode mode)
3d4c6a21 169{
79c3e0e1 170 if (m_iolimit == 1)
3d4c6a21
GL
171 return 0;
172
79c3e0e1
GL
173 switch (mode) {
174 case wxFromStart:
3d4c6a21
GL
175 if ((size_t)pos > m_length)
176 return m_position_o;
177 return (m_position_o = pos);
178
79c3e0e1 179 case wxFromCurrent:
3d4c6a21
GL
180 if ((size_t)(m_position_o+pos) > m_length)
181 return m_position_o;
182
183 return (m_position_o += pos);
184
79c3e0e1 185 case wxFromEnd:
3d4c6a21
GL
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
79c3e0e1
GL
195// ----------------------------------------------------------------------------
196// wxMemoryStream
197// ----------------------------------------------------------------------------
198
199wxMemoryStream::wxMemoryStream(char *data, size_t len)
200 : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0)
3d4c6a21 201{
79c3e0e1
GL
202 m_persistent = FALSE;
203 m_buffer = data;
204 m_length = len;
205 m_iolimit = 0;
206}
3d4c6a21 207
79c3e0e1
GL
208wxMemoryStream::~wxMemoryStream()
209{
3d4c6a21 210}