PHP_FUNCTION(file) { zval **ppfilename; char *filename; int filename_len; char *target_buf=NULL; register int i = 0; int target_len; long flags = 0; zend_bool use_include_path; zend_bool include_new_line; zend_bool skip_blank_lines; zend_bool text_mode; php_stream *stream; zval *zcontext = NULL; php_stream_context *context = NULL; /* Parse arguments */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|lr!", &ppfilename, &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 | PHP_FILE_TEXT)) { 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; text_mode = flags & PHP_FILE_TEXT; context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); if (php_stream_path_param_encode(ppfilename, &filename, &filename_len, REPORT_ERRORS, context) == FAILURE) { RETURN_FALSE; } stream = php_stream_open_wrapper_ex(filename, text_mode ? "rt" : "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); if (!stream) { RETURN_FALSE; } /* Initialize return array */ array_init(return_value); target_len = php_stream_copy_to_mem_ex(stream, stream->readbuf_type, (void**)&target_buf, PHP_STREAM_COPY_ALL, -1, 0); if (!target_len) { /* Empty file, do nothing and return an empty array */ } else if (stream->readbuf_type == IS_UNICODE) { UChar *s = (UChar*)target_buf, *p; UChar *e = s + target_len, eol_marker = '\n'; if (!(p = php_stream_locate_eol(stream, ZSTR(target_buf), target_len TSRMLS_CC))) { p = e; goto uparse_eol; } if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) { eol_marker = '\r'; } /* for performance reasons the code is quadruplicated, so that the if (include_new_line/unicode * will not need to be done for every single line in the file. */ if (include_new_line) { do { p++; uparse_eol: add_index_unicodel(return_value, i++, eustrndup(s, p-s), p-s, 0); s = p; } while ((p = u_memchr(p, eol_marker, (e-p)))); } else { do { if (skip_blank_lines && !(p-s)) { s = ++p; continue; } add_index_unicodel(return_value, i++, eustrndup(s, p-s), p-s, 0); s = ++p; } while ((p = u_memchr(p, eol_marker, (e-p)))); } /* handle any left overs of files without new lines */ if (s != e) { p = e; goto uparse_eol; } } else { /* !IS_UNICODE */ char *s = target_buf, *p; char *e = target_buf + target_len, eol_marker = '\n'; if (!(p = php_stream_locate_eol(stream, ZSTR(target_buf), target_len TSRMLS_CC))) { p = e; goto parse_eol; } if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) { eol_marker = '\r'; } if (include_new_line) { do { p++; parse_eol: add_index_stringl(return_value, i++, estrndup(s, p-s), p-s, 0); s = p; } while ((p = memchr(p, eol_marker, (e-p)))); } else { do { if (skip_blank_lines && !(p-s)) { s = ++p; continue; } add_index_stringl(return_value, i++, estrndup(s, p-s), p-s, 0); s = ++p; } while ((p = memchr(p, eol_marker, (e-p)))); } /* handle any left overs of files without new lines */ if (s != e) { p = e; goto parse_eol; } } if (target_buf) { efree(target_buf); } php_stream_close(stream); } /* }}} */