cvs: php4 /ext/standard basic_functions.c php_var.h var.c

From: Date: Sat, 08 Dec 2001 23:44:34 +0000
Subject: cvs: php4 /ext/standard basic_functions.c php_var.h var.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-8524@lists.php.net to get a copy of this message
derick Sat Dec 8 18:44:34 2001 EDT Modified files: /php4/ext/standard var.c basic_functions.c php_var.h Log: - Added var_export, which shows a representation of a variable, much like var_dump, but in such a way you can use it as PHP code. @- Added var_export, which shows a representation of a variable, much like @ var_dump, but in such a way you can use it as PHP code. (Derick) Index: php4/ext/standard/var.c diff -u php4/ext/standard/var.c:1.120 php4/ext/standard/var.c:1.121 --- php4/ext/standard/var.c:1.120 Sat Dec 1 10:17:50 2001 +++ php4/ext/standard/var.c Sat Dec 8 18:44:34 2001 @@ -136,6 +136,101 @@ /* }}} */ +/* {{{ php_var_export */ + +static int php_array_element_export(zval **zv, int num_args, va_list args, zend_hash_key *hash_key) +{ + int level; + TSRMLS_FETCH(); + + level = va_arg(args, int); + + if (hash_key->nKeyLength==0) { /* numeric key */ + php_printf("%*c%ld => ", level + 1, ' ', hash_key->h); + } else { /* string key */ + php_printf("%*c'%s' => ", level + 1, ' ', hash_key->arKey); + } + php_var_export(zv, level + 2 TSRMLS_CC); + PUTS (",\n"); + return 0; +} + +void php_var_export(zval **struc, int level TSRMLS_DC) +{ + HashTable *myht; +/* + if (level > 1) { + php_printf("%*c", level - 1, ' '); + } +*/ + switch (Z_TYPE_PP(struc)) { + case IS_BOOL: + php_printf("%s", Z_LVAL_PP(struc) ? "true" : "false"); + break; + case IS_NULL: + php_printf("NULL"); + break; + case IS_LONG: + php_printf("%ld", Z_LVAL_PP(struc)); + break; + case IS_DOUBLE: + php_printf("%.*G", (int) EG(precision), Z_DVAL_PP(struc)); + break; + case IS_STRING: + PUTS ("'"); + PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc)); + PUTS ("'"); + break; + case IS_ARRAY: + myht = Z_ARRVAL_PP(struc); + goto head_done; + case IS_OBJECT: + myht = Z_OBJPROP_PP(struc); +head_done: + if (level > 1) { + php_printf("\n%*c", level - 1, ' '); + } + PUTS ("array (\n"); + zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_array_element_export, 1, level); + if (level > 1) { + php_printf("%*c", level-1, ' '); + } + PUTS(")"); + break; + default: + PUTS ("NULL"); + break; + } +} + +/* }}} */ + + +/* {{{ proto void var_export(mixed var) + Dumps a string representation of variable to output */ +PHP_FUNCTION(var_export) +{ + zval ***args; + int argc; + int i; + + argc = ZEND_NUM_ARGS(); + + args = (zval ***)emalloc(argc * sizeof(zval **)); + if (ZEND_NUM_ARGS() == 0 || zend_get_parameters_array_ex(argc, args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT; + } + + for (i=0; i<argc; i++) + php_var_export(args[i], 1 TSRMLS_CC); + + efree(args); +} +/* }}} */ + + + /* {{{ php_var_serialize */ static void php_var_serialize_intern(smart_str *buf, zval **struc, HashTable *var_hash TSRMLS_DC); Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.428 php4/ext/standard/basic_functions.c:1.429 --- php4/ext/standard/basic_functions.c:1.428 Fri Dec 7 08:20:18 2001 +++ php4/ext/standard/basic_functions.c Sat Dec 8 18:44:34 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.428 2001/12/07 13:20:18 sterling Exp $ */ +/* $Id: basic_functions.c,v 1.429 2001/12/08 23:44:34 derick Exp $ */ #include "php.h" #include "php_main.h" @@ -533,6 +533,7 @@ PHP_FE(unserialize, NULL) PHP_FE(var_dump, NULL) + PHP_FE(var_export, NULL) PHP_FE(print_r, NULL) PHP_FE(register_shutdown_function, NULL) Index: php4/ext/standard/php_var.h diff -u php4/ext/standard/php_var.h:1.15 php4/ext/standard/php_var.h:1.16 --- php4/ext/standard/php_var.h:1.15 Sat Nov 10 16:18:34 2001 +++ php4/ext/standard/php_var.h Sat Dec 8 18:44:34 2001 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_var.h,v 1.15 2001/11/10 21:18:34 sas Exp $ */ +/* $Id: php_var.h,v 1.16 2001/12/08 23:44:34 derick Exp $ */ #ifndef PHP_VAR_H #define PHP_VAR_H @@ -24,10 +24,12 @@ #include "ext/standard/php_smart_str_public.h" PHP_FUNCTION(var_dump); +PHP_FUNCTION(var_export); PHP_FUNCTION(serialize); PHP_FUNCTION(unserialize); void php_var_dump(zval **struc, int level TSRMLS_DC); +void php_var_export(zval **struc, int level TSRMLS_DC); /* typdef HashTable php_serialize_data_t; */ #define php_serialize_data_t HashTable

« previous php.cvs (#8524) next »