- fix PHP4 build (has no only_exceptions INI setting
[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 #if defined(PHP_WIN32)
10 # if defined(PHPSTR_EXPORTS)
11 # define PHPSTR_API __declspec(dllexport)
12 # elif defined(COMPILE_DL_PHPSTR)
13 # define PHPSTR_API __declspec(dllimport)
14 # else
15 # define PHPSTR_API
16 # endif
17 #else
18 # define PHPSTR_API
19 #endif
20
21 #define PHPSTR(p) ((phpstr *) (p))
22 #define PHPSTR_VAL(p) (PHPSTR(p))->data
23 #define PHPSTR_LEN(p) (PHPSTR(p))->used
24
25 #define FREE_PHPSTR_PTR(STR) efree(STR)
26 #define FREE_PHPSTR_VAL(STR) phpstr_dtor(STR)
27 #define FREE_PHPSTR_ALL(STR) phpstr_free(STR)
28 #define FREE_PHPSTR(free, STR) \
29 switch (free) \
30 { \
31 case PHPSTR_FREE_PTR: efree(STR); break; \
32 case PHPSTR_FREE_VAL: phpstr_dtor(STR); break; \
33 case PHPSTR_FREE_ALL: phpstr_free(STR); break; \
34 case PHPSTR_FREE_NOT: break; \
35 default: break; \
36 }
37
38 #define RETURN_PHPSTR_PTR(STR) RETURN_PHPSTR((STR), PHPSTR_FREE_PTR, 0)
39 #define RETURN_PHPSTR_VAL(STR) RETURN_PHPSTR(&(STR), PHPSTR_FREE_NOT, 0)
40 #define RETVAL_PHPSTR_PTR(STR) RETVAL_PHPSTR((STR), PHPSTR_FREE_PTR, 0)
41 #define RETVAL_PHPSTR_VAL(STR) RETVAL_PHPSTR(&(STR), PHPSTR_FREE_NOT, 0)
42 /* RETURN_PHPSTR(buf, PHPSTR_FREE_PTR, 0) */
43 #define RETURN_PHPSTR(STR, free, dup) \
44 RETVAL_PHPSTR((STR), (free), (dup)); \
45 return;
46
47 #define RETVAL_PHPSTR(STR, free, dup) \
48 phpstr_fix(STR); \
49 RETVAL_STRINGL((STR)->data, (STR)->used, (dup)); \
50 FREE_PHPSTR((free), (STR));
51
52 typedef struct {
53 size_t size;
54 char *data;
55 size_t used;
56 size_t free;
57 } phpstr;
58
59 typedef enum {
60 PHPSTR_FREE_NOT = 0,
61 PHPSTR_FREE_PTR, /* efree() */
62 PHPSTR_FREE_VAL, /* phpstr_dtor() */
63 PHPSTR_FREE_ALL /* phpstr_free() */
64 } phpstr_free_t;
65
66 #define PHPSTR_ALL_FREE(STR) PHPSTR_FREE_ALL,(STR)
67 #define PHPSTR_PTR_FREE(STR) PHPSTR_FREE_PTR,(STR)
68 #define PHPSTR_VAL_FREE(STR) PHPSTR_FREE_VAL,(STR)
69 #define PHPSTR_NOT_FREE(STR) PHPSTR_FREE_NOT,(STR)
70
71 /* create a new phpstr */
72 #define phpstr_new() phpstr_init(NULL)
73 #define phpstr_init(b) phpstr_init_ex(b, 0, 0)
74 #define phpstr_clone(phpstr_pointer) phpstr_init_ex(NULL, (phpstr_pointer)->size, 0)
75 PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, zend_bool pre_alloc);
76
77 /* create a phpstr from a zval or c-string */
78 #define phpstr_from_zval(z) phpstr_from_string(Z_STRVAL(z), Z_STRLEN(z))
79 #define phpstr_from_zval_ex(b, z) phpstr_from_string_ex(b, Z_STRVAL(z), Z_STRLEN(z))
80 #define phpstr_from_string(s, l) phpstr_from_string_ex(NULL, (s), (l))
81 PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t length);
82
83 /* usually only called from within the internal functions */
84 #define phpstr_resize(b, s) phpstr_resize_ex((b), (s), 0)
85 PHPSTR_API void phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size);
86
87 /* append data to the phpstr */
88 #define phpstr_appends(b, a) phpstr_append((b), (a), sizeof(a)-1)
89 #define phpstr_appendl(b, a) phpstr_append((b), (a), strlen(a))
90 PHPSTR_API size_t phpstr_append(phpstr *buf, const char *append, size_t append_len);
91 PHPSTR_API size_t phpstr_appendf(phpstr *buf, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
92
93 /* insert data at a specific position of the phpstr */
94 #define phpstr_inserts(b, i, o) phpstr_insert((b), (i), sizeof(i)-1, (o))
95 #define phpstr_insertl(b, i, o) phpstr_insert((b), (i), strlen(i), (o))
96 PHPSTR_API size_t phpstr_insert(phpstr *buf, const char *insert, size_t insert_len, size_t offset);
97 PHPSTR_API size_t phpstr_insertf(phpstr *buf, size_t offset, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 3, 4);
98
99 /* prepend data */
100 #define phpstr_prepends(b, p) phpstr_prepend((b), (p), sizeof(p)-1)
101 #define phpstr_prependl(b, p) phpstr_prepend((b), (p), strlen(p))
102 PHPSTR_API size_t phpstr_prepend(phpstr *buf, const char *prepend, size_t prepend_len);
103 PHPSTR_API size_t phpstr_prependf(phpstr *buf, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
104
105 /* get a zero-terminated string */
106 PHPSTR_API char *phpstr_data(const phpstr *buf, char **into, size_t *len);
107
108 /* get a part of the phpstr */
109 #define phpstr_mid(b, o, l) phpstr_sub((b), (o), (l))
110 #define phpstr_left(b, l) phpstr_sub((b), 0, (l))
111 PHPSTR_API phpstr *phpstr_right(const phpstr *buf, size_t length);
112 PHPSTR_API phpstr *phpstr_sub(const phpstr *buf, size_t offset, size_t len);
113
114 /* remove a substring */
115 PHPSTR_API size_t phpstr_cut(phpstr *buf, size_t offset, size_t length);
116
117 /* get a complete phpstr duplicate */
118 PHPSTR_API phpstr *phpstr_dup(const phpstr *buf);
119
120 /* merge several phpstr objects
121 use like:
122
123 phpstr *final = phpstr_merge(3,
124 PHPSTR_NOT_FREE(&keep),
125 PHPSTR_ALL_FREE(middle_ptr),
126 PHPSTR_VAL_FREE(&local);
127 */
128 PHPSTR_API phpstr *phpstr_merge(unsigned argc, ...);
129 PHPSTR_API phpstr *phpstr_merge_ex(phpstr *buf, unsigned argc, ...);
130 PHPSTR_API phpstr *phpstr_merge_va(phpstr *buf, unsigned argc, va_list argv);
131
132 /* sets a trailing NUL byte */
133 PHPSTR_API void phpstr_fix(phpstr *buf);
134
135 /* free a phpstr objects contents */
136 PHPSTR_API void phpstr_dtor(phpstr *buf);
137
138 /* free a phpstr object completely */
139 PHPSTR_API void phpstr_free(phpstr *buf);
140
141 #endif
142
143
144 /*
145 * Local variables:
146 * tab-width: 4
147 * c-basic-offset: 4
148 * End:
149 * vim600: sw=4 ts=4 fdm=marker
150 * vim<600: sw=4 ts=4
151 */