-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbgm_sds.c
76 lines (65 loc) · 1.45 KB
/
bgm_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
/* System */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "trackerlogic.h"
#include "bgm_sds.h"
void sds_init( bgm_sds *dest )
{
//memset( dest, 0, sizeof(bgm_sds) );
dest->data = NULL;
dest->space = 0;
sds_realloc( dest, 1 );
}
void sds_free( bgm_sds *dest )
{
free(dest->data);
dest->data = NULL;
dest->space = 0;
}
int sds_realloc( bgm_sds *dest, size_t new_size )
{
char *new_data = NULL;
size_t new_space;
if ( new_size > dest->space ) {
new_space = (new_size / 1024 + 1) * 1024;
if ( dest->space == 0 ) {
new_data = malloc( new_space * sizeof(char) );
if ( new_data ) {
*new_data = 0;
}
} else {
new_data = realloc( dest->data, new_space * sizeof(char) );
}
if( !new_data ) {
exerr("bangumi: sds realloc data error");
return 0;
}
#ifdef _DEBUG_BANGUMI_SDS
fprintf(stderr, "(0x%08x) size %zu space %zu -> %zu\n", dest, dest->data ? sds_strlen(dest) : 0, dest->space, new_space);
#endif
dest->space = new_space;
dest->data = new_data;
}
return 1;
}
int sds_strcpy( bgm_sds *dest, char *src )
{
if (sds_realloc( dest, strlen(src) + 1 )) {
strcpy(dest->data, src);
return 1;
}
return 0;
}
int sds_strcat( bgm_sds *dest, char *src )
{
if (sds_realloc( dest, sds_strlen(dest) + strlen(src) + 1 )) {
strcat(dest->data, src);
return 1;
}
return 0;
}
size_t sds_strlen( bgm_sds *dest )
{
return strlen( dest->data );
}