]> git.saurik.com Git - wxWidgets.git/blame - src/common/mstream.cpp
fixed compilation problems under Windows
[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;
3d4c6a21
GL
70}
71
0cd9bfe8
GL
72wxMemoryInputStream::~wxMemoryInputStream()
73{
74}
75
79c3e0e1 76wxInputStream& wxMemoryInputStream::Read(void *buffer, size_t size)
3d4c6a21
GL
77{
78 if (m_iolimit == 2) {
79 m_eof = TRUE;
80 return *this;
81 }
82 if (m_position_i+size > m_length)
83 size = m_length-m_position_i;
84
85 memcpy((void *)((unsigned long)buffer+m_position_i), m_buffer, size);
86 m_position_i += size;
87 m_lastread = size;
88
89 return *this;
90}
91
79c3e0e1 92off_t wxMemoryInputStream::SeekI(off_t pos, wxSeekMode mode)
3d4c6a21
GL
93{
94 if (m_iolimit == 2)
95 return 0;
96
79c3e0e1
GL
97 switch (mode) {
98 case wxFromStart:
3d4c6a21
GL
99 if ((size_t)pos > m_length)
100 return m_position_i;
101 return (m_position_i = pos);
102
79c3e0e1 103 case wxFromCurrent:
3d4c6a21
GL
104 if ((size_t)(m_position_i+pos) > m_length)
105 return m_position_i;
106
107 return (m_position_i += pos);
108
79c3e0e1 109 case wxFromEnd:
3d4c6a21
GL
110 if ((size_t)(m_length-pos) > m_length)
111 return m_position_i;
112
113 return (m_position_i = m_length-pos);
114 }
115
116 return m_position_i;
117}
118
79c3e0e1
GL
119// ----------------------------------------------------------------------------
120// wxMemoryOutputStream
121// ----------------------------------------------------------------------------
122
123wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len)
124{
125 m_persistent = FALSE;
126 m_buffer = data;
127 m_length = len;
128 m_position_o = 0;
129 m_lastwrite = 0;
130 m_bad = FALSE;
131 m_iolimit = 2;
132}
133
0cd9bfe8
GL
134wxMemoryOutputStream::~wxMemoryOutputStream()
135{
136}
137
79c3e0e1 138wxOutputStream& wxMemoryOutputStream::Write(const void *buffer, size_t size)
3d4c6a21
GL
139{
140 if (m_iolimit == 1) {
141 m_bad = TRUE;
142 return *this;
143 }
144
145 if (m_position_o+size > m_length)
146 if (!ChangeBufferSize(m_position_o+size)) {
147 m_bad = TRUE;
148 return *this;
149 }
150
151 memcpy(m_buffer+m_position_o, buffer, size);
152 m_position_o += size;
153 m_lastwrite = size;
154
155 return *this;
156}
157
79c3e0e1 158off_t wxMemoryOutputStream::SeekO(off_t pos, wxSeekMode mode)
3d4c6a21 159{
79c3e0e1 160 if (m_iolimit == 1)
3d4c6a21
GL
161 return 0;
162
79c3e0e1
GL
163 switch (mode) {
164 case wxFromStart:
3d4c6a21
GL
165 if ((size_t)pos > m_length)
166 return m_position_o;
167 return (m_position_o = pos);
168
79c3e0e1 169 case wxFromCurrent:
3d4c6a21
GL
170 if ((size_t)(m_position_o+pos) > m_length)
171 return m_position_o;
172
173 return (m_position_o += pos);
174
79c3e0e1 175 case wxFromEnd:
3d4c6a21
GL
176 if ((size_t)(m_length-pos) > m_length)
177 return m_position_o;
178
179 return (m_position_o = m_length-pos);
180 }
181
182 return m_position_o;
183}
184
79c3e0e1
GL
185// ----------------------------------------------------------------------------
186// wxMemoryStream
187// ----------------------------------------------------------------------------
188
189wxMemoryStream::wxMemoryStream(char *data, size_t len)
190 : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0)
3d4c6a21 191{
79c3e0e1
GL
192 m_persistent = FALSE;
193 m_buffer = data;
194 m_length = len;
195 m_iolimit = 0;
196}
3d4c6a21 197
79c3e0e1
GL
198wxMemoryStream::~wxMemoryStream()
199{
3d4c6a21 200}