| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cstdio> |
| 21 | #include <cstdlib> |
| 22 | #include <cstring> |
| 23 | #include <cstdarg> |
| 24 | #include <cerrno> |
| 25 | #include <cctype> |
| 26 | #include <filesystem> |
| 27 | #include <map> |
| 28 | #include <memory> |
| 29 | #include <vector> |
| 30 | |
| 31 | #if !defined(POSIXcxx . . $ `<built-in>:478:9`!) |
| 32 | // Non-Linux Build Includes |
| 33 | #include <io.h> |
| 34 | #else |
| 35 | // Linux Build Includes |
| 36 | #include "linux_fix.h"cxx . . $ `<file>/linux/linux_fix.h`/ |
| 37 | #endif |
| 38 | |
| 39 | #include "byteswap.h"cxx . . $ `<file>/lib/byteswap.h`/ |
| 40 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 41 | #include "ddio.h"cxx . . $ `<file>/ddio/ddio.h`/ |
| 42 | #include "psglob.h"cxx . . $ `<file>/misc/psglob.h`/ |
| 43 | #include "cfile.h"cxx . . $ `<file>/cfile/cfile.h`/ |
| 44 | #include "hogfile.h"cxx . . $ `<file>/cfile/hogfile.h`/ //info about library file |
| 45 | #include "mem.h"cxx . . $ `<file>/mem/mem.h`/ |
| 46 | |
| 47 | // Library structures |
| 48 | struct library_entrycxx . . $ library_entry# { |
| 49 | char namecxx . . $ library_entry#name.[PSFILENAME_LENcxx . . $ `lib/pstypes.h:31:9`! + 1]; // just the filename part |
| 50 | uint32_tcxx . . $ uint32_t# offsetcxx . . $ library_entry#offset.; // offset into library file |
| 51 | uint32_tcxx . . $ uint32_t# lengthcxx . . $ library_entry#length.; // length of this file |
| 52 | uint32_tcxx . . $ uint32_t# timestampcxx . . $ library_entry#timestamp.; // time and date of file |
| 53 | uint32_tcxx . . $ uint32_t# flagscxx . . $ library_entry#flags.; // misc flags |
| 54 | }; |
| 55 | |
| 56 | struct librarycxx . . $ library# { |
| 57 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# namecxx . . $ library#name.; // includes path + filename |
| 58 | uint32_tcxx . . $ uint32_t# nfilescxx . . $ library#nfiles. = 0; |
| 59 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<library_entrycxx . . $ library_entry#>> entriescxx . . $ library#entries.; |
| 60 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> nextcxx . . $ library#next.; |
| 61 | int handlecxx . . $ library#handle. = 0; // identifier for this lib |
| 62 | FILE *filecxx . . $ library#file. = nullptr; // pointer to file for this lib, if no one using it |
| 63 | }; |
| 64 | |
| 65 | // Map of paths. If value of entry is true, path is only for specific extensions |
| 66 | stdcxx . . $ std/::mapcxx . . $ std/map#<stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#, bool> pathscxx . . $ paths.; |
| 67 | |
| 68 | // Map of extensions <=> relevant paths |
| 69 | stdcxx . . $ std/::mapcxx . . $ std/map#<stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#, stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#> extensionscxx . . $ extensions.; |
| 70 | |
| 71 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> Librariescxx . . $ Libraries.; |
| 72 | int lib_handlecxx . . $ lib_handle. = 0; |
| 73 | |
| 74 | // Structure thrown on disk error |
| 75 | cfile_errorcxx . . $ cfile_error# cfecxx . . $ cfe.; |
| 76 | // The message for unexpected end of file |
| 77 | const char *eof_errorcxx . . $ eof_error. = "Unexpected end of file"; |
| 78 | |
| 79 | // Generates a cfile error |
| 80 | void ThrowCFileErrorcxx . . $ ThrowCFileError(ae9c7e42b2302f99).(int type, CFILEcxx . . $ CFILE# *file, const char *msg) { |
| 81 | cfecxx . . $ cfe..read_writecxx . . $ cfile_error#read_write. = type; |
| 82 | cfecxx . . $ cfe..msgcxx . . $ cfile_error#msg. = msg; |
| 83 | cfecxx . . $ cfe..filecxx . . $ cfile_error#file. = file; |
| 84 | throw &cfecxx . . $ cfe.; |
| 85 | } |
| 86 | |
| 87 | static void cf_Closecxx . . $ cf_Close(49f6e7a06ebc5aa8).(); |
| 88 | |
| 89 | // searches through the open HOG files, and opens a file if it finds it in any of the libs |
| 90 | static CFILEcxx . . $ CFILE# *open_file_in_libcxx . . $ open_file_in_lib(6cfe12765e653818).(const char *filename); |
| 91 | |
| 92 | // Opens a HOG file. Future calls to cfopen(), etc. will look in this HOG. |
| 93 | // Parameters: libname - the path & filename of the HOG file |
| 94 | // NOTE: libname must be valid for the entire execution of the program. Therefore, it should either |
| 95 | // be a fully-specified path name, or the current directory must not change. |
| 96 | // Returns: 0 if error, else library handle that can be used to close the library |
| 97 | int cf_OpenLibrarycxx . . $ cf_OpenLibrary(293b0826c8d9cb54).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &libname) { |
| 98 | FILE *fp; |
| 99 | int i; |
| 100 | uint32_tcxx . . $ uint32_t# offset; |
| 101 | static int first_time = 1; |
| 102 | tHogHeadercxx . . $ tHogHeader# header{}; |
| 103 | tHogFileEntrycxx . . $ tHogFileEntry# entry{}; |
| 104 | |
| 105 | // allocation library structure |
| 106 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> lib = stdcxx . . $ std/::make_sharedcxx . . $ std/make_shared(ffca0f3977536948).<librarycxx . . $ library#>(); |
| 107 | |
| 108 | // resolve library name |
| 109 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# resolve_dir = libname.parent_pathcxx . . $ std/filesystem/__cxx11/path#parent_path(159787a050b29eb0).(); |
| 110 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# resolve_name = libnamecxx . . $ std/filesystem/__cxx11/path#path(3f42e2c2fac0d5fc).; |
| 111 | |
| 112 | if (!resolve_dir.emptycxx . . $ std/filesystem/__cxx11/path#empty(3482b152b9333168).()) { |
| 113 | resolve_name =cxx . . $ std/filesystem/__cxx11/path#`operator=`(9c9aa2f17f237d0f). libname.filenamecxx . . $ std/filesystem/__cxx11/path#filename(159787a050b29eb0).(); |
| 114 | } |
| 115 | |
| 116 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# t_out = cf_FindRealFileNameCaseInsensitivecxx . . $ cf_FindRealFileNameCaseInsensitive(578e0438ad5f0e7e).(resolve_name, resolve_dir); |
| 117 | if (t_out.emptycxx . . $ std/filesystem/__cxx11/path#empty(3482b152b9333168).()) { |
| 118 | return 0; // CF_NO_FILE |
| 119 | } |
| 120 | // re-assemble |
| 121 | if (!resolve_dir.emptycxx . . $ std/filesystem/__cxx11/path#empty(3482b152b9333168).()) |
| 122 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).namecxx . . $ library#name. =cxx . . $ std/filesystem/__cxx11/path#`operator=`(9c9aa2f17f237d0f). resolve_dir /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). t_out; |
| 123 | else |
| 124 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).namecxx . . $ library#name. =cxx . . $ std/filesystem/__cxx11/path#`operator=`(554473e25f389c7d). t_out; |
| 125 | |
| 126 | fp = fopen(lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).namecxx . . $ library#name..u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "rb"); |
| 127 | if (fp == nullptr) { |
| 128 | return 0; // CF_NO_FILE; |
| 129 | } |
| 130 | // check if this if first library opened |
| 131 | if (first_time) { |
| 132 | atexitcxx . . $ atexit(cc8c495369588262).(cf_Closecxx . . $ cf_Close(49f6e7a06ebc5aa8).); |
| 133 | first_time = 0; |
| 134 | } |
| 135 | // read HOG header |
| 136 | if (!ReadHogHeadercxx . . $ ReadHogHeader(e75510b02592b94c).(fp, &header)) { |
| 137 | fclose(fp); |
| 138 | return 0; // CF_BAD_LIB; |
| 139 | } |
| 140 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nfilescxx . . $ library#nfiles. = header.nfilescxx . . $ tHogHeader#nfiles.; |
| 141 | // allocate CFILE hog info. |
| 142 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries..reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nfilescxx . . $ library#nfiles.); |
| 143 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next. =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). Librariescxx . . $ Libraries.; |
| 144 | Librariescxx . . $ Libraries. =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib; |
| 145 | // set data offset of first file |
| 146 | offset = header.file_data_offsetcxx . . $ tHogHeader#file_data_offset.; |
| 147 | // Go to index start |
| 148 | fseek(fp, HOG_HDR_SIZEcxx . . $ `cfile/hogfile.h:66:9`!, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 149 | |
| 150 | // read in index table |
| 151 | for (i = 0; i < lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nfilescxx . . $ library#nfiles.; i++) { |
| 152 | if (!ReadHogEntrycxx . . $ ReadHogEntry(3c4762c826749ad0).(fp, &entry)) { |
| 153 | fclose(fp); |
| 154 | return 0; |
| 155 | } |
| 156 | // Make sure files are in order |
| 157 | ASSERTcxx . . $ `misc/pserror.h:246:9`!((i == 0) || (stricmpcxx . . $ `linux/linux_fix.h:43:9`!(entry.name, lib->entries[i - 1]->name) >= 0)); |
| 158 | // Copy into table |
| 159 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<library_entrycxx . . $ library_entry#> lib_entry = stdcxx . . $ std/::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<library_entrycxx . . $ library_entry#>(); |
| 160 | strcpy(lib_entry->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name., entry.namecxx . . $ tHogFileEntry#name.); |
| 161 | lib_entry->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).flagscxx . . $ library_entry#flags. = entry.flagscxx . . $ tHogFileEntry#flags.; |
| 162 | lib_entry->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).lengthcxx . . $ library_entry#length. = entry.lencxx . . $ tHogFileEntry#len.; |
| 163 | lib_entry->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).offsetcxx . . $ library_entry#offset. = offset; |
| 164 | lib_entry->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).timestampcxx . . $ library_entry#timestamp. = entry.timestampcxx . . $ tHogFileEntry#timestamp.; |
| 165 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(lib_entry)); |
| 166 | |
| 167 | offset += entry.lencxx . . $ tHogFileEntry#len.; |
| 168 | } |
| 169 | // assign a handle |
| 170 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle. = ++lib_handlecxx . . $ lib_handle.; |
| 171 | // Save the file pointer |
| 172 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file. = fp; |
| 173 | // Success. Return the handle |
| 174 | return lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle.; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Closes a library file. |
| 179 | * @param handle the handle returned by cf_OpenLibrary() |
| 180 | */ |
| 181 | void cf_CloseLibrarycxx . . $ cf_CloseLibrary(d4f767463ce0a6b3).(int handle) { |
| 182 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> libcxx . . $ std/shared_ptr#shared_ptr(ced63f7c635d850d)., prevcxx . . $ std/shared_ptr#shared_ptr(ced63f7c635d850d).; |
| 183 | for (lib =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). Librariescxx . . $ Libraries.; lib; prev =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib, lib =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.) { |
| 184 | if (lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle. == handle) { |
| 185 | if (prev) |
| 186 | prev->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next. =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.; |
| 187 | else |
| 188 | Librariescxx . . $ Libraries. =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.; |
| 189 | if (lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file.) |
| 190 | fclose(lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file.); |
| 191 | return; // successful close |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Closes down the CFILE system, freeing up all data, etc. |
| 197 | void cf_Closecxx . . $ cf_Close(49f6e7a06ebc5aa8).() { |
| 198 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> nextcxx . . $ std/shared_ptr#shared_ptr(ced63f7c635d850d).; |
| 199 | while (Librariescxx . . $ Libraries.) { |
| 200 | next =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). Librariescxx . . $ Libraries.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.; |
| 201 | Librariescxx . . $ Libraries. =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). next; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | bool cf_SetSearchPathcxx . . $ cf_SetSearchPath(29ebe24842deaaf3).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &path, const stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#> &ext_list) { |
| 206 | // Don't add non-existing path into search paths |
| 207 | if (!stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::is_directorycxx . . $ std/filesystem/is_directory(c53a6ef3278968b1).(path)) |
| 208 | return false; |
| 209 | // Get & store full path |
| 210 | pathscxx . . $ paths..insert_or_assigncxx . . $ std/map#insert_or_assign(e23b589f5fc14fc4).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::absolutecxx . . $ std/filesystem/absolute(4ac62a867fe4334a).(path), !ext_list.emptycxx . . $ std/vector#empty(3482b152b9333168).()); |
| 211 | // Set extensions for this path |
| 212 | if (!ext_list.emptycxx . . $ std/vector#empty(3482b152b9333168).()) { |
| 213 | for (auto const &ext : ext_list) { |
| 214 | if (!ext.emptycxx . . $ std/filesystem/__cxx11/path#empty(3482b152b9333168).()) { |
| 215 | extensionscxx . . $ extensions..insert_or_assigncxx . . $ std/map#insert_or_assign(a356f559b7c7a273).(ext, path); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Removes all search paths that have been added by cf_SetSearchPath |
| 224 | */ |
| 225 | void cf_ClearAllSearchPathscxx . . $ cf_ClearAllSearchPaths(49f6e7a06ebc5aa8).() { |
| 226 | pathscxx . . $ paths..clearcxx . . $ std/map#clear(ced63f7c635d850d).(); |
| 227 | extensionscxx . . $ extensions..clearcxx . . $ std/map#clear(ced63f7c635d850d).(); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Opens a file for reading in a library, given the library id |
| 232 | * @param filename |
| 233 | * @param libhandle |
| 234 | * @return |
| 235 | */ |
| 236 | CFILEcxx . . $ CFILE# *cf_OpenFileInLibrarycxx . . $ cf_OpenFileInLibrary(b06e90447c91b9c9).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &filename, int libhandle) { |
| 237 | if (libhandle <= 0) |
| 238 | return nullptr; |
| 239 | |
| 240 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> lib = Librariescxx . . $ std/shared_ptr#shared_ptr(159b2ef84668bf93).; |
| 241 | |
| 242 | // find the library that we want to use |
| 243 | while (lib) { |
| 244 | if (lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle. == libhandle) |
| 245 | break; |
| 246 | lib =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.; |
| 247 | } |
| 248 | |
| 249 | if (nullptr ==cxx . . $ std/`operator==`(c9e7e956296fd68c). lib) { |
| 250 | // couldn't find the library handle |
| 251 | return nullptr; |
| 252 | } |
| 253 | |
| 254 | // now do a binary search for the file entry |
| 255 | int i, first = 0, last = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nfilescxx . . $ library#nfiles. - 1, c; |
| 256 | bool found = false; |
| 257 | |
| 258 | do { |
| 259 | i = (first + last) / 2; |
| 260 | c = stricmpcxx . . $ `linux/linux_fix.h:43:9`!(filename.u8string().c_str(), lib->entries[i]->name); // compare to current |
| 261 | if (c == 0) { |
| 262 | found = true; |
| 263 | break; |
| 264 | } |
| 265 | if (first >= last) // exhausted search |
| 266 | break; |
| 267 | if (c > 0) // search key after check key |
| 268 | first = i + 1; |
| 269 | else // search key before check key |
| 270 | last = i - 1; |
| 271 | } while (true); |
| 272 | |
| 273 | if (!found) |
| 274 | return nullptr; // file not in library |
| 275 | |
| 276 | // open the file for reading |
| 277 | FILE *fp; |
| 278 | int r; |
| 279 | // See if there's an available FILE |
| 280 | if (lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file.) { |
| 281 | fp = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file.; |
| 282 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file. = nullptr; |
| 283 | } else { |
| 284 | fp = fopen(lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).namecxx . . $ library#name..u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "rb"); |
| 285 | if (!fp) { |
| 286 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(1, "Error opening library <%s> when opening file <%s>; errno=%d.", |
| 287 | lib->name.u8string().c_str(), filename.u8string().c_str(), errno); |
| 288 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 289 | return nullptr; |
| 290 | } |
| 291 | } |
| 292 | CFILEcxx . . $ CFILE# *cfile = (CFILEcxx . . $ CFILE# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(*cfile)); |
| 293 | if (!cfile) |
| 294 | Errorcxx . . $ Error(326aeb464d16410c).("Out of memory in cf_OpenFileInLibrary()"); |
| 295 | cfile->namecxx . . $ CFILE#name. = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name.; |
| 296 | cfile->filecxx . . $ CFILE#file. = fp; |
| 297 | cfile->lib_handlecxx . . $ CFILE#lib_handle. = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle.; |
| 298 | cfile->sizecxx . . $ CFILE#size. = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).lengthcxx . . $ library_entry#length.; |
| 299 | cfile->lib_offsetcxx . . $ CFILE#lib_offset. = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).offsetcxx . . $ library_entry#offset.; |
| 300 | cfile->positioncxx . . $ CFILE#position. = 0; |
| 301 | cfile->flagscxx . . $ CFILE#flags. = 0; |
| 302 | r = fseek(fp, cfile->lib_offsetcxx . . $ CFILE#lib_offset., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 303 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(r == 0); |
| 304 | return cfile; |
| 305 | } |
| 306 | |
| 307 | // searches through the open HOG files, and opens a file if it finds it in any of the libs |
| 308 | CFILEcxx . . $ CFILE# *open_file_in_libcxx . . $ open_file_in_lib(6cfe12765e653818).(const char *filename) { |
| 309 | CFILEcxx . . $ CFILE# *cfile; |
| 310 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> lib = Librariescxx . . $ std/shared_ptr#shared_ptr(159b2ef84668bf93).; |
| 311 | while (lib) { |
| 312 | int i; |
| 313 | // Do binary search for the file |
| 314 | int first = 0, last = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nfilescxx . . $ library#nfiles. - 1, c, found = 0; |
| 315 | do { |
| 316 | i = (first + last) / 2; |
| 317 | c = stricmpcxx . . $ `linux/linux_fix.h:43:9`!(filename, lib->entries[i]->name); // compare to current |
| 318 | if (c == 0) { // found it |
| 319 | found = 1; |
| 320 | break; |
| 321 | } |
| 322 | if (first >= last) // exhausted search |
| 323 | break; |
| 324 | if (c > 0) // search key after check key |
| 325 | first = i + 1; |
| 326 | else // search key before check key |
| 327 | last = i - 1; |
| 328 | } while (true); |
| 329 | if (found) { |
| 330 | FILE *fp; |
| 331 | int r; |
| 332 | // See if there's an available FILE |
| 333 | if (lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file.) { |
| 334 | fp = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file.; |
| 335 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file. = nullptr; |
| 336 | } else { |
| 337 | fp = fopen(lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).namecxx . . $ library#name..u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "rb"); |
| 338 | if (!fp) { |
| 339 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(1, "Error opening library <%s> when opening file <%s>; errno=%d.", lib->name.u8string().c_str(), |
| 340 | filename, errno); |
| 341 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 342 | return nullptr; |
| 343 | } |
| 344 | } |
| 345 | cfile = (CFILEcxx . . $ CFILE# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(*cfile)); |
| 346 | if (!cfile) |
| 347 | Errorcxx . . $ Error(326aeb464d16410c).("Out of memory in open_file_in_lib()"); |
| 348 | cfile->namecxx . . $ CFILE#name. = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name.; |
| 349 | cfile->filecxx . . $ CFILE#file. = fp; |
| 350 | cfile->lib_handlecxx . . $ CFILE#lib_handle. = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle.; |
| 351 | cfile->sizecxx . . $ CFILE#size. = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).lengthcxx . . $ library_entry#length.; |
| 352 | cfile->lib_offsetcxx . . $ CFILE#lib_offset. = lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).offsetcxx . . $ library_entry#offset.; |
| 353 | cfile->positioncxx . . $ CFILE#position. = 0; |
| 354 | cfile->flagscxx . . $ CFILE#flags. = 0; |
| 355 | r = fseek(fp, cfile->lib_offsetcxx . . $ CFILE#lib_offset., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 356 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(r == 0); |
| 357 | return cfile; |
| 358 | } |
| 359 | lib =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.; |
| 360 | } |
| 361 | return nullptr; |
| 362 | } |
| 363 | |
| 364 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# cf_FindRealFileNameCaseInsensitivecxx . . $ cf_FindRealFileNameCaseInsensitive(578e0438ad5f0e7e).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &fname, |
| 365 | const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &directory) { |
| 366 | // Dumb check, maybe there already all ok? |
| 367 | if (existscxx . . $ std/filesystem/exists(c53a6ef3278968b1).((directory /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). fname))) { |
| 368 | return fname.filenamecxx . . $ std/filesystem/__cxx11/path#filename(159787a050b29eb0).(); |
| 369 | } |
| 370 | |
| 371 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# resultcxx . . $ std/filesystem/__cxx11/path#path(ced63f7c635d850d)., search_pathcxx . . $ std/filesystem/__cxx11/path#path(ced63f7c635d850d)., search_filecxx . . $ std/filesystem/__cxx11/path#path(ced63f7c635d850d).; |
| 372 | |
| 373 | search_path =cxx . . $ std/filesystem/__cxx11/path#`operator=`(9c9aa2f17f237d0f). directory /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). fname.parent_pathcxx . . $ std/filesystem/__cxx11/path#parent_path(159787a050b29eb0).(); |
| 374 | search_file =cxx . . $ std/filesystem/__cxx11/path#`operator=`(9c9aa2f17f237d0f). fname.filenamecxx . . $ std/filesystem/__cxx11/path#filename(159787a050b29eb0).(); |
| 375 | |
| 376 | // If directory does not exist, nothing to search. |
| 377 | if (!stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::is_directorycxx . . $ std/filesystem/is_directory(c53a6ef3278968b1).(search_path) || search_file.emptycxx . . $ std/filesystem/__cxx11/path#empty(3482b152b9333168).()) { |
| 378 | return {cxx . . $ std/filesystem/__cxx11/path#path(ced63f7c635d850d).}; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | // Search component in search_path |
| 383 | auto const &it = stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::directory_iteratorcxx . . $ std/filesystem/__cxx11/directory_iterator#(search_path); |
| 384 | |
| 385 | auto found = stdcxx . . $ std/::find_ifcxx . . $ std/find_if(f0a0a4f94ee5cb00).(itcxx . . $ std/filesystem/__cxx11/directory_iterator#directory_iterator(2b1c09f6362f5fab)., endcxx . . $ std/filesystem/__cxx11/end(9e6d9b12f6269414).(itcxx . . $ std/filesystem/__cxx11/directory_iterator#directory_iterator(2b1c09f6362f5fab).), [&search_file, &search_path, &result](const auto& dir_entry) { |
| 386 | return stricmpcxx . . $ `linux/linux_fix.h:43:9`!(dir_entry.path().filename().u8string().c_str(), search_file.u8string().c_str()) == 0; |
| 387 | }); |
| 388 | |
| 389 | if (found !=cxx . . $ std/filesystem/__cxx11/`operator!=`(8ee6a0f42479e7b4). endcxx . . $ std/filesystem/__cxx11/end(9e6d9b12f6269414).(itcxx . . $ std/filesystem/__cxx11/directory_iterator#directory_iterator(2b1c09f6362f5fab).)) { |
| 390 | // Match, append to result |
| 391 | result =cxx . . $ std/filesystem/__cxx11/path#`operator=`(554473e25f389c7d). found->cxx . . $ std/filesystem/__cxx11/directory_iterator#`operator->`(3c9087be5a309c38).pathcxx . . $ std/filesystem/__cxx11/directory_entry#path(3df3a2cdf6f4d926).(); |
| 392 | search_path =cxx . . $ std/filesystem/__cxx11/path#`operator=`(554473e25f389c7d). result; |
| 393 | } else { |
| 394 | // Component not found, mission failed |
| 395 | return {cxx . . $ std/filesystem/__cxx11/path#path(ced63f7c635d850d).}; |
| 396 | } |
| 397 | |
| 398 | return result.filenamecxx . . $ std/filesystem/__cxx11/path#filename(159787a050b29eb0).(); |
| 399 | } |
| 400 | |
| 401 | // look for the file in the specified directory |
| 402 | static CFILEcxx . . $ CFILE# *open_file_in_directorycxx . . $ open_file_in_directory(b5252ebdf252c7fc).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &filename, const char *mode, |
| 403 | const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &directory); |
| 404 | |
| 405 | // look for the file in the specified directory |
| 406 | CFILEcxx . . $ CFILE# *open_file_in_directorycxx . . $ open_file_in_directory(b5252ebdf252c7fc).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &filename, const char *mode, |
| 407 | const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &directory) { |
| 408 | FILE *fp; |
| 409 | CFILEcxx . . $ CFILE# *cfile; |
| 410 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# using_filenamecxx . . $ std/filesystem/__cxx11/path#path(ced63f7c635d850d).; |
| 411 | char tmode[3] = "rb"; |
| 412 | if (stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::is_directorycxx . . $ std/filesystem/is_directory(c53a6ef3278968b1).(directory)) { |
| 413 | // Make a full path |
| 414 | using_filename =cxx . . $ std/filesystem/__cxx11/path#`operator=`(9c9aa2f17f237d0f). directory /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). filename; |
| 415 | } else { |
| 416 | // no directory specified, so just use filename passed |
| 417 | using_filename =cxx . . $ std/filesystem/__cxx11/path#`operator=`(554473e25f389c7d). filename; |
| 418 | } |
| 419 | |
| 420 | // set read or write mode |
| 421 | tmode[0] = mode[0]; |
| 422 | // if mode is "w", then open in text or binary as requested. If "r", always open in "rb" |
| 423 | tmode[1] = (mode[0] == 'w') ? mode[1] : 'b'; |
| 424 | // try to open file |
| 425 | fp = fopen(using_filename.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), tmode); |
| 426 | |
| 427 | if (!fp) { |
| 428 | #if defined(POSIXcxx . . $ `<built-in>:478:9`!) |
| 429 | // If we tried to open file for reading, assume there maybe case-sensitive files |
| 430 | if (tmode[0] == 'r') { |
| 431 | // Try different cases of the filename |
| 432 | using_filename =cxx . . $ std/filesystem/__cxx11/path#`operator=`(9c9aa2f17f237d0f). cf_FindRealFileNameCaseInsensitivecxx . . $ cf_FindRealFileNameCaseInsensitive(578e0438ad5f0e7e).(filename, directory); |
| 433 | if (using_filename.emptycxx . . $ std/filesystem/__cxx11/path#empty(3482b152b9333168).()) { |
| 434 | // just give up |
| 435 | return nullptr; |
| 436 | } |
| 437 | |
| 438 | if (stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::is_directorycxx . . $ std/filesystem/is_directory(c53a6ef3278968b1).(directory)) { |
| 439 | // Make a full path |
| 440 | using_filename =cxx . . $ std/filesystem/__cxx11/path#`operator=`(9c9aa2f17f237d0f). directory /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). using_filename; |
| 441 | } |
| 442 | |
| 443 | fp = fopen(using_filename.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), tmode); |
| 444 | if (!fp) { |
| 445 | // no dice |
| 446 | return nullptr; |
| 447 | } |
| 448 | } else { |
| 449 | // Error on writing file |
| 450 | return nullptr; |
| 451 | } |
| 452 | #else |
| 453 | // We on incase-sensitive filesystem, no file means no file. |
| 454 | return nullptr; |
| 455 | #endif |
| 456 | } else { |
| 457 | using_filename =cxx . . $ std/filesystem/__cxx11/path#`operator=`(554473e25f389c7d). filename; |
| 458 | } |
| 459 | |
| 460 | // found the file, open it |
| 461 | cfile = (CFILEcxx . . $ CFILE# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(*cfile)); |
| 462 | if (!cfile) |
| 463 | Errorcxx . . $ Error(326aeb464d16410c).("Out of memory in open_file_in_directory()"); |
| 464 | cfile->namecxx . . $ CFILE#name. = (char *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(char) * (strlen(using_filename.u8string().c_str()) + 1)); |
| 465 | if (!cfile->namecxx . . $ CFILE#name.) |
| 466 | Errorcxx . . $ Error(326aeb464d16410c).("Out of memory in open_file_in_directory()"); |
| 467 | strcpy(cfile->namecxx . . $ CFILE#name., using_filename.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 468 | cfile->filecxx . . $ CFILE#file. = fp; |
| 469 | cfile->lib_handlecxx . . $ CFILE#lib_handle. = -1; |
| 470 | cfile->sizecxx . . $ CFILE#size. = ddio_GetFileLengthcxx . . $ ddio_GetFileLength(89bc1efcdac0ca22).(fp); |
| 471 | cfile->lib_offsetcxx . . $ CFILE#lib_offset. = 0; // 0 means on disk, not in HOG |
| 472 | cfile->positioncxx . . $ CFILE#position. = 0; |
| 473 | cfile->flagscxx . . $ CFILE#flags. = 0; |
| 474 | return cfile; |
| 475 | } |
| 476 | |
| 477 | // Opens a file for reading or writing |
| 478 | // If a path is specified, will try to open the file only in that path. |
| 479 | // If no path is specified, will look through search directories and library files. |
| 480 | // Parameters: filename - the name if the file, with or without a path |
| 481 | // mode - the standard C mode string |
| 482 | // Returns: the CFile handle, or NULL if file not opened |
| 483 | CFILEcxx . . $ CFILE# *cfopencxx . . $ cfopen(7c7b94e455f1ae56).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &filename, const char *mode) { |
| 484 | CFILEcxx . . $ CFILE# *cfile; |
| 485 | |
| 486 | // Check for valid mode |
| 487 | ASSERTcxx . . $ `misc/pserror.h:246:9`!((mode[0] == 'r') || (mode[0] == 'w')); |
| 488 | ASSERTcxx . . $ `misc/pserror.h:246:9`!((mode[1] == 'b') || (mode[1] == 't')); |
| 489 | // get the parts of the pathname |
| 490 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# path = filename.parent_pathcxx . . $ std/filesystem/__cxx11/path#parent_path(159787a050b29eb0).(); |
| 491 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# fname = filename.stemcxx . . $ std/filesystem/__cxx11/path#stem(159787a050b29eb0).(); |
| 492 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# ext = filename.extensioncxx . . $ std/filesystem/__cxx11/path#extension(159787a050b29eb0).(); |
| 493 | |
| 494 | // if there is a path specified, use it instead of the libraries, search dirs, etc. |
| 495 | // if the file is writable, just open it, instead of looking in libs, etc. |
| 496 | if (!path.emptycxx . . $ std/filesystem/__cxx11/path#empty(3482b152b9333168).() || (mode[0] == 'w')) { |
| 497 | // use path specified with file |
| 498 | cfile = open_file_in_directorycxx . . $ open_file_in_directory(b5252ebdf252c7fc).(filename, mode, stdcxx . . $ std/filesystem/__cxx11/path#path(ced63f7c635d850d).::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#()); |
| 499 | goto got_file; // don't look in libs, etc. |
| 500 | } |
| 501 | |
| 502 | // First look in the directories for this file's extension |
| 503 | for (auto const &entry : extensionscxx . . $ extensions.) { |
| 504 | if (!strnicmpcxx . . $ `linux/linux_fix.h:44:9`!(entry.first.u8string().c_str(), ext.u8string().c_str(), _MAX_EXTcxx . . $ `linux/linux_fix.h:36:9`!)) { |
| 505 | // found ext |
| 506 | cfile = open_file_in_directorycxx . . $ open_file_in_directory(b5252ebdf252c7fc).(filename, mode, entry.secondcxx . . $ std/pair#second.); |
| 507 | if (cfile) { |
| 508 | goto got_file; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Next look in the general directories |
| 514 | for (auto const &entry : pathscxx . . $ paths.) { |
| 515 | if (!entry.secondcxx . . $ std/pair#second.) { |
| 516 | cfile = open_file_in_directorycxx . . $ open_file_in_directory(b5252ebdf252c7fc).(filename, mode, entry.firstcxx . . $ std/pair#first.); |
| 517 | if (cfile) |
| 518 | goto got_file; |
| 519 | } |
| 520 | } |
| 521 | // Lastly, try the hog files |
| 522 | cfile = open_file_in_libcxx . . $ open_file_in_lib(6cfe12765e653818).(filename.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 523 | got_file:; |
| 524 | if (cfile) { |
| 525 | if (mode[0] == 'w') |
| 526 | cfile->flagscxx . . $ CFILE#flags. |= CFF_WRITINGcxx . . $ CFF_WRITING.; |
| 527 | if (mode[1] == 't') |
| 528 | cfile->flagscxx . . $ CFILE#flags. |= CFF_TEXTcxx . . $ CFF_TEXT.; |
| 529 | } |
| 530 | return cfile; |
| 531 | } |
| 532 | |
| 533 | // Returns the length of the specified file |
| 534 | // Parameters: cfp - the file pointer returned by cfopen() |
| 535 | uint32_tcxx . . $ uint32_t# cfilelengthcxx . . $ cfilelength(e4541f75b25d0ae5).(CFILEcxx . . $ CFILE# *cfp) { return cfp->sizecxx . . $ CFILE#size.; } |
| 536 | |
| 537 | // Closes an open CFILE. |
| 538 | // Parameters: cfile - the file pointer returned by cfopen() |
| 539 | void cfclosecxx . . $ cfclose(3639e799710acb68).(CFILEcxx . . $ CFILE# *cfp) { |
| 540 | // Either give the file back to the library, or close it |
| 541 | if (cfp->lib_handlecxx . . $ CFILE#lib_handle. != -1) { |
| 542 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> libcxx . . $ std/shared_ptr#shared_ptr(ced63f7c635d850d).; |
| 543 | for (lib =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). Librariescxx . . $ Libraries.; lib; lib =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.) { |
| 544 | if (lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle. == cfp->lib_handlecxx . . $ CFILE#lib_handle.) { // found the library |
| 545 | // if library doesn't already have a file, give it this one |
| 546 | if (lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file. == nullptr) { |
| 547 | lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).filecxx . . $ library#file. = cfp->filecxx . . $ CFILE#file.; |
| 548 | cfp->filecxx . . $ CFILE#file. = nullptr; |
| 549 | } |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | // If the file handle wasn't given back to library, close the file |
| 555 | if (cfp->filecxx . . $ CFILE#file.) |
| 556 | fclose(cfp->filecxx . . $ CFILE#file.); |
| 557 | // free the name, if allocated |
| 558 | if (!cfp->lib_offsetcxx . . $ CFILE#lib_offset.) |
| 559 | mem_freecxx . . $ `mem/mem.h:80:9`!(cfp->name); |
| 560 | // free the cfile struct |
| 561 | mem_freecxx . . $ `mem/mem.h:80:9`!(cfp); |
| 562 | } |
| 563 | |
| 564 | // Just like stdio fgetc(), except works on a CFILE |
| 565 | // Returns a char or EOF |
| 566 | int cfgetccxx . . $ cfgetc(413716a422df0036).(CFILEcxx . . $ CFILE# *cfp) { |
| 567 | int c; |
| 568 | static uint8_tcxx . . $ uint8_t# ch[3] = "\0\0"; |
| 569 | if (cfp->positioncxx . . $ CFILE#position. >= cfp->sizecxx . . $ CFILE#size.) |
| 570 | return EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!; |
| 571 | |
| 572 | fread(ch, sizeof(char), 1, cfp->filecxx . . $ CFILE#file.); |
| 573 | c = ch[0]; |
| 574 | // c = getc( cfp->file ); |
| 575 | if (cfeofcxx . . $ cfeof(413716a422df0036).(cfp)) |
| 576 | c = EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!; |
| 577 | if (c != EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!) { |
| 578 | cfp->positioncxx . . $ CFILE#position.++; |
| 579 | // do special newline handling for text files: |
| 580 | // if CR or LF by itself, return as newline |
| 581 | // if CR/LF pair, return as newline |
| 582 | if (cfp->flagscxx . . $ CFILE#flags. & CFF_TEXTcxx . . $ CFF_TEXT.) { |
| 583 | if (c == 10) // return LF as newline |
| 584 | c = '\n'; |
| 585 | else if (c == 13) { // check for CR/LF pair |
| 586 | fread(ch, sizeof(char), 1, cfp->filecxx . . $ CFILE#file.); |
| 587 | int cc = ch[0]; // getc(cfp->file); |
| 588 | // if (cc != EOF) { |
| 589 | if (!cfeofcxx . . $ cfeof(413716a422df0036).(cfp)) { |
| 590 | if (cc == 10) // line feed? |
| 591 | cfp->positioncxx . . $ CFILE#position.++; //..yes, so swallow it |
| 592 | else { |
| 593 | // ungetc(cc,cfp->file); //..no, so put it back |
| 594 | fseek(cfp->filecxx . . $ CFILE#file., -1, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 595 | } |
| 596 | } |
| 597 | c = '\n'; // return CR or CR/LF pair as newline |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | return c; |
| 602 | } |
| 603 | // Just like stdio fseek(), except works on a CFILE |
| 604 | int cfseekcxx . . $ cfseek(f9992d249f032805).(CFILEcxx . . $ CFILE# *cfp, long offset, int where) { |
| 605 | int c; |
| 606 | long goal_position; |
| 607 | switch (where) { |
| 608 | case SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!: |
| 609 | goal_position = offset; |
| 610 | break; |
| 611 | case SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!: |
| 612 | goal_position = cfp->positioncxx . . $ CFILE#position. + offset; |
| 613 | break; |
| 614 | case SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!: |
| 615 | goal_position = cfp->sizecxx . . $ CFILE#size. + offset; |
| 616 | break; |
| 617 | default: |
| 618 | return 1; |
| 619 | } |
| 620 | c = fseek(cfp->filecxx . . $ CFILE#file., cfp->lib_offsetcxx . . $ CFILE#lib_offset. + goal_position, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 621 | cfp->positioncxx . . $ CFILE#position. = ftell(cfp->filecxx . . $ CFILE#file.) - cfp->lib_offsetcxx . . $ CFILE#lib_offset.; |
| 622 | return c; |
| 623 | } |
| 624 | |
| 625 | // Just like stdio ftell(), except works on a CFILE |
| 626 | long cftellcxx . . $ cftell(65b457210a6528a1).(CFILEcxx . . $ CFILE# *cfp) { return cfp->positioncxx . . $ CFILE#position.; } |
| 627 | |
| 628 | // Returns true if at EOF |
| 629 | int cfeofcxx . . $ cfeof(413716a422df0036).(CFILEcxx . . $ CFILE# *cfp) { return (cfp->positioncxx . . $ CFILE#position. >= cfp->sizecxx . . $ CFILE#size.); } |
| 630 | |
| 631 | // Tells if the file exists |
| 632 | // Returns non-zero if file exists. Also tells if the file is on disk |
| 633 | // or in a hog - See return values in cfile.h |
| 634 | int cfexistcxx . . $ cfexist(293b0826c8d9cb54).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &filename) { |
| 635 | CFILEcxx . . $ CFILE# *cfp; |
| 636 | int ret; |
| 637 | |
| 638 | cfp = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(filename, "rb"); |
| 639 | if (!cfp) { // Didn't get file. Why? |
| 640 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!) // File exists, but couldn't open it |
| 641 | return CFES_ON_DISKcxx . . $ CFES_ON_DISK.; // so say it exists on the disk |
| 642 | |
| 643 | return CFES_NOT_FOUNDcxx . . $ CFES_NOT_FOUND.; // Say we didn't find the file |
| 644 | } |
| 645 | ret = cfp->lib_offsetcxx . . $ CFILE#lib_offset. ? CFES_IN_LIBRARYcxx . . $ CFES_IN_LIBRARY. : CFES_ON_DISKcxx . . $ CFES_ON_DISK.; |
| 646 | cfclosecxx . . $ cfclose(3639e799710acb68).(cfp); |
| 647 | return ret; |
| 648 | } |
| 649 | // Reads the specified number of bytes from a file into the buffer |
| 650 | // DO NOT USE THIS TO READ STRUCTURES. This function is for byte |
| 651 | // data, such as a string or a bitmap of 8-bit pixels. |
| 652 | // Returns the number of bytes read. |
| 653 | // Throws an exception of type (cfile_error *) if the OS returns an error on read |
| 654 | int cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(uint8_tcxx . . $ uint8_t# *buf, int count, CFILEcxx . . $ CFILE# *cfp) { |
| 655 | int i; |
| 656 | const char *error_msg = eof_errorcxx . . $ eof_error.; // default error |
| 657 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(!(cfp->flags & CFF_TEXT)); |
| 658 | if (cfp->positioncxx . . $ CFILE#position. + count <= cfp->sizecxx . . $ CFILE#size.) { |
| 659 | i = fread(buf, 1, count, cfp->filecxx . . $ CFILE#file.); |
| 660 | if (i == count) { |
| 661 | cfp->positioncxx . . $ CFILE#position. += i; |
| 662 | return i; |
| 663 | } |
| 664 | // if not EOF, then get the error message |
| 665 | if (!feof(cfp->filecxx . . $ CFILE#file.)) |
| 666 | error_msg = strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 667 | } |
| 668 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(1, "Error reading %d bytes from position %d of file <%s>; errno=%d.", count, cfp->position, cfp->name, errno); |
| 669 | return 0; |
| 670 | } |
| 671 | // The following functions read numeric vales from a CFILE. All values are |
| 672 | // stored in the file in Intel (little-endian) format. These functions |
| 673 | // will convert to big-endian if required. |
| 674 | // These funtions will exit the program with an error if the value |
| 675 | // cannot be read, so do not call these if you don't require the data |
| 676 | // to be present. |
| 677 | // Read and return an integer (32 bits) |
| 678 | // Throws an exception of type (cfile_error *) if the OS returns an error on read |
| 679 | int32_t cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(CFILEcxx . . $ CFILE# *cfp, bool little_endian) { |
| 680 | int32_t i; |
| 681 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)&i, sizeof(i), cfp); |
| 682 | return little_endian ? D3cxx . . $ D3/::convert_lecxx . . $ D3/convert_le(ada6a8422704cf8a).(i) : D3cxx . . $ D3/::convert_becxx . . $ D3/convert_be(ada6a8422704cf8a).(i); |
| 683 | } |
| 684 | // Read and return a int16_t (16 bits) |
| 685 | // Throws an exception of type (cfile_error *) if the OS returns an error on read |
| 686 | int16_t cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(CFILEcxx . . $ CFILE# *cfp, bool little_endian) { |
| 687 | int16_t i; |
| 688 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)&i, sizeof(i), cfp); |
| 689 | return little_endian ? D3cxx . . $ D3/::convert_lecxx . . $ D3/convert_le(ada6a8422704cf8a).(i) : D3cxx . . $ D3/::convert_becxx . . $ D3/convert_be(ada6a8422704cf8a).(i); |
| 690 | } |
| 691 | // Read and return a byte (8 bits) |
| 692 | // Throws an exception of type (cfile_error *) if the OS returns an error on read |
| 693 | int8_t cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(CFILEcxx . . $ CFILE# *cfp) { |
| 694 | int8_t i; |
| 695 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)&i, sizeof(i), cfp); |
| 696 | return i; |
| 697 | } |
| 698 | // Read and return a float (32 bits) |
| 699 | // Throws an exception of type (cfile_error *) if the OS returns an error on read |
| 700 | float cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(CFILEcxx . . $ CFILE# *cfp) { |
| 701 | float f; |
| 702 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)&f, sizeof(f), cfp); |
| 703 | return INTEL_FLOATcxx . . $ `lib/byteswap.h:109:9`!(f); |
| 704 | } |
| 705 | // Read and return a double (64 bits) |
| 706 | // Throws an exception of type (cfile_error *) if the OS returns an error on read |
| 707 | double cf_ReadDoublecxx . . $ cf_ReadDouble(96e019f41f67302c).(CFILEcxx . . $ CFILE# *cfp) { |
| 708 | double f; |
| 709 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)&f, sizeof(f), cfp); |
| 710 | return D3cxx . . $ D3/::convert_lecxx . . $ D3/convert_le(ada6a8422704cf8a).<double>(f); |
| 711 | } |
| 712 | // Reads a string from a CFILE. If the file is type binary, this |
| 713 | // function reads until a NULL or EOF is found. If the file is text, |
| 714 | // the function reads until a newline or EOF is found. The string is always |
| 715 | // written to the destination buffer null-terminated, without the newline. |
| 716 | // Parameters: buf - where the string is written |
| 717 | // n - the maximum string length, including the terminating 0 |
| 718 | // cfp - the CFILE pointer |
| 719 | // Returns the number of bytes in the string, before the terminator |
| 720 | // Does not generate an exception on EOF |
| 721 | int cf_ReadStringcxx . . $ cf_ReadString(38cd788e4423eff6).(char *buf, size_t n, CFILEcxx . . $ CFILE# *cfp) { |
| 722 | int c; |
| 723 | int count; |
| 724 | char *bp; |
| 725 | if (n == 0) |
| 726 | return -1; |
| 727 | bp = buf; |
| 728 | for (count = 0;; count++) { |
| 729 | c = cfgetccxx . . $ cfgetc(413716a422df0036).(cfp); |
| 730 | if (c == EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!) { |
| 731 | if (!cfeofcxx . . $ cfeof(413716a422df0036).(cfp)) // not actually at EOF, so must be error |
| 732 | ThrowCFileErrorcxx . . $ ThrowCFileError(ae9c7e42b2302f99).(CFE_READINGcxx . . $ CFE_READING., cfp, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | if ((!(cfp->flagscxx . . $ CFILE#flags. & CFF_TEXTcxx . . $ CFF_TEXT.) && (c == 0)) || ((cfp->flagscxx . . $ CFILE#flags. & CFF_TEXTcxx . . $ CFF_TEXT.) && (c == '\n'))) |
| 737 | break; // end-of-string |
| 738 | if (count < n - 1) // store char if room in buffer |
| 739 | *bp++ = c; |
| 740 | } |
| 741 | *bp = 0; // write terminator |
| 742 | return count; |
| 743 | } |
| 744 | // Writes the specified number of bytes from a file into the buffer |
| 745 | // DO NOT USE THIS TO WRITE STRUCTURES. This function is for byte |
| 746 | // data, such as a string or a bitmap of 8-bit pixels. |
| 747 | // Returns the number of bytes written. |
| 748 | // Throws an exception of type (cfile_error *) if the OS returns an error on write |
| 749 | int cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).(const uint8_tcxx . . $ uint8_t# *buf, int count, CFILEcxx . . $ CFILE# *cfp) { |
| 750 | int i; |
| 751 | if (!(cfp->flagscxx . . $ CFILE#flags. & CFF_WRITINGcxx . . $ CFF_WRITING.)) |
| 752 | return 0; |
| 753 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(count > 0); |
| 754 | i = fwrite(buf, 1, count, cfp->filecxx . . $ CFILE#file.); |
| 755 | cfp->positioncxx . . $ CFILE#position. += i; |
| 756 | if (i != count) |
| 757 | ThrowCFileErrorcxx . . $ ThrowCFileError(ae9c7e42b2302f99).(CFE_WRITINGcxx . . $ CFE_WRITING., cfp, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 758 | return i; |
| 759 | } |
| 760 | // Writes a null-terminated string to a file. If the file is type binary, |
| 761 | // the string is terminated in the file with a null. If the file is type |
| 762 | // text, the string is terminated with a newline. |
| 763 | // Parameters: buf - pointer to the string |
| 764 | // cfp = the CFILE pointer |
| 765 | // Returns the number of bytes written |
| 766 | // Throws an exception of type (cfile_error *) if the OS returns an error on write |
| 767 | int cf_WriteStringcxx . . $ cf_WriteString(643e1411966bd56c).(CFILEcxx . . $ CFILE# *cfp, const char *buf) { |
| 768 | int len; |
| 769 | len = strlen(buf); |
| 770 | if (len != 0) // write string |
| 771 | cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)buf, len, cfp); |
| 772 | // Terminate with newline (text file) or NULL (binary file) |
| 773 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(cfp, (cfp->flagscxx . . $ CFILE#flags. & CFF_TEXTcxx . . $ CFF_TEXT.) ? '\n' : 0); |
| 774 | return len + 1; |
| 775 | } |
| 776 | |
| 777 | // Just like stdio fprintf(), except works on a CFILE |
| 778 | int cfprintfcxx . . $ cfprintf(4762586383887345).(CFILEcxx . . $ CFILE# *cfp, const char *format, ...) { |
| 779 | va_list args; |
| 780 | int count; |
| 781 | va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`!(args, format); |
| 782 | count = vfprintf(cfp->filecxx . . $ CFILE#file., format, args); |
| 783 | va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`!(args); |
| 784 | cfp->positioncxx . . $ CFILE#position. += count + 1; // count doesn't include terminator |
| 785 | return count; |
| 786 | } |
| 787 | |
| 788 | // The following functions write numeric vales to a CFILE. All values are |
| 789 | // stored to the file in Intel (little-endian) format. |
| 790 | // All these throw an exception if there's an error on write. |
| 791 | // Write an integer (32 bits) |
| 792 | // Throws an exception of type (cfile_error *) if the OS returns an error on write |
| 793 | void cf_WriteIntcxx . . $ cf_WriteInt(966989f25543eb13).(CFILEcxx . . $ CFILE# *cfp, int32_t i) { |
| 794 | int t = INTEL_INTcxx . . $ `lib/byteswap.h:107:9`!(i); |
| 795 | cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)&t, sizeof(t), cfp); |
| 796 | } |
| 797 | |
| 798 | // Write a int16_t (16 bits) |
| 799 | // Throws an exception of type (cfile_error *) if the OS returns an error on write |
| 800 | void cf_WriteShortcxx . . $ cf_WriteShort(d7cf1a05aca96fe5).(CFILEcxx . . $ CFILE# *cfp, int16_t s) { |
| 801 | int16_t t = INTEL_SHORTcxx . . $ `lib/byteswap.h:108:9`!(s); |
| 802 | cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)&t, sizeof(t), cfp); |
| 803 | } |
| 804 | |
| 805 | // Write a byte (8 bits). |
| 806 | // Throws an exception of type (cfile_error *) if the OS returns an error on write |
| 807 | void cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(CFILEcxx . . $ CFILE# *cfp, int8_t b) { |
| 808 | if (fputc(b, cfp->filecxx . . $ CFILE#file.) == EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!) |
| 809 | ThrowCFileErrorcxx . . $ ThrowCFileError(ae9c7e42b2302f99).(CFE_WRITINGcxx . . $ CFE_WRITING., cfp, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 810 | cfp->positioncxx . . $ CFILE#position.++; |
| 811 | // If text file & writing newline, increment again for LF |
| 812 | if ((cfp->flagscxx . . $ CFILE#flags. & CFF_TEXTcxx . . $ CFF_TEXT.) && (b == '\n')) // check for text mode newline |
| 813 | cfp->positioncxx . . $ CFILE#position.++; |
| 814 | } |
| 815 | |
| 816 | // Write a float (32 bits) |
| 817 | // Throws an exception of type (cfile_error *) if the OS returns an error on write |
| 818 | void cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(CFILEcxx . . $ CFILE# *cfp, float f) { |
| 819 | float t = INTEL_FLOATcxx . . $ `lib/byteswap.h:109:9`!(f); |
| 820 | cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)&t, sizeof(t), cfp); |
| 821 | } |
| 822 | |
| 823 | // Write a double (64 bits) |
| 824 | // Throws an exception of type (cfile_error *) if the OS returns an error on write |
| 825 | void cf_WriteDoublecxx . . $ cf_WriteDouble(a93307bf66375cef).(CFILEcxx . . $ CFILE# *cfp, double d) { |
| 826 | auto t = D3cxx . . $ D3/::convert_lecxx . . $ D3/convert_le(ada6a8422704cf8a).<double>(d); |
| 827 | cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)&t, sizeof(t), cfp); |
| 828 | } |
| 829 | |
| 830 | // Copies a file. Returns TRUE if copied ok. Returns FALSE if error opening either file. |
| 831 | // Throws an exception of type (cfile_error *) if the OS returns an error on read or write |
| 832 | bool cf_CopyFilecxx . . $ cf_CopyFile(773c05b60430b9f6).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &dest, const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &src, int copytime) { |
| 833 | CFILEcxx . . $ CFILE# *infile, *outfile; |
| 834 | if (!stricmpcxx . . $ `linux/linux_fix.h:43:9`!(dest.u8string().c_str(), src.u8string().c_str())) |
| 835 | return true; // don't copy files if they are the same |
| 836 | infile = (CFILEcxx . . $ CFILE# *)cfopencxx . . $ cfopen(7c7b94e455f1ae56).(src, "rb"); |
| 837 | if (!infile) |
| 838 | return false; |
| 839 | outfile = (CFILEcxx . . $ CFILE# *)cfopencxx . . $ cfopen(7c7b94e455f1ae56).(dest, "wb"); |
| 840 | if (!outfile) { |
| 841 | cfclosecxx . . $ cfclose(3639e799710acb68).(infile); |
| 842 | return false; |
| 843 | } |
| 844 | int progress = 0; |
| 845 | int readcount = 0; |
| 846 | #define COPY_CHUNK_SIZEcxx . . $ `cfile/cfile.cpp:846:9`! 5000 |
| 847 | uint8_tcxx . . $ uint8_t# copybuf[COPY_CHUNK_SIZEcxx . . $ `cfile/cfile.cpp:846:9`!]; |
| 848 | while (!cfeofcxx . . $ cfeof(413716a422df0036).(infile)) { |
| 849 | // uint8_t c; |
| 850 | |
| 851 | if (progress + COPY_CHUNK_SIZEcxx . . $ `cfile/cfile.cpp:846:9`! <= infile->sizecxx . . $ CFILE#size.) { |
| 852 | readcount = COPY_CHUNK_SIZEcxx . . $ `cfile/cfile.cpp:846:9`!; |
| 853 | } else { |
| 854 | readcount = infile->sizecxx . . $ CFILE#size. - progress; |
| 855 | } |
| 856 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(copybuf, readcount, infile); |
| 857 | cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).(copybuf, readcount, outfile); |
| 858 | progress += readcount; |
| 859 | // c=cf_ReadByte (infile); |
| 860 | // cf_WriteByte (outfile,c); |
| 861 | } |
| 862 | cfclosecxx . . $ cfclose(3639e799710acb68).(infile); |
| 863 | cfclosecxx . . $ cfclose(3639e799710acb68).(outfile); |
| 864 | if (!infile->lib_offsetcxx . . $ CFILE#lib_offset. && copytime) { |
| 865 | cf_CopyFileTimecxx . . $ cf_CopyFileTime(df5f3ffd01de66e0).(dest, src); |
| 866 | } |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | // Checks to see if two files are different. |
| 871 | // Returns TRUE if the files are different, or FALSE if they are the same. |
| 872 | bool cf_Diffcxx . . $ cf_Diff(1b1fb50d0437e4a5).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &a, const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &b) { return (ddio_FileDiffcxx . . $ ddio_FileDiff(1b1fb50d0437e4a5).(a, b)); } |
| 873 | |
| 874 | // Copies the file time from one file to another |
| 875 | void cf_CopyFileTimecxx . . $ cf_CopyFileTime(df5f3ffd01de66e0).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &dest, const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &src) { |
| 876 | ddio_CopyFileTimecxx . . $ ddio_CopyFileTime(df5f3ffd01de66e0).(dest, src); |
| 877 | } |
| 878 | |
| 879 | // Changes a files attributes (ie read/write only) |
| 880 | void cf_ChangeFileAttributescxx . . $ cf_ChangeFileAttributes(34d26762b42c433a).(const char *name, int attr) { |
| 881 | if (_chmodcxx . . $ `linux/linux_fix.h:45:9`!(name, attr) == -1) |
| 882 | Int3cxx . . $ `misc/pserror.h:247:9`!(); // Get Jason or Matt, file not found! |
| 883 | } |
| 884 | |
| 885 | // rewinds cfile position |
| 886 | void cf_Rewindcxx . . $ cf_Rewind(3639e799710acb68).(CFILEcxx . . $ CFILE# *fp) { |
| 887 | if (fp->lib_offsetcxx . . $ CFILE#lib_offset.) { |
| 888 | int r = fseek(fp->filecxx . . $ CFILE#file., fp->lib_offsetcxx . . $ CFILE#lib_offset., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 889 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(r == 0); |
| 890 | } else { |
| 891 | rewind(fp->filecxx . . $ CFILE#file.); |
| 892 | } |
| 893 | fp->positioncxx . . $ CFILE#position. = 0; |
| 894 | } |
| 895 | |
| 896 | // Calculates a 32-bit CRC for the specified file. a return code of -1 means file note found |
| 897 | #define CRC32_POLYNOMIALcxx . . $ `cfile/cfile.cpp:897:9`! 0xEDB88320L |
| 898 | #define CRC_BUFFER_SIZEcxx . . $ `cfile/cfile.cpp:898:9`! 5000 |
| 899 | |
| 900 | uint32_tcxx . . $ uint32_t# cf_CalculateFileCRCcxx . . $ cf_CalculateFileCRC(e4541f75b25d0ae5).(CFILEcxx . . $ CFILE# *infile) { |
| 901 | int i, j; |
| 902 | uint8_tcxx . . $ uint8_t# crcbuf[CRC_BUFFER_SIZEcxx . . $ `cfile/cfile.cpp:898:9`!]; |
| 903 | static bool Cfile_crc_calculated = false; |
| 904 | static uint32_tcxx . . $ uint32_t# CRCTable[256]; |
| 905 | uint32_tcxx . . $ uint32_t# crc; |
| 906 | uint32_tcxx . . $ uint32_t# temp1; |
| 907 | uint32_tcxx . . $ uint32_t# temp2; |
| 908 | uint32_tcxx . . $ uint32_t# readlen; |
| 909 | |
| 910 | // Only make the lookup table once |
| 911 | if (!Cfile_crc_calculated) { |
| 912 | Cfile_crc_calculated = true; |
| 913 | |
| 914 | for (i = 0; i <= 255; i++) { |
| 915 | crc = i; |
| 916 | for (j = 8; j > 0; j--) { |
| 917 | if (crc & 1) |
| 918 | crc = (crc >> 1) ^ CRC32_POLYNOMIALcxx . . $ `cfile/cfile.cpp:897:9`!; |
| 919 | else |
| 920 | crc >>= 1; |
| 921 | } |
| 922 | CRCTable[i] = crc; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | crc = 0xffffffffl; |
| 927 | while (!cfeofcxx . . $ cfeof(413716a422df0036).(infile)) { |
| 928 | if ((infile->sizecxx . . $ CFILE#size. - infile->positioncxx . . $ CFILE#position.) < CRC_BUFFER_SIZEcxx . . $ `cfile/cfile.cpp:898:9`!) |
| 929 | readlen = infile->sizecxx . . $ CFILE#size. - infile->positioncxx . . $ CFILE#position.; |
| 930 | else |
| 931 | readlen = CRC_BUFFER_SIZEcxx . . $ `cfile/cfile.cpp:898:9`!; |
| 932 | if (!cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(crcbuf, readlen, infile)) { |
| 933 | // Doh, error time! |
| 934 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 935 | return 0xFFFFFFFF; |
| 936 | } |
| 937 | for (uint32_tcxx . . $ uint32_t# a = 0; a < readlen; a++) { |
| 938 | temp1 = (crc >> 8) & 0x00FFFFFFL; |
| 939 | temp2 = CRCTable[((int)crc ^ crcbuf[a]) & 0xff]; |
| 940 | crc = temp1 ^ temp2; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | return crc ^ 0xffffffffl; |
| 945 | } |
| 946 | |
| 947 | uint32_tcxx . . $ uint32_t# cf_GetfileCRCcxx . . $ cf_GetfileCRC(eb3222378d8a05e0).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &src) { |
| 948 | CFILEcxx . . $ CFILE# *infile = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(src, "rb"); |
| 949 | if (!infile) |
| 950 | return 0xFFFFFFFF; |
| 951 | |
| 952 | uint32_tcxx . . $ uint32_t# crc = cf_CalculateFileCRCcxx . . $ cf_CalculateFileCRC(e4541f75b25d0ae5).(infile); |
| 953 | cfclosecxx . . $ cfclose(3639e799710acb68).(infile); |
| 954 | |
| 955 | return crc; |
| 956 | } |
| 957 | |
| 958 | char cfile_search_wildcardcxx . . $ cfile_search_wildcard.[256]; |
| 959 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> cfile_search_librarycxx . . $ cfile_search_library.; |
| 960 | int cfile_search_curr_indexcxx . . $ cfile_search_curr_index. = 0; |
| 961 | bool cfile_search_ispatterncxx . . $ cfile_search_ispattern. = false; |
| 962 | // the following cf_LibraryFind function are similar to the ddio_Find functions as they look |
| 963 | // for files that match the wildcard passed in, however, this is to be used for hog files. |
| 964 | bool cf_LibraryFindFirstcxx . . $ cf_LibraryFindFirst(58fb7ae714029740).(int handle, const char *wildcard, char *buffer) { |
| 965 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(wildcard); |
| 966 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(buffer); |
| 967 | if (!wildcard || !buffer) |
| 968 | return false; |
| 969 | *buffer = '\0'; |
| 970 | if (cfile_search_librarycxx . . $ cfile_search_library.) |
| 971 | cf_LibraryFindClosecxx . . $ cf_LibraryFindClose(49f6e7a06ebc5aa8).(); |
| 972 | // find the library |
| 973 | cfile_search_librarycxx . . $ cfile_search_library. =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). Librariescxx . . $ Libraries.; |
| 974 | while (cfile_search_librarycxx . . $ cfile_search_library. && cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle. != handle) { |
| 975 | cfile_search_librarycxx . . $ cfile_search_library. =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.; |
| 976 | } |
| 977 | if (!cfile_search_librarycxx . . $ cfile_search_library.) |
| 978 | return false; |
| 979 | // now find the first matching file |
| 980 | strncpy(cfile_search_wildcardcxx . . $ cfile_search_wildcard., wildcard, 255); |
| 981 | cfile_search_wildcardcxx . . $ cfile_search_wildcard.[255] = '\0'; |
| 982 | cfile_search_ispatterncxx . . $ cfile_search_ispattern. = (bool)(PSGlobHasPatterncxx . . $ PSGlobHasPattern(1ef8e6ec083aabb9).(cfile_search_wildcardcxx . . $ cfile_search_wildcard.) != 0); |
| 983 | cfile_search_curr_indexcxx . . $ cfile_search_curr_index. = 0; |
| 984 | |
| 985 | while (cfile_search_curr_indexcxx . . $ cfile_search_curr_index. < cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nfilescxx . . $ library#nfiles.) { |
| 986 | if (cfile_search_ispatterncxx . . $ cfile_search_ispattern.) { |
| 987 | if (PSGlobMatchcxx . . $ PSGlobMatch(19ba54a6588ca5f0).(cfile_search_wildcardcxx . . $ cfile_search_wildcard., cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).cfile_search_curr_indexcxx . . $ cfile_search_curr_index.]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name., 0, 0)) { |
| 988 | // it's a match |
| 989 | strcpy(buffer, cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).cfile_search_curr_indexcxx . . $ cfile_search_curr_index.]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name.); |
| 990 | cfile_search_curr_indexcxx . . $ cfile_search_curr_index.++; |
| 991 | return true; |
| 992 | } |
| 993 | } else { |
| 994 | if (!stricmpcxx . . $ `linux/linux_fix.h:43:9`!(cfile_search_library->entries[cfile_search_curr_index]->name, cfile_search_wildcard)) { |
| 995 | strcpy(buffer, cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).cfile_search_curr_indexcxx . . $ cfile_search_curr_index.]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name.); |
| 996 | cfile_search_curr_indexcxx . . $ cfile_search_curr_index.++; |
| 997 | return true; |
| 998 | } |
| 999 | } |
| 1000 | cfile_search_curr_indexcxx . . $ cfile_search_curr_index.++; |
| 1001 | } |
| 1002 | // we didn't find a match |
| 1003 | return false; |
| 1004 | } |
| 1005 | |
| 1006 | bool cf_LibraryFindNextcxx . . $ cf_LibraryFindNext(c6b7e35ff6de5671).(char *buffer) { |
| 1007 | while (cfile_search_curr_indexcxx . . $ cfile_search_curr_index. < cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nfilescxx . . $ library#nfiles.) { |
| 1008 | if (cfile_search_ispatterncxx . . $ cfile_search_ispattern.) { |
| 1009 | if (PSGlobMatchcxx . . $ PSGlobMatch(19ba54a6588ca5f0).(cfile_search_wildcardcxx . . $ cfile_search_wildcard., cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).cfile_search_curr_indexcxx . . $ cfile_search_curr_index.]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name., 0, 0)) { |
| 1010 | // it's a match |
| 1011 | strcpy(buffer, cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).cfile_search_curr_indexcxx . . $ cfile_search_curr_index.]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name.); |
| 1012 | cfile_search_curr_indexcxx . . $ cfile_search_curr_index.++; |
| 1013 | return true; |
| 1014 | } |
| 1015 | } else { |
| 1016 | if (!stricmpcxx . . $ `linux/linux_fix.h:43:9`!(cfile_search_library->entries[cfile_search_curr_index]->name, cfile_search_wildcard)) { |
| 1017 | strcpy(buffer, cfile_search_librarycxx . . $ cfile_search_library.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).entriescxx . . $ library#entries.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).cfile_search_curr_indexcxx . . $ cfile_search_curr_index.]->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).namecxx . . $ library_entry#name.); |
| 1018 | cfile_search_curr_indexcxx . . $ cfile_search_curr_index.++; |
| 1019 | return true; |
| 1020 | } |
| 1021 | } |
| 1022 | cfile_search_curr_indexcxx . . $ cfile_search_curr_index.++; |
| 1023 | } |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
| 1027 | void cf_LibraryFindClosecxx . . $ cf_LibraryFindClose(49f6e7a06ebc5aa8).() { |
| 1028 | cfile_search_librarycxx . . $ cfile_search_library. =cxx . . $ std/shared_ptr#`operator=`(3501e79a789ff953). nullptrcxx . . $ std/shared_ptr#shared_ptr(6d988fd07f070dfc).; |
| 1029 | cfile_search_curr_indexcxx . . $ cfile_search_curr_index. = 0; |
| 1030 | cfile_search_ispatterncxx . . $ cfile_search_ispattern. = false; |
| 1031 | } |
| 1032 | |
| 1033 | bool cf_IsFileInHogcxx . . $ cf_IsFileInHog(1b1fb50d0437e4a5).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &filename, const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &hogname) { |
| 1034 | stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<librarycxx . . $ library#> lib = Librariescxx . . $ std/shared_ptr#shared_ptr(159b2ef84668bf93).; |
| 1035 | |
| 1036 | while (lib) { |
| 1037 | if (stricmpcxx . . $ `linux/linux_fix.h:43:9`!(lib->name.u8string().c_str(), hogname.u8string().c_str()) == 0) { |
| 1038 | // Now look for filename |
| 1039 | CFILEcxx . . $ CFILE# *cf; |
| 1040 | cf = cf_OpenFileInLibrarycxx . . $ cf_OpenFileInLibrary(b06e90447c91b9c9).(filename, lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).handlecxx . . $ library#handle.); |
| 1041 | if (cf) { |
| 1042 | cfclosecxx . . $ cfclose(3639e799710acb68).(cf); |
| 1043 | return true; |
| 1044 | } |
| 1045 | } |
| 1046 | lib =cxx . . $ std/shared_ptr#`operator=`(7563df7e21f73290). lib->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).nextcxx . . $ library#next.; |
| 1047 | } |
| 1048 | |
| 1049 | return false; |
| 1050 | } |