php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #54252
Patch SplFileObject_countLines revision 2011-03-14 22:21 UTC by proofek at gmail dot com

Patch SplFileObject_countLines for SPL related Bug #54252

Patch version 2011-03-14 22:21 UTC

Return to Bug #54252 | Download this patch
Patch Revisions:

Developer: proofek@gmail.com

Index: ext/spl/tests/SplFileObject_countLines_variation_001.phpt
===================================================================
--- ext/spl/tests/SplFileObject_countLines_variation_001.phpt	(revision 0)
+++ ext/spl/tests/SplFileObject_countLines_variation_001.phpt	(revision 0)
@@ -0,0 +1,35 @@
+--TEST--
+SplFileObject::countLines counts number of lines in a csv file and doesn't change current line number
+--FILE--
+<?php
+$fp = fopen('SplFileObject__fgetcsv.csv', 'w+');
+fputcsv($fp, array('field1','field2','field3',1));
+fputcsv($fp, array('field1','field2','field3',2));
+fputcsv($fp, array('field1','field2','field3',3));
+fputcsv($fp, array('field1','field2','field3',4));
+fputcsv($fp, array('field1','field2','field3',5));
+
+fclose($fp);
+
+$fo = new SplFileObject('SplFileObject__fgetcsv.csv');
+$fo->seek(2);
+echo 'Current line: ' . $fo->key() . "\n";
+echo $fo->countLines() . "\n";
+echo 'Current line: ' . $fo->key() . "\n";
+
+$fo->seek(4);
+echo 'Current line: ' . $fo->key() . "\n";
+echo $fo->countLines() . "\n";
+echo 'Current line: ' . $fo->key() . "\n";
+?>
+--CLEAN--
+<?php
+unlink('SplFileObject__fgetcsv.csv');
+?>
+--EXPECTF--
+Current line: 2
+5
+Current line: 2
+Current line: 4
+5
+Current line: 4
Index: ext/spl/tests/SplFileObject_countLines_basic.phpt
===================================================================
--- ext/spl/tests/SplFileObject_countLines_basic.phpt	(revision 0)
+++ ext/spl/tests/SplFileObject_countLines_basic.phpt	(revision 0)
@@ -0,0 +1,22 @@
+--TEST--
+SplFileObject::countLines counts number of lines in a csv file
+--FILE--
+<?php
+$fp = fopen('SplFileObject__fgetcsv.csv', 'w+');
+fputcsv($fp, array('field1','field2','field3',1));
+fputcsv($fp, array('field1','field2','field3',2));
+fputcsv($fp, array('field1','field2','field3',3));
+fputcsv($fp, array('field1','field2','field3',4));
+fputcsv($fp, array('field1','field2','field3',5));
+
+fclose($fp);
+
+$fo = new SplFileObject('SplFileObject__fgetcsv.csv');
+echo $fo->countLines();
+?>
+--CLEAN--
+<?php
+unlink('SplFileObject__fgetcsv.csv');
+?>
+--EXPECTF--
+5
Index: ext/spl/spl_directory.c
===================================================================
--- ext/spl/spl_directory.c	(revision 309233)
+++ ext/spl/spl_directory.c	(working copy)
@@ -2777,6 +2777,38 @@
 	}
 } /* }}} */
 
+/* {{{ proto int SplFileObject::countLines()
+Get total number of lines */
+SPL_METHOD(SplFileObject, countLines)
+{
+	spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+	long current_pos = intern->u.file.current_line_num;
+	long lines_number = 0;
+
+	if (zend_parse_parameters_none() == FAILURE) {
+		return;
+	}
+
+	// find last line in a file
+	spl_filesystem_file_rewind(getThis(), intern TSRMLS_CC);
+	while(!php_stream_eof(intern->u.file.stream)) {
+		if (spl_filesystem_file_read_line(getThis(), intern, 0 TSRMLS_CC) == FAILURE) {
+			break;
+		}
+	}
+	lines_number = intern->u.file.current_line_num;
+
+	// rewind to the original position
+	spl_filesystem_file_rewind(getThis(), intern TSRMLS_CC);
+	while(intern->u.file.current_line_num < current_pos) {
+		if (spl_filesystem_file_read_line(getThis(), intern, 0 TSRMLS_CC) == FAILURE) {
+			break;
+		}
+	}
+
+	RETURN_LONG(lines_number);
+} /* }}} */
+
 /* {{{ Function/Class/Method definitions */
 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object___construct, 0, 0, 1)
 	ZEND_ARG_INFO(0, file_name)
@@ -2859,6 +2891,7 @@
 	SPL_ME(SplFileObject, hasChildren,    arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
 	SPL_ME(SplFileObject, getChildren,    arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
 	SPL_ME(SplFileObject, seek,           arginfo_file_object_seek,          ZEND_ACC_PUBLIC)
+	SPL_ME(SplFileObject, countLines,     arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
 	/* mappings */
 	SPL_MA(SplFileObject, getCurrentLine, SplFileObject, fgets,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
 	SPL_MA(SplFileObject, __toString,     SplFileObject, current,    arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Aug 10 07:01:25 2020 UTC