git subrepo clone https://github.com/libretro/libretro-common.git deps/libretro-common
[pcsx_rearmed.git] / deps / libretro-common / file / retro_dirent.c
1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (retro_dirent.c).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <retro_common.h>
28
29 #include <boolean.h>
30 #include <retro_dirent.h>
31 #define VFS_FRONTEND
32 #include <vfs/vfs_implementation.h>
33
34 /* TODO/FIXME - static globals */
35 static retro_vfs_opendir_t dirent_opendir_cb                 = NULL;
36 static retro_vfs_readdir_t dirent_readdir_cb                 = NULL;
37 static retro_vfs_dirent_get_name_t dirent_dirent_get_name_cb = NULL;
38 static retro_vfs_dirent_is_dir_t dirent_dirent_is_dir_cb     = NULL;
39 static retro_vfs_closedir_t dirent_closedir_cb               = NULL;
40
41 void dirent_vfs_init(const struct retro_vfs_interface_info* vfs_info)
42 {
43    const struct retro_vfs_interface* vfs_iface;
44
45    dirent_opendir_cb         = NULL;
46    dirent_readdir_cb         = NULL;
47    dirent_dirent_get_name_cb = NULL;
48    dirent_dirent_is_dir_cb   = NULL;
49    dirent_closedir_cb        = NULL;
50
51    vfs_iface                 = vfs_info->iface;
52
53    if (
54          vfs_info->required_interface_version < DIRENT_REQUIRED_VFS_VERSION || 
55          !vfs_iface)
56       return;
57
58    dirent_opendir_cb         = vfs_iface->opendir;
59    dirent_readdir_cb         = vfs_iface->readdir;
60    dirent_dirent_get_name_cb = vfs_iface->dirent_get_name;
61    dirent_dirent_is_dir_cb   = vfs_iface->dirent_is_dir;
62    dirent_closedir_cb        = vfs_iface->closedir;
63 }
64
65 struct RDIR *retro_opendir_include_hidden(
66       const char *name, bool include_hidden)
67 {
68    if (dirent_opendir_cb)
69       return (struct RDIR *)dirent_opendir_cb(name, include_hidden);
70    return (struct RDIR *)retro_vfs_opendir_impl(name, include_hidden);
71 }
72
73 struct RDIR *retro_opendir(const char *name)
74 {
75    return retro_opendir_include_hidden(name, false);
76 }
77
78 bool retro_dirent_error(struct RDIR *rdir)
79 {
80    /* Left for compatibility */
81    return false;
82 }
83
84 int retro_readdir(struct RDIR *rdir)
85 {
86    if (dirent_readdir_cb)
87       return dirent_readdir_cb((struct retro_vfs_dir_handle *)rdir);
88    return retro_vfs_readdir_impl((struct retro_vfs_dir_handle *)rdir);
89 }
90
91 const char *retro_dirent_get_name(struct RDIR *rdir)
92 {
93    if (dirent_dirent_get_name_cb)
94       return dirent_dirent_get_name_cb((struct retro_vfs_dir_handle *)rdir);
95    return retro_vfs_dirent_get_name_impl((struct retro_vfs_dir_handle *)rdir);
96 }
97
98 /**
99  *
100  * retro_dirent_is_dir:
101  * @rdir         : pointer to the directory entry.
102  * @unused       : deprecated, included for compatibility reasons, pass NULL
103  *
104  * Is the directory listing entry a directory?
105  *
106  * Returns: true if directory listing entry is
107  * a directory, false if not.
108  */
109 bool retro_dirent_is_dir(struct RDIR *rdir, const char *unused)
110 {
111    if (dirent_dirent_is_dir_cb)
112       return dirent_dirent_is_dir_cb((struct retro_vfs_dir_handle *)rdir);
113    return retro_vfs_dirent_is_dir_impl((struct retro_vfs_dir_handle *)rdir);
114 }
115
116 void retro_closedir(struct RDIR *rdir)
117 {
118    if (dirent_closedir_cb)
119       dirent_closedir_cb((struct retro_vfs_dir_handle *)rdir);
120    else
121       retro_vfs_closedir_impl((struct retro_vfs_dir_handle *)rdir);
122 }