source: dev/trunk/ab5.0/jenga/include/common/Binary.h@ 529

Last change on this file since 529 was 529, checked in by dai_9181, 16 years ago

Binaryクラスをjengaプロジェクトに移動した。

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