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