Ooops ! I've forgotten the include files.
[wxWidgets.git] / src / common / mstream.cpp
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 #include <stdlib.h>
17 #include <wx/stream.h>
18 #include <wx/mstream.h>
19
20 wxMemoryStreamBase::wxMemoryStreamBase(char *data, size_t length, int iolimit)
21 {
22 m_buffer = data;
23 m_iolimit = iolimit;
24 m_persistent = FALSE;
25 m_length = length;
26 m_position_i = m_position_o = 0;
27 }
28
29 wxMemoryStreamBase::~wxMemoryStreamBase()
30 {
31 free(m_buffer);
32 }
33
34 wxInputStream& wxMemoryStreamBase::Read(void *buffer, size_t size)
35 {
36 if (m_iolimit == 2) {
37 m_eof = TRUE;
38 return *this;
39 }
40 if (m_position_i+size > m_length)
41 size = m_length-m_position_i;
42
43 memcpy((void *)((unsigned long)buffer+m_position_i), m_buffer, size);
44 m_position_i += size;
45 m_lastread = size;
46
47 return *this;
48 }
49
50 size_t wxMemoryStreamBase::SeekI(int pos, wxWhenceType whence)
51 {
52 if (m_iolimit == 2)
53 return 0;
54
55 switch (whence) {
56 case wxBeginPosition:
57 if ((size_t)pos > m_length)
58 return m_position_i;
59 return (m_position_i = pos);
60
61 case wxCurrentPosition:
62 if ((size_t)(m_position_i+pos) > m_length)
63 return m_position_i;
64
65 return (m_position_i += pos);
66
67 case wxEndPosition:
68 if ((size_t)(m_length-pos) > m_length)
69 return m_position_i;
70
71 return (m_position_i = m_length-pos);
72 }
73
74 return m_position_i;
75 }
76
77 wxOutputStream& wxMemoryStreamBase::Write(const void *buffer, size_t size)
78 {
79 if (m_iolimit == 1) {
80 m_bad = TRUE;
81 return *this;
82 }
83
84 if (m_position_o+size > m_length)
85 if (!ChangeBufferSize(m_position_o+size)) {
86 m_bad = TRUE;
87 return *this;
88 }
89
90 memcpy(m_buffer+m_position_o, buffer, size);
91 m_position_o += size;
92 m_lastwrite = size;
93
94 return *this;
95 }
96
97 size_t wxMemoryStreamBase::SeekO(int pos, wxWhenceType whence)
98 {
99 if (m_iolimit == 2)
100 return 0;
101
102 switch (whence) {
103 case wxBeginPosition:
104 if ((size_t)pos > m_length)
105 return m_position_o;
106 return (m_position_o = pos);
107
108 case wxCurrentPosition:
109 if ((size_t)(m_position_o+pos) > m_length)
110 return m_position_o;
111
112 return (m_position_o += pos);
113
114 case wxEndPosition:
115 if ((size_t)(m_length-pos) > m_length)
116 return m_position_o;
117
118 return (m_position_o = m_length-pos);
119 }
120
121 return m_position_o;
122 }
123
124 bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size)
125 {
126 m_length = new_size;
127 if (!m_buffer)
128 m_buffer = (char *)malloc(m_length);
129 else
130 m_buffer = (char *)realloc(m_buffer, m_length);
131
132 return (m_buffer != NULL);
133 }