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