| 1 | /* |
| 2 | * Copyright (C) 2001,2006 Dizzy |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #define PDIR_INTERNAL_ACCESScxx . . $ `src/compat/pdir.cpp:18:9`! |
| 19 | #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 20 | #include "pdir.h"cxx . . $ `<file>/src/compat/pdir.h`/ |
| 21 | |
| 22 | #include <cstring> |
| 23 | |
| 24 | #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/ |
| 25 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 26 | #include "compat/strcasecmp.h"cxx . . $ `<file>/src/compat/strcasecmp.h`/ |
| 27 | |
| 28 | #ifdef WIN32 |
| 29 | #include "win32/dirent.h" |
| 30 | #else |
| 31 | #include <dirent.h> |
| 32 | #endif |
| 33 | |
| 34 | namespace pvpgncxx . . $ pvpgn/ |
| 35 | { |
| 36 | |
| 37 | Directorycxx . . $ pvpgn/Directory#::Directorycxx . . $ pvpgn/Directory#Directory(a3b17e0268c60856).(const stdcxx . . $ std/::stringcxx . . $ std/string#& path_, bool lazyread_) |
| 38 | { |
| 39 | opencxx . . $ pvpgn/Directory#open(a3b17e0268c60856).(path_, lazyread_); |
| 40 | } |
| 41 | |
| 42 | Directorycxx . . $ pvpgn/Directory#::~cxx . . $ pvpgn/Directory#`~Directory`(ced63f7c635d850d).Directorycxx . . $ pvpgn/Directory#() throw() |
| 43 | { |
| 44 | closecxx . . $ pvpgn/Directory#close(49f6e7a06ebc5aa8).(); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | Directorycxx . . $ pvpgn/Directory#::closecxx . . $ pvpgn/Directory#close(49f6e7a06ebc5aa8).() |
| 49 | { |
| 50 | #ifdef WIN32 |
| 51 | if (lFindHandle >= 0) |
| 52 | _findclose(lFindHandle); |
| 53 | #else /* POSIX */ |
| 54 | if (dircxx . . $ pvpgn/Directory#dir.) closedir(dircxx . . $ pvpgn/Directory#dir.); |
| 55 | #endif /* WIN32-POSIX */ |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | Directorycxx . . $ pvpgn/Directory#::opencxx . . $ pvpgn/Directory#open(a3b17e0268c60856).(const stdcxx . . $ std/::stringcxx . . $ std/string#& path_, bool lazyread_) |
| 60 | { |
| 61 | #ifdef WIN32 |
| 62 | std::string tmp(path_); |
| 63 | |
| 64 | if (tmp.size() + 1 + 3 >= _MAX_PATH) |
| 65 | throw std::runtime_error("pvpgn::Directory::Directory(): WIN32: path too long"); |
| 66 | tmp += "/*.*"; |
| 67 | |
| 68 | status = 0; |
| 69 | std::memset(&fileinfo, 0, sizeof(fileinfo)); |
| 70 | lFindHandle = _findfirst(tmp.c_str(), &fileinfo); |
| 71 | if (lFindHandle < 0 && !lazyread_) |
| 72 | throw OpenError(tmp); |
| 73 | |
| 74 | path = tmp; |
| 75 | #else /* POSIX style */ |
| 76 | if (!(dircxx . . $ pvpgn/Directory#dir. = opendir(path_.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).())) && !lazyread_) |
| 77 | throw OpenErrorcxx . . $ pvpgn/Directory#OpenError#(pathcxx . . $ pvpgn/Directory#path.); |
| 78 | pathcxx . . $ pvpgn/Directory#path. =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). path_; |
| 79 | #endif /* WIN32-POSIX */ |
| 80 | |
| 81 | lazyreadcxx . . $ pvpgn/Directory#lazyread. = lazyread_; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | Directorycxx . . $ pvpgn/Directory#::rewindcxx . . $ pvpgn/Directory#rewind(49f6e7a06ebc5aa8).() |
| 86 | { |
| 87 | #ifdef WIN32 |
| 88 | close(); |
| 89 | status = 0; |
| 90 | std::memset(&fileinfo, 0, sizeof(fileinfo)); |
| 91 | lFindHandle = _findfirst(path.c_str(), &fileinfo); |
| 92 | if (lFindHandle < 0 && !lazyread) { |
| 93 | ERROR0("WIN32: couldn't rewind directory"); |
| 94 | status = -1; |
| 95 | } |
| 96 | #else /* POSIX */ |
| 97 | if (dircxx . . $ pvpgn/Directory#dir.) rewinddir(dircxx . . $ pvpgn/Directory#dir.); |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | |
| 102 | char const * |
| 103 | Directorycxx . . $ pvpgn/Directory#::readcxx . . $ pvpgn/Directory#read(a9f41ea0e82d88cf).() const |
| 104 | { |
| 105 | const char * result; |
| 106 | |
| 107 | #ifdef WIN32 |
| 108 | switch (status) { |
| 109 | default: |
| 110 | case -1: /* couldn't rewind */ |
| 111 | ERROR0("got status -1"); |
| 112 | return 0; |
| 113 | case 0: /* freshly opened */ |
| 114 | status = 1; |
| 115 | if (lFindHandle < 0) return 0; |
| 116 | result = fileinfo.name; |
| 117 | break; |
| 118 | case 1: /* reading */ |
| 119 | if (lFindHandle < 0) return 0; |
| 120 | |
| 121 | if (_findnext(lFindHandle, &fileinfo) < 0) { |
| 122 | status = 2; |
| 123 | return 0; |
| 124 | } |
| 125 | else result = fileinfo.name; |
| 126 | break; |
| 127 | case 2: /* EOF */ |
| 128 | return 0; |
| 129 | } |
| 130 | #else /* POSIX */ |
| 131 | struct dirent *dentry = dircxx . . $ pvpgn/Directory#dir. ? readdir(dircxx . . $ pvpgn/Directory#dir.) : 0; |
| 132 | if (!dentry) return 0; |
| 133 | |
| 134 | result = dentry->d_name; |
| 135 | #endif /* WIN32-POSIX */ |
| 136 | |
| 137 | if (!(strcmp(result, ".") && strcmp(result, ".."))) |
| 138 | /* here we presume we don't get an infinite number of "." or ".." ;) */ |
| 139 | return readcxx . . $ pvpgn/Directory#read(a9f41ea0e82d88cf).(); |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | Directorycxx . . $ pvpgn/Directory#::operatorcxx . . $ pvpgn/Directory#`operator bool`(50ce9a9e25b4a850). bool() const |
| 144 | { |
| 145 | #ifdef WIN32 |
| 146 | return lFindHandle >= 0; |
| 147 | #else |
| 148 | return dircxx . . $ pvpgn/Directory#dir. != 0; |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | bool is_directorycxx . . $ pvpgn/is_directory(c5f7da30cbc0f4e9).(const char* pzPath); |
| 153 | |
| 154 | |
| 155 | |
| 156 | /* Returns a list of files in a directory (except the ones that begin with a dot) */ |
| 157 | extern stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> dir_getfilescxx . . $ pvpgn/dir_getfiles(aac66aa6092d8307).(const char * directory, const char* ext, bool recursive) |
| 158 | { |
| 159 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> filescxx . . $ std/vector#vector(49f6e7a06ebc5aa8)., dfilescxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 160 | const char* _ext; |
| 161 | |
| 162 | DIR *dir; |
| 163 | struct dirent* ent; |
| 164 | |
| 165 | dir = opendir(directory); |
| 166 | if (!dir) |
| 167 | return filescxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 168 | |
| 169 | while ((ent = readdir(dir)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 170 | { |
| 171 | const stdcxx . . $ std/::stringcxx . . $ std/string# file_name = entcxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).->d_name; |
| 172 | const stdcxx . . $ std/::stringcxx . . $ std/string# full_file_name = stdcxx . . $ std/::stringcxx . . $ std/string#(directory) +cxx . . $ std/operator+(900ec9581c410ad9). "/" +cxx . . $ std/operator+(db5dc2ea912c6495). file_name; |
| 173 | |
| 174 | if (file_name[cxx . . $ std/__cxx11/basic_string#`operator[]`(dd01cf3f78ddc127).0] == '.') |
| 175 | continue; |
| 176 | |
| 177 | if (is_directorycxx . . $ pvpgn/is_directory(c5f7da30cbc0f4e9).(full_file_name.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).())) |
| 178 | { |
| 179 | // iterate subdirectories |
| 180 | if (recursive) |
| 181 | { |
| 182 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> subfiles = dir_getfilescxx . . $ pvpgn/dir_getfiles(aac66aa6092d8307).(full_file_name.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), ext, recursive); |
| 183 | |
| 184 | for (stdcxx . . $ std/::size_tcxx . . $ std/size_t# i = 0; i < subfiles.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); ++i) |
| 185 | dfiles.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(subfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i]); |
| 186 | } |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | // filter by extension |
| 191 | _ext = strrchr(file_name.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), '.'); |
| 192 | if (strcmp(ext, "*") != 0) |
| 193 | if (!_ext || strcasecmp(_ext, ext) != 0) |
| 194 | continue; |
| 195 | |
| 196 | files.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(full_file_name); |
| 197 | } |
| 198 | closedir(dir); |
| 199 | |
| 200 | // merge files and files from directories, so we will receive directories at begin, files at the end |
| 201 | // (otherwise files and directories are read alphabetically - as is) |
| 202 | files.insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).(filescxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..begincxx . . $ std/vector#begin(55a6de06420d4903).(), dfiles.begincxx . . $ std/vector#begin(55a6de06420d4903).(), dfiles.endcxx . . $ std/vector#end(55a6de06420d4903).()); |
| 203 | |
| 204 | return filescxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 205 | } |
| 206 | |
| 207 | bool is_directorycxx . . $ pvpgn/is_directory(c5f7da30cbc0f4e9).(const char* pzPath) |
| 208 | { |
| 209 | if (pzPath == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) return false; |
| 210 | |
| 211 | DIR *pDir; |
| 212 | bool bExists = false; |
| 213 | |
| 214 | pDir = opendir(pzPath); |
| 215 | |
| 216 | if (pDir != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 217 | { |
| 218 | bExists = true; |
| 219 | (void)closedir(pDir); |
| 220 | } |
| 221 | |
| 222 | return bExists; |
| 223 | } |
| 224 | } |