Skip to content

Commit 951a5fc

Browse files
committed
MFB: new parameter-parsing API
1 parent f1f97ac commit 951a5fc

File tree

1 file changed

+99
-59
lines changed

1 file changed

+99
-59
lines changed

ext/dba/dba.c

Lines changed: 99 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -207,33 +207,28 @@ ZEND_GET_MODULE(dba)
207207
/* {{{ macromania */
208208

209209
#define DBA_ID_PARS \
210-
zval **id; \
210+
zval *id; \
211211
dba_info *info = NULL; \
212212
int ac = ZEND_NUM_ARGS()
213213

214214
/* these are used to get the standard arguments */
215215

216-
#define DBA_GET1 \
217-
if(ac != 1 || zend_get_parameters_ex(ac, &id) != SUCCESS) { \
218-
WRONG_PARAM_COUNT; \
219-
}
220-
221216
/* {{{ php_dba_myke_key */
222-
static size_t php_dba_make_key(zval **key, char **key_str, char **key_free TSRMLS_DC)
217+
static size_t php_dba_make_key(zval *key, char **key_str, char **key_free TSRMLS_DC)
223218
{
224-
if (Z_TYPE_PP(key) == IS_ARRAY) {
219+
if (Z_TYPE_P(key) == IS_ARRAY) {
225220
zval **group, **name;
226221
HashPosition pos;
227222
size_t len;
228223

229-
if (zend_hash_num_elements(Z_ARRVAL_PP(key)) != 2) {
224+
if (zend_hash_num_elements(Z_ARRVAL_P(key)) != 2) {
230225
php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Key does not have exactly two elements: (key, name)");
231226
return -1;
232227
}
233-
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(key), &pos);
234-
zend_hash_get_current_data_ex(Z_ARRVAL_PP(key), (void **) &group, &pos);
235-
zend_hash_move_forward_ex(Z_ARRVAL_PP(key), &pos);
236-
zend_hash_get_current_data_ex(Z_ARRVAL_PP(key), (void **) &name, &pos);
228+
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(key), &pos);
229+
zend_hash_get_current_data_ex(Z_ARRVAL_P(key), (void **) &group, &pos);
230+
zend_hash_move_forward_ex(Z_ARRVAL_P(key), &pos);
231+
zend_hash_get_current_data_ex(Z_ARRVAL_P(key), (void **) &name, &pos);
237232
convert_to_string_ex(group);
238233
convert_to_string_ex(name);
239234
if (Z_STRLEN_PP(group) == 0) {
@@ -245,43 +240,42 @@ static size_t php_dba_make_key(zval **key, char **key_str, char **key_free TSRML
245240
*key_free = *key_str;
246241
return len;
247242
} else {
248-
convert_to_string_ex(key);
249-
*key_str = Z_STRVAL_PP(key);
250243
*key_free = NULL;
251-
return Z_STRLEN_PP(key);
244+
245+
convert_to_string(key);
246+
*key_str = Z_STRVAL_P(key);
247+
248+
return Z_STRLEN_P(key);
252249
}
253250
}
254251
/* }}} */
255252

256253
#define DBA_GET2 \
257-
zval **key; \
254+
zval *key;\
258255
char *key_str, *key_free; \
259256
size_t key_len; \
260-
if(ac != 2 || zend_get_parameters_ex(ac, &key, &id) != SUCCESS) { \
261-
WRONG_PARAM_COUNT; \
257+
if (zend_parse_parameters(ac TSRMLS_CC, "zr", &key, &id) == FAILURE) { \
258+
return; \
262259
} \
263260
if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {\
264261
RETURN_FALSE; \
265262
}
266263

267264
#define DBA_GET2_3\
268-
zval **key; \
265+
zval *key;\
269266
char *key_str, *key_free; \
270267
size_t key_len; \
271-
zval **tmp; \
272268
int skip = 0; \
273269
switch(ac) { \
274270
case 2: \
275-
if (zend_get_parameters_ex(ac, &key, &id) != SUCCESS) { \
276-
WRONG_PARAM_COUNT; \
271+
if (zend_parse_parameters(ac TSRMLS_CC, "zr", &key, &id) == FAILURE) { \
272+
return;\
277273
} \
278274
break; \
279275
case 3: \
280-
if (zend_get_parameters_ex(ac, &key, &tmp, &id) != SUCCESS) { \
281-
WRONG_PARAM_COUNT; \
276+
if (zend_parse_parameters(ac TSRMLS_CC, "zlr", &key, &skip, &id) == FAILURE) { \
277+
return;\
282278
} \
283-
convert_to_long_ex(tmp); \
284-
skip = Z_LVAL_PP(tmp); \
285279
break; \
286280
default: \
287281
WRONG_PARAM_COUNT; \
@@ -290,25 +284,12 @@ static size_t php_dba_make_key(zval **key, char **key_str, char **key_free TSRML
290284
RETURN_FALSE; \
291285
}
292286

293-
#define DBA_GET3 \
294-
zval **key, **val; \
295-
char *key_str, *key_free; \
296-
size_t key_len; \
297-
if(ac != 3 || zend_get_parameters_ex(ac, &key, &val, &id) != SUCCESS) { \
298-
WRONG_PARAM_COUNT; \
299-
} \
300-
convert_to_string_ex(val); \
301-
if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {\
302-
RETURN_FALSE; \
303-
}
304287

305-
#define DBA_ID_GET \
288+
#define DBA_FETCH_RESOURCE(info, id)\
306289
ZEND_FETCH_RESOURCE2(info, dba_info *, id, -1, "DBA identifier", le_db, le_pdb);
307-
308-
#define DBA_ID_GET1 DBA_ID_PARS; DBA_GET1; DBA_ID_GET
309-
#define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_ID_GET
310-
#define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_ID_GET
311-
#define DBA_ID_GET3 DBA_ID_PARS; DBA_GET3; DBA_ID_GET
290+
291+
#define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_FETCH_RESOURCE(info, &id)
292+
#define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_FETCH_RESOURCE(info, &id)
312293

313294
#define DBA_ID_DONE\
314295
if (key_free) efree(key_free)
@@ -565,11 +546,28 @@ PHP_MINFO_FUNCTION(dba)
565546
*/
566547
static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
567548
{
568-
DBA_ID_GET3;
549+
int val_len;
550+
zval *id;
551+
dba_info *info = NULL;
552+
int ac = ZEND_NUM_ARGS();
553+
zval *key;
554+
char *val;
555+
char *key_str, *key_free;
556+
size_t key_len;
557+
558+
if (zend_parse_parameters(ac TSRMLS_CC, "zsr", &key, &val, &val_len, &id) == FAILURE) {
559+
return;
560+
}
561+
562+
if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {
563+
RETURN_FALSE;
564+
}
565+
566+
DBA_FETCH_RESOURCE(info, &id);
569567

570568
DBA_WRITE_CHECK;
571569

572-
if(info->hnd->update(info, key_str, key_len, VALLEN(val), mode TSRMLS_CC) == SUCCESS)
570+
if(info->hnd->update(info, key_str, key_len, val, val_len, mode TSRMLS_CC) == SUCCESS)
573571
{
574572
DBA_ID_DONE;
575573
RETURN_TRUE;
@@ -966,9 +964,16 @@ PHP_FUNCTION(dba_open)
966964
Closes database */
967965
PHP_FUNCTION(dba_close)
968966
{
969-
DBA_ID_GET1;
970-
971-
zend_list_delete(Z_RESVAL_PP(id));
967+
zval *id;
968+
dba_info *info = NULL;
969+
970+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
971+
return;
972+
}
973+
974+
DBA_FETCH_RESOURCE(info, &id);
975+
976+
zend_list_delete(Z_RESVAL_P(id));
972977
}
973978
/* }}} */
974979

@@ -1064,11 +1069,20 @@ PHP_FUNCTION(dba_firstkey)
10641069
{
10651070
char *fkey;
10661071
int len;
1067-
DBA_ID_GET1;
1072+
zval *id;
1073+
dba_info *info = NULL;
1074+
1075+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
1076+
return;
1077+
}
1078+
1079+
DBA_FETCH_RESOURCE(info, &id);
10681080

10691081
fkey = info->hnd->firstkey(info, &len TSRMLS_CC);
1070-
if(fkey)
1082+
1083+
if (fkey)
10711084
RETURN_STRINGL(fkey, len, 0);
1085+
10721086
RETURN_FALSE;
10731087
}
10741088
/* }}} */
@@ -1079,11 +1093,20 @@ PHP_FUNCTION(dba_nextkey)
10791093
{
10801094
char *nkey;
10811095
int len;
1082-
DBA_ID_GET1;
1096+
zval *id;
1097+
dba_info *info = NULL;
1098+
1099+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
1100+
return;
1101+
}
1102+
1103+
DBA_FETCH_RESOURCE(info, &id);
10831104

10841105
nkey = info->hnd->nextkey(info, &len TSRMLS_CC);
1085-
if(nkey)
1106+
1107+
if (nkey)
10861108
RETURN_STRINGL(nkey, len, 0);
1109+
10871110
RETURN_FALSE;
10881111
}
10891112
/* }}} */
@@ -1129,12 +1152,21 @@ PHP_FUNCTION(dba_replace)
11291152
Optimizes (e.g. clean up, vacuum) database */
11301153
PHP_FUNCTION(dba_optimize)
11311154
{
1132-
DBA_ID_GET1;
1133-
1155+
zval *id;
1156+
dba_info *info = NULL;
1157+
1158+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
1159+
return;
1160+
}
1161+
1162+
DBA_FETCH_RESOURCE(info, &id);
1163+
11341164
DBA_WRITE_CHECK;
1135-
if(info->hnd->optimize(info TSRMLS_CC) == SUCCESS) {
1165+
1166+
if (info->hnd->optimize(info TSRMLS_CC) == SUCCESS) {
11361167
RETURN_TRUE;
11371168
}
1169+
11381170
RETURN_FALSE;
11391171
}
11401172
/* }}} */
@@ -1143,11 +1175,19 @@ PHP_FUNCTION(dba_optimize)
11431175
Synchronizes database */
11441176
PHP_FUNCTION(dba_sync)
11451177
{
1146-
DBA_ID_GET1;
1147-
1148-
if(info->hnd->sync(info TSRMLS_CC) == SUCCESS) {
1178+
zval *id;
1179+
dba_info *info = NULL;
1180+
1181+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
1182+
return;
1183+
}
1184+
1185+
DBA_FETCH_RESOURCE(info, &id);
1186+
1187+
if (info->hnd->sync(info TSRMLS_CC) == SUCCESS) {
11491188
RETURN_TRUE;
11501189
}
1190+
11511191
RETURN_FALSE;
11521192
}
11531193
/* }}} */

0 commit comments

Comments
 (0)