cvs: php4 /ext/standard basic_functions.c formatted_print.c php_string.h
| From: | Andrei Zmievski | Date: | Tue, 07 Aug 2001 19:44:46 +0000 |
| Subject: | cvs: php4 /ext/standard basic_functions.c formatted_print.c php_string.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-6638@lists.php.net to get a copy of this message | ||
andrei Tue Aug 7 15:44:46 2001 EDT
Modified files:
/php4/ext/standard basic_functions.c formatted_print.c php_string.h
Log:
@- Added vprintf() and vsprintf() functions that allow passing all arguments
@ after format as an array. (Andrei)
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.379 php4/ext/standard/basic_functions.c:1.380
--- php4/ext/standard/basic_functions.c:1.379 Mon Aug 6 09:36:08 2001
+++ php4/ext/standard/basic_functions.c Tue Aug 7 15:44:44 2001
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.379 2001/08/06 13:36:08 thies Exp $ */
+/* $Id: basic_functions.c,v 1.380 2001/08/07 19:44:44 andrei Exp $ */
#include "php.h"
#include "php_main.h"
@@ -208,6 +208,8 @@
PHP_FALIAS(strchr, strstr, NULL)
PHP_NAMED_FE(sprintf, PHP_FN(user_sprintf), NULL)
PHP_NAMED_FE(printf, PHP_FN(user_printf), NULL)
+ PHP_FE(vprintf, NULL)
+ PHP_FE(vsprintf, NULL)
PHP_FE(sscanf, third_and_rest_force_ref)
PHP_FE(fscanf, third_and_rest_force_ref)
PHP_FE(parse_url, NULL)
Index: php4/ext/standard/formatted_print.c
diff -u php4/ext/standard/formatted_print.c:1.38 php4/ext/standard/formatted_print.c:1.39
--- php4/ext/standard/formatted_print.c:1.38 Mon Jul 30 04:24:36 2001
+++ php4/ext/standard/formatted_print.c Tue Aug 7 15:44:45 2001
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: formatted_print.c,v 1.38 2001/07/30 08:24:36 zeev Exp $ */
+/* $Id: formatted_print.c,v 1.39 2001/08/07 19:44:45 andrei Exp $ */
#include <math.h> /* modf() */
#include "php.h"
@@ -408,23 +408,40 @@
*
*/
static char *
-php_formatted_print(int ht, int *len TSRMLS_DC)
+php_formatted_print(int ht, int *len, int use_array TSRMLS_DC)
{
- pval ***args;
+ zval ***args, **z_format, **array;
int argc, size = 240, inpos = 0, outpos = 0, temppos;
int alignment, width, precision, currarg, adjusting, argnum;
char *format, *result, padding;
argc = ZEND_NUM_ARGS();
- if (argc < 1) {
- WRONG_PARAM_COUNT_WITH_RETVAL(NULL);
- }
- args = (pval ***)emalloc(argc * sizeof(pval *));
+ if (use_array) {
+ int i = 1;
+
+ if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(argc, &z_format, &array) == FAILURE) {
+ WRONG_PARAM_COUNT_WITH_RETVAL(NULL);
+ }
+ SEPARATE_ZVAL(array);
+ convert_to_array_ex(array);
+ argc = 1 + zend_hash_num_elements(Z_ARRVAL_PP(array));
+ args = (zval ***)emalloc(argc * sizeof(zval *));
+ args[0] = z_format;
+ for (zend_hash_internal_pointer_reset(Z_ARRVAL_PP(array));
+ zend_hash_get_current_data(Z_ARRVAL_PP(array), (void **)&args[i++]) == SUCCESS;
+ zend_hash_move_forward(Z_ARRVAL_PP(array)));
+ } else {
+ if (argc < 1) {
+ WRONG_PARAM_COUNT_WITH_RETVAL(NULL);
+ }
+
+ args = (zval ***)emalloc(argc * sizeof(zval *));
- if (zend_get_parameters_array_ex(argc, args) == FAILURE) {
- efree(args);
- WRONG_PARAM_COUNT_WITH_RETVAL(NULL);
+ if (zend_get_parameters_array_ex(argc, args) == FAILURE) {
+ efree(args);
+ WRONG_PARAM_COUNT_WITH_RETVAL(NULL);
+ }
}
convert_to_string_ex(args[0]);
format = (*args[0])->value.str.val;
@@ -632,7 +649,22 @@
char *result;
int len;
- if ((result=php_formatted_print(ht, &len TSRMLS_CC))==NULL) {
+ if ((result=php_formatted_print(ht, &len, 0 TSRMLS_CC))==NULL) {
+ RETURN_FALSE;
+ }
+ RETVAL_STRINGL(result,len,1);
+ efree(result);
+}
+/* }}} */
+
+/* {{{ proto string vsprintf(string format, array args)
+ Return a formatted string */
+PHP_FUNCTION(vsprintf)
+{
+ char *result;
+ int len;
+
+ if ((result=php_formatted_print(ht, &len, 1 TSRMLS_CC))==NULL) {
RETURN_FALSE;
}
RETVAL_STRINGL(result,len,1);
@@ -647,7 +679,22 @@
char *result;
int len;
- if ((result=php_formatted_print(ht, &len TSRMLS_CC))==NULL) {
+ if ((result=php_formatted_print(ht, &len, 0 TSRMLS_CC))==NULL) {
+ RETURN_FALSE;
+ }
+ PHPWRITE(result,len);
+ efree(result);
+}
+/* }}} */
+
+/* {{{ proto int printf(string format, array args)
+ Output a formatted string */
+PHP_FUNCTION(vprintf)
+{
+ char *result;
+ int len;
+
+ if ((result=php_formatted_print(ht, &len, 1 TSRMLS_CC))==NULL) {
RETURN_FALSE;
}
PHPWRITE(result,len);
Index: php4/ext/standard/php_string.h
diff -u php4/ext/standard/php_string.h:1.47 php4/ext/standard/php_string.h:1.48
--- php4/ext/standard/php_string.h:1.47 Sun Aug 5 23:50:51 2001
+++ php4/ext/standard/php_string.h Tue Aug 7 15:44:45 2001
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_string.h,v 1.47 2001/08/06 03:50:51 sas Exp $ */
+/* $Id: php_string.h,v 1.48 2001/08/07 19:44:45 andrei Exp $ */
/* Synced with php 3.0 revision 1.43 1999-06-16 [ssb] */
@@ -57,6 +57,8 @@
PHP_FUNCTION(hebrevc);
PHP_FUNCTION(user_sprintf);
PHP_FUNCTION(user_printf);
+PHP_FUNCTION(vprintf);
+PHP_FUNCTION(vsprintf);
PHP_FUNCTION(addcslashes);
PHP_FUNCTION(addslashes);
PHP_FUNCTION(stripcslashes);