-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathfast_buffer.c
242 lines (211 loc) · 5.62 KB
/
fast_buffer.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (c) 2020 YuQing <384681@qq.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the Lesser GNU General Public License, version 3
* or later ("LGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include "logger.h"
#include "shared_func.h"
#include "fc_memory.h"
#include "fast_buffer.h"
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity)
{
buffer->length = 0;
if (init_capacity > 0)
{
buffer->alloc_size = init_capacity;
}
else
{
buffer->alloc_size = 256;
}
buffer->data = (char *)fc_malloc(buffer->alloc_size);
if (buffer->data == NULL)
{
return ENOMEM;
}
*(buffer->data) = '\0';
return 0;
}
void fast_buffer_destroy(FastBuffer *buffer)
{
if (buffer->data != NULL)
{
free(buffer->data);
buffer->data = NULL;
buffer->length = 0;
}
}
int fast_buffer_set_capacity(FastBuffer *buffer, const int capacity)
{
int alloc_size;
int new_capacity;
char *buff;
new_capacity = FC_MAX(capacity, buffer->length + 1);
if (buffer->alloc_size >= new_capacity) {
if (new_capacity > 1024) {
alloc_size = 2048;
} else if (new_capacity > 512) {
alloc_size = 1024;
} else if (new_capacity > 256) {
alloc_size = 512;
} else {
alloc_size = 256;
}
} else {
alloc_size = buffer->alloc_size * 2;
}
while (alloc_size < new_capacity) {
alloc_size *= 2;
}
buff = (char *)fc_malloc(alloc_size);
if (buff == NULL) {
return ENOMEM;
}
if (buffer->length > 0) {
memcpy(buff, buffer->data, buffer->length);
*(buff + buffer->length) = '\0';
}
free(buffer->data);
buffer->data = buff;
buffer->alloc_size = alloc_size;
return 0;
}
int fast_buffer_append(FastBuffer *buffer, const char *format, ...)
{
va_list ap;
int result;
int len;
if ((result=fast_buffer_check(buffer, 64)) != 0)
{
return result;
}
va_start(ap, format);
len = vsnprintf(buffer->data + buffer->length,
buffer->alloc_size - buffer->length, format, ap);
va_end(ap);
if (len < buffer->alloc_size - buffer->length)
{
buffer->length += len;
}
else //maybe full, realloc and try again
{
if ((result=fast_buffer_check(buffer, len + 1)) == 0)
{
va_start(ap, format);
buffer->length += vsnprintf(buffer->data + buffer->length,
buffer->alloc_size - buffer->length, format, ap);
va_end(ap);
}
else
{
*(buffer->data + buffer->length) = '\0'; //restore
}
}
return result;
}
int fast_buffer_append_buff(FastBuffer *buffer, const char *data, const int len)
{
int result;
if (len <= 0)
{
return 0;
}
if ((result=fast_buffer_check(buffer, len + 1)) != 0)
{
return result;
}
memcpy(buffer->data + buffer->length, data, len);
buffer->length += len;
*(buffer->data + buffer->length) = '\0';
return 0;
}
int fast_buffer_append_binary(FastBuffer *buffer,
const void *data, const int len)
{
int result;
if (len <= 0)
{
return 0;
}
if ((result=fast_buffer_check(buffer, len)) != 0)
{
return result;
}
memcpy(buffer->data + buffer->length, data, len);
buffer->length += len;
return 0;
}
int fast_buffer_append_int(FastBuffer *buffer, const int n)
{
int result;
if ((result=fast_buffer_check(buffer, 16)) != 0)
{
return result;
}
buffer->length += sprintf(buffer->data + buffer->length, "%d", n);
return 0;
}
int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n)
{
int result;
if ((result=fast_buffer_check(buffer, 32)) != 0)
{
return result;
}
buffer->length += sprintf(buffer->data + buffer->length, "%"PRId64, n);
return 0;
}
int fast_buffer_append_file(FastBuffer *buffer, const char *filename)
{
struct stat st;
int result;
int64_t file_size;
if (stat(filename, &st) != 0) {
result = errno != 0 ? errno : ENOENT;
if (result == ENOENT) {
logError("file: "__FILE__", line: %d, "
"file %s not exist!", __LINE__,
filename);
} else {
logError("file: "__FILE__", line: %d, "
"stat file %s fail, "
"result: %d, error info: %s", __LINE__,
filename, result, strerror(result));
}
return result;
}
if (!S_ISREG(st.st_mode)) {
logError("file: "__FILE__", line: %d, "
"file %s is NOT a regular file!",
__LINE__, filename);
return EINVAL;
}
file_size = st.st_size + 1;
if ((result=fast_buffer_check(buffer, file_size)) != 0) {
return result;
}
if ((result=getFileContentEx(filename, buffer->data + buffer->length,
0, &file_size)) != 0)
{
return result;
}
buffer->length += file_size;
return 0;
}