9 #ifndef PHPSTR_DEFAULT_SIZE
10 # define PHPSTR_DEFAULT_SIZE 256
13 #define PHPSTR_NOMEM ((size_t) -1)
16 # define STR_SET(STR, SET) \
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)
35 #define PHPSTR(p) ((phpstr *) (p))
36 #define PHPSTR_VAL(p) (PHPSTR(p))->data
37 #define PHPSTR_LEN(p) (PHPSTR(p))->used
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) \
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: \
50 phpstr *PTR = (STR); \
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)); \
68 #define RETVAL_PHPSTR(STR, free, dup) \
70 RETVAL_STRINGL((STR)->data, (STR)->used, (dup)); \
71 FREE_PHPSTR((free), (STR));
73 typedef struct _phpstr_t
{
82 typedef enum _phpstr_free_t
{
84 PHPSTR_FREE_PTR
, /* pefree() */
85 PHPSTR_FREE_VAL
, /* phpstr_dtor() */
86 PHPSTR_FREE_ALL
/* phpstr_free() */
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)
94 #define PHPSTR_INIT_PREALLOC 0x01
95 #define PHPSTR_INIT_PERSISTENT 0x02
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
);
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
);
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
);
113 /* shrink memory chunk to actually used size (+1) */
114 PHPSTR_API
size_t phpstr_shrink(phpstr
*buf
);
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);
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);
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);
134 /* get a zero-terminated string */
135 PHPSTR_API
char *phpstr_data(const phpstr
*buf
, char **into
, size_t *len
);
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
);
143 /* remove a substring */
144 PHPSTR_API
size_t phpstr_cut(phpstr
*buf
, size_t offset
, size_t length
);
146 /* get a complete phpstr duplicate */
147 PHPSTR_API phpstr
*phpstr_dup(const phpstr
*buf
);
149 /* merge several phpstr objects
152 phpstr *final = phpstr_merge(3,
153 PHPSTR_NOT_FREE(&keep),
154 PHPSTR_ALL_FREE(middle_ptr),
155 PHPSTR_VAL_FREE(&local);
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
);
161 /* sets a trailing NUL byte */
162 PHPSTR_API phpstr
*phpstr_fix(phpstr
*buf
);
164 /* memcmp for phpstr objects */
165 PHPSTR_API
int phpstr_cmp(phpstr
*left
, phpstr
*right
);
167 /* reset phpstr object */
168 PHPSTR_API
void phpstr_reset(phpstr
*buf
);
170 /* free a phpstr objects contents */
171 PHPSTR_API
void phpstr_dtor(phpstr
*buf
);
173 /* free a phpstr object completely */
174 PHPSTR_API
void phpstr_free(phpstr
**buf
);
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
);
179 typedef void (*phpstr_passthru_func
)(void *opaque
, const char *, size_t TSRMLS_DC
);
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
);
192 * vim600: sw=4 ts=4 fdm=marker