@@ -1887,7 +1887,7 @@ static zend_result spl_filesystem_object_cast(zend_object *readobj, zval *writeo
1887
1887
}
1888
1888
/* }}} */
1889
1889
1890
- static zend_result spl_filesystem_file_read_ex (spl_filesystem_object * intern , bool silent , zend_long line_add ) /* {{{ */
1890
+ static zend_result spl_filesystem_file_read_ex (spl_filesystem_object * intern , bool silent , zend_long line_add , bool csv ) /* {{{ */
1891
1891
{
1892
1892
char * buf ;
1893
1893
size_t line_len = 0 ;
@@ -1917,7 +1917,7 @@ static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bo
1917
1917
intern -> u .file .current_line = estrdup ("" );
1918
1918
intern -> u .file .current_line_len = 0 ;
1919
1919
} else {
1920
- if (SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_DROP_NEW_LINE )) {
1920
+ if (! csv && SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_DROP_NEW_LINE )) {
1921
1921
if (line_len > 0 && buf [line_len - 1 ] == '\n' ) {
1922
1922
line_len -- ;
1923
1923
if (line_len > 0 && buf [line_len - 1 ] == '\r' ) {
@@ -1935,20 +1935,30 @@ static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bo
1935
1935
return SUCCESS ;
1936
1936
} /* }}} */
1937
1937
1938
- static inline zend_result spl_filesystem_file_read (spl_filesystem_object * intern , bool silent )
1938
+ static inline zend_result spl_filesystem_file_read (spl_filesystem_object * intern , bool silent , bool csv )
1939
1939
{
1940
1940
zend_long line_add = (intern -> u .file .current_line ) ? 1 : 0 ;
1941
- return spl_filesystem_file_read_ex (intern , silent , line_add );
1941
+ return spl_filesystem_file_read_ex (intern , silent , line_add , csv );
1942
+ }
1943
+
1944
+ static bool is_line_empty (spl_filesystem_object * intern )
1945
+ {
1946
+ char * current_line = intern -> u .file .current_line ;
1947
+ size_t current_line_len = intern -> u .file .current_line_len ;
1948
+ return current_line_len == 0
1949
+ || ((SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_READ_CSV ) && SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_DROP_NEW_LINE )
1950
+ && ((current_line_len == 1 && current_line [0 ] == '\n' )
1951
+ || (current_line_len == 2 && current_line [0 ] == '\r' && current_line [1 ] == '\n' ))));
1942
1952
}
1943
1953
1944
1954
static zend_result spl_filesystem_file_read_csv (spl_filesystem_object * intern , char delimiter , char enclosure , int escape , zval * return_value ) /* {{{ */
1945
1955
{
1946
1956
do {
1947
- zend_result ret = spl_filesystem_file_read (intern , /* silent */ true);
1957
+ zend_result ret = spl_filesystem_file_read (intern , /* silent */ true, /* csv */ true );
1948
1958
if (ret != SUCCESS ) {
1949
1959
return ret ;
1950
1960
}
1951
- } while (! intern -> u . file . current_line_len && SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_SKIP_EMPTY ));
1961
+ } while (is_line_empty ( intern ) && SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_SKIP_EMPTY ));
1952
1962
1953
1963
size_t buf_len = intern -> u .file .current_line_len ;
1954
1964
char * buf = estrndup (intern -> u .file .current_line , buf_len );
@@ -2006,7 +2016,7 @@ static zend_result spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesys
2006
2016
zval_ptr_dtor (& retval );
2007
2017
return SUCCESS ;
2008
2018
} else {
2009
- return spl_filesystem_file_read (intern , /* silent */ true);
2019
+ return spl_filesystem_file_read (intern , /* silent */ true, /* csv */ false );
2010
2020
}
2011
2021
} /* }}} */
2012
2022
@@ -2214,7 +2224,7 @@ PHP_METHOD(SplFileObject, fgets)
2214
2224
2215
2225
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED (intern );
2216
2226
2217
- if (spl_filesystem_file_read_ex (intern , /* silent */ false, /* line_add */ 1 ) == FAILURE ) {
2227
+ if (spl_filesystem_file_read_ex (intern , /* silent */ false, /* line_add */ 1 , /* csv */ false ) == FAILURE ) {
2218
2228
RETURN_THROWS ();
2219
2229
}
2220
2230
RETURN_STRINGL (intern -> u .file .current_line , intern -> u .file .current_line_len );
@@ -2644,7 +2654,7 @@ PHP_METHOD(SplFileObject, fscanf)
2644
2654
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED (intern );
2645
2655
2646
2656
/* Get next line */
2647
- if (spl_filesystem_file_read (intern , /* silent */ false) == FAILURE ) {
2657
+ if (spl_filesystem_file_read (intern , /* silent */ false, /* csv */ false ) == FAILURE ) {
2648
2658
RETURN_THROWS ();
2649
2659
}
2650
2660
0 commit comments