-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstring_buffer.h
87 lines (67 loc) · 1.77 KB
/
string_buffer.h
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
/**
* MIT License (MIT)
*
* Copyright (c) 2015 Zhiyuan Wang
*/
#ifndef _STRING_BUFFER_H_
#define _STRING_BUFFER_H_
#define STEP_LENGTH 512
#define EXPAND_BUFFER(self, n) \
if (expand_buffer(self, (n))) { \
return -1; \
} \
struct StringBuffer {
size_t length;
size_t capacity;
char *buf;
};
/**
* A buffer of C string.
*/
typedef struct StringBuffer StringBuffer;
/**
* Create a new buffer initialized to zero, return NULL if fail.
*/
extern StringBuffer *string_buffer_new();
/**
* Delete a StringBuffer.
*/
extern void string_buffer_delete(StringBuffer *self);
/**
* Get length of data in the buffer.
*/
extern size_t string_buffer_length(StringBuffer *self);
/**
* Get capacity of the buffer.
*/
extern size_t string_buffer_capacity(StringBuffer *self);
/**
* Get the string.
*/
extern char *string_buffer_get_string(StringBuffer *self);
/**
* Clear all data in the buffer.
*/
extern void string_buffer_clear(StringBuffer *self);
/**
* Add string to the buffer.
* Return length of str on success, -1 on failure.
*/
extern int string_buffer_append(StringBuffer *self, const char *str);
/**
* Add the first n bytes of str to the buffer.
* Return n on success, -1 on failure.
*/
extern int string_buffer_append_n(StringBuffer *self, const char *str, size_t n);
/**
* Add data to the buffer, the format specifier is the same with printf.
* Return value is the number of characters that has been written to the buffer,
* not counting the terminating null character.
*/
extern int string_buffer_appendf(StringBuffer *self, const char *format, ...);
/**
* Sets the n bytes of the tail of buffer to the specified value.
* Return n on sucess, -1 on failure.
*/
extern int string_buffer_append_set(StringBuffer *self, int value, size_t n);
#endif