[829] | 1 | #include <boost/serialization/serialization.hpp>
|
---|
| 2 | #include <boost/serialization/split_member.hpp>
|
---|
| 3 |
|
---|
| 4 | #pragma once
|
---|
| 5 |
|
---|
| 6 | namespace Jenga{ namespace Common{
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | class Binary
|
---|
| 10 | {
|
---|
| 11 | int allocateSize;
|
---|
| 12 | char *buffer;
|
---|
| 13 | int size;
|
---|
| 14 |
|
---|
| 15 | // XMLシリアライズ用
|
---|
| 16 | private:
|
---|
| 17 | friend class boost::serialization::access;
|
---|
| 18 | BOOST_SERIALIZATION_SPLIT_MEMBER();
|
---|
| 19 | template<class Archive> void load(Archive& ar, const unsigned int version)
|
---|
| 20 | {
|
---|
| 21 | std::string _buffer;
|
---|
| 22 | ar & BOOST_SERIALIZATION_NVP( _buffer );
|
---|
| 23 | ar & BOOST_SERIALIZATION_NVP( size );
|
---|
| 24 |
|
---|
| 25 | // 読み込み後の処理
|
---|
| 26 | Realloc( size );
|
---|
| 27 | for( int i=0; i<size; i++ )
|
---|
| 28 | {
|
---|
| 29 | ULONG_PTR l1 = ( ( _buffer[i*3] >= 'a' ) ? ( _buffer[i*3] - 'a' + 0x0a ) : ( _buffer[i*3] - '0' ) ) * 0x10;
|
---|
| 30 | ULONG_PTR l2 = ( _buffer[i*3+1] >= 'a' ) ? ( _buffer[i*3+1] - 'a' + 0x0a ) : ( _buffer[i*3+1] - '0' );
|
---|
| 31 | ULONG_PTR l = l1 + l2;
|
---|
| 32 | buffer[i] = static_cast<char>(l);
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | template<class Archive> void save(Archive& ar, const unsigned int version) const
|
---|
| 36 | {
|
---|
| 37 | // 保存準備
|
---|
| 38 | char *tempCode = (char *)calloc( (size+1) * 3, 1 );
|
---|
| 39 | for( int i=0; i<size; i++ )
|
---|
| 40 | {
|
---|
| 41 | char temp[32];
|
---|
| 42 | sprintf( temp, "%02x,", (unsigned char)buffer[i] );
|
---|
| 43 | tempCode[i*3] = temp[0];
|
---|
| 44 | tempCode[i*3+1] = temp[1];
|
---|
| 45 | tempCode[i*3+2] = temp[2];
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | std::string _buffer = tempCode;
|
---|
| 49 | free( tempCode );
|
---|
| 50 |
|
---|
| 51 | ar & BOOST_SERIALIZATION_NVP( _buffer );
|
---|
| 52 | ar & BOOST_SERIALIZATION_NVP( size );
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void Realloc( int newSize )
|
---|
| 56 | {
|
---|
| 57 | if( allocateSize < newSize + 8192 )
|
---|
| 58 | {
|
---|
| 59 | while( allocateSize < newSize + 8192 )
|
---|
| 60 | {
|
---|
| 61 | allocateSize += 8192;
|
---|
| 62 | }
|
---|
| 63 | buffer = (char *)realloc( buffer, allocateSize );
|
---|
| 64 |
|
---|
| 65 | // 再確保した部分を0で埋める
|
---|
| 66 | memset( buffer + size, 0, allocateSize - size );
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public:
|
---|
| 71 | Binary()
|
---|
| 72 | : allocateSize( 8192 )
|
---|
| 73 | , buffer( (char *)malloc( allocateSize ) )
|
---|
| 74 | , size( 0 )
|
---|
| 75 | {
|
---|
| 76 | }
|
---|
| 77 | Binary( const char *buffer, int size )
|
---|
| 78 | : allocateSize( 8192 )
|
---|
| 79 | , buffer( (char *)malloc( allocateSize ) )
|
---|
| 80 | , size( 0 )
|
---|
| 81 | {
|
---|
| 82 | Put( buffer, size );
|
---|
| 83 | }
|
---|
| 84 | ~Binary()
|
---|
| 85 | {
|
---|
| 86 | free( buffer );
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void Clear()
|
---|
| 90 | {
|
---|
| 91 | size = 0;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | const char *GetBuffer() const
|
---|
| 95 | {
|
---|
| 96 | return buffer;
|
---|
| 97 | }
|
---|
| 98 | int GetSize() const
|
---|
| 99 | {
|
---|
| 100 | return size;
|
---|
| 101 | }
|
---|
| 102 | void Resize( int newSize )
|
---|
| 103 | {
|
---|
| 104 | Realloc( newSize );
|
---|
| 105 | size = newSize;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | long GetLong( int pos ) const
|
---|
| 109 | {
|
---|
| 110 | return *(long *)( buffer + pos );
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void Overwrite( int pos, const char *buffer, int size )
|
---|
| 114 | {
|
---|
| 115 | memcpy( this->buffer + pos, buffer, size );
|
---|
| 116 | }
|
---|
| 117 | void Overwrite( int pos, char c )
|
---|
| 118 | {
|
---|
| 119 | buffer[pos] = c;
|
---|
| 120 | }
|
---|
| 121 | void Overwrite( int pos, long newLongValue )
|
---|
| 122 | {
|
---|
| 123 | *(long *)( buffer + pos ) = newLongValue;
|
---|
| 124 | }
|
---|
| 125 | void Overwrite( int pos, _int64 newInt64Value )
|
---|
| 126 | {
|
---|
| 127 | *(_int64 *)( buffer + pos ) = newInt64Value;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void Put( const char *buffer, int size )
|
---|
| 131 | {
|
---|
| 132 | Realloc( this->size + size );
|
---|
| 133 |
|
---|
| 134 | memcpy( this->buffer + this->size, buffer, size );
|
---|
| 135 | this->size += size;
|
---|
| 136 | }
|
---|
| 137 | void Put( const Binary &binary )
|
---|
| 138 | {
|
---|
| 139 | Put( binary.GetBuffer(), binary.GetSize() );
|
---|
| 140 | }
|
---|
| 141 | void Put( double dbl )
|
---|
| 142 | {
|
---|
| 143 | Put( (const char *)(&dbl), sizeof(double) );
|
---|
| 144 | }
|
---|
| 145 | void Put( float flt )
|
---|
| 146 | {
|
---|
| 147 | Put( (const char *)(&flt), sizeof(float) );
|
---|
| 148 | }
|
---|
| 149 | void Put( _int64 i64data )
|
---|
| 150 | {
|
---|
| 151 | Put( (const char *)(&i64data), sizeof(_int64) );
|
---|
| 152 | }
|
---|
| 153 | void Put( long l )
|
---|
| 154 | {
|
---|
| 155 | Realloc( size + sizeof(long) );
|
---|
| 156 | *((long *)(buffer+size))=l;
|
---|
| 157 | size += sizeof(long);
|
---|
| 158 | }
|
---|
| 159 | void Put( short s )
|
---|
| 160 | {
|
---|
| 161 | Realloc( size + sizeof(short) );
|
---|
| 162 | *((short *)(buffer+size))=s;
|
---|
| 163 | size += sizeof(short);
|
---|
| 164 | }
|
---|
| 165 | void Put( char c )
|
---|
| 166 | {
|
---|
| 167 | Realloc( size + 1 );
|
---|
| 168 | buffer[size++] = c;
|
---|
| 169 | }
|
---|
| 170 | };
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | }}
|
---|