wez Thu Jan 9 12:29:31 2003 EDT
Modified files:
/php4/ext/standard basic_functions.c basic_functions.h
formatted_print.c
/php4/tests/strings 002.phpt
Log:
Implement fprintf() and vfprintf().
Add a couple of tests.
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.556
php4/ext/standard/basic_functions.c:1.557
--- php4/ext/standard/basic_functions.c:1.556 Tue Jan 7 06:37:09 2003
+++ php4/ext/standard/basic_functions.c Thu Jan 9 12:29:31 2003
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.556 2003/01/07 11:37:09 zeev Exp $ */
+/* $Id: basic_functions.c,v 1.557 2003/01/09 17:29:31 wez Exp $ */
#include "php.h"
#include "php_streams.h"
@@ -387,6 +387,8 @@
PHP_NAMED_FE(printf, PHP_FN(user_printf),
NULL)
PHP_FE(vprintf,
NULL)
PHP_FE(vsprintf,
NULL)
+ PHP_FE(fprintf,
+ NULL)
+ PHP_FE(vfprintf,
+ 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/basic_functions.h
diff -u php4/ext/standard/basic_functions.h:1.112
php4/ext/standard/basic_functions.h:1.113
--- php4/ext/standard/basic_functions.h:1.112 Sat Jan 4 22:24:38 2003
+++ php4/ext/standard/basic_functions.h Thu Jan 9 12:29:31 2003
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.h,v 1.112 2003/01/05 03:24:38 pollita Exp $ */
+/* $Id: basic_functions.h,v 1.113 2003/01/09 17:29:31 wez Exp $ */
#ifndef BASIC_FUNCTIONS_H
#define BASIC_FUNCTIONS_H
@@ -83,6 +83,8 @@
PHP_FUNCTION(restore_include_path);
PHP_FUNCTION(print_r);
+PHP_FUNCTION(fprintf);
+PHP_FUNCTION(vfprintf);
PHP_FUNCTION(connection_aborted);
PHP_FUNCTION(connection_status);
Index: php4/ext/standard/formatted_print.c
diff -u php4/ext/standard/formatted_print.c:1.60
php4/ext/standard/formatted_print.c:1.61
--- php4/ext/standard/formatted_print.c:1.60 Tue Dec 31 11:07:41 2002
+++ php4/ext/standard/formatted_print.c Thu Jan 9 12:29:31 2003
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: formatted_print.c,v 1.60 2002/12/31 16:07:41 sebastian Exp $ */
+/* $Id: formatted_print.c,v 1.61 2003/01/09 17:29:31 wez Exp $ */
#include <math.h> /* modf() */
#include "php.h"
@@ -452,9 +452,9 @@
*
*/
static char *
-php_formatted_print(int ht, int *len, int use_array TSRMLS_DC)
+php_formatted_print(int ht, int *len, int use_array, int format_offset TSRMLS_DC)
{
- zval ***args, **z_format, **array;
+ zval ***args, **z_format;
int argc, size = 240, inpos = 0, outpos = 0, temppos;
int alignment, width, precision, currarg, adjusting, argnum;
char *format, *result, padding;
@@ -462,39 +462,49 @@
argc = ZEND_NUM_ARGS();
+ /* verify the number of args */
+ if ((use_array && argc != (2 + format_offset))
+ || (!use_array && argc < (1 + format_offset))) {
+ 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 (use_array) {
int i = 1;
+ zval ***newargs;
+ zval **array;
- if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(argc, &z_format,
&array) == FAILURE) {
- WRONG_PARAM_COUNT_WITH_RETVAL(NULL);
- }
+ z_format = args[format_offset];
+ array = args[1 + format_offset];
+
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;
+ newargs = (zval ***)emalloc(argc * sizeof(zval *));
+ newargs[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_get_current_data(Z_ARRVAL_PP(array), (void
+**)&newargs[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);
- }
+ efree(args);
+ args = newargs;
+ format_offset = 0;
}
- convert_to_string_ex(args[0]);
- format = Z_STRVAL_PP(args[0]);
+
+ convert_to_string_ex(args[format_offset]);
+ format = Z_STRVAL_PP(args[format_offset]);
result = emalloc(size);
currarg = 1;
- while (inpos<Z_STRLEN_PP(args[0])) {
+ while (inpos<Z_STRLEN_PP(args[format_offset])) {
int expprec = 0;
PRINTF_DEBUG(("sprintf: format[%d]='%c'\n", inpos, format[inpos]));
@@ -538,6 +548,9 @@
} else {
argnum = currarg++;
}
+
+ argnum += format_offset;
+
if (argnum >= argc) {
efree(result);
efree(args);
@@ -596,7 +609,7 @@
PRINTF_DEBUG(("sprintf: precision=%d\n", precision));
} else {
width = precision = 0;
- argnum = currarg++;
+ argnum = currarg++ + format_offset;
}
if (format[inpos] == 'l') {
@@ -708,11 +721,10 @@
char *result;
int len;
- if ((result=php_formatted_print(ht, &len, 0 TSRMLS_CC))==NULL) {
+ if ((result=php_formatted_print(ht, &len, 0, 0 TSRMLS_CC))==NULL) {
RETURN_FALSE;
}
- RETVAL_STRINGL(result, len, 1);
- efree(result);
+ RETVAL_STRINGL(result, len, 0);
}
/* }}} */
@@ -723,11 +735,10 @@
char *result;
int len;
- if ((result=php_formatted_print(ht, &len, 1 TSRMLS_CC))==NULL) {
+ if ((result=php_formatted_print(ht, &len, 1, 0 TSRMLS_CC))==NULL) {
RETURN_FALSE;
}
- RETVAL_STRINGL(result, len, 1);
- efree(result);
+ RETVAL_STRINGL(result, len, 0);
}
/* }}} */
@@ -738,7 +749,7 @@
char *result;
int len;
- if ((result=php_formatted_print(ht, &len, 0 TSRMLS_CC))==NULL) {
+ if ((result=php_formatted_print(ht, &len, 0, 0 TSRMLS_CC))==NULL) {
RETURN_FALSE;
}
PHPWRITE(result, len);
@@ -753,13 +764,75 @@
char *result;
int len;
- if ((result=php_formatted_print(ht, &len, 1 TSRMLS_CC))==NULL) {
+ if ((result=php_formatted_print(ht, &len, 1, 0 TSRMLS_CC))==NULL) {
RETURN_FALSE;
}
PHPWRITE(result, len);
efree(result);
}
/* }}} */
+
+/* {{{ proto int fprintf(resource stream, string format [, mixed arg1 [, mixed ...]])
+ Output a formatted string into a stream */
+PHP_FUNCTION(fprintf)
+{
+ php_stream *stream;
+ zval **arg1;
+ char *result;
+ int len;
+
+ if (ZEND_NUM_ARGS() < 2) {
+ WRONG_PARAM_COUNT;
+ }
+
+ if (zend_get_parameters_ex(1, &arg1)==FAILURE) {
+ RETURN_FALSE;
+ }
+
+ php_stream_from_zval(stream, arg1);
+
+ if ((result=php_formatted_print(ht, &len, 0, 1 TSRMLS_CC))==NULL) {
+ RETURN_FALSE;
+ }
+
+ php_stream_write(stream, result, len);
+
+ efree(result);
+
+ RETVAL_LONG(len - 1);
+}
+
+/* {{{ proto int vfprintf(resource stream, string format, array args)
+ Output a formatted string into a stream */
+PHP_FUNCTION(vfprintf)
+{
+ php_stream *stream;
+ zval **arg1;
+ char *result;
+ int len;
+
+ if (ZEND_NUM_ARGS() != 3) {
+ WRONG_PARAM_COUNT;
+ }
+
+ if (zend_get_parameters_ex(1, &arg1)==FAILURE) {
+ RETURN_FALSE;
+ }
+
+ php_stream_from_zval(stream, arg1);
+
+ if ((result=php_formatted_print(ht, &len, 1, 1 TSRMLS_CC))==NULL) {
+ RETURN_FALSE;
+ }
+
+ php_stream_write(stream, result, len);
+
+ efree(result);
+
+ RETVAL_LONG(len - 1);
+}
+
+
/*
* Local variables:
Index: php4/tests/strings/002.phpt
diff -u php4/tests/strings/002.phpt:1.4 php4/tests/strings/002.phpt:1.5
--- php4/tests/strings/002.phpt:1.4 Fri Mar 22 04:09:36 2002
+++ php4/tests/strings/002.phpt Thu Jan 9 12:29:31 2003
@@ -39,6 +39,13 @@
printf("printf test 28:%2\$02d %1\$2d\n", 1, 2);
printf("printf test 29:%2\$-2d %1\$2d\n", 1, 2);
print("printf test 30:"); printf("%0\$s", 1); print("x\n");
+vprintf("vprintf test 1:%2\$-2d %1\$2d\n", array(1, 2));
+
+$fp = fopen("php://stdout", "w") or die("Arrggsgg!!");
+$x = fprintf($fp, "fprintf test 1:%.5s\n", "abcdefghij");
+var_dump($x);
+fclose($fp);
+
?>
--EXPECT--
@@ -74,3 +81,6 @@
printf test 28:02 1
printf test 29:2 1
printf test 30:x
+vprintf test 1:2 1
+fprintf test 1:abcde
+int(20)
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php