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