AppendBuffer.cpp
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "AppendBuffer.h"
#include "stdlib.h"
#include <string.h>
//
namespace DmapCore_30
{
AppendBuffer::AppendBuffer(void)
{
allSize=0;
head=0;
now=0;
}
AppendBuffer::AppendBuffer(const char *fileName)
{
allSize = 0;
head = 0;
now = 0;
if (fileName)
{
f = fopen(fileName, "wb");
}
}
AppendBuffer::~AppendBuffer(void)
{
Reset();
}
//分配一个新的BUFFER
AppendBuffer::Buffer *AppendBuffer::NewABuffer(void)
{
Buffer *buf=new Buffer;
buf->buffer=(char *)malloc(APPEND_BUFFER_SIZE); //每个BUFFER都留这么大的空间?
if(buf->buffer==0)
{
delete buf;
return 0;
}
buf->next=0;
buf->bufferNowCount=0;
return buf;
}
char *AppendBuffer::GetString()
{
char *s = (char *)malloc(this->allSize + 1);
if (s == 0)return 0;
char *ss = s; int cc = 0;
for (Buffer *h = head; h;)
{
memcpy(ss, h->buffer, h->bufferNowCount);
ss += h->bufferNowCount;
// cc += h->bufferNowCount;
h = h->next;
}
s[this->allSize] = 0;
return s;
}
void AppendBuffer::AppendString(const char * str, int len)
{
if(now==0)AddABuffer(); //当前的指针为0
int remain;
for(;;)
{
remain=APPEND_BUFFER_SIZE-now->bufferNowCount;
if(remain==0)
{
AddABuffer();
remain=APPEND_BUFFER_SIZE-now->bufferNowCount;
}
if(remain>=len)//放得下
{
memcpy(now->buffer+now->bufferNowCount,str,len);
//len-=len;
now->bufferNowCount+=len;
this->allSize+=len;
break;
}
else //放不下
{
memcpy(now->buffer+now->bufferNowCount,str,remain);
len-=remain;
now->bufferNowCount=APPEND_BUFFER_SIZE;
str+=remain;
this->allSize+=remain;
AddABuffer();
}
}
}
//int AppendBuffer::GetSize()
//{
// int size = 0;
// for (AppendBuffer::Buffer *pb = this->head; pb; pb = pb->next)
// {
// size = size + sizeof(pb->buffer);
// }
// return size;
//}
void AppendBuffer::SetBuffer(AppendBuffer::Buffer *buf,const char * str, int len)
{
memcpy(buf->buffer,str,len);
this->allSize+=len;
buf->bufferNowCount=len;
}
int AppendBuffer::AppendString(const char * str)
{
AppendString(str,(int)(strlen(str)));
return 0;
}
int AppendBuffer::AddABuffer(void)
{
if(head==0)
{
head=NewABuffer();
now=head; //now是当前的指针
}
else
{
now->next=NewABuffer();
now=now->next;
}
if(now==0)return 1;//stbstb
return 0;
}
int AppendBuffer::AppendOtherOne(AppendBuffer * other)
{
this->now->next=other->head;
this->now=other->now;
this->allSize+=other->allSize;
other->head=0;
//other->allSize=0; //stbstb
//other->now=0;
return 0;
}
void AppendBuffer::Reset(void)
{
for(;head;)
{
now=head;
head=now->next;
free(now->buffer);
delete now;
}
head=0;
now=0;
allSize=0;
}
}