1 | #include "GeoSerializer.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | namespace Geometry {
|
---|
5 |
|
---|
6 | //---------------------------------------------------------------------
|
---|
7 | Serializer::Serializer(String name, Mode mode)
|
---|
8 | : mMode(mode)
|
---|
9 | {
|
---|
10 | char *char_mode = 0;
|
---|
11 | switch (mMode)
|
---|
12 | {
|
---|
13 | case READ :
|
---|
14 | char_mode = "rb";
|
---|
15 | break;
|
---|
16 | case APPEND:
|
---|
17 | char_mode = "a+b";
|
---|
18 | break;
|
---|
19 | case WRITE:
|
---|
20 | char_mode = "wb";
|
---|
21 | break;
|
---|
22 | }
|
---|
23 |
|
---|
24 | mFile = fopen(name.c_str(),char_mode);
|
---|
25 | mSize = 0;
|
---|
26 | assert(mFile);
|
---|
27 | }
|
---|
28 |
|
---|
29 | //---------------------------------------------------------------------
|
---|
30 | Serializer::~Serializer()
|
---|
31 | {
|
---|
32 | fflush(mFile);
|
---|
33 | fclose(mFile);
|
---|
34 | }
|
---|
35 |
|
---|
36 | //---------------------------------------------------------------------
|
---|
37 | void Serializer::WriteArray(const float* const pFloat, size_t count)
|
---|
38 | {
|
---|
39 | # if GEO_ENDIAN == GEO_ENDIAN_BIG
|
---|
40 | float *float_to_write = (float *)malloc(sizeof(float) * count);
|
---|
41 | memcpy(float_to_write, pFloat, sizeof(float) * count);
|
---|
42 |
|
---|
43 | flipToLittleEndian(float_to_write, sizeof(float), count);
|
---|
44 | writeData(float_to_write, sizeof(float), count);
|
---|
45 |
|
---|
46 | free(float_to_write);
|
---|
47 | # else
|
---|
48 | WriteData(pFloat, sizeof(float), count);
|
---|
49 | # endif
|
---|
50 | }
|
---|
51 |
|
---|
52 | //---------------------------------------------------------------------
|
---|
53 | void Serializer::WriteArray(const uint16* const pShort, size_t count = 1)
|
---|
54 | {
|
---|
55 | # if GEO_ENDIAN == GEO_ENDIAN_BIG
|
---|
56 | unsigned short *short_to_write = (unsigned short *)malloc(sizeof(unsigned short) * count);
|
---|
57 | memcpy(short_to_write, pShort, sizeof(unsigned short) * count);
|
---|
58 |
|
---|
59 | flipToLittleEndian(short_to_write, sizeof(unsigned short), count);
|
---|
60 | WriteData(short_to_write, sizeof(unsigned short), count);
|
---|
61 |
|
---|
62 | free(short_to_write);
|
---|
63 | # else
|
---|
64 | WriteData(pShort, sizeof(unsigned short), count);
|
---|
65 | # endif
|
---|
66 | }
|
---|
67 | //---------------------------------------------------------------------
|
---|
68 | void Serializer::WriteArray(const uint32* const pInt, size_t count = 1)
|
---|
69 | {
|
---|
70 | # if GEO_ENDIAN == GEO_ENDIAN_BIG
|
---|
71 | unsigned int *int_to_write = (unsigned int *)malloc(sizeof(unsigned int) * count);
|
---|
72 | memcpy(int_to_write, pInt, sizeof(unsigned int) * count);
|
---|
73 |
|
---|
74 | FlipToLittleEndian(int_to_write, sizeof(unsigned int), count);
|
---|
75 | WriteData(int_to_write, sizeof(unsigned int), count);
|
---|
76 |
|
---|
77 | free(int_to_write);
|
---|
78 | # else
|
---|
79 | WriteData(pInt, sizeof(unsigned int), count);
|
---|
80 | # endif
|
---|
81 | }
|
---|
82 |
|
---|
83 | //---------------------------------------------------------------------
|
---|
84 | void Serializer::WriteArray(const bool* const pBool, size_t count = 1)
|
---|
85 | {
|
---|
86 | //TODO: Apple has problems
|
---|
87 | WriteData(pBool, sizeof(bool), count);
|
---|
88 | }
|
---|
89 | //---------------------------------------------------------------------
|
---|
90 | void Serializer::WriteArray(const Vector3* const pDest, size_t count)
|
---|
91 | {
|
---|
92 | for(size_t i = 0; i < count; ++i)
|
---|
93 | {
|
---|
94 | WriteArray(pDest[i].val,3);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | //---------------------------------------------------------------------
|
---|
98 | void Serializer::WriteArray(const Vector2* const pDest, size_t count)
|
---|
99 | {
|
---|
100 | for(size_t i = 0; i < count; ++i)
|
---|
101 | {
|
---|
102 | WriteArray(pDest[i].val,2);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | //---------------------------------------------------------------------
|
---|
107 | void Serializer::WriteData(const void* const buf, size_t size, size_t count)
|
---|
108 | {
|
---|
109 | size_t wcount = fwrite((void* const)buf, size, count, mFile);
|
---|
110 | assert(wcount == count);
|
---|
111 | mSize += size*count;
|
---|
112 | }
|
---|
113 |
|
---|
114 | //---------------------------------------------------------------------
|
---|
115 | void Serializer::ReadData(void *pDest, size_t size, size_t count)
|
---|
116 | {
|
---|
117 | size_t rcount = fread(pDest,size,count,mFile);
|
---|
118 | assert(rcount == count);
|
---|
119 | mSize += size*count;
|
---|
120 | };
|
---|
121 |
|
---|
122 |
|
---|
123 | // JGA
|
---|
124 | char * Serializer::miReadData(char *pDest, size_t pDestsize)
|
---|
125 | {
|
---|
126 | return fgets(pDest, (int)pDestsize, mFile);
|
---|
127 | };
|
---|
128 |
|
---|
129 | void Serializer::miReadData2(void *pDest, size_t size, size_t count)
|
---|
130 | {
|
---|
131 | size_t rcount = fread(pDest,size,count,mFile);
|
---|
132 | assert(rcount == count);
|
---|
133 | mSize += size*count;
|
---|
134 | };
|
---|
135 | // fin JGA
|
---|
136 |
|
---|
137 | // GPR.
|
---|
138 | void Serializer::WriteData(const String &string)
|
---|
139 | {
|
---|
140 | fputs(string.c_str(), mFile);
|
---|
141 |
|
---|
142 | // Write terminating newline char
|
---|
143 | fputc('\n', mFile);
|
---|
144 |
|
---|
145 | mSize += string.length() + 1;
|
---|
146 | }
|
---|
147 | // fin GPR
|
---|
148 |
|
---|
149 |
|
---|
150 | //---------------------------------------------------------------------
|
---|
151 | void Serializer::ReadArray(bool *pDest, size_t count)
|
---|
152 | {
|
---|
153 | //TODO:: Apple has problmes with bools
|
---|
154 | ReadData(pDest,sizeof(bool), count);
|
---|
155 | }
|
---|
156 |
|
---|
157 | //---------------------------------------------------------------------
|
---|
158 | void Serializer::ReadArray(float *pDest, size_t count)
|
---|
159 | {
|
---|
160 | ReadData(pDest, sizeof(float), count);
|
---|
161 | FlipFromLittleEndian(pDest, sizeof(float), count);
|
---|
162 | }
|
---|
163 | //---------------------------------------------------------------------
|
---|
164 | void Serializer::ReadArray(unsigned short *pDest, size_t count)
|
---|
165 | {
|
---|
166 | ReadData(pDest, sizeof(unsigned short), count);
|
---|
167 | FlipFromLittleEndian(pDest, sizeof(unsigned short), count);
|
---|
168 | }
|
---|
169 | //---------------------------------------------------------------------
|
---|
170 | void Serializer::ReadArray(unsigned int *pDest, size_t count)
|
---|
171 | {
|
---|
172 | ReadData(pDest, sizeof(unsigned int), count);
|
---|
173 | FlipFromLittleEndian(pDest, sizeof(unsigned int), count);
|
---|
174 | }
|
---|
175 | //---------------------------------------------------------------------
|
---|
176 | void Serializer::ReadArray(Vector3 *pDest, size_t count)
|
---|
177 | {
|
---|
178 | for(size_t i = 0; i < count; ++i)
|
---|
179 | {
|
---|
180 | ReadArray(pDest[i].val,3);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | //---------------------------------------------------------------------
|
---|
184 | void Serializer::ReadArray(Vector2 *pDest, size_t count)
|
---|
185 | {
|
---|
186 | for(size_t i = 0; i < count; ++i)
|
---|
187 | {
|
---|
188 | ReadArray(pDest[i].val,2);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | //---------------------------------------------------------------------
|
---|
194 |
|
---|
195 |
|
---|
196 | void Serializer::FlipToLittleEndian(void *pData, size_t size, size_t count)
|
---|
197 | {
|
---|
198 | # if GEO_ENDIAN == GEO_ENDIAN_BIG
|
---|
199 | FlipEndian(pData, size, count);
|
---|
200 | # endif
|
---|
201 | }
|
---|
202 |
|
---|
203 | void Serializer::FlipFromLittleEndian(void *pData, size_t size, size_t count)
|
---|
204 | {
|
---|
205 | # if GEO_ENDIAN == GEO_ENDIAN_BIG
|
---|
206 | FlipEndian(pData, size, count);
|
---|
207 | # endif
|
---|
208 | }
|
---|
209 |
|
---|
210 | void Serializer::FlipEndian(void *pData, size_t size, size_t count)
|
---|
211 | {
|
---|
212 | for(unsigned int index = 0; index < count; index++)
|
---|
213 | {
|
---|
214 | FlipEndian((void *)((char*)pData + (index * size)), size);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | void Serializer::FlipEndian(void *pData, size_t size)
|
---|
219 | {
|
---|
220 | char swapByte;
|
---|
221 | for(unsigned int byteIndex = 0; byteIndex < size/2; byteIndex++)
|
---|
222 | {
|
---|
223 | swapByte = *((char *)pData + byteIndex);
|
---|
224 | *((char *)pData + byteIndex) = *((char *)pData + size - byteIndex - 1);
|
---|
225 | *((char *)pData + size - byteIndex - 1) = swapByte;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | }
|
---|
230 |
|
---|