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

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

Binaryクラスを追加

File size: 2.8 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 }
65
66public:
67 Binary()
68 : allocateSize( 8192 )
69 , buffer( (char *)malloc( allocateSize ) )
70 , size( 0 )
71 {
72 }
73 Binary( const char *buffer, int size )
74 : allocateSize( 8192 )
75 , buffer( (char *)malloc( allocateSize ) )
76 , size( 0 )
77 {
78 Put( buffer, size );
79 }
80 ~Binary()
81 {
82 free( buffer );
83 }
84
85 void Clear()
86 {
87 size = 0;
88 }
89
90 const char *GetBuffer() const
91 {
92 return buffer;
93 }
94 int GetSize() const
95 {
96 return size;
97 }
98
99 long GetLong( int pos ) const
100 {
101 return *(long *)( buffer + pos );
102 }
103
104 void Overwrite( int pos, char c )
105 {
106 buffer[pos] = c;
107 }
108 void Overwrite( int pos, long newLongValue )
109 {
110 *(long *)( buffer + pos ) = newLongValue;
111 }
112
113 void Put( const char *buffer, int size )
114 {
115 Realloc( this->size + size );
116
117 memcpy( this->buffer + this->size, buffer, size );
118 this->size += size;
119 }
120 void Put( _int64 i64data )
121 {
122 Put( (const char *)(&i64data), sizeof(_int64) );
123 }
124 void Put( long l )
125 {
126 Realloc( size + sizeof(long) );
127 *((long *)(buffer+size))=l;
128 size += sizeof(long);
129 }
130 void Put( short s )
131 {
132 Realloc( size + sizeof(short) );
133 *((short *)(buffer+size))=s;
134 size += sizeof(short);
135 }
136 void Put( char c )
137 {
138 Realloc( size + 1 );
139 buffer[size++] = c;
140 }
141};
Note: See TracBrowser for help on using the repository browser.