Debian Codegraph @

src/engine/dir.cpp

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2019 - 2024 *
4 * *
5 * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
6 * Copyright (C) 2009 by Andrey Afletdinov <[email protected]> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24 #include "dir.h"
25
26 #include <utility>
27
28 #if defined( TARGET_PS_VITA )
29 #include <psp2/io/dirent.h>
30 #else
31 #include <filesystem>
32 #include <system_error>
33 #endif
34
35 #if defined( _WIN32 )
36 #include <cstring>
37 #else
38 #include <strings.h>
39 #endif
40
41 #include "system.h"
42
43 namespace
44 {
45 template <typename F>
46 bool nameFilter( const std::string & filename, const bool needExactMatch, const std::string & filter, const F & strCmp )
47 {
48 if ( !needExactMatch && filter.empty() ) {
49 return true;
50 }
51
52 if ( filename.length() < filter.length() ) {
53 return false;
54 }
55
56 if ( needExactMatch && filename.length() != filter.length() ) {
57 return false;
58 }
59
60 const char * filenamePtr = filename.c_str() + filename.length() - filter.length();
61
62 return ( strCmp( filenamePtr, filter.c_str() ) == 0 );
63 }
64
65 void getFilesFromDirectory( const std::string & path, const std::string & filter, const bool needExactMatch, ListFiles & files )
66 {
67 std::string correctedPath;
68 if ( !System::GetCaseInsensitivePath( path, correctedPath ) ) {
69 return;
70 }
71
72 #if defined( _WIN32 )
73 auto * const strCmp = _stricmp;
74 #else
75 auto * const strCmp = strcasecmp;
76 #endif
77
78 #if defined( TARGET_PS_VITA )
79 // On PS Vita, getting a list of files using std::filesystem for some reason works much slower than using the native file system API
80 class SceUIDWrapper
81 {
82 public:
83 SceUIDWrapper( const std::string & path )
84 : uid( sceIoDopen( path.c_str() ) )
85 {}
86
87 SceUIDWrapper( const SceUIDWrapper & ) = delete;
88
89 ~SceUIDWrapper()
90 {
91 if ( !isValid() ) {
92 return;
93 }
94
95 sceIoDclose( uid );
96 }
97
98 SceUIDWrapper & operator=( const SceUIDWrapper & ) = delete;
99
100 bool isValid() const
101 {
102 return uid >= 0;
103 }
104
105 SceUID get() const
106 {
107 return uid;
108 }
109
110 private:
111 const SceUID uid;
112 };
113
114 const SceUIDWrapper uid( path );
115 if ( !uid.isValid() ) {
116 return;
117 }
118
119 SceIoDirent entry;
120
121 while ( sceIoDread( uid.get(), &entry ) > 0 ) {
122 // Ensure that this directory entry is a regular file
123 if ( !SCE_S_ISREG( entry.d_stat.st_mode ) ) {
124 continue;
125 }
126
127 if ( !nameFilter( entry.d_name, needExactMatch, filter, strCmp ) ) {
128 continue;
129 }
130
131 files.emplace_back( System::concatPath( path, entry.d_name ) );
132 }
133 #else
134 std::error_code ec;
135
136 // Using the non-throwing overload
137 for ( const std::filesystem::directory_entry & entry : std::filesystem::directory_iterator( correctedPath, ec ) ) {
138 // Using the non-throwing overload
139 if ( !entry.is_regular_file( ec ) ) {
140 continue;
141 }
142
143 const std::filesystem::path & entryPath = entry.path();
144
145 if ( !nameFilter( System::fsPathToString( entryPath.filename() ), needExactMatch, filter, strCmp ) ) {
146 continue;
147 }
148
149 files.emplace_back( System::fsPathToString( entryPath ) );
150 }
151 #endif
152 }
153 }
154
155 void ListFiles::Append( ListFiles && files )
156 {
157 for ( std::string & file : files ) {
158 emplace_back( std::move( file ) );
159 }
160 }
161
162 void ListFiles::ReadDir( const std::string & path, const std::string & filter )
163 {
164 getFilesFromDirectory( path, filter, false, *this );
165 }
166
167 void ListFiles::FindFileInDir( const std::string_view path, const std::string_view fileName )
168 {
169 std::string correctedFilePath;
170 // If the file system is case-sensitive, then here we will get the actual file path using the case-insensitive
171 // search (if such a file exists). If the file system is case-insensitive, we will just get the passed path to
172 // the file back intact.
173 if ( !System::GetCaseInsensitivePath( System::concatPath( path, fileName ), correctedFilePath ) ) {
174 return;
175 }
176
177 // In case the file system is case-insensitive, we additionally check the file for existence.
178 if ( !System::IsFile( correctedFilePath ) ) {
179 return;
180 }
181
182 emplace_back( std::move( correctedFilePath ) );
183 }
184
185 bool ListFiles::IsEmpty( const std::string & path, const std::string & filter )
186 {
187 ListFiles list;
188 list.ReadDir( path, filter );
189 return list.empty();
190 }