forked from somma/_MyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCStream.cpp
More file actions
188 lines (150 loc) · 4.31 KB
/
CStream.cpp
File metadata and controls
188 lines (150 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*-----------------------------------------------------------------------------
* CStream.cpp
*-----------------------------------------------------------------------------
*
*-----------------------------------------------------------------------------
* All rights reserved by Noh Yong Hwan (fixbrain@gmail.com, unsorted@msn.com)
*-----------------------------------------------------------------------------
* Revision History:
* Date Who What
* ---------------- ---------------- ----------------
* 22/03/2007 Noh Yong Hwan birth
**---------------------------------------------------------------------------*/
#include "stdafx.h"
#include "CStream.h"
#include <windows.h>
#include <crtdbg.h>
#include <stdlib.h>
#include <strsafe.h>
/** -----------------------------------------------------------------------
\brief
\param
\return
\code
\endcode
-------------------------------------------------------------------------*/
#define MAX_UNSIGNED_LONG 0xFFFFFFFF
unsigned long CStream::ChangeCursor(const unsigned long offset, unsigned long from)
{
_ASSERTE(0 <= offset);
_ASSERTE(0 <= from);
_ASSERTE(m_pos >= from);
_ASSERTE(MAX_UNSIGNED_LONG >= from);
_ASSERTE(MAX_UNSIGNED_LONG >= offset);
_ASSERTE(MAX_UNSIGNED_LONG >= from + offset);
if (
!(0 <= offset) ||
!(0 <= from) ||
!(m_pos >= from) ||
!(MAX_UNSIGNED_LONG >= from) ||
!(MAX_UNSIGNED_LONG >= offset) ||
!(MAX_UNSIGNED_LONG >= from + offset)
)
{
return MAX_UNSIGNED_LONG;
}
unsigned long newPosition = from + offset;
// position 이 스트림의 크기보다 크면 마지막으로 커서를 옮긴다.
//
if (newPosition > m_size)
{
newPosition = m_size;
}
setPos(newPosition);
return newPosition;
};
/** -----------------------------------------------------------------------
\brief memory stream 의 크기를 변경한다.
\param
\return
\code
\endcode
-------------------------------------------------------------------------*/
unsigned long CMemoryStream::SetSize(unsigned long newSize)
{
unsigned long oldPosition = GetCurrentCusor();
char *ptr=NULL;
if (0 == newSize)
{
free(m_pMemory); m_pMemory=NULL;
}
else
{
ptr = (char *) realloc(m_pMemory, newSize);
if (NULL == ptr)
{
// 메모리가 부족함.
// m_pMemory 는 변경되지 않음.
//
CHAR log[512]={0};
StringCbPrintfA(log, sizeof(log), "%s(), can not reallocate memory, new memory size=%u bytes", __FUNCTION__, newSize);
OutputDebugStringA(log);
return MAX_UNSIGNED_LONG;
}
m_pMemory = ptr;
}
m_size = newSize;
if (oldPosition > newSize) ChangeCursor(0, newSize);
return newSize;
}
/** -----------------------------------------------------------------------
\brief 스트림으로 부터 데이터를 읽어서 버퍼에 쓴다.
\param
\return
성공시 읽은 바이트 수 리턴
(스트림이 Count 보다 작은 경우 포함)
실패시 -1 리턴
\code
\endcode
-------------------------------------------------------------------------*/
unsigned long CMemoryStream::ReadFromStream(void *Buffer, unsigned long Count)
{
_ASSERTE( TRUE != IsBadWritePtr(Buffer, Count) );
if ( TRUE == IsBadWritePtr(Buffer, Count) )
{
_ASSERTE(!"Invalid Parameter");
return MAX_UNSIGNED_LONG;
}
if ( (m_pos >= 0) && (Count >= 0) )
{
unsigned long ret = m_size - m_pos;
if (0 < ret)
{
if (ret > Count) ret = Count;
memmove(Buffer, (char *)(DWORD_PTR(m_pMemory) + m_pos), ret);
ChangeCursor(ret, m_pos);
return ret;
}
}
return 0;
}
/** -----------------------------------------------------------------------
\brief 버퍼로부터 데이터를 읽어 스트림의 현재 포지션에 쓴다.
\param
\return
성공시 Write 한 바이트 수
실패시 -1
\code
\endcode
-------------------------------------------------------------------------*/
unsigned long CMemoryStream::WriteToStream(const void *Buffer, unsigned long Count)
{
if ( (m_pos >= 0) && (Count >= 0) )
{
unsigned long pos = m_pos + Count;
if (pos > 0)
{
if (pos > m_size)
{
if (MAX_UNSIGNED_LONG == SetSize(pos))
{
return MAX_UNSIGNED_LONG;
}
}
memmove(&m_pMemory[m_pos], Buffer, Count);
this->m_pos = pos;
return Count;
}
}
return 0; // write 한 바이트가 0 이므로
}