source: dev/trunk/abdev/BasicCompiler_Common/include/Binary.h@ 288

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