json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
sstr.h
Go to the documentation of this file.
1
35#ifndef SSTR_H_
36#define SSTR_H_
37
38#include <stdarg.h>
39#include <stddef.h>
40#include <stdbool.h>
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46#define SHORT_STR_CAPACITY 25
47#define CAP_ADD_DELTA 256
48
49struct sstr_s {
50 size_t length; // MUST FIRST, see sstr_length at sstr.h
51 char type;
52 union {
53 // short string store datas in short_str
54 char short_str[SHORT_STR_CAPACITY + 1];
55 // long string allocate a buffer and store datas in long_str
56 struct {
57 size_t capacity;
58 char* data;
59 } long_str;
60 // reference to a memory buffer
61 struct {
62 char* data;
63 } ref_str;
64 } un;
65};
66
67#define SSTR_TYPE_SHORT 0
68#define SSTR_TYPE_LONG 1
69#define SSTR_TYPE_REF 2
70
74typedef void* sstr_t;
75
81extern sstr_t sstr_new(void);
82
88extern void sstr_free(sstr_t s);
89
99extern sstr_t sstr_of(const void* data, size_t length);
100
112extern sstr_t sstr_ref(const void* data, size_t length);
113
122extern sstr_t sstr(const char* cstr);
123
136extern char* sstr_cstr(sstr_t s);
137
151#define sstr_length(s) ((struct sstr_s*)s)->length
152
169extern int sstr_compare(sstr_t a, sstr_t b);
170
177extern int sstr_compare_c(sstr_t a, const char* b);
178
186extern void sstr_append_zero(sstr_t s, size_t length);
187
196extern void sstr_append_of(sstr_t s, const void* data, size_t length);
197
205extern void sstr_append(sstr_t dst, sstr_t src);
206
214extern void sstr_append_cstr(sstr_t dst, const char* src);
215
222extern sstr_t sstr_dup(sstr_t s);
223
233extern sstr_t sstr_substr(sstr_t s, size_t index, size_t len);
234
240extern void sstr_clear(sstr_t s);
241
268extern sstr_t sstr_vslprintf(const char* fmt, va_list args);
269
279extern sstr_t sstr_vslprintf_append(sstr_t buf, const char* fmt, va_list args);
280
288extern sstr_t sstr_printf(const char* fmt, ...);
289
299extern sstr_t sstr_printf_append(sstr_t buf, const char* fmt, ...);
300
302
303extern void sstr_append_int_str(sstr_t s, int i);
304extern int sstr_parse_long(sstr_t s, long* v);
305extern int sstr_parse_int(sstr_t* s, int* v);
306extern void sstr_append_long_str(sstr_t s, long l);
307extern void sstr_append_float_str(sstr_t s, float f, int precission);
308extern void sstr_append_double_str(sstr_t s, double f, int precision);
309extern int sstr_parse_double(sstr_t s, double* v);
310
319extern void sstr_append_of_if(sstr_t s, const void* data, size_t length, bool cond);
326#define sstr_append_cstr_if(dst, src, cond) \
327 sstr_append_of_if(dst, src, strlen(src), cond)
328
329// escape string to json string format
330extern int sstr_json_escape_string_append(sstr_t out, sstr_t in);
331
338extern void sstr_append_indent(sstr_t s, size_t indent);
339
345extern const char* sstr_version(void);
346
347#ifdef __cplusplus
348}
349#endif
350
351#endif /* SSTR_H_ */
sstr_t sstr_vslprintf(const char *fmt, va_list args)
Printf implement.
Definition sstr.c:237
const char * sstr_version(void)
return version string.
Definition sstr.c:599
sstr_t sstr_printf_append(sstr_t buf, const char *fmt,...)
Same as sstr_printf(), but but print to buf instead of create a new one.
Definition sstr.c:227
int sstr_compare_c(sstr_t a, const char *b)
compare sstr_t a and c-style string b
Definition sstr.c:111
sstr_t sstr_substr(sstr_t s, size_t index, size_t len)
Get substring of s starting at index with length bytes.
Definition sstr.c:179
sstr_t sstr_new(void)
Create an empty sstr_t.
Definition sstr.c:42
sstr_t sstr_dup(sstr_t s)
Duplicate s and return.
Definition sstr.c:177
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition sstr.h:74
void sstr_append_cstr(sstr_t dst, const char *src)
Extends the sstr_t by appending additional characters contained in src.
Definition sstr.c:173
sstr_t sstr(const char *cstr)
Create a sstr_t from C-style (NULL-terminated) string str.
Definition sstr.c:84
sstr_t sstr_ref(const void *data, size_t length)
Create a sstr_t from data with length bytes. The data is not copied, but have a pointer to data.
Definition sstr.c:76
sstr_t sstr_printf(const char *fmt,...)
printf implement.
Definition sstr.c:217
void sstr_append_of(sstr_t s, const void *data, size_t length)
Extends the sstr_t by appending additional characters in data with length of length at the end of its...
Definition sstr.c:162
void sstr_append_indent(sstr_t s, size_t indent)
append spaces at the end of the sstr_t.
Definition sstr.c:811
void sstr_clear(sstr_t s)
clear the sstr_t. After this call, the sstr_t is empty.
Definition sstr.c:191
int sstr_compare(sstr_t a, sstr_t b)
Compare a and b return 0 if equal, <0 if a < b, >0 if a > b.
Definition sstr.c:88
sstr_t sstr_of(const void *data, size_t length)
Create a sstr_t from data with length bytes.
Definition sstr.c:59
sstr_t sstr_vslprintf_append(sstr_t buf, const char *fmt, va_list args)
Same as sstr_vslprintf, but print to buf instead of create a new one.
Definition sstr.c:243
void sstr_append(sstr_t dst, sstr_t src)
Extends the sstr_t by appending additional characters contained in src.
Definition sstr.c:169
void sstr_free(sstr_t s)
delete a sstr_t.
Definition sstr.c:48
char * sstr_cstr(sstr_t s)
Return C-style string representation of s.
Definition sstr.c:86
void sstr_append_of_if(sstr_t s, const void *data, size_t length, bool cond)
Append if cond is true, otherwise do nothing.
Definition sstr.c:802
void sstr_append_int_str(sstr_t s, int i)
convert sstr <-> int,long,float,double
Definition sstr.c:604
void sstr_append_zero(sstr_t s, size_t length)
Extends the sstr_t by appending additional '\0' characters at the end of its current value.
Definition sstr.c:125
Definition sstr.h:49