forked from snowyu/idb.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisdk_sds.c
82 lines (72 loc) · 1.78 KB
/
isdk_sds.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
/*
* =====================================================================================
*
* Filename: isdk_sds.c
*
* Description: sds wrapper
*
* Version: 1.0
* Created: 2013/03/13
* Revision: none
* Compiler: gcc
*
* Author: Riceball LEE([email protected])
* Company:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <string.h>
#include "deps/zmalloc.h"
#include "isdk_sds.h"
sds sdsalloc(const void *init, size_t initlen) {
struct sdshdr *sh;
if (init) {
sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
} else {
sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
}
if (sh == NULL) return NULL;
if (init) {
sh->len = initlen;
sh->free = 0;
} else {
sh->len = 0;
sh->free = initlen;
}
if (initlen && init) {
memcpy(sh->buf, init, initlen);
}
sh->buf[sh->len] = '\0';
return (char*)sh->buf;
}
sds sdsprintf(sds s, const char *fmt, ...) {
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
sh->free += sh->len;
sh->len = 0;
va_list ap;
char *t;
va_start(ap, fmt);
t = sdscatvprintf(s,fmt,ap);
va_end(ap);
return t;
}
#ifdef ISDK_SDS_TEST_MAIN
#include <stdio.h>
#include "testhelp.h"
int main(void) {
{
struct sdshdr *sh;
sds x = sdsempty();
test_cond("sdsempty() should be strlen 0",
strlen(x) == 0 && sdslen(x) == 0 && memcmp(x,"\0",1) == 0);
sdsfree(x);
x = sdsalloc(NULL, 2);
test_cond("Create a NULL string with reserved space 2 bytes",
sdslen(x) == 0 && sdsavail(x) == 2);
sdsfree(x);
}
test_report()
return 0;
}
#endif