]>
git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: "Memory stream" classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mstream.h"
17 #include "wx/stream.h"
18 #include "wx/mstream.h"
20 #if !USE_SHARED_LIBRARY
21 IMPLEMENT_CLASS(wxMemoryStreamBase
, wxStream
)
22 IMPLEMENT_CLASS(wxMemoryInputStream
, wxMemoryStreamBase
)
23 //IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxMemoryStreamBase)
24 //IMPLEMENT_DYNAMIC_CLASS(wxMemoryStream, wxMemoryStreamBase)
27 wxMemoryStreamBase::wxMemoryStreamBase(char *data
, size_t length
, int iolimit
)
33 m_position_i
= m_position_o
= 0;
36 wxMemoryStreamBase::~wxMemoryStreamBase()
41 wxInputStream
& wxMemoryStreamBase::Read(void *buffer
, size_t size
)
47 if (m_position_i
+size
> m_length
)
48 size
= m_length
-m_position_i
;
50 memcpy((void *)((unsigned long)buffer
+m_position_i
), m_buffer
, size
);
57 size_t wxMemoryStreamBase::SeekI(int pos
, wxWhenceType whence
)
64 if ((size_t)pos
> m_length
)
66 return (m_position_i
= pos
);
68 case wxCurrentPosition
:
69 if ((size_t)(m_position_i
+pos
) > m_length
)
72 return (m_position_i
+= pos
);
75 if ((size_t)(m_length
-pos
) > m_length
)
78 return (m_position_i
= m_length
-pos
);
84 wxOutputStream
& wxMemoryStreamBase::Write(const void *buffer
, size_t size
)
91 if (m_position_o
+size
> m_length
)
92 if (!ChangeBufferSize(m_position_o
+size
)) {
97 memcpy(m_buffer
+m_position_o
, buffer
, size
);
104 size_t wxMemoryStreamBase::SeekO(int pos
, wxWhenceType whence
)
110 case wxBeginPosition
:
111 if ((size_t)pos
> m_length
)
113 return (m_position_o
= pos
);
115 case wxCurrentPosition
:
116 if ((size_t)(m_position_o
+pos
) > m_length
)
119 return (m_position_o
+= pos
);
122 if ((size_t)(m_length
-pos
) > m_length
)
125 return (m_position_o
= m_length
-pos
);
131 bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size
)
135 m_buffer
= (char *)malloc(m_length
);
137 m_buffer
= (char *)realloc(m_buffer
, m_length
);
139 return (m_buffer
!= NULL
);