source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoSerializer.cpp @ 1009

Revision 1009, 9.0 KB checked in by gumbau, 18 years ago (diff)
Line 
1#include "GeoSerializer.h"
2
3using   namespace       Geometry;
4
5//---------------------------------------------------------------------------
6//      Constructor.
7//---------------------------------------------------------------------------
8Serializer::Serializer(String name, Mode mode): mMode(mode)
9{
10        char *char_mode = 0;
11
12        switch (mMode)
13        {
14                case READ :
15                        char_mode = "rb";
16                        break;
17                case APPEND:
18                        char_mode = "a+b";
19                        break;
20                case WRITE:
21                        char_mode = "wb";
22                        break;
23        }
24
25        mFile = fopen(name.c_str(),char_mode);
26        mSize = 0;
27
28        assert(mFile);
29}
30
31//---------------------------------------------------------------------------
32//      Destroyer.
33//---------------------------------------------------------------------------
34Serializer::~Serializer()
35{
36        fflush(mFile);
37        fclose(mFile);
38}
39
40//---------------------------------------------------------------------------
41//      Write array of floats.
42//---------------------------------------------------------------------------
43void Serializer::WriteArray(const float* const pFloat, size_t count)
44{
45#       if GEO_ENDIAN == GEO_ENDIAN_BIG
46        float *float_to_write = (float *)malloc(sizeof(float) * count);
47        memcpy(float_to_write, pFloat, sizeof(float) * count);
48
49        flipToLittleEndian(float_to_write, sizeof(float), count);
50        writeData(float_to_write, sizeof(float), count);
51
52        free(float_to_write);
53#       else
54        WriteData(pFloat, sizeof(float), count);
55#       endif
56}
57
58//---------------------------------------------------------------------------
59//      Write array of shots.
60//---------------------------------------------------------------------------
61void Serializer::WriteArray(const uint16* const pShort, size_t count = 1)
62{
63#       if GEO_ENDIAN == GEO_ENDIAN_BIG
64        unsigned short *short_to_write = (unsigned short *)malloc(sizeof(unsigned short) * count);
65        memcpy(short_to_write, pShort, sizeof(unsigned short) * count);
66
67        flipToLittleEndian(short_to_write, sizeof(unsigned short), count);
68        WriteData(short_to_write, sizeof(unsigned short), count);
69
70        free(short_to_write);
71#       else
72        WriteData(pShort, sizeof(unsigned short), count);
73#       endif
74}
75       
76//---------------------------------------------------------------------------
77//      Write array of integers.
78//---------------------------------------------------------------------------
79void Serializer::WriteArray(const uint32* const pInt, size_t count = 1)
80{
81#       if GEO_ENDIAN == GEO_ENDIAN_BIG
82        unsigned int *int_to_write = (unsigned int *)malloc(sizeof(unsigned int) * count);
83        memcpy(int_to_write, pInt, sizeof(unsigned int) * count);
84
85        FlipToLittleEndian(int_to_write, sizeof(unsigned int), count);
86        WriteData(int_to_write, sizeof(unsigned int), count);
87
88        free(int_to_write);
89#       else
90        WriteData(pInt, sizeof(unsigned int), count);
91#       endif
92}
93
94//---------------------------------------------------------------------------
95//      Write array of booleans.
96//---------------------------------------------------------------------------
97void Serializer::WriteArray(const bool* const pBool, size_t count = 1)
98{
99        //TODO: Apple has problems
100        WriteData(pBool, sizeof(bool), count);
101}
102       
103//---------------------------------------------------------------------------
104//      Write array of Vector3.
105//---------------------------------------------------------------------------
106void Serializer::WriteArray(const Vector3* const pDest, size_t count)
107{
108        for(size_t i = 0; i < count; ++i)
109        {
110                WriteArray(pDest[i].val,3);
111        }
112}
113       
114//---------------------------------------------------------------------------
115//      Write array of Vector2.
116//---------------------------------------------------------------------------
117void Serializer::WriteArray(const Vector2* const pDest, size_t count)
118{
119        for(size_t i = 0; i < count; ++i)
120        {
121                WriteArray(pDest[i].val,2);
122        }
123}
124
125//---------------------------------------------------------------------------
126//      Write data.
127//---------------------------------------------------------------------------
128void Serializer::WriteData(const void* const buf, size_t size, size_t count)
129{
130        size_t wcount = fwrite((void* const)buf, size, count, mFile);
131        assert(wcount == count);
132        mSize += size*count;
133}
134
135//---------------------------------------------------------------------------
136//      Read data.
137//---------------------------------------------------------------------------
138void Serializer::ReadData(void *pDest, size_t size, size_t count)
139{
140        size_t rcount = fread(pDest,size,count,mFile);
141        assert(rcount == count);
142        mSize += size*count;
143}
144
145//---------------------------------------------------------------------------
146//      Read data.
147//---------------------------------------------------------------------------
148char * Serializer::ReadData(char *pDest, size_t pDestsize)
149{
150        return fgets(pDest, (int)pDestsize, mFile);
151}
152
153//---------------------------------------------------------------------------
154//      Write data.
155//---------------------------------------------------------------------------
156void    Serializer::WriteData(const     String  &string)
157{
158        fputs(string.c_str(), mFile);
159
160        // Write terminating newline char
161        //fputc('\n', mFile);
162
163        mSize   +=      string.length();
164}
165
166//---------------------------------------------------------------------------
167//      Write string data.
168//---------------------------------------------------------------------------
169void    Serializer::WriteData(const char        *string)
170{
171        fputs(string, mFile);
172
173        // Write terminating newline char
174        //fputc('\n', mFile);
175
176        mSize   +=      strlen(string) /*+ 1*/;
177}
178
179//---------------------------------------------------------------------------
180//      Read array of booleans.
181//---------------------------------------------------------------------------
182void Serializer::ReadArray(bool *pDest, size_t count)
183{
184        //TODO:: Apple has problmes with bools
185        ReadData(pDest,sizeof(bool), count);
186}
187
188//---------------------------------------------------------------------------
189//      Read array of floats.
190//---------------------------------------------------------------------------
191void Serializer::ReadArray(float *pDest, size_t count)
192{
193        ReadData(pDest, sizeof(float), count);
194        FlipFromLittleEndian(pDest, sizeof(float), count);
195}
196       
197//---------------------------------------------------------------------------
198//      Read array of unsigned shots.
199//---------------------------------------------------------------------------
200void Serializer::ReadArray(unsigned short *pDest, size_t count)
201{
202        ReadData(pDest, sizeof(unsigned short), count);
203        FlipFromLittleEndian(pDest, sizeof(unsigned short), count);
204}
205       
206//---------------------------------------------------------------------------
207//      Read array of unsigned integers.
208//---------------------------------------------------------------------------
209void Serializer::ReadArray(unsigned int *pDest, size_t count)
210{
211        ReadData(pDest, sizeof(unsigned int), count);
212        FlipFromLittleEndian(pDest, sizeof(unsigned int), count);
213}
214       
215//---------------------------------------------------------------------------
216//      Read array of Vector3.
217//---------------------------------------------------------------------------
218void Serializer::ReadArray(Vector3 *pDest, size_t count)
219{
220        for(size_t i = 0; i < count; ++i)
221        {
222                ReadArray(pDest[i].val,3);
223        }
224}
225       
226//---------------------------------------------------------------------------
227//      Read array of Vector2.
228//---------------------------------------------------------------------------
229void Serializer::ReadArray(Vector2 *pDest, size_t count)
230{
231        for(size_t i = 0; i < count; ++i)
232        {
233                ReadArray(pDest[i].val,2);
234        }
235}
236
237//---------------------------------------------------------------------------
238//      Flip to Little Endian.
239//---------------------------------------------------------------------------
240void Serializer::FlipToLittleEndian(void *pData, size_t size, size_t count)
241{
242#       if GEO_ENDIAN == GEO_ENDIAN_BIG
243        FlipEndian(pData, size, count);
244#       endif
245}
246
247//---------------------------------------------------------------------------
248//      Flip from Little Endian.
249//---------------------------------------------------------------------------
250void Serializer::FlipFromLittleEndian(void *pData, size_t size, size_t count)
251{
252#       if GEO_ENDIAN == GEO_ENDIAN_BIG
253        FlipEndian(pData, size, count);
254#       endif
255}
256
257//---------------------------------------------------------------------------
258//      Flip Endian.
259//---------------------------------------------------------------------------
260void Serializer::FlipEndian(void *pData, size_t size, size_t count)
261{
262        for(unsigned int index = 0; index < count; index++)
263        {
264                FlipEndian((void *)((char*)pData + (index * size)), size);
265        }
266}
267
268//---------------------------------------------------------------------------
269//      Flip Endian.
270//---------------------------------------------------------------------------
271void Serializer::FlipEndian(void *pData, size_t size)
272{
273        char swapByte;
274        for(unsigned int byteIndex = 0; byteIndex < size/2; byteIndex++)
275        {
276                swapByte = *((char *)pData + byteIndex);
277                *((char *)pData + byteIndex) = *((char *)pData + size - byteIndex - 1);
278                *((char *)pData + size - byteIndex - 1) = swapByte;
279        }
280}
281
Note: See TracBrowser for help on using the repository browser.