| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (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, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "file.h"cxx . . $ `<file>/src/file.h`/ |
| 20 | |
| 21 | #include "opentyr.h"cxx . . $ `<file>/src/opentyr.h`/ |
| 22 | #include "varz.h"cxx . . $ `<file>/src/varz.h`/ |
| 23 | |
| 24 | #include "SDL.h" |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | const char *custom_data_dircxx . . $ custom_data_dir. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 32 | |
| 33 | // finds the Tyrian data directory |
| 34 | const char *data_dircxx . . $ data_dir(33a1f659e94feb42).(void) |
| 35 | { |
| 36 | const char *const dirs[] = |
| 37 | { |
| 38 | custom_data_dircxx . . $ custom_data_dir., |
| 39 | TYRIAN_DIRcxx . . $ `<built-in>:412:9`!, |
| 40 | "data", |
| 41 | ".", |
| 42 | }; |
| 43 | |
| 44 | static const char *dir = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 45 | |
| 46 | if (dir != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 47 | return dir; |
| 48 | |
| 49 | for (uintcxx . . $ uint# i = 0; i < COUNTOFcxx . . $ `src/opentyr.h:27:9`!(dirs); ++i) |
| 50 | { |
| 51 | if (dirs[i] == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 52 | continue; |
| 53 | |
| 54 | FILEcxx . . $ FILE# *f = dir_fopencxx . . $ dir_fopen(2493aa730c89ee72).(dirs[i], "tyrian1.lvl", "rb"); |
| 55 | if (f) |
| 56 | { |
| 57 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 58 | |
| 59 | dir = dirs[i]; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (dir == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) // data not found |
| 65 | dir = ""; |
| 66 | |
| 67 | return dir; |
| 68 | } |
| 69 | |
| 70 | // prepend directory and fopen |
| 71 | FILEcxx . . $ FILE# *dir_fopencxx . . $ dir_fopen(2493aa730c89ee72).(const char *dir, const char *file, const char *mode) |
| 72 | { |
| 73 | char *path = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(strlencxx . . $ strlen(751346f8406fd082).(dir) + 1 + strlencxx . . $ strlen(751346f8406fd082).(file) + 1); |
| 74 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(path, "%s/%s", dir, file); |
| 75 | |
| 76 | FILEcxx . . $ FILE# *f = fopencxx . . $ fopen(82ec0787cd37e0a3).(path, mode); |
| 77 | |
| 78 | freecxx . . $ free(5d22bdacc48458e8).(path); |
| 79 | |
| 80 | return f; |
| 81 | } |
| 82 | |
| 83 | // warn when dir_fopen fails |
| 84 | FILEcxx . . $ FILE# *dir_fopen_warncxx . . $ dir_fopen_warn(2493aa730c89ee72).(const char *dir, const char *file, const char *mode) |
| 85 | { |
| 86 | FILEcxx . . $ FILE# *f = dir_fopencxx . . $ dir_fopen(2493aa730c89ee72).(dir, file, mode); |
| 87 | |
| 88 | if (f == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 89 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "warning: failed to open '%s': %s\n", file, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 90 | |
| 91 | return f; |
| 92 | } |
| 93 | |
| 94 | // die when dir_fopen fails |
| 95 | FILEcxx . . $ FILE# *dir_fopen_diecxx . . $ dir_fopen_die(2493aa730c89ee72).(const char *dir, const char *file, const char *mode) |
| 96 | { |
| 97 | FILEcxx . . $ FILE# *f = dir_fopencxx . . $ dir_fopen(2493aa730c89ee72).(dir, file, mode); |
| 98 | |
| 99 | if (f == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 100 | { |
| 101 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: failed to open '%s': %s\n", file, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 102 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: One or more of the required Tyrian " TYRIAN_VERSIONcxx . . $ `src/opentyr.h:54:9`! " data files could not be found.\n" |
| 103 | " Please read the README file.\n"); |
| 104 | JE_tyrianHaltcxx . . $ JE_tyrianHalt(ac50b47256ae7414).(1); |
| 105 | } |
| 106 | |
| 107 | return f; |
| 108 | } |
| 109 | |
| 110 | // check if file can be opened for reading |
| 111 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! dir_file_existscxx . . $ dir_file_exists(b7a7aa4a824b7595).(const char *dir, const char *file) |
| 112 | { |
| 113 | FILEcxx . . $ FILE# *f = dir_fopencxx . . $ dir_fopen(2493aa730c89ee72).(dir, file, "rb"); |
| 114 | if (f != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 115 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 116 | return (f != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 117 | } |
| 118 | |
| 119 | // returns end-of-file position |
| 120 | long ftell_eofcxx . . $ ftell_eof(1dcafcd8ef4c6e6c).(FILEcxx . . $ FILE# *f) |
| 121 | { |
| 122 | long pos = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 123 | |
| 124 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!); |
| 125 | long size = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 126 | |
| 127 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, pos, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 128 | |
| 129 | return size; |
| 130 | } |
| 131 | |
| 132 | void fread_diecxx . . $ fread_die(82d71c9ec5b61e0f).(void *buffer, size_t size, size_t count, FILEcxx . . $ FILE# *stream) |
| 133 | { |
| 134 | size_t result = freadcxx . . $ fread(738fc8ac1d711e58).(buffer, size, count, stream); |
| 135 | if (result != count) |
| 136 | { |
| 137 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: An unexpected problem occurred while reading from a file.\n"); |
| 138 | SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).(); |
| 139 | exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void fwrite_diecxx . . $ fwrite_die(8541cce38b5beb5e).(const void *buffer, size_t size, size_t count, FILEcxx . . $ FILE# *stream) |
| 144 | { |
| 145 | size_t result = fwritecxx . . $ fwrite(ab0609fa07c47d6e).(buffer, size, count, stream); |
| 146 | if (result != count) |
| 147 | { |
| 148 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: An unexpected problem occurred while writing to a file.\n"); |
| 149 | SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).(); |
| 150 | exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!); |
| 151 | } |
| 152 | } |