Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ PHP_MINIT_FUNCTION(file)
REGISTER_LONG_CONSTANT("FILE_USE_INCLUDE_PATH", PHP_FILE_USE_INCLUDE_PATH, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("FILE_IGNORE_NEW_LINES", PHP_FILE_IGNORE_NEW_LINES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("FILE_SKIP_EMPTY_LINES", PHP_FILE_SKIP_EMPTY_LINES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("FILE_INDEX_LINES", PHP_FILE_INDEX_LINES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("FILE_APPEND", PHP_FILE_APPEND, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("FILE_NO_DEFAULT_CONTEXT", PHP_FILE_NO_DEFAULT_CONTEXT, CONST_CS | CONST_PERSISTENT);

Expand Down Expand Up @@ -715,6 +716,7 @@ PHP_FUNCTION(file)
zend_bool use_include_path;
zend_bool include_new_line;
zend_bool skip_blank_lines;
zend_bool index_lines;
php_stream *stream;
zval *zcontext = NULL;
php_stream_context *context = NULL;
Expand All @@ -723,14 +725,20 @@ PHP_FUNCTION(file)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|lr!", &filename, &filename_len, &flags, &zcontext) == FAILURE) {
return;
}
if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_INDEX_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%ld' flag is not supported", flags);
RETURN_FALSE;
}

use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH;
include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES);
skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES;

/* Added a new line-number to array-key indexing optional flag by Sherif */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think crediting this way in a comment makes much sense and is more confusing than helping. Code comments should explain things.

index_lines = flags & PHP_FILE_INDEX_LINES;
if (index_lines) {
++i;
}

context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);

Expand Down Expand Up @@ -759,7 +767,20 @@ PHP_FUNCTION(file)
* will not need to be done for every single line in the file. */
if (include_new_line) {
do {
p++;
int windows_eol = 0;
if (p != target_buf && eol_marker == '\n' && *(p - 1) == '\r') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will *(p -1) always be valid? - I didn't check all the code paths, on first sight it looks dangerous.

windows_eol++;
}
if (skip_blank_lines && !(p-s-windows_eol)) {
s = ++p;
if (index_lines) {
++i;
}
continue;
}
else {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else should be on the same line as }

p++;
}
parse_eol:
add_index_stringl(return_value, i++, estrndup(s, p-s), p-s, 0);
s = p;
Expand All @@ -772,6 +793,9 @@ PHP_FUNCTION(file)
}
if (skip_blank_lines && !(p-s-windows_eol)) {
s = ++p;
if (index_lines) {
++i;
}
continue;
}
add_index_stringl(return_value, i++, estrndup(s, p-s-windows_eol), p-s-windows_eol, 0);
Expand Down
1 change: 1 addition & 0 deletions ext/standard/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ PHPAPI int php_fputcsv(php_stream *stream, zval *fields, char delimiter, char en
#define PHP_FILE_SKIP_EMPTY_LINES 4
#define PHP_FILE_APPEND 8
#define PHP_FILE_NO_DEFAULT_CONTEXT 16
#define PHP_FILE_INDEX_LINES 32

typedef enum _php_meta_tags_token {
TOK_EOF = 0,
Expand Down
36 changes: 36 additions & 0 deletions ext/standard/tests/file/file_skipindex_test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Test file() function : Support for FILE_SKIP_EMPTY_LINES without FILE_IGNORE_NEW_LINES and FILE_INDEX_LINES flags
--FILE--
<?php

$f = tmpnam(__DIR__, "phpfiletest");
file_put_contents($f, "Line 1" . PHP_EOL . "Line 2" . PHP_EOL . PHP_EOL . "Last line...");
$a = file($f, FILE_SKIP_EMPTY_LINES);
$b = file($f, FILE_INDEX_LINES | FILE_SKIP_EMPTY_LINES);

var_dump($a, $b);

@unlink($f);

?>
--EXPECTF--
array(3) {
[0]=>
string(7) "Line 1
"
[1]=>
string(7) "Line 2
"
[2]=>
string(12) "Last line..."
}
array(3) {
[1]=>
string(7) "Line 1
"
[2]=>
string(7) "Line 2
"
[4]=>
string(12) "Last line..."
}