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

Last change on this file since 392 was 357, checked in by dai_9181, 17 years ago

例外処理機構実装中...

File size: 3.4 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 void Overwrite( int pos, _int64 newInt64Value )
125 {
126 *(_int64 *)( buffer + pos ) = newInt64Value;
127 }
128
129 void Put( const char *buffer, int size )
130 {
131 Realloc( this->size + size );
132
133 memcpy( this->buffer + this->size, buffer, size );
134 this->size += size;
135 }
136 void Put( double dbl )
137 {
138 Put( (const char *)(&dbl), sizeof(double) );
139 }
140 void Put( float flt )
141 {
142 Put( (const char *)(&flt), sizeof(float) );
143 }
144 void Put( _int64 i64data )
145 {
146 Put( (const char *)(&i64data), sizeof(_int64) );
147 }
148 void Put( long l )
149 {
150 Realloc( size + sizeof(long) );
151 *((long *)(buffer+size))=l;
152 size += sizeof(long);
153 }
154 void Put( short s )
155 {
156 Realloc( size + sizeof(short) );
157 *((short *)(buffer+size))=s;
158 size += sizeof(short);
159 }
160 void Put( char c )
161 {
162 Realloc( size + 1 );
163 buffer[size++] = c;
164 }
165};
Note: See TracBrowser for help on using the repository browser.