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