7 #ifndef PHPSTR_DEFAULT_SIZE
8 # define PHPSTR_DEFAULT_SIZE 256
11 #define PHPSTR_NOMEM ((size_t) -1)
14 # define STR_FREE(STR) \
22 # define STR_SET(STR, SET) \
34 #ifdef PHP_ATTRIBUTE_FORMAT
35 # define PHPSTR_ATTRIBUTE_FORMAT(f, a, b) PHP_ATTRIBUTE_FORMAT(f, a, b)
37 # define PHPSTR_ATTRIBUTE_FORMAT(f, a, b)
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
)
47 char *r
= (char *) malloc(s
+1);
48 if (r
) memcpy((void *) r
, p
, s
), r
[s
] = '\0';
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)
65 #define PHPSTR(p) ((phpstr *) (p))
66 #define PHPSTR_VAL(p) (PHPSTR(p))->data
67 #define PHPSTR_LEN(p) (PHPSTR(p))->used
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) \
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: \
80 phpstr *PTR = (STR); \
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)); \
98 #define RETVAL_PHPSTR(STR, free, dup) \
100 RETVAL_STRINGL((STR)->data, (STR)->used, (dup)); \
101 FREE_PHPSTR((free), (STR));
103 typedef struct _phpstr_t
{
109 unsigned reserved
:31;
112 typedef enum _phpstr_free_t
{
114 PHPSTR_FREE_PTR
, /* pefree() */
115 PHPSTR_FREE_VAL
, /* phpstr_dtor() */
116 PHPSTR_FREE_ALL
/* phpstr_free() */
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)
124 #define PHPSTR_INIT_PREALLOC 0x01
125 #define PHPSTR_INIT_PERSISTENT 0x02
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
);
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
);
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
);
143 /* shrink memory chunk to actually used size (+1) */
144 PHPSTR_API
size_t phpstr_shrink(phpstr
*buf
);
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);
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);
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);
164 /* get a zero-terminated string */
165 PHPSTR_API
char *phpstr_data(const phpstr
*buf
, char **into
, size_t *len
);
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
);
173 /* remove a substring */
174 PHPSTR_API
size_t phpstr_cut(phpstr
*buf
, size_t offset
, size_t length
);
176 /* get a complete phpstr duplicate */
177 PHPSTR_API phpstr
*phpstr_dup(const phpstr
*buf
);
179 /* merge several phpstr objects
182 phpstr *final = phpstr_merge(3,
183 PHPSTR_NOT_FREE(&keep),
184 PHPSTR_ALL_FREE(middle_ptr),
185 PHPSTR_VAL_FREE(&local);
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
);
191 /* sets a trailing NUL byte */
192 PHPSTR_API phpstr
*phpstr_fix(phpstr
*buf
);
194 /* memcmp for phpstr objects */
195 PHPSTR_API
int phpstr_cmp(phpstr
*left
, phpstr
*right
);
197 /* reset phpstr object */
198 PHPSTR_API
void phpstr_reset(phpstr
*buf
);
200 /* free a phpstr objects contents */
201 PHPSTR_API
void phpstr_dtor(phpstr
*buf
);
203 /* free a phpstr object completely */
204 PHPSTR_API
void phpstr_free(phpstr
**buf
);
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
);
209 typedef void (*phpstr_passthru_func
)(void *opaque
, const char *, size_t TSRMLS_DC
);
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
);
222 * vim600: sw=4 ts=4 fdm=marker