xref: /php-src/ext/spl/spl_directory.h (revision 1fbbd2b2)
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    | https://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 /* Internal objecte structure and helpers for Directory and File SPL objects */
34 typedef struct _spl_filesystem_object  spl_filesystem_object;
35 
36 typedef void (*spl_foreign_dtor_t)(spl_filesystem_object *object);
37 typedef void (*spl_foreign_clone_t)(spl_filesystem_object *src, spl_filesystem_object *dst);
38 
39 PHPAPI zend_string *spl_filesystem_object_get_path(spl_filesystem_object *intern);
40 
41 typedef struct _spl_other_handler {
42 	spl_foreign_dtor_t     dtor;
43 	spl_foreign_clone_t    clone;
44 } spl_other_handler;
45 
46 typedef enum {
47 	SPL_FS_INFO, /* must be 0 */
48 	SPL_FS_DIR,
49 	SPL_FS_FILE
50 } SPL_FS_OBJ_TYPE;
51 
52 struct _spl_filesystem_object {
53 	void               *oth;
54 	const spl_other_handler  *oth_handler;
55 	zend_string        *path;
56 	zend_string        *orig_path;
57 	zend_string        *file_name;
58 	SPL_FS_OBJ_TYPE    type;
59 	zend_long               flags;
60 	zend_class_entry   *file_class;
61 	zend_class_entry   *info_class;
62 	union {
63 		struct {
64 			php_stream         *dirp;
65 			zend_string        *sub_path;
66 			int                index;
67 			zend_function      *func_rewind;
68 			zend_function      *func_next;
69 			zend_function      *func_valid;
70 			php_stream_dirent  entry;
71 		} dir;
72 		struct {
73 			php_stream         *stream;
74 			php_stream_context *context;
75 			zval               *zcontext;
76 			zend_string        *open_mode;
77 			zval               current_zval;
78 			char               *current_line;
79 			size_t             current_line_len;
80 			size_t             max_line_len;
81 			zend_long               current_line_num;
82 			zval               zresource;
83 			zend_function      *func_getCurr;
84 			char               delimiter;
85 			char               enclosure;
86 			int                escape;
87 		} file;
88 	} u;
89 	zend_object        std;
90 };
91 
92 #define SPL_FILE_OBJECT_DROP_NEW_LINE      0x00000001 /* drop new lines */
93 #define SPL_FILE_OBJECT_READ_AHEAD         0x00000002 /* read on rewind/next */
94 #define SPL_FILE_OBJECT_SKIP_EMPTY         0x00000004 /* skip empty lines */
95 #define SPL_FILE_OBJECT_READ_CSV           0x00000008 /* read via fgetcsv */
96 #define SPL_FILE_OBJECT_MASK               0x0000000F /* read via fgetcsv */
97 
98 #define SPL_FILE_DIR_CURRENT_AS_FILEINFO   0x00000000 /* make RecursiveDirectoryTree::current() return SplFileInfo */
99 #define SPL_FILE_DIR_CURRENT_AS_SELF       0x00000010 /* make RecursiveDirectoryTree::current() return getSelf() */
100 #define SPL_FILE_DIR_CURRENT_AS_PATHNAME   0x00000020 /* make RecursiveDirectoryTree::current() return getPathname() */
101 #define SPL_FILE_DIR_CURRENT_MODE_MASK     0x000000F0 /* mask RecursiveDirectoryTree::current() */
102 #define SPL_FILE_DIR_CURRENT(intern,mode)  ((intern->flags&SPL_FILE_DIR_CURRENT_MODE_MASK)==mode)
103 
104 #define SPL_FILE_DIR_KEY_AS_PATHNAME       0x00000000 /* make RecursiveDirectoryTree::key() return getPathname() */
105 #define SPL_FILE_DIR_KEY_AS_FILENAME       0x00000100 /* make RecursiveDirectoryTree::key() return getFilename() */
106 #define SPL_FILE_DIR_KEY_MODE_MASK         0x00000F00 /* mask RecursiveDirectoryTree::key() */
107 #define SPL_FILE_NEW_CURRENT_AND_KEY       SPL_FILE_DIR_KEY_AS_FILENAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO
108 #define SPL_FILE_DIR_KEY(intern,mode)      ((intern->flags&SPL_FILE_DIR_KEY_MODE_MASK)==mode)
109 
110 #define SPL_FILE_DIR_SKIPDOTS              0x00001000 /* Tells whether it should skip dots or not */
111 #define SPL_FILE_DIR_UNIXPATHS             0x00002000 /* Whether to unixify path separators */
112 #define SPL_FILE_DIR_FOLLOW_SYMLINKS       0x00004000 /* make RecursiveDirectoryTree::hasChildren() follow symlinks */
113 #define SPL_FILE_DIR_OTHERS_MASK           0x00007000 /* mask used for get/setFlags */
114 
115 #endif /* SPL_DIRECTORY_H */
116