-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbstring.h
109 lines (93 loc) · 2.55 KB
/
bstring.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef __STRING_H__
#define __STRING_H__
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
/* FUNCTIONS WORKING WITH struct string */
/* ------------------------------------ */
struct string {
union __u__ {
char align[16];
struct __s__ {
uintptr_t length;
uintptr_t size;
uintptr_t growby;
} _s;
} _u;
char* s;
};
void string_clear(struct string*);
int string_concat(struct string*, const char*);
int string_concatb(struct string*, const char*, uintptr_t);
int string_concat_sprintf(struct string* s, const char *msg, ...);
int string_putc(struct string*, char);
int string_putint(struct string*, int);
void string_free(struct string*);
char* string_get(struct string*);
int string_init(struct string*, uintptr_t, uintptr_t);
void string_move(struct string*, struct string*);
int string_equals(struct string*, struct string*);
void string_lazyinit(struct string*, uintptr_t);
int string_initfromstringz(struct string*, const char const*);
static inline uintptr_t string_length(struct string *s) {
/* amount of bytes filled with content */
return s->_u._s.length;
}
static inline uintptr_t string_size(struct string *s) {
/* amount of memory (in bytes) currently allocated */
return s->_u._s.size;
}
static inline int
string_concats(struct string* s, struct string* ss)
{
return string_concatb(s, ss->s, ss->_u._s.length);
}
static inline int
string_ensurez(struct string* s)
{
if (s->s)
if (s->s[s->_u._s.length - 1] == 0)
return 0;
if (string_putc(s, 0)) return 1;
--s->_u._s.length;
return 0;
}
static inline void
string_mkstatic(struct string* s, char* z)
{
s->s = z;
s->_u._s.growby = 0;
s->_u._s.size = strlen(z);
s->_u._s.length = strlen(z);
}
/* FUNCTIONS WORKING WITH PLAIN char* */
/* ---------------------------------- */
static inline bool str_is_empty(char *s) {
if (s != NULL && strlen(s) > 0) {
return false;
} else {
return true;
}
}
static inline bool str_is_nonempty(char *s) {
if (s != NULL && strlen(s) > 0) {
return true;
} else {
return false;
}
}
static inline bool str_is_true(char *s, bool def) {
if (str_is_nonempty(s)) {
if (strcmp(s, "0")==0 || strcasecmp(s,"no")==0) {
return false;
} else if (strcmp(s, "1")==0 || strcasecmp(s,"yes")==0) {
return true;
} else {
return def; /* TODO: something better needed */
}
} else {
return def;
}
}
#endif