@@ -2047,10 +2047,12 @@ static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *i
2047
2047
}
2048
2048
if (-1 == php_stream_rewind (intern -> u .file .stream )) {
2049
2049
zend_throw_exception_ex (spl_ce_RuntimeException , 0 , "Cannot rewind file %s" , ZSTR_VAL (intern -> file_name ));
2050
- } else {
2051
- spl_filesystem_file_free_line (intern );
2052
- intern -> u .file .current_line_num = 0 ;
2050
+ return ;
2053
2051
}
2052
+
2053
+ spl_filesystem_file_free_line (intern );
2054
+ intern -> u .file .current_line_num = 0 ;
2055
+ intern -> u .file .lines_to_catch_up = 0 ;
2054
2056
if (SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_READ_AHEAD )) {
2055
2057
spl_filesystem_file_read_line (this_ptr , intern , /* silent */ true);
2056
2058
}
@@ -2191,6 +2193,8 @@ PHP_METHOD(SplFileObject, fgets)
2191
2193
if (spl_filesystem_file_read_ex (intern , /* silent */ false, /* line_add */ 1 ) == FAILURE ) {
2192
2194
RETURN_THROWS ();
2193
2195
}
2196
+ /* Inform current() that it needs to catch up a line */
2197
+ intern -> u .file .lines_to_catch_up ++ ;
2194
2198
RETURN_STRINGL (intern -> u .file .current_line , intern -> u .file .current_line_len );
2195
2199
} /* }}} */
2196
2200
@@ -2205,9 +2209,19 @@ PHP_METHOD(SplFileObject, current)
2205
2209
2206
2210
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED (intern );
2207
2211
2212
+ /* No lines have been read yet (rewind or just opened file) */
2208
2213
if (!intern -> u .file .current_line && Z_ISUNDEF (intern -> u .file .current_zval )) {
2209
2214
spl_filesystem_file_read_line (ZEND_THIS , intern , /* silent */ true);
2210
2215
}
2216
+ /* next() of fgets() has been called and we need to read the line which is now pointed at */
2217
+ if (intern -> u .file .lines_to_catch_up > 0 ) {
2218
+ ZEND_ASSERT (intern -> u .file .current_line || !Z_ISUNDEF (intern -> u .file .current_zval ));
2219
+ for (; 0 < intern -> u .file .lines_to_catch_up ; intern -> u .file .lines_to_catch_up -- ) {
2220
+ /* Free fetched line */
2221
+ spl_filesystem_file_free_line (intern );
2222
+ spl_filesystem_file_read_line (ZEND_THIS , intern , /* silent */ true);
2223
+ }
2224
+ }
2211
2225
if (intern -> u .file .current_line && (!SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_READ_CSV ) || Z_ISUNDEF (intern -> u .file .current_zval ))) {
2212
2226
RETURN_STRINGL (intern -> u .file .current_line , intern -> u .file .current_line_len );
2213
2227
} else if (!Z_ISUNDEF (intern -> u .file .current_zval )) {
@@ -2242,9 +2256,11 @@ PHP_METHOD(SplFileObject, next)
2242
2256
RETURN_THROWS ();
2243
2257
}
2244
2258
2245
- spl_filesystem_file_free_line (intern );
2246
2259
if (SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_READ_AHEAD )) {
2260
+ spl_filesystem_file_free_line (intern );
2247
2261
spl_filesystem_file_read_line (ZEND_THIS , intern , /* silent */ true);
2262
+ } else {
2263
+ intern -> u .file .lines_to_catch_up ++ ;
2248
2264
}
2249
2265
intern -> u .file .current_line_num ++ ;
2250
2266
} /* }}} */
0 commit comments