-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.c
More file actions
319 lines (280 loc) · 8.12 KB
/
table.c
File metadata and controls
319 lines (280 loc) · 8.12 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "table.h"
static int32_t strcat_safe2(char *p1, uint32_t p1len, const char *p2) {
uint32_t i;
uint32_t p2len;
if (!p1 || !p1len || !p2) {
return -1;
}
if ((p2len = strlen(p2)) == 0) {
return 0;
}
for (i = 0; i < p1len - 1 && p1[i]; i++);
if (i >= p1len - 1) {
return -2;
}
if (p2len > p1len - 1 - i) {
return -3;
}
memcpy(&p1[i], p2, p2len);
return p2len;
}
/**
* append the source string to destination string, every append with a NULL terminated.
* e.g:
* char buf[10] = {0};
* strcat_safe(buf, sizeof(buf), "_123"); // buf="_123", 0
* strcat_safe(buf, sizeof(buf), "_456"); // buf="_123", 0, "_456", 0
* @param p1: the destination string
* @param p1len: the length(in bytes) of @p1
* @param p2: the source string
* @return: <0 : failure
* >=0 : the appended bytes
*/
static int32_t strcat_safe(char *p1, uint32_t p1len, const char *p2) {
uint32_t i, j;
uint32_t space;
uint32_t p2len;
if (!p1 || !p1len || !p2) {
return -1;
}
p1[p1len - 1] = 0;
if (p1[0] == 0) {
j = 0;
} else {
for (i = 1; i < p1len - 2 && (p1[i] || p1[i + 1]); i++); // find "00"
j = i + 1;
}
space = p1len - 1 - j;
p2len = strlen(p2);
p2len = (p2len > space ? space : p2len);
memcpy(&p1[j], p2, p2len);
p1[j + p2len] = 0;
return p2len;
}
static int32_t addstring(table_handle_t *h, uint32_t col, const char *buf) {
uint32_t buflen = strlen(buf);
if (strcat_safe(h->pool, h->poollen, buf) < 0) {
return -1;
}
if (buflen > h->colwidth[col]) {
h->colwidth[col] = buflen;
}
return 0;
}
/**
* get a string(e.g: {'1', '2', '3', 0, '4', '5', '6', 0, '7', '8', '9', 0}) by
* specified index
* @param h: a @table instance
* @param index: the sequence number of the string(start with 0) in @h
* @return: !NULL : the address of the string
* NULL : the string can not be found
*/
static char *getitem(table_handle_t *h, uint32_t index) {
uint32_t i;
uint32_t find;
if (!h) {
return NULL;
}
if (index == 0) {
return (h->pool[0] ? &h->pool[0] : NULL);
}
if (index > h->poollen / 2) { // if index > max index
return NULL;
}
for (find = 0, i = 1; i < h->poollen - 1; i++) {
if (h->pool[i] == 0 && ++find == index) {
return (h->pool[i + 1] ? &h->pool[i + 1] : NULL);
}
}
return NULL;
}
/**
* append a string to a buffer, if the string is shorter than a specify value, padding it
* @param p: the buffer to be appended to
* @param plen: the length(in bytes) of @p
* @param item: the string append to @p
* @param padlen: a fix length, if strlen(item) <= padlen, zero or many ' ' will be padded
* or, '...' will be padded
* @return: <0 : failure
* >=0 : the wrote bytes + 1(the "1" means the end ' ')
*/
static int32_t addstring_pad(char *p, uint32_t plen, const char *item, uint32_t padlen) {
uint32_t i;
char buf[CONFIG_ITEMSIZE + 1]; // there is a ' ' in the end
uint32_t itemlen;
if (!p || !plen || !item || (padlen < MINIMAL_ITEMSIZE || padlen > CONFIG_ITEMSIZE)) {
return -1;
}
p[plen - 1] = 0;
for (i = 0; i < plen - 1 && p[i]; i++);
if (i >= plen - 1) { // if can't find '\0', which means p is full
return -2;
}
if (padlen > plen - 1 - i) { // if no enough noom to write
return -3;
}
memset(buf, ' ', sizeof(buf));
itemlen = strlen(item);
if (itemlen <= padlen) {
memcpy(buf, item, itemlen);
} else {
memcpy(buf, item, padlen);
buf[padlen - 1] = '.';
buf[padlen - 2] = '.';
buf[padlen - 3] = '.';
}
memcpy(&p[i], buf, padlen + 1);
return padlen + 1;
}
static int32_t write_string(table_handle_t *h, const char *s) {
if (addstring(h, h->currcol, s) < 0) {
return -2;
}
if (++h->currcol >= h->totcol) {
h->currcol = 0;
}
return 0;
}
/**
* initialize a @table instance
* @param h: a @table instance
* @param mem: the memory to be attached to @h
* @param memlen: the length(in bytes) of @mem
* @param totcol: total column
* @param end: line end character. can be "\r", "\n" or "\r\n"
* @param title: the title of @h. if NULL, the title will not exist. note that the length of it must equals to @totcol
* @return: !0: failure
* 0: success
*/
int32_t table_init(table_handle_t *h, char *mem, uint32_t memlen, int32_t totcol, const char *end,
const char *title[]) {
uint32_t i;
if (!h || !mem || !memlen || (!totcol || totcol > CONFIG_MAXCOLLEN) || !end) {
return -1;
}
if (strcmp(end, "\r") != 0 && strcmp(end, "\n") != 0 && strcmp(end, "\r\n") != 0) {
return -2;
}
memset(mem, 0, memlen);
memset(h, 0, sizeof(*h));
for (i = 0; i < totcol; i++) {
h->colwidth[i] = MINIMAL_ITEMSIZE;
}
h->pool = (char *)mem;
h->poollen = memlen;
h->totcol = totcol;
strncpy(h->end, end, 2);
h->title = (title ? 1 : 0);
for (i = 0; i < totcol && title; i++) {
write_string(h, title[i]);
}
return 0;
}
/**
* write an item to @table
* @param h: a @table instance
* @param fmt,...: the formatted string
* @return: !0: failure
* 0: success
*/
int32_t table_write(table_handle_t *h, const char *fmt, ...) {
static char buf[CONFIG_ITEMSIZE];
va_list args;
memset(buf, 0, sizeof(buf));
va_start(args, fmt);
if (vsnprintf(buf, sizeof(buf) - 1, fmt, args) <= 0) {
return -1;
}
va_end(args);
return write_string(h, buf);
}
/**
* read all string from @table
* @param h: a @table instance
* @param mem: the memory to be written into
* @param memlen: the length(in bytes) of @mem
* @param reallen: the real length(in bytes) of writing
* @return: !0: failure
* 0: success
*/
int32_t table_read(table_handle_t *h, char *mem, uint32_t memlen, uint32_t *reallen) {
char *p;
uint32_t i;
uint32_t linelen;
uint32_t col;
int32_t writelen;
uint32_t start;
if (!h || !mem || !memlen || !reallen) {
return -1;
}
memset(mem, 0, memlen);
*reallen = 0;
if (h->title) {
for (linelen = 0, i = 0; i < h->totcol; i++) {
linelen += h->colwidth[i] + 1;
}
for (i = 0; i < linelen; i++) {
if (strcat_safe2(mem, memlen, "=") < 0) {
return -4;
}
}
if ((writelen = strcat_safe2(mem, memlen, h->end)) < 0) {
return -3;
}
*reallen += linelen + 2;
for (i = 0, col = 0; (p = getitem(h, i)) != NULL; i++) {
if ((writelen = addstring_pad(mem, memlen, p, h->colwidth[col])) < 0) {
return -2;
}
*reallen += writelen;
if (++col >= h->totcol) {
if ((writelen = strcat_safe2(mem, memlen, h->end)) < 0) {
return -3;
}
*reallen += writelen;
col = 0;
break;
}
}
for (i = 0; i < linelen; i++) {
if (strcat_safe2(mem, memlen, "=") < 0) {
return -4;
}
}
if ((writelen = strcat_safe2(mem, memlen, h->end)) < 0) {
return -3;
}
*reallen += linelen + 2;
start = h->totcol;
} else {
start = 0;
}
for (i = start, col = 0; (p = getitem(h, i)) != NULL; i++) {
if ((writelen = addstring_pad(mem, memlen, p, h->colwidth[col])) < 0) {
return -2;
}
*reallen += writelen;
if (++col >= h->totcol) {
if ((writelen = strcat_safe2(mem, memlen, h->end)) < 0) {
return -3;
}
*reallen += writelen;
col = 0;
}
}
return 0;
}
/**
* destroy a @table instance
* @param h: a @table instance
*/
void table_destroy(table_handle_t *h) {
if (!h) {
return;
}
memset(h->pool, 0, h->poollen);
memset(h, 0, sizeof(*h));
}