-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrBuffer.h
More file actions
220 lines (209 loc) · 3.63 KB
/
StrBuffer.h
File metadata and controls
220 lines (209 loc) · 3.63 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* StrBuffer.h
*
* Created on: 31 Oct 2013
* Author: lester
*/
#ifndef STRBUFFER_H_
#define STRBUFFER_H_
/*
* Create a buffer using a simple link list with element of size 50.
* send list to output port using iterator.
* when finish release everything.
*
*/
template<class A = OneRelease>
class StrNode
{
public:
/*
* Write node content to string
*/
inline unsigned short pop(char *&str, unsigned short &max)
{
CHECK_RET_CODE(pos <= max, ERR_MEMORY);
memcpy(str, this->data, this->pos);
str += this->pos;
max -= this->pos;
return NO_ERR;
}
/*
* Try to put content on node
* pointer and size are actualized
*/
void push(const char* &str, unsigned short &str_size, A *mem)
{
unsigned short count = MIN(size - pos, str_size);
if (count == 0)
return;
memcpy(&data[pos], str, count);
pos += count;
str_size -= count;
str += count;
}
unsigned short getUse()
{
return pos;
}
unsigned short getFree()
{
return (size - pos);
}
inline StrNode<A>* getNext()
{
return next;
}
/*
* Get length of string from this segment
*/
unsigned short getLength()
{
unsigned short len = 0;
StrNode *n = this;
while (n)
{
len += n->getUse();
n = n->next;
}
return len;
}
/*
* Create a node to store the required size.
* 100 is a minimum size,
*/
static StrNode *create(unsigned short size, A *mem)
{
StrNode *n;
size += 100;
n = (StrNode *) mem->alloc(sizeof(StrNode<A> ) + size);
CHECK_RET_CODE(n != 0, 0);
n->init();
n->setSize(size);
return n;
}
/*
* Add a new node after this
*/
unsigned int add(unsigned short size, A *mem)
{
if (this == 0)
return ERR_WRONG_PARAM;
StrNode *n = create(size, mem);
CHECK_RET_CODE(n != 0, ERR_MEMORY);
n->next = next;
next = n;
return NO_ERR;
}
inline void release(A *mem)
{
if (A::canFree())
{
if (this == 0)
return;
if (next)
next->release(mem);
mem->free(this);
}
}
private:
void init()
{
next = 0;
size = 0;
pos = 0;
}
inline void setSize(unsigned short s)
{
size = s;
}
StrNode<A>* next;
unsigned char size; // size
unsigned char pos; // write pos
char data[1]; // string data
};
template<class A = OneRelease>
class StrBuffer
{
public:
StrBuffer(A &mem)
{
this->mem = &mem;
first = 0;
last = 0;
}
inline void release()
{
first->release(mem);
}
~StrBuffer()
{
release();
}
inline unsigned short push(const char* str)
{
return push(str, strlen(str));
}
unsigned short push(const char* str, unsigned short size)
{
if (first == 0)
{
first = first->create(size, mem);
CHECK_RET_CODE(first != 0, ERR_MEMORY);
last = first;
}
while (size)
{
last->push(str, size, mem);
if (size)
{
CHECK_ERR_RET(last->add(size,mem));
last = last->getNext();
}
}
return NO_ERR;
}
unsigned short getSize()
{
return first->getLength();
}
/*
* Get all on a buffer
* null terminate character will be include
*/
unsigned short pop(char* str, unsigned short max_size)
{
StrNode<A> *n = first;
// loop all nodes and copy string
while (n)
{
RET_ON_ERROR(n->pop(str, max_size));
n = n->getNext();
}
CHECK_RET_CODE(max_size, ERR_MEMORY);
*str = 0;
return NO_ERR;
}
/*
* get entire string
* a zero is add at the end of string
* return size of string no including null terminate
*/
unsigned short pop(char* &str, unsigned short &size, A &lmem)
{
if (first == 0)
{
str = 0;
size = 0;
return NO_ERR;
}
size = first->getLength() + 1;
str = (char*) lmem.alloc(size);
CHECK_ERR_RET(pop(str, size));
size--;
return NO_ERR;
}
private:
A *mem;
StrNode<A>* first, *last;
};
#endif /* STRBUFFER_H_ */