cvs: php4 /ext/standard file.c

From: Date: Thu, 23 May 2002 07:03:44 +0000
Subject: cvs: php4 /ext/standard file.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-12056@lists.php.net to get a copy of this message
yohgaki Thu May 23 03:03:44 2002 EDT Modified files: /php4/ext/standard file.c Log: Added 4th parameter to specify enclosure character. Patch by Dean Richard Benson <dean@vipersoft.co.uk> Spit more meaningful error messages when delim and/or enclosure char is null. Index: php4/ext/standard/file.c diff -u php4/ext/standard/file.c:1.229 php4/ext/standard/file.c:1.230 --- php4/ext/standard/file.c:1.229 Sun May 12 11:59:42 2002 +++ php4/ext/standard/file.c Thu May 23 03:03:43 2002 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.229 2002/05/12 15:59:42 rasmus Exp $ */ +/* $Id: file.c,v 1.230 2002/05/23 07:03:43 yohgaki Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -1924,16 +1924,17 @@ } /* }}} */ -/* {{{ proto array fgetcsv(resource fp, int length [, string delimiter]) +/* {{{ proto array fgetcsv(resource fp, int length [, string delimiter[, string enclosure]]) Get line from file pointer and parse for CSV fields */ PHP_FUNCTION(fgetcsv) { char *temp, *tptr, *bptr, *lineEnd; char delimiter = ','; /* allow this to be set as parameter */ + char enclosure = '"'; /* allow this to be set as parameter */ /* first section exactly as php_fgetss */ - zval **fd, **bytes, **p_delim; + zval **fd, **bytes, **p_delim, **p_enclosure; int len, type; char *buf; php_stream *stream; @@ -1952,10 +1953,38 @@ convert_to_string_ex(p_delim); /* Make sure that there is at least one character in string */ if (Z_STRLEN_PP(p_delim) < 1) { + php_error(E_WARNING, "%s() 3rd paramater must be a character", + get_active_function_name(TSRMLS_C)); + return; + } + /* use first character from string */ + delimiter = Z_STRVAL_PP(p_delim)[0]; + break; + + case 4: + if (zend_get_parameters_ex(4, &fd, &bytes, &p_delim, &p_enclosure) == FAILURE) { WRONG_PARAM_COUNT; } - /* use first character from string */ + convert_to_string_ex(p_delim); + /* Make sure that there is at least one character in string */ + if (Z_STRLEN_PP(p_delim) < 1) { + php_error(E_WARNING, "%s() 3rd paramater must be a character", + get_active_function_name(TSRMLS_C)); + return; + } + /* use first character from string */ delimiter = Z_STRVAL_PP(p_delim)[0]; + + convert_to_string_ex(p_enclosure); + /* Make sure that there is at least one character in string */ + if (Z_STRLEN_PP(p_enclosure) < 1) { + php_error(E_WARNING, "%s() 3rd paramater must be a character", + get_active_function_name(TSRMLS_C)); + return; + } + /* use first character from string */ + enclosure = Z_STRVAL_PP(p_enclosure)[0]; + break; default: @@ -1983,9 +2012,9 @@ RETURN_FALSE; } - /* Now into new section that parses buf for comma/quote delimited fields */ + /* Now into new section that parses buf for delimiter/enclosure fields */ - /* Strip trailing space from buf, saving end of line in case required for quoted field */ + /* Strip trailing space from buf, saving end of line in case required for enclosure field */ lineEnd = emalloc(len + 1); bptr = buf; @@ -2013,14 +2042,14 @@ /* 1. Strip any leading space */ while(isspace((int) *bptr) && (*bptr!=delimiter)) bptr++; /* 2. Read field, leaving bptr pointing at start of next field */ - if (*bptr == '"') { - /* 2A. handle quote delimited field */ + if (*bptr == enclosure) { + /* 2A. handle enclosure delimited field */ bptr++; /* move on to first character in field */ while (*bptr) { - if (*bptr == '"') { - /* handle the double-quote */ + if (*bptr == enclosure) { + /* handle the enclosure */ if ( *(bptr+1) == '"') { - /* embedded double quotes */ + /* embedded enclosure */ *tptr++ = *bptr; bptr +=2; } else { /* must be end of string - skip to start of next field or end */ @@ -2064,7 +2093,7 @@ } } } else { - /* 2B. Handle non-quoted field */ + /* 2B. Handle non-enclosure field */ while ((*bptr != delimiter) && *bptr) *tptr++ = *bptr++; *tptr=0; /* terminate temporary string */

« previous php.cvs (#12056) next »