-
Notifications
You must be signed in to change notification settings - Fork 7.9k
fputcsv improvements to allow RFC 4180 compliance #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
49f1ef4
1c3c343
86ca8a9
eaee0c7
94d655e
6d91cd7
ed8c008
6fcbc1d
dff75cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -818,7 +818,7 @@ PHP_FUNCTION(tempnam) | |
if (p_len > 64) { | ||
p[63] = '\0'; | ||
} | ||
|
||
RETVAL_FALSE; | ||
|
||
if ((fd = php_open_temporary_fd_ex(dir, p, &opened_path, 1 TSRMLS_CC)) >= 0) { | ||
|
@@ -1380,13 +1380,13 @@ PHP_FUNCTION(umask) | |
{ | ||
long arg1 = 0; | ||
int oldumask; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @theoreticaLee cleaned up stray tab |
||
oldumask = umask(077); | ||
|
||
if (BG(umask) == -1) { | ||
BG(umask) = oldumask; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @theoreticaLee cleaned up stray tab |
||
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &arg1) == FAILURE) { | ||
RETURN_FALSE; | ||
} | ||
|
@@ -1799,22 +1799,23 @@ static const char *php_fgetcsv_lookup_trailing_spaces(const char *ptr, size_t le | |
|
||
#define FPUTCSV_FLD_CHK(c) memchr(Z_STRVAL(field), c, Z_STRLEN(field)) | ||
|
||
/* {{{ proto int fputcsv(resource fp, array fields [, string delimiter [, string enclosure]]) | ||
/* {{{ proto int fputcsv(resource fp, array fields [, string delimiter [, string enclosure [, string escape_char]]]) | ||
Format line as CSV and write to file pointer */ | ||
PHP_FUNCTION(fputcsv) | ||
{ | ||
char delimiter = ','; /* allow this to be set as parameter */ | ||
char enclosure = '"'; /* allow this to be set as parameter */ | ||
const char escape_char = '\\'; | ||
char delimiter = ','; /* allow this to be set as parameter */ | ||
char enclosure = '"'; /* allow this to be set as parameter */ | ||
char escape_char = '\\'; /* allow this to be set as parameter */ | ||
php_stream *stream; | ||
zval *fp = NULL, *fields = NULL; | ||
int ret; | ||
char *delimiter_str = NULL, *enclosure_str = NULL; | ||
int delimiter_str_len = 0, enclosure_str_len = 0; | ||
char *delimiter_str = NULL, *enclosure_str = NULL, *escape_str = NULL; | ||
int delimiter_str_len = 0, enclosure_str_len = 0, escape_str_len = 0; | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra|ss", | ||
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra|sss", | ||
&fp, &fields, &delimiter_str, &delimiter_str_len, | ||
&enclosure_str, &enclosure_str_len) == FAILURE) { | ||
&enclosure_str, &enclosure_str_len, | ||
&escape_str, &escape_str_len) == FAILURE) { | ||
return; | ||
} | ||
|
||
|
@@ -1842,6 +1843,17 @@ PHP_FUNCTION(fputcsv) | |
enclosure = *enclosure_str; | ||
} | ||
|
||
if (escape_str != NULL) { | ||
if (escape_str_len < 1) { | ||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "escape must be a character"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Message should be "Escape string must be a character" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and line 1848 are fine — the messages are consistent with fgetcsv() and the other warnings in the function. |
||
RETURN_FALSE; | ||
} else if (escape_str_len > 1) { | ||
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "escape must be a single character"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be "Escape string must be a single character" |
||
} | ||
/* use first character from string */ | ||
escape_char = *escape_str; | ||
} | ||
|
||
PHP_STREAM_TO_ZVAL(stream, &fp); | ||
|
||
ret = php_fputcsv(stream, fields, delimiter, enclosure, escape_char TSRMLS_CC); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
--TEST-- | ||
various fputcsv() functionality tests | ||
--CREDITS-- | ||
Lee Leathers <[email protected]> | ||
--FILE-- | ||
<?php | ||
|
||
$list = array ( | ||
0 => 'aaa,bbb', | ||
1 => 'aaa,"bbb"', | ||
2 => '"aaa","bbb"', | ||
3 => 'aaa,bbb', | ||
4 => '"aaa",bbb', | ||
5 => '"aaa", "bbb"', | ||
6 => ',', | ||
7 => 'aaa,', | ||
8 => ',"aaa"', | ||
9 => '"",""', | ||
10 => '"""""",', | ||
11 => '""""",aaa', | ||
12 => 'aaa,bbb ', | ||
13 => 'aaa,"bbb "', | ||
14 => 'aaa"aaa","bbb"bbb', | ||
15 => 'aaa"aaa""",bbb', | ||
16 => 'aaa,"/"bbb,ccc', | ||
17 => 'aaa"/"a","bbb"', | ||
18 => '"/"","aaa"', | ||
19 => '"/""",aaa', | ||
); | ||
|
||
$file = dirname(__FILE__) . 'fgetcsv.csv'; | ||
@unlink($file); | ||
|
||
$fp = fopen($file, "w"); | ||
foreach ($list as $v) { | ||
fputcsv($fp, explode(',', $v), ',', '"', '/'); | ||
} | ||
fclose($fp); | ||
|
||
$res = file($file); | ||
foreach($res as &$val) | ||
{ | ||
$val = substr($val, 0, -1); | ||
} | ||
echo '$list = ';var_export($res);echo ";\n"; | ||
|
||
$fp = fopen($file, "r"); | ||
$res = array(); | ||
while($l=fgetcsv($fp, 0, ',', '"', '/')) | ||
{ | ||
$res[] = join(',',$l); | ||
} | ||
fclose($fp); | ||
|
||
echo '$list = ';var_export($res);echo ";\n"; | ||
|
||
@unlink($file); | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
$list = array ( | ||
0 => 'aaa,bbb', | ||
1 => 'aaa,"""bbb"""', | ||
2 => '"""aaa""","""bbb"""', | ||
3 => 'aaa,bbb', | ||
4 => '"""aaa""",bbb', | ||
5 => '"""aaa"""," ""bbb"""', | ||
6 => ',', | ||
7 => 'aaa,', | ||
8 => ',"""aaa"""', | ||
9 => '"""""",""""""', | ||
10 => '"""""""""""""",', | ||
11 => '"""""""""""",aaa', | ||
12 => 'aaa,"bbb "', | ||
13 => 'aaa,"""bbb """', | ||
14 => '"aaa""aaa""","""bbb""bbb"', | ||
15 => '"aaa""aaa""""""",bbb', | ||
16 => 'aaa,"""/"bbb",ccc', | ||
17 => '"aaa""/"a""","""bbb"""', | ||
18 => '"""/"""","""aaa"""', | ||
19 => '"""/"""""",aaa', | ||
); | ||
$list = array ( | ||
0 => 'aaa,bbb', | ||
1 => 'aaa,"bbb"', | ||
2 => '"aaa","bbb"', | ||
3 => 'aaa,bbb', | ||
4 => '"aaa",bbb', | ||
5 => '"aaa", "bbb"', | ||
6 => ',', | ||
7 => 'aaa,', | ||
8 => ',"aaa"', | ||
9 => '"",""', | ||
10 => '"""""",', | ||
11 => '""""",aaa', | ||
12 => 'aaa,bbb ', | ||
13 => 'aaa,"bbb "', | ||
14 => 'aaa"aaa","bbb"bbb', | ||
15 => 'aaa"aaa""",bbb', | ||
16 => 'aaa,"/"bbb,ccc', | ||
17 => 'aaa"/"a","bbb"', | ||
18 => '"/"","aaa"', | ||
19 => '"/""",aaa', | ||
); | ||
===DONE=== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@theoreticaLee cleaned up stray tab