1 |
bearsoft |
1.1 |
#include "BinaryInput.h"
|
2 |
|
|
#include "../System/System.h"
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
BinaryInput::BinaryInput()
|
6 |
|
|
{
|
7 |
|
|
internBuffer=0;
|
8 |
|
|
sourceBuffer=0;
|
9 |
|
|
endBuffer=0;
|
10 |
|
|
fileSize=0;
|
11 |
|
|
scanBinaryStatus=true;
|
12 |
|
|
wordBuffer=0;
|
13 |
|
|
bigEndian=false;
|
14 |
|
|
fileStatus=0;
|
15 |
|
|
charCounter=0;
|
16 |
|
|
fileTools = new FileTools();
|
17 |
|
|
stringTools = new StringTools();
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
BinaryInput::~BinaryInput()
|
21 |
|
|
{
|
22 |
|
|
if (internBuffer != 0 )
|
23 |
|
|
{
|
24 |
|
|
free(internBuffer);
|
25 |
|
|
internBuffer=0;
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
if (wordBuffer != 0 )
|
29 |
|
|
{
|
30 |
|
|
free(wordBuffer);
|
31 |
|
|
wordBuffer=0;
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
if (fileTools!=null)
|
35 |
|
|
{
|
36 |
|
|
delete fileTools;
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
if (stringTools!=null)
|
40 |
|
|
{
|
41 |
|
|
delete stringTools;
|
42 |
|
|
}
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
// internBuffer=the start of the ascii buffer
|
46 |
|
|
// sourceBuffer=where the pointer is in the internBuffer
|
47 |
|
|
// endBuffer=the end of the internBuffer
|
48 |
|
|
|
49 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
50 |
|
|
//read a ascii file
|
51 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
52 |
|
|
|
53 |
|
|
void BinaryInput::readFile(const String & string)
|
54 |
|
|
{
|
55 |
|
|
readFile(string.buffer);
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
void BinaryInput::readFile(char *filename)
|
60 |
|
|
{
|
61 |
|
|
if ( internBuffer != 0 )
|
62 |
|
|
{
|
63 |
|
|
free(internBuffer);
|
64 |
|
|
internBuffer=0;
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
int fileBuffer=0;
|
68 |
|
|
|
69 |
|
|
FileTools fileTools;
|
70 |
|
|
|
71 |
|
|
fileStatus=fileTools.readStringFile(filename,&fileBuffer,&fileSize);
|
72 |
|
|
|
73 |
|
|
internBuffer=(char*)fileBuffer;
|
74 |
|
|
sourceBuffer=(char*)internBuffer;
|
75 |
|
|
endBuffer=(char*)internBuffer;
|
76 |
|
|
endBuffer+=fileSize;
|
77 |
|
|
scanBinaryStatus=true;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
81 |
|
|
//can i read more words??
|
82 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
83 |
|
|
|
84 |
|
|
bool BinaryInput::getScanBinaryStatus()
|
85 |
|
|
{
|
86 |
|
|
return scanBinaryStatus;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
91 |
|
|
//set a String to the InternBuffer
|
92 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
93 |
|
|
|
94 |
|
|
void BinaryInput::setInternBuffer(char *iInternBuffer, int size)
|
95 |
|
|
{
|
96 |
|
|
if ( iInternBuffer != 0 )
|
97 |
|
|
{
|
98 |
|
|
if ( internBuffer != 0 )
|
99 |
|
|
{
|
100 |
|
|
free(internBuffer);
|
101 |
|
|
internBuffer=0;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
internBuffer=iInternBuffer;
|
105 |
|
|
fileSize=size;
|
106 |
|
|
sourceBuffer=(char*)internBuffer;
|
107 |
|
|
endBuffer=(char*)internBuffer;
|
108 |
|
|
endBuffer+=fileSize;
|
109 |
|
|
scanBinaryStatus=true;
|
110 |
|
|
}
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
114 |
|
|
//takes the start address from the internBuffer and copy its to the sourcebuffer pointer
|
115 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
116 |
|
|
|
117 |
|
|
void BinaryInput::resetScanWord()
|
118 |
|
|
{
|
119 |
|
|
sourceBuffer=internBuffer;
|
120 |
|
|
scanBinaryStatus=true;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
char *BinaryInput::getWord(int size)
|
124 |
|
|
{
|
125 |
|
|
binaryStatus=false;
|
126 |
|
|
|
127 |
|
|
if (wordBuffer!=0)
|
128 |
|
|
{
|
129 |
|
|
free(wordBuffer);
|
130 |
|
|
wordBuffer=0;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
if ((sourceBuffer >= internBuffer) && ((sourceBuffer+size) < endBuffer) )
|
134 |
|
|
{
|
135 |
|
|
binaryStatus=true;
|
136 |
|
|
|
137 |
|
|
wordBuffer=(char*)malloc(sizeof(char)*size+sizeof(char));
|
138 |
|
|
|
139 |
|
|
stringTools->copyString(sourceBuffer, size, wordBuffer);
|
140 |
|
|
|
141 |
|
|
sourceBuffer+=size;
|
142 |
|
|
charCounter+=size;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
return wordBuffer;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
char *BinaryInput::getWord()
|
149 |
|
|
{
|
150 |
|
|
binaryStatus=false;
|
151 |
|
|
|
152 |
|
|
if (wordBuffer!=0)
|
153 |
|
|
{
|
154 |
|
|
free(wordBuffer);
|
155 |
|
|
wordBuffer=0;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
if ((sourceBuffer >= internBuffer) && ((sourceBuffer+sizeof(char)) <= endBuffer) )
|
159 |
|
|
{
|
160 |
|
|
binaryStatus=true;
|
161 |
|
|
|
162 |
|
|
int size=stringTools->sizeOfString(sourceBuffer, endBuffer);
|
163 |
|
|
|
164 |
|
|
wordBuffer=(char*)malloc(sizeof(char)*size);
|
165 |
|
|
|
166 |
|
|
stringTools->copyString(sourceBuffer, (size-1), wordBuffer);
|
167 |
|
|
|
168 |
|
|
sourceBuffer+=size;
|
169 |
|
|
charCounter+=size;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
return wordBuffer;
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
char BinaryInput::getChar()
|
176 |
|
|
{
|
177 |
|
|
binaryStatus=false;
|
178 |
|
|
char charValue=0;
|
179 |
|
|
|
180 |
|
|
if ((sourceBuffer >= internBuffer) && ((sourceBuffer+sizeof(char)) <= endBuffer))
|
181 |
|
|
{
|
182 |
|
|
binaryStatus=true;
|
183 |
|
|
charValue=sourceBuffer[0];
|
184 |
|
|
sourceBuffer++;
|
185 |
|
|
charCounter++;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
return charValue;
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
short BinaryInput::getShortButGoNotFoward()
|
193 |
|
|
{
|
194 |
|
|
short shortValue=getShort();
|
195 |
|
|
|
196 |
|
|
if (binaryStatus)
|
197 |
|
|
{
|
198 |
|
|
sourceBuffer-=sizeof(short);
|
199 |
|
|
charCounter-=sizeof(short);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
return shortValue;
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
short BinaryInput::getShort()
|
206 |
|
|
{
|
207 |
|
|
binaryStatus=false;
|
208 |
|
|
short shortValue=0;
|
209 |
|
|
|
210 |
|
|
if ((sourceBuffer >= internBuffer) && ((sourceBuffer+sizeof(short)) <= endBuffer))
|
211 |
|
|
{
|
212 |
|
|
binaryStatus=true;
|
213 |
|
|
|
214 |
|
|
if (bigEndian)
|
215 |
|
|
{
|
216 |
|
|
char *shortPointer=(char*)&shortValue;
|
217 |
|
|
|
218 |
|
|
shortPointer[1]=sourceBuffer[0];
|
219 |
|
|
sourceBuffer++;
|
220 |
|
|
shortPointer[0]=sourceBuffer[0];
|
221 |
|
|
sourceBuffer++;
|
222 |
|
|
charCounter+=sizeof(short);
|
223 |
|
|
}
|
224 |
|
|
else
|
225 |
|
|
{
|
226 |
|
|
short *shortPointer=(short*)&shortValue;
|
227 |
|
|
short *shortSourceBuffer=(short*)sourceBuffer;
|
228 |
|
|
shortPointer[0]=shortSourceBuffer[0];
|
229 |
|
|
sourceBuffer+=sizeof(short);
|
230 |
|
|
charCounter+=sizeof(short);
|
231 |
|
|
}
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
return shortValue;
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
int BinaryInput::getInt()
|
238 |
|
|
{
|
239 |
|
|
binaryStatus=false;
|
240 |
|
|
int intValue=0;
|
241 |
|
|
|
242 |
|
|
if ((sourceBuffer >= internBuffer) && ((sourceBuffer+sizeof(int)) <= endBuffer))
|
243 |
|
|
{
|
244 |
|
|
binaryStatus=true;
|
245 |
|
|
|
246 |
|
|
if (bigEndian)
|
247 |
|
|
{
|
248 |
|
|
char *intPointer=(char*)&intValue;
|
249 |
|
|
|
250 |
|
|
intPointer[3]=sourceBuffer[0];
|
251 |
|
|
sourceBuffer++;
|
252 |
|
|
intPointer[2]=sourceBuffer[0];
|
253 |
|
|
sourceBuffer++;
|
254 |
|
|
intPointer[1]=sourceBuffer[0];
|
255 |
|
|
sourceBuffer++;
|
256 |
|
|
intPointer[0]=sourceBuffer[0];
|
257 |
|
|
sourceBuffer++;
|
258 |
|
|
charCounter+=sizeof(int);
|
259 |
|
|
}
|
260 |
|
|
else
|
261 |
|
|
{
|
262 |
|
|
int *intPointer=(int*)&intValue;
|
263 |
|
|
int *intSourceBuffer=(int*)sourceBuffer;
|
264 |
|
|
intPointer[0]=intSourceBuffer[0];
|
265 |
|
|
sourceBuffer+=sizeof(int);
|
266 |
|
|
charCounter+=sizeof(int);
|
267 |
|
|
}
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
return intValue;
|
271 |
|
|
}
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
float BinaryInput::getFloat()
|
275 |
|
|
{
|
276 |
|
|
binaryStatus=false;
|
277 |
|
|
float floatValue=0;
|
278 |
|
|
|
279 |
|
|
if (((sourceBuffer+sizeof(float)) >= internBuffer) && ((sourceBuffer+sizeof(float)) <= endBuffer))
|
280 |
|
|
{
|
281 |
|
|
binaryStatus=true;
|
282 |
|
|
|
283 |
|
|
if (bigEndian)
|
284 |
|
|
{
|
285 |
|
|
char *floatPointer=(char*)&floatValue;
|
286 |
|
|
|
287 |
|
|
floatPointer[3]=sourceBuffer[0];
|
288 |
|
|
sourceBuffer++;
|
289 |
|
|
floatPointer[2]=sourceBuffer[0];
|
290 |
|
|
sourceBuffer++;
|
291 |
|
|
floatPointer[1]=sourceBuffer[0];
|
292 |
|
|
sourceBuffer++;
|
293 |
|
|
floatPointer[0]=sourceBuffer[0];
|
294 |
|
|
sourceBuffer++;
|
295 |
|
|
charCounter+=sizeof(float);
|
296 |
|
|
}
|
297 |
|
|
else
|
298 |
|
|
{
|
299 |
|
|
float *floatPointer=(float*)&floatValue;
|
300 |
|
|
float *floatSourceBuffer=(float*)sourceBuffer;
|
301 |
|
|
floatPointer[0]=floatSourceBuffer[0];
|
302 |
|
|
sourceBuffer+=sizeof(float);
|
303 |
|
|
charCounter+=sizeof(float);
|
304 |
|
|
}
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
return floatValue;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
void BinaryInput::setBigEndian(bool bigEndian)
|
311 |
|
|
{
|
312 |
|
|
this->bigEndian=bigEndian;
|
313 |
|
|
}
|
314 |
|
|
|
315 |
|
|
bool BinaryInput::getBinaryStatus()
|
316 |
|
|
{
|
317 |
|
|
return binaryStatus;
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
bool BinaryInput::jumpForward(int jumpValue)
|
321 |
|
|
{
|
322 |
|
|
binaryStatus=false;
|
323 |
|
|
|
324 |
|
|
if (jumpValue!=0)
|
325 |
|
|
{
|
326 |
|
|
if ( (sourceBuffer+jumpValue) <= endBuffer )
|
327 |
|
|
{
|
328 |
|
|
binaryStatus=true;
|
329 |
|
|
sourceBuffer+=jumpValue;
|
330 |
|
|
charCounter+=jumpValue;
|
331 |
|
|
}
|
332 |
|
|
}
|
333 |
|
|
|
334 |
|
|
return binaryStatus;
|
335 |
|
|
}
|
336 |
|
|
|
337 |
|
|
char *BinaryInput::getInternBuffer()
|
338 |
|
|
{
|
339 |
|
|
return internBuffer;
|
340 |
|
|
}
|
341 |
|
|
|
342 |
|
|
int BinaryInput::getInternBufferSize()
|
343 |
|
|
{
|
344 |
|
|
return fileSize;
|
345 |
|
|
}
|
346 |
|
|
|
347 |
|
|
int BinaryInput::getFileStatus()
|
348 |
|
|
{
|
349 |
|
|
return fileStatus;
|
350 |
|
|
}
|
351 |
|
|
|
352 |
|
|
int BinaryInput::socketSend(int socket)
|
353 |
|
|
{
|
354 |
|
|
NetWorkTools *netWorkTools = new NetWorkTools();
|
355 |
|
|
int status=0;
|
356 |
|
|
|
357 |
|
|
if (internBuffer!=null)
|
358 |
|
|
{
|
359 |
|
|
status=netWorkTools->sendData(socket,internBuffer,fileSize);
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
delete netWorkTools;
|
363 |
|
|
|
364 |
|
|
return status;
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
void BinaryInput::setInternBufferPosition(int position)
|
368 |
|
|
{
|
369 |
|
|
binaryStatus=false;
|
370 |
|
|
|
371 |
|
|
if ( ((internBuffer+position) >= internBuffer) && ((internBuffer+position) <= endBuffer))
|
372 |
|
|
{
|
373 |
|
|
sourceBuffer=internBuffer+position;
|
374 |
|
|
binaryStatus=true;
|
375 |
|
|
}
|
376 |
|
|
}
|
377 |
|
|
|
378 |
|
|
void BinaryInput::resetCharCounter()
|
379 |
|
|
{
|
380 |
|
|
charCounter=0;
|
381 |
|
|
}
|
382 |
|
|
|
383 |
|
|
int BinaryInput::getCharCounter()
|
384 |
|
|
{
|
385 |
|
|
return charCounter;
|
386 |
|
|
}
|