- improve struct layout
[m6w6/ext-http] / phpstr / phpstr.h
1
2 /* $Id$ */
3
4 #ifndef _PHPSTR_H_
5 #define _PHPSTR_H_
6
7 #include "php.h"
8
9 #ifndef PHPSTR_DEFAULT_SIZE
10 # define PHPSTR_DEFAULT_SIZE 256
11 #endif
12
13 #ifndef STR_SET
14 # define STR_SET(STR, SET) \
15 { \
16 STR_FREE(STR); \
17 STR = SET; \
18 }
19 #endif
20
21 #if defined(PHP_WIN32)
22 # if defined(PHPSTR_EXPORTS)
23 # define PHPSTR_API __declspec(dllexport)
24 # elif defined(COMPILE_DL_PHPSTR)
25 # define PHPSTR_API __declspec(dllimport)
26 # else
27 # define PHPSTR_API
28 # endif
29 #else
30 # define PHPSTR_API
31 #endif
32
33 #define PHPSTR(p) ((phpstr *) (p))
34 #define PHPSTR_VAL(p) (PHPSTR(p))->data
35 #define PHPSTR_LEN(p) (PHPSTR(p))->used
36
37 #define FREE_PHPSTR_PTR(STR) pefree(STR, STR->pmem)
38 #define FREE_PHPSTR_VAL(STR) phpstr_dtor(STR)
39 #define FREE_PHPSTR_ALL(STR) phpstr_free(&(STR))
40 #define FREE_PHPSTR(free, STR) \
41 switch (free) \
42 { \
43 case PHPSTR_FREE_NOT: break; \
44 case PHPSTR_FREE_PTR: pefree(STR, STR->pmem); break; \
45 case PHPSTR_FREE_VAL: phpstr_dtor(STR); break; \
46 case PHPSTR_FREE_ALL: \
47 { \
48 phpstr *PTR = (STR); \
49 phpstr_free(&PTR); \
50 } \
51 break; \
52 default: break; \
53 }
54
55 #define RETURN_PHPSTR_PTR(STR) RETURN_PHPSTR((STR), PHPSTR_FREE_PTR, 0)
56 #define RETURN_PHPSTR_VAL(STR) RETURN_PHPSTR((STR), PHPSTR_FREE_NOT, 0)
57 #define RETURN_PHPSTR_DUP(STR) RETURN_PHPSTR((STR), PHPSTR_FREE_NOT, 1)
58 #define RETVAL_PHPSTR_PTR(STR) RETVAL_PHPSTR((STR), PHPSTR_FREE_PTR, 0)
59 #define RETVAL_PHPSTR_VAL(STR) RETVAL_PHPSTR((STR), PHPSTR_FREE_NOT, 0)
60 #define RETVAL_PHPSTR_DUP(STR) RETVAL_PHPSTR((STR), PHPSTR_FREE_NOT, 1)
61 /* RETURN_PHPSTR(buf, PHPSTR_FREE_PTR, 0) */
62 #define RETURN_PHPSTR(STR, free, dup) \
63 RETVAL_PHPSTR((STR), (free), (dup)); \
64 return;
65
66 #define RETVAL_PHPSTR(STR, free, dup) \
67 phpstr_fix(STR); \
68 RETVAL_STRINGL((STR)->data, (STR)->used, (dup)); \
69 FREE_PHPSTR((free), (STR));
70
71 typedef struct _phpstr_t {
72 char *data;
73 size_t used;
74 size_t free;
75 size_t size;
76 unsigned pmem:1;
77 unsigned reserved:31;
78 } phpstr;
79
80 typedef enum _phpstr_free_t {
81 PHPSTR_FREE_NOT = 0,
82 PHPSTR_FREE_PTR, /* pefree() */
83 PHPSTR_FREE_VAL, /* phpstr_dtor() */
84 PHPSTR_FREE_ALL /* phpstr_free() */
85 } phpstr_free_t;
86
87 #define PHPSTR_ALL_FREE(STR) PHPSTR_FREE_ALL,(STR)
88 #define PHPSTR_PTR_FREE(STR) PHPSTR_FREE_PTR,(STR)
89 #define PHPSTR_VAL_FREE(STR) PHPSTR_FREE_VAL,(STR)
90 #define PHPSTR_NOT_FREE(STR) PHPSTR_FREE_NOT,(STR)
91
92 #define PHPSTR_INIT_PREALLOC 0x01
93 #define PHPSTR_INIT_PERSISTENT 0x02
94
95 /* create a new phpstr */
96 #define phpstr_new() phpstr_init(NULL)
97 #define phpstr_init(b) phpstr_init_ex(b, PHPSTR_DEFAULT_SIZE, 0)
98 #define phpstr_clone(phpstr_pointer) phpstr_init_ex(NULL, (phpstr_pointer)->size, (phpstr_pointer)->pmem ? PHPSTR_INIT_PERSISTENT:0)
99 PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, int flags);
100
101 /* create a phpstr from a zval or c-string */
102 #define phpstr_from_zval(z) phpstr_from_string(Z_STRVAL(z), Z_STRLEN(z))
103 #define phpstr_from_zval_ex(b, z) phpstr_from_string_ex(b, Z_STRVAL(z), Z_STRLEN(z))
104 #define phpstr_from_string(s, l) phpstr_from_string_ex(NULL, (s), (l))
105 PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t length);
106
107 /* usually only called from within the internal functions */
108 #define phpstr_resize(b, s) phpstr_resize_ex((b), (s), 0)
109 PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size);
110
111 /* shrink memory chunk to actually used size (+1) */
112 PHPSTR_API size_t phpstr_shrink(phpstr *buf);
113
114 /* append data to the phpstr */
115 #define phpstr_appends(b, a) phpstr_append((b), (a), sizeof(a)-1)
116 #define phpstr_appendl(b, a) phpstr_append((b), (a), strlen(a))
117 PHPSTR_API size_t phpstr_append(phpstr *buf, const char *append, size_t append_len);
118 PHPSTR_API size_t phpstr_appendf(phpstr *buf, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
119
120 /* insert data at a specific position of the phpstr */
121 #define phpstr_inserts(b, i, o) phpstr_insert((b), (i), sizeof(i)-1, (o))
122 #define phpstr_insertl(b, i, o) phpstr_insert((b), (i), strlen(i), (o))
123 PHPSTR_API size_t phpstr_insert(phpstr *buf, const char *insert, size_t insert_len, size_t offset);
124 PHPSTR_API size_t phpstr_insertf(phpstr *buf, size_t offset, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 3, 4);
125
126 /* prepend data */
127 #define phpstr_prepends(b, p) phpstr_prepend((b), (p), sizeof(p)-1)
128 #define phpstr_prependl(b, p) phpstr_prepend((b), (p), strlen(p))
129 PHPSTR_API size_t phpstr_prepend(phpstr *buf, const char *prepend, size_t prepend_len);
130 PHPSTR_API size_t phpstr_prependf(phpstr *buf, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
131
132 /* get a zero-terminated string */
133 PHPSTR_API char *phpstr_data(const phpstr *buf, char **into, size_t *len);
134
135 /* get a part of the phpstr */
136 #define phpstr_mid(b, o, l) phpstr_sub((b), (o), (l))
137 #define phpstr_left(b, l) phpstr_sub((b), 0, (l))
138 PHPSTR_API phpstr *phpstr_right(const phpstr *buf, size_t length);
139 PHPSTR_API phpstr *phpstr_sub(const phpstr *buf, size_t offset, size_t len);
140
141 /* remove a substring */
142 PHPSTR_API size_t phpstr_cut(phpstr *buf, size_t offset, size_t length);
143
144 /* get a complete phpstr duplicate */
145 PHPSTR_API phpstr *phpstr_dup(const phpstr *buf);
146
147 /* merge several phpstr objects
148 use like:
149
150 phpstr *final = phpstr_merge(3,
151 PHPSTR_NOT_FREE(&keep),
152 PHPSTR_ALL_FREE(middle_ptr),
153 PHPSTR_VAL_FREE(&local);
154 */
155 PHPSTR_API phpstr *phpstr_merge(unsigned argc, ...);
156 PHPSTR_API phpstr *phpstr_merge_ex(phpstr *buf, unsigned argc, ...);
157 PHPSTR_API phpstr *phpstr_merge_va(phpstr *buf, unsigned argc, va_list argv);
158
159 /* sets a trailing NUL byte */
160 PHPSTR_API phpstr *phpstr_fix(phpstr *buf);
161
162 /* memcmp for phpstr objects */
163 PHPSTR_API int phpstr_cmp(phpstr *left, phpstr *right);
164
165 /* reset phpstr object */
166 PHPSTR_API void phpstr_reset(phpstr *buf);
167
168 /* free a phpstr objects contents */
169 PHPSTR_API void phpstr_dtor(phpstr *buf);
170
171 /* free a phpstr object completely */
172 PHPSTR_API void phpstr_free(phpstr **buf);
173
174 /* stores data in a phpstr until it reaches chunk_size */
175 PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_len, char **chunk, size_t chunk_size);
176
177 typedef void (*phpstr_passthru_func)(void *opaque, const char *, size_t TSRMLS_DC);
178
179 /* wrapper around phpstr_chunk_buffer, which passes available chunks to passthru() */
180 PHPSTR_API void phpstr_chunked_output(phpstr **s, const char *data, size_t data_len, size_t chunk_size, phpstr_passthru_func passthru, void *opaque TSRMLS_DC);
181
182 #endif
183
184
185 /*
186 * Local variables:
187 * tab-width: 4
188 * c-basic-offset: 4
189 * End:
190 * vim600: sw=4 ts=4 fdm=marker
191 * vim<600: sw=4 ts=4
192 */