[138] | 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 | void Serializer::ReadArray(bool *pDest, size_t count)
|
---|
| 124 | {
|
---|
| 125 | //TODO:: Apple has problmes with bools
|
---|
| 126 | ReadData(pDest,sizeof(bool), count);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | //---------------------------------------------------------------------
|
---|
| 130 | void Serializer::ReadArray(float *pDest, size_t count)
|
---|
| 131 | {
|
---|
| 132 | ReadData(pDest, sizeof(float), count);
|
---|
| 133 | FlipFromLittleEndian(pDest, sizeof(float), count);
|
---|
| 134 | }
|
---|
| 135 | //---------------------------------------------------------------------
|
---|
| 136 | void Serializer::ReadArray(unsigned short *pDest, size_t count)
|
---|
| 137 | {
|
---|
| 138 | ReadData(pDest, sizeof(unsigned short), count);
|
---|
| 139 | FlipFromLittleEndian(pDest, sizeof(unsigned short), count);
|
---|
| 140 | }
|
---|
| 141 | //---------------------------------------------------------------------
|
---|
| 142 | void Serializer::ReadArray(unsigned int *pDest, size_t count)
|
---|
| 143 | {
|
---|
| 144 | ReadData(pDest, sizeof(unsigned int), count);
|
---|
| 145 | FlipFromLittleEndian(pDest, sizeof(unsigned int), count);
|
---|
| 146 | }
|
---|
| 147 | //---------------------------------------------------------------------
|
---|
| 148 | void Serializer::ReadArray(Vector3 *pDest, size_t count)
|
---|
| 149 | {
|
---|
| 150 | for(size_t i = 0; i < count; ++i)
|
---|
| 151 | {
|
---|
| 152 | ReadArray(pDest[i].val,3);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | //---------------------------------------------------------------------
|
---|
| 156 | void Serializer::ReadArray(Vector2 *pDest, size_t count)
|
---|
| 157 | {
|
---|
| 158 | for(size_t i = 0; i < count; ++i)
|
---|
| 159 | {
|
---|
| 160 | ReadArray(pDest[i].val,2);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | //---------------------------------------------------------------------
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | void Serializer::FlipToLittleEndian(void *pData, size_t size, size_t count)
|
---|
| 169 | {
|
---|
| 170 | # if GEO_ENDIAN == GEO_ENDIAN_BIG
|
---|
| 171 | FlipEndian(pData, size, count);
|
---|
| 172 | # endif
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | void Serializer::FlipFromLittleEndian(void *pData, size_t size, size_t count)
|
---|
| 176 | {
|
---|
| 177 | # if GEO_ENDIAN == GEO_ENDIAN_BIG
|
---|
| 178 | FlipEndian(pData, size, count);
|
---|
| 179 | # endif
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void Serializer::FlipEndian(void *pData, size_t size, size_t count)
|
---|
| 183 | {
|
---|
| 184 | for(unsigned int index = 0; index < count; index++)
|
---|
| 185 | {
|
---|
| 186 | FlipEndian((void *)((char*)pData + (index * size)), size);
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | void Serializer::FlipEndian(void *pData, size_t size)
|
---|
| 191 | {
|
---|
| 192 | char swapByte;
|
---|
| 193 | for(unsigned int byteIndex = 0; byteIndex < size/2; byteIndex++)
|
---|
| 194 | {
|
---|
| 195 | swapByte = *((char *)pData + byteIndex);
|
---|
| 196 | *((char *)pData + byteIndex) = *((char *)pData + size - byteIndex - 1);
|
---|
| 197 | *((char *)pData + size - byteIndex - 1) = swapByte;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | }
|
---|
| 202 |
|
---|