Created
March 11, 2011 23:06
-
-
Save proofek/866767 to your computer and use it in GitHub Desktop.
Make SplFileObject countable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* {{{ proto int SplFileObject::count() | |
Get total number of lines */ | |
SPL_METHOD(SplFileObject, count) | |
{ | |
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, 1 TSRMLS_CC) == FAILURE) { | |
break; | |
} | |
} | |
RETURN_LONG(lines_number); | |
} /* }}} */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment