]> Creatis software - clitk.git/blob - utilities/CxImage/xmemfile.cpp
cosmetic
[clitk.git] / utilities / CxImage / xmemfile.cpp
1 #include "xmemfile.h"
2
3 //////////////////////////////////////////////////////////
4 CxMemFile::CxMemFile(BYTE* pBuffer, DWORD size)
5 {
6         m_pBuffer = pBuffer;
7         m_Position = 0;
8         m_Size = m_Edge = size;
9         m_bFreeOnClose = (bool)(pBuffer==0);
10 }
11 //////////////////////////////////////////////////////////
12 CxMemFile::~CxMemFile()
13 {
14         Close();
15 }
16 //////////////////////////////////////////////////////////
17 bool CxMemFile::Close()
18 {
19         if ( (m_pBuffer) && (m_bFreeOnClose) ){
20                 free(m_pBuffer);
21                 m_pBuffer = NULL;
22                 m_Size = 0;
23         }
24         return true;
25 }
26 //////////////////////////////////////////////////////////
27 bool CxMemFile::Open()
28 {
29         if (m_pBuffer) return false;    // Can't re-open without closing first
30
31         m_Position = m_Size = m_Edge = 0;
32         m_pBuffer=(BYTE*)malloc(1);
33         m_bFreeOnClose = true;
34
35         return (m_pBuffer!=0);
36 }
37 //////////////////////////////////////////////////////////
38 BYTE* CxMemFile::GetBuffer(bool bDetachBuffer)
39 {
40         //can only detach, avoid inadvertantly attaching to
41         // memory that may not be ours [Jason De Arte]
42         if( bDetachBuffer )
43                 m_bFreeOnClose = false;
44         return m_pBuffer;
45 }
46 //////////////////////////////////////////////////////////
47 size_t CxMemFile::Read(void *buffer, size_t size, size_t count)
48 {
49         if (buffer==NULL) return 0;
50
51         if (m_pBuffer==NULL) return 0;
52         if (m_Position >= (long)m_Size) return 0;
53
54         long nCount = (long)(count*size);
55         if (nCount == 0) return 0;
56
57         long nRead;
58         if (m_Position + nCount > (long)m_Size)
59                 nRead = (m_Size - m_Position);
60         else
61                 nRead = nCount;
62
63         memcpy(buffer, m_pBuffer + m_Position, nRead);
64         m_Position += nRead;
65
66         return (size_t)(nRead/size);
67 }
68 //////////////////////////////////////////////////////////
69 size_t CxMemFile::Write(const void *buffer, size_t size, size_t count)
70 {
71         if (m_pBuffer==NULL) return 0;
72         if (buffer==NULL) return 0;
73
74         long nCount = (long)(count*size);
75         if (nCount == 0) return 0;
76
77         if (m_Position + nCount > m_Edge){
78                 if (!Alloc(m_Position + nCount)){
79                         return false;
80                 }
81         }
82
83         memcpy(m_pBuffer + m_Position, buffer, nCount);
84
85         m_Position += nCount;
86
87         if (m_Position > (long)m_Size) m_Size = m_Position;
88         
89         return count;
90 }
91 //////////////////////////////////////////////////////////
92 bool CxMemFile::Seek(long offset, int origin)
93 {
94         if (m_pBuffer==NULL) return false;
95         long lNewPos = m_Position;
96
97         if (origin == SEEK_SET)          lNewPos = offset;
98         else if (origin == SEEK_CUR) lNewPos += offset;
99         else if (origin == SEEK_END) lNewPos = m_Size + offset;
100         else return false;
101
102         if (lNewPos < 0) lNewPos = 0;
103
104         m_Position = lNewPos;
105         return true;
106 }
107 //////////////////////////////////////////////////////////
108 long CxMemFile::Tell()
109 {
110         if (m_pBuffer==NULL) return -1;
111         return m_Position;
112 }
113 //////////////////////////////////////////////////////////
114 long CxMemFile::Size()
115 {
116         if (m_pBuffer==NULL) return -1;
117         return m_Size;
118 }
119 //////////////////////////////////////////////////////////
120 bool CxMemFile::Flush()
121 {
122         if (m_pBuffer==NULL) return false;
123         return true;
124 }
125 //////////////////////////////////////////////////////////
126 bool CxMemFile::Eof()
127 {
128         if (m_pBuffer==NULL) return true;
129         return (m_Position >= (long)m_Size);
130 }
131 //////////////////////////////////////////////////////////
132 long CxMemFile::Error()
133 {
134         if (m_pBuffer==NULL) return -1;
135         return (m_Position > (long)m_Size);
136 }
137 //////////////////////////////////////////////////////////
138 bool CxMemFile::PutC(unsigned char c)
139 {
140         if (m_pBuffer==NULL) return false;
141
142         if (m_Position >= m_Edge){
143                 if (!Alloc(m_Position + 1)){
144                         return false;
145                 }
146         }
147
148         m_pBuffer[m_Position++] = c;
149
150         if (m_Position > (long)m_Size) m_Size = m_Position;
151         
152         return true;
153 }
154 //////////////////////////////////////////////////////////
155 long CxMemFile::GetC()
156 {
157         if (Eof()) return EOF;
158         return *(BYTE*)((BYTE*)m_pBuffer + m_Position++);
159 }
160 //////////////////////////////////////////////////////////
161 char * CxMemFile::GetS(char *string, int n)
162 {
163         n--;
164         long c,i=0;
165         while (i<n){
166                 c = GetC();
167                 if (c == EOF) return 0;
168                 string[i++] = (char)c;
169                 if (c == '\n') break;
170         }
171         string[i] = 0;
172         return string;
173 }
174 //////////////////////////////////////////////////////////
175 long    CxMemFile::Scanf(const char *format, void* output)
176 {
177         return 0;
178 }
179 //////////////////////////////////////////////////////////
180 bool CxMemFile::Alloc(DWORD dwNewLen)
181 {
182         if (dwNewLen > (DWORD)m_Edge)
183         {
184                 // find new buffer size
185                 DWORD dwNewBufferSize = (DWORD)(((dwNewLen>>16)+1)<<16);
186
187                 // allocate new buffer
188                 if (m_pBuffer == NULL) m_pBuffer = (BYTE*)malloc(dwNewBufferSize);
189                 else    m_pBuffer = (BYTE*)realloc(m_pBuffer, dwNewBufferSize);
190                 // I own this buffer now (caller knows nothing about it)
191                 m_bFreeOnClose = true;
192
193                 m_Edge = dwNewBufferSize;
194         }
195         return (m_pBuffer!=0);
196 }
197 //////////////////////////////////////////////////////////
198 void CxMemFile::Free()
199 {
200         Close();
201 }
202 //////////////////////////////////////////////////////////