cvs: php-src /ext/spl spl.php spl_directory.c

From: Date: Mon, 10 Oct 2005 21:06:57 +0000
Subject: cvs: php-src /ext/spl spl.php spl_directory.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-34504@lists.php.net to get a copy of this message
helly Mon Oct 10 17:06:57 2005 EDT Modified files: /php-src/ext/spl spl.php spl_directory.c Log: - Finalize work on SplFileInfo and derived for now (add missing get*Info()) - Update docu

http://cvs.php.net/diff.php/php-src/ext/spl/spl.php?r1=1.60&r2=1.61&ty=u Index: php-src/ext/spl/spl.php diff -u php-src/ext/spl/spl.php:1.60 php-src/ext/spl/spl.php:1.61 --- php-src/ext/spl/spl.php:1.60 Sat Oct 8 14:57:15 2005 +++ php-src/ext/spl/spl.php Mon Oct 10 17:06:54 2005 @@ -749,10 +749,18 @@ */ function getFilename(); + /** @return SplFileInfo created for the file + */ + function getFileInfo(); + /** @return The current entries path and file name. */ function getPathname(); + /** @return SplFileInfo created for the path + */ + function getPathInfo(); + /** @return The current entry's permissions. */ function getPerms(); @@ -898,6 +906,19 @@ /** @return a RecursiveDirectoryIterator for the current entry. */ function getChildren(); + + /** @return sub path only (without main path) + */ + function getSubPath(); + + /** @return the current sub path + */ + function getSubPathname(); + + /** @return SplFileInfo created for the current sub path + */ + function getSubPathInfo(); + } /** @ingroup SPL http://cvs.php.net/diff.php/php-src/ext/spl/spl_directory.c?r1=1.51&r2=1.52&ty=u Index: php-src/ext/spl/spl_directory.c diff -u php-src/ext/spl/spl_directory.c:1.51 php-src/ext/spl/spl_directory.c:1.52 --- php-src/ext/spl/spl_directory.c:1.51 Sat Oct 8 14:57:16 2005 +++ php-src/ext/spl/spl_directory.c Mon Oct 10 17:06:54 2005 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: spl_directory.c,v 1.51 2005/10/08 18:57:16 helly Exp $ */ +/* $Id: spl_directory.c,v 1.52 2005/10/10 21:06:54 helly Exp $ */ #ifdef HAVE_CONFIG_H # include "config.h" @@ -265,7 +265,49 @@ } /* }}} */ -static void spl_filesystem_object_create_type(int ht, spl_filesystem_object *source, int type, zval *return_value TSRMLS_DC) /* {{{ */ +void spl_filesystem_info_set_filename(spl_filesystem_object *intern, char *path, int len, int use_copy TSRMLS_DC) /* {{{ */ +{ + char *p1, *p2; + + intern->file_name = use_copy ? estrndup(path, len) : path; + intern->file_name_len = len; + + p1 = strrchr(path, '/'); + p2 = strrchr(path, '\\'); + if (p1 || p2) { + intern->path_len = (p1 > p2 ? p1 : p2) - path; + } else { + intern->path_len = 0; + } + intern->path = estrndup(path, intern->path_len); +} /* }}} */ + +static spl_filesystem_object * spl_filesystem_object_create_info(spl_filesystem_object *source, char *file_path, int file_path_len, int use_copy, zval *return_value TSRMLS_DC) /* {{{ */ +{ + spl_filesystem_object *intern; + + if (!file_path || !file_path_len) { + zend_throw_exception_ex(U_CLASS_ENTRY(spl_ce_RuntimeException), 0 TSRMLS_CC, "Cannot create SplFileInfo for empty path"); + if (file_path && !use_copy) + { + efree(file_path); + } + return NULL; + } + + php_set_error_handling(EH_THROW, U_CLASS_ENTRY(spl_ce_RuntimeException) TSRMLS_CC); + + return_value->value.obj = spl_filesystem_object_new_ex(spl_ce_SplFileInfo, &intern TSRMLS_CC); + Z_TYPE_P(return_value) = IS_OBJECT; + + spl_filesystem_info_set_filename(intern, file_path, file_path_len, use_copy TSRMLS_CC); + + php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); + + return intern; +} /* }}} */ + +static spl_filesystem_object * spl_filesystem_object_create_type(int ht, spl_filesystem_object *source, int type, zval *return_value TSRMLS_DC) /* {{{ */ { spl_filesystem_object *intern; zend_bool use_include_path = 0; @@ -280,7 +322,7 @@ if (!source->u.dir.entry.d_name[0]) { zend_throw_exception_ex(U_CLASS_ENTRY(spl_ce_RuntimeException), 0 TSRMLS_CC, "Could not open file"); php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); - return; + return NULL; } } @@ -292,8 +334,6 @@ spl_filesystem_object_get_file_name(source TSRMLS_CC); intern->file_name = estrndup(source->file_name, source->file_name_len); intern->file_name_len = source->file_name_len; - - php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); break; case SPL_FS_FILE: return_value->value.obj = spl_filesystem_object_new_ex(spl_ce_SplFileObject, &intern TSRMLS_CC); @@ -306,27 +346,29 @@ intern->u.file.open_mode = "r"; intern->u.file.open_mode_len = 1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sbr", + if (ht && zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sbr", &intern->u.file.open_mode, &intern->u.file.open_mode_len, &use_include_path, &intern->u.file.zcontext) == FAILURE) { php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); intern->u.file.open_mode = NULL; zval_dtor(return_value); Z_TYPE_P(return_value) = IS_NULL; - return; + return NULL; } if (spl_filesystem_file_open(intern, use_include_path, 0 TSRMLS_CC) == FAILURE) { php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); zval_dtor(return_value); Z_TYPE_P(return_value) = IS_NULL; - return; + return NULL; } case SPL_FS_DIR: + php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); zend_throw_exception_ex(U_CLASS_ENTRY(spl_ce_RuntimeException), 0 TSRMLS_CC, "Operation not supported"); - break; + return NULL; } php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); + return NULL; } /* }}} */ /* {{{ proto void DirectoryIterator::__construct(string path [, int flags]) @@ -532,7 +574,7 @@ SPL_METHOD(SplFileInfo, __construct) { spl_filesystem_object *intern; - char *path, *p1, *p2; + char *path; int len; php_set_error_handling(EH_THROW, U_CLASS_ENTRY(spl_ce_RuntimeException) TSRMLS_CC); @@ -544,18 +586,8 @@ intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC); - intern->file_name = estrndup(path, len); - intern->file_name_len = len; - - p1 = strrchr(path, '/'); - p2 = strrchr(path, '\\'); - if (p1 || p2) { - intern->path_len = (p1 > p2 ? p1 : p2) - path; - } else { - intern->path_len = 0; - } - intern->path = estrndup(path, intern->path_len); - + spl_filesystem_info_set_filename(intern, path, len, 1 TSRMLS_CC); + /* intern->type = SPL_FS_INFO; already set */ php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); @@ -657,7 +689,7 @@ spl_filesystem_object_create_type(ht, intern, SPL_FS_FILE, return_value TSRMLS_CC); } -/* {{{ proto SplFileObject SplFileInfo::getFileInfo() +/* {{{ proto SplFileInfo SplFileInfo::getFileInfo() Get/copy file info */ SPL_METHOD(SplFileInfo, getFileInfo) { @@ -666,6 +698,15 @@ spl_filesystem_object_create_type(ht, intern, SPL_FS_INFO, return_value TSRMLS_CC); } +/* {{{ proto SplFileInfo SplFileInfo::getPathInfo() + Get/copy file info */ +SPL_METHOD(SplFileInfo, getPathInfo) +{ + spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC); + + spl_filesystem_object_create_info(intern, intern->path, intern->path_len, 1, return_value TSRMLS_CC); +} + /* {{{ proto void RecursiveDirectoryIterator::rewind() Rewind dir back to the start */ SPL_METHOD(RecursiveDirectoryIterator, rewind) @@ -787,6 +828,23 @@ } /* }}} */ +/* {{{ proto SplFileInfo RecursiveDirectoryIterator::getSubPathInfo() + Create SplFileInfo for sub path */ +SPL_METHOD(RecursiveDirectoryIterator, getSubPathInfo) +{ + spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC); + char *sub_name; + int len; + + if (intern->u.dir.sub_path) { + len = spprintf(&sub_name, 0, "%s/%s", intern->u.dir.sub_path, intern->u.dir.entry.d_name); + spl_filesystem_object_create_info(intern, sub_name, len, 0, return_value TSRMLS_CC); + } else { + spl_filesystem_object_create_info(intern, intern->path, intern->path_len, 1, return_value TSRMLS_CC); + } +} +/* }}} */ + /* define an overloaded iterator structure */ typedef struct { zend_object_iterator intern; @@ -1083,6 +1141,7 @@ SPL_ME(SplFileInfo, isDir, NULL, ZEND_ACC_PUBLIC) SPL_ME(SplFileInfo, isLink, NULL, ZEND_ACC_PUBLIC) SPL_ME(SplFileInfo, getFileInfo, NULL, ZEND_ACC_PUBLIC) + SPL_ME(SplFileInfo, getPathInfo, NULL, ZEND_ACC_PUBLIC) SPL_ME(SplFileInfo, openFile, arginfo_info_openFile, ZEND_ACC_PUBLIC) SPL_MA(SplFileInfo, __toString, SplFileInfo, getPathname, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} @@ -1117,6 +1176,7 @@ SPL_ME(RecursiveDirectoryIterator, getChildren, NULL, ZEND_ACC_PUBLIC) SPL_ME(RecursiveDirectoryIterator, getSubPath, NULL, ZEND_ACC_PUBLIC) SPL_ME(RecursiveDirectoryIterator, getSubPathname,NULL, ZEND_ACC_PUBLIC) + SPL_ME(RecursiveDirectoryIterator, getSubPathInfo,NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} };
« previous php.cvs (#34504) next »