Debian Codegraph @

mednafen/file.cpp

raw symbols SCIP
1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "mednafen.h"cxx . . $ `<file>/mednafen/mednafen.h`/
19 #include <stdarg.h>
20 #include <string.h>
21 #include <errno.h>
22
23 #include "file.h"cxx . . $ `<file>/mednafen/file.h`/
24 #include "general.h"cxx . . $ `<file>/mednafen/general.h`/
25
26 bool MDFNFILEcxx . . $ MDFNFILE#::ApplyIPScxx . . $ MDFNFILE#ApplyIPS(db00c6a08f501612).(void *unused)
27 {
28 (void)unused;
29
30 return 1;
31 }
32
33 // This function should ALWAYS close the system file "descriptor"(gzip library, zip library, or FILE *) it's given,
34 // even if it errors out.
35 bool MDFNFILEcxx . . $ MDFNFILE#::MakeMemWrapAndClosecxx . . $ MDFNFILE#MakeMemWrapAndClose(db00c6a08f501612).(void *fp)
36 {
37 bool ret = FALSEcxx . . $ `mednafen/mednafen-types.h:109:9`!;
38
39 locationcxx . . $ MDFNFILE#location. = 0;
40
41 ::fseek((FILEcxx . . $ FILE# *)fp, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!);
42 f_sizecxx . . $ MDFNFILE#f_size. = ::ftell((FILEcxx . . $ FILE# *)fp);
43 ::fseek((FILEcxx . . $ FILE# *)fp, 0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
44
45 if (!(f_datacxx . . $ MDFNFILE#f_data. = (uint8cxx . . $ uint8#*)malloccxx . . $ malloc(9f008b2a9698428b).(f_sizecxx . . $ MDFNFILE#f_size.)))
46 goto fail;
47 ::fread(f_datacxx . . $ MDFNFILE#f_data., 1, f_sizecxx . . $ MDFNFILE#f_size., (FILEcxx . . $ FILE# *)fp);
48
49 ret = TRUEcxx . . $ `mednafen/mednafen-types.h:113:9`!;
50 fail:
51 fclose((FILEcxx . . $ FILE#*)fp);
52 return ret;
53 }
54
55 MDFNFILEcxx . . $ MDFNFILE#::MDFNFILEcxx . . $ MDFNFILE#MDFNFILE(49f6e7a06ebc5aa8).()
56 {
57 f_datacxx . . $ MDFNFILE#f_data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
58 f_sizecxx . . $ MDFNFILE#f_size. = 0;
59 f_extcxx . . $ MDFNFILE#f_ext. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
60
61 locationcxx . . $ MDFNFILE#location. = 0;
62 }
63
64 MDFNFILEcxx . . $ MDFNFILE#::MDFNFILEcxx . . $ MDFNFILE#MDFNFILE(9c85eaa12893977b).(const char *path, const void *known_ext, const char *purpose)
65 {
66 (void)known_ext;
67 if (!Opencxx . . $ MDFNFILE#Open(eb4f300025480a43).(path, known_ext, purpose, false))
68 throw(MDFN_Errorcxx . . $ MDFN_Error#MDFN_Error(3021f9d48bee4f7).(0, "TODO ERROR"));
69 }
70
71
72 MDFNFILEcxx . . $ MDFNFILE#::~cxx . . $ MDFNFILE#`~MDFNFILE`(ced63f7c635d850d).MDFNFILEcxx . . $ MDFNFILE#()
73 {
74 Closecxx . . $ MDFNFILE#Close(c9857a73d68e59ba).();
75 }
76
77
78 bool MDFNFILEcxx . . $ MDFNFILE#::Opencxx . . $ MDFNFILE#Open(eb4f300025480a43).(const char *path, const void *known_ext, const char *purpose, const bool suppress_notfound_pe)
79 {
80 FILEcxx . . $ FILE# *fp;
81 (void)known_ext;
82
83 if (!(fp = fopen(path, "rb")))
84 return FALSEcxx . . $ `mednafen/mednafen-types.h:109:9`!;
85
86 ::fseek(fp, 0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
87
88 if (!MakeMemWrapAndClosecxx . . $ MDFNFILE#MakeMemWrapAndClose(db00c6a08f501612).(fp))
89 return FALSEcxx . . $ `mednafen/mednafen-types.h:109:9`!;
90
91 const char *ld = (const char*)strrchr(path, '.');
92 f_extcxx . . $ MDFNFILE#f_ext. = strdup(ld ? ld + 1 : "");
93
94 return(TRUEcxx . . $ `mednafen/mednafen-types.h:113:9`!);
95 }
96
97 bool MDFNFILEcxx . . $ MDFNFILE#::Closecxx . . $ MDFNFILE#Close(c9857a73d68e59ba).(void)
98 {
99 if (f_extcxx . . $ MDFNFILE#f_ext.)
100 freecxx . . $ free(bd21765a0afc8e3c).(f_extcxx . . $ MDFNFILE#f_ext.);
101 f_extcxx . . $ MDFNFILE#f_ext. = 0;
102
103 if (f_datacxx . . $ MDFNFILE#f_data.)
104 freecxx . . $ free(bd21765a0afc8e3c).(f_datacxx . . $ MDFNFILE#f_data.);
105 f_datacxx . . $ MDFNFILE#f_data. = 0;
106
107 return(1);
108 }
109
110 uint64cxx . . $ uint64# MDFNFILEcxx . . $ MDFNFILE#::freadcxx . . $ MDFNFILE#fread(e2eaf0fd878a292f).(void *ptr, size_t element_size, size_t nmemb)
111 {
112 int64cxx . . $ int64# avail = f_sizecxx . . $ MDFNFILE#f_size. - locationcxx . . $ MDFNFILE#location.;
113 if (nmemb > avail)
114 nmemb = avail;
115
116 memcpy((uint8cxx . . $ uint8#*)ptr, f_datacxx . . $ MDFNFILE#f_data. + locationcxx . . $ MDFNFILE#location., nmemb);
117 locationcxx . . $ MDFNFILE#location. += nmemb;
118 return nmemb;
119 }
120
121 int MDFNFILEcxx . . $ MDFNFILE#::fseekcxx . . $ MDFNFILE#fseek(4a3ed3c01b538bac).(int64cxx . . $ int64# offset, int whence)
122 {
123 size_t ptr;
124
125 switch(whence)
126 {
127 case SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!:
128 ptr = offset;
129 break;
130 case SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!:
131 ptr = locationcxx . . $ MDFNFILE#location. + offset;
132 break;
133 case SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!:
134 ptr = f_sizecxx . . $ MDFNFILE#f_size. + offset;
135 break;
136 }
137
138 if (ptr <= f_sizecxx . . $ MDFNFILE#f_size.)
139 {
140 locationcxx . . $ MDFNFILE#location. = ptr;
141 return 0;
142 }
143
144 return -1;
145 }
146
147 int MDFNFILEcxx . . $ MDFNFILE#::read16lecxx . . $ MDFNFILE#read16le(910e03eb156beb90).(uint16cxx . . $ uint16# *val)
148 {
149 if ((locationcxx . . $ MDFNFILE#location. + 2) > f_sizecxx . . $ MDFNFILE#f_size.)
150 return 0;
151
152 *val = MDFN_de16lsb(f_datacxx . . $ MDFNFILE#f_data. + locationcxx . . $ MDFNFILE#location.);
153
154 locationcxx . . $ MDFNFILE#location. += 2;
155
156 return 1;
157 }
158
159 int MDFNFILEcxx . . $ MDFNFILE#::read32lecxx . . $ MDFNFILE#read32le(4ebf403dd87dd80).(uint32cxx . . $ uint32# *val)
160 {
161 if ((locationcxx . . $ MDFNFILE#location. + 4) > f_sizecxx . . $ MDFNFILE#f_size.)
162 return 0;
163
164 *val = MDFN_de32lsb(f_datacxx . . $ MDFNFILE#f_data. + locationcxx . . $ MDFNFILE#location.);
165
166 locationcxx . . $ MDFNFILE#location. += 4;
167
168 return 1;
169 }
170
171 char *MDFNFILEcxx . . $ MDFNFILE#::fgetscxx . . $ MDFNFILE#fgets(ad13dc7cd1ddbf0a).(char *s, int buffer_size)
172 {
173 int pos = 0;
174
175 if (!buffer_size)
176 return(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
177
178 if (locationcxx . . $ MDFNFILE#location. >= buffer_size)
179 return(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
180
181 while(pos < (buffer_size - 1) && locationcxx . . $ MDFNFILE#location. < buffer_size)
182 {
183 int v = f_datacxx . . $ MDFNFILE#f_data.[locationcxx . . $ MDFNFILE#location.];
184 s[pos] = v;
185 locationcxx . . $ MDFNFILE#location.++;
186 pos++;
187 if (v == '\n')
188 break;
189 }
190
191 if (buffer_size)
192 s[pos] = 0;
193
194 return s;
195 }