]>
git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
a12041ee0e633286fb6d1064d4692197baf95d06
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: "Memory stream" classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mmstream.h"
17 #include <wx/stream.h>
18 #include <wx/mmstream.h>
20 wxMemoryStreamBase::wxMemoryStreamBase(char *data
, size_t length
, int iolimit
)
26 m_position_i
= m_position_o
= 0;
29 wxMemoryStreamBase::~wxMemoryStreamBase()
34 wxInputStream
& wxMemoryStreamBase::Read(void *buffer
, size_t size
)
40 if (m_position_i
+size
> m_length
)
41 size
= m_length
-m_position_i
;
43 memcpy((void *)((unsigned long)buffer
+m_position_i
), m_buffer
, size
);
50 size_t wxMemoryStreamBase::SeekI(int pos
, wxWhenceType whence
)
57 if ((size_t)pos
> m_length
)
59 return (m_position_i
= pos
);
61 case wxCurrentPosition
:
62 if ((size_t)(m_position_i
+pos
) > m_length
)
65 return (m_position_i
+= pos
);
68 if ((size_t)(m_length
-pos
) > m_length
)
71 return (m_position_i
= m_length
-pos
);
77 wxOutputStream
& wxMemoryStreamBase::Write(const void *buffer
, size_t size
)
84 if (m_position_o
+size
> m_length
)
85 if (!ChangeBufferSize(m_position_o
+size
)) {
90 memcpy(m_buffer
+m_position_o
, buffer
, size
);
97 size_t wxMemoryStreamBase::SeekO(int pos
, wxWhenceType whence
)
103 case wxBeginPosition
:
104 if ((size_t)pos
> m_length
)
106 return (m_position_o
= pos
);
108 case wxCurrentPosition
:
109 if ((size_t)(m_position_o
+pos
) > m_length
)
112 return (m_position_o
+= pos
);
115 if ((size_t)(m_length
-pos
) > m_length
)
118 return (m_position_o
= m_length
-pos
);
124 bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size
)
128 m_buffer
= (char *)malloc(m_length
);
130 m_buffer
= (char *)realloc(m_buffer
, m_length
);
132 return (m_buffer
!= NULL
);