xref: /PHP-8.0/ext/spl/spl_directory.h (revision 5d6e923d)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Marcus Boerger <helly@php.net>                              |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifndef SPL_DIRECTORY_H
18 #define SPL_DIRECTORY_H
19 
20 #include "php.h"
21 #include "php_spl.h"
22 
23 extern PHPAPI zend_class_entry *spl_ce_SplFileInfo;
24 extern PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
25 extern PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
26 extern PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
27 extern PHPAPI zend_class_entry *spl_ce_GlobIterator;
28 extern PHPAPI zend_class_entry *spl_ce_SplFileObject;
29 extern PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
30 
31 PHP_MINIT_FUNCTION(spl_directory);
32 
33 typedef enum {
34 	SPL_FS_INFO, /* must be 0 */
35 	SPL_FS_DIR,
36 	SPL_FS_FILE
37 } SPL_FS_OBJ_TYPE;
38 
39 typedef struct _spl_filesystem_object  spl_filesystem_object;
40 
41 typedef void (*spl_foreign_dtor_t)(spl_filesystem_object *object);
42 typedef void (*spl_foreign_clone_t)(spl_filesystem_object *src, spl_filesystem_object *dst);
43 
44 PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, size_t *len);
45 
46 typedef struct _spl_other_handler {
47 	spl_foreign_dtor_t     dtor;
48 	spl_foreign_clone_t    clone;
49 } spl_other_handler;
50 
51 /* define an overloaded iterator structure */
52 typedef struct {
53 	zend_object_iterator  intern;
54 	zval                  current;
55 	void                 *object;
56 } spl_filesystem_iterator;
57 
58 struct _spl_filesystem_object {
59 	void               *oth;
60 	const spl_other_handler  *oth_handler;
61 	char               *_path;
62 	size_t             _path_len;
63 	char               *orig_path;
64 	char               *file_name;
65 	size_t             file_name_len;
66 	SPL_FS_OBJ_TYPE    type;
67 	zend_long               flags;
68 	zend_class_entry   *file_class;
69 	zend_class_entry   *info_class;
70 	union {
71 		struct {
72 			php_stream         *dirp;
73 			php_stream_dirent  entry;
74 			char               *sub_path;
75 			size_t             sub_path_len;
76 			int                index;
77 			int                is_recursive;
78 			zend_function      *func_rewind;
79 			zend_function      *func_next;
80 			zend_function      *func_valid;
81 		} dir;
82 		struct {
83 			php_stream         *stream;
84 			php_stream_context *context;
85 			zval               *zcontext;
86 			char               *open_mode;
87 			size_t             open_mode_len;
88 			zval               current_zval;
89 			char               *current_line;
90 			size_t             current_line_len;
91 			size_t             max_line_len;
92 			zend_long               current_line_num;
93 			zval               zresource;
94 			zend_function      *func_getCurr;
95 			char               delimiter;
96 			char               enclosure;
97 			int                escape;
98 		} file;
99 	} u;
100 	zend_object        std;
101 };
102 
spl_filesystem_from_obj(zend_object * obj)103 static inline spl_filesystem_object *spl_filesystem_from_obj(zend_object *obj) /* {{{ */ {
104 	return (spl_filesystem_object*)((char*)(obj) - XtOffsetOf(spl_filesystem_object, std));
105 }
106 /* }}} */
107 
108 #define Z_SPLFILESYSTEM_P(zv)  spl_filesystem_from_obj(Z_OBJ_P((zv)))
109 
spl_filesystem_object_to_iterator(spl_filesystem_object * obj)110 static inline spl_filesystem_iterator* spl_filesystem_object_to_iterator(spl_filesystem_object *obj)
111 {
112 	spl_filesystem_iterator    *it;
113 
114 	it = ecalloc(1, sizeof(spl_filesystem_iterator));
115 	it->object = (void *)obj;
116 	zend_iterator_init(&it->intern);
117 	return it;
118 }
119 
spl_filesystem_iterator_to_object(spl_filesystem_iterator * it)120 static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_filesystem_iterator *it)
121 {
122 	return (spl_filesystem_object*)it->object;
123 }
124 
125 #define SPL_FILE_OBJECT_DROP_NEW_LINE      0x00000001 /* drop new lines */
126 #define SPL_FILE_OBJECT_READ_AHEAD         0x00000002 /* read on rewind/next */
127 #define SPL_FILE_OBJECT_SKIP_EMPTY         0x00000004 /* skip empty lines */
128 #define SPL_FILE_OBJECT_READ_CSV           0x00000008 /* read via fgetcsv */
129 #define SPL_FILE_OBJECT_MASK               0x0000000F /* read via fgetcsv */
130 
131 #define SPL_FILE_DIR_CURRENT_AS_FILEINFO   0x00000000 /* make RecursiveDirectoryTree::current() return SplFileInfo */
132 #define SPL_FILE_DIR_CURRENT_AS_SELF       0x00000010 /* make RecursiveDirectoryTree::current() return getSelf() */
133 #define SPL_FILE_DIR_CURRENT_AS_PATHNAME   0x00000020 /* make RecursiveDirectoryTree::current() return getPathname() */
134 #define SPL_FILE_DIR_CURRENT_MODE_MASK     0x000000F0 /* mask RecursiveDirectoryTree::current() */
135 #define SPL_FILE_DIR_CURRENT(intern,mode)  ((intern->flags&SPL_FILE_DIR_CURRENT_MODE_MASK)==mode)
136 
137 #define SPL_FILE_DIR_KEY_AS_PATHNAME       0x00000000 /* make RecursiveDirectoryTree::key() return getPathname() */
138 #define SPL_FILE_DIR_KEY_AS_FILENAME       0x00000100 /* make RecursiveDirectoryTree::key() return getFilename() */
139 #define SPL_FILE_DIR_FOLLOW_SYMLINKS       0x00000200 /* make RecursiveDirectoryTree::hasChildren() follow symlinks */
140 #define SPL_FILE_DIR_KEY_MODE_MASK         0x00000F00 /* mask RecursiveDirectoryTree::key() */
141 #define SPL_FILE_DIR_KEY(intern,mode)      ((intern->flags&SPL_FILE_DIR_KEY_MODE_MASK)==mode)
142 
143 #define SPL_FILE_DIR_SKIPDOTS              0x00001000 /* Tells whether it should skip dots or not */
144 #define SPL_FILE_DIR_UNIXPATHS             0x00002000 /* Whether to unixify path separators */
145 #define SPL_FILE_DIR_OTHERS_MASK           0x00003000 /* mask used for get/setFlags */
146 
147 #endif /* SPL_DIRECTORY_H */
148