Debian Codegraph @

cfile/cfile.h

raw symbols SCIP
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 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/lib/CFILE.H $
21 * $Revision: 16 $
22 * $Date: 10/18/99 1:27p $
23 * $Author: Kevin $
24 *
25 * Functions for reading & writing files. Includes code to read from libraries.
26 *
27 * $Log: /DescentIII/Main/lib/CFILE.H $
28 *
29 * 16 10/18/99 1:27p Kevin
30 * Added cf_IsFileInHog
31 *
32 * 15 9/14/99 7:49p Jeff
33 * added cf_OpenFileInLibrary() to force a file to be opened from a
34 * specific library. Added a way to get a crc of a file given it's CFILE
35 * *.
36 *
37 * 14 5/20/99 5:32p Matt
38 * Store a lib handle, instead of a lib pointer, in the cfile struct.
39 * This will keep us from using a pointer to lib that's already been
40 * closed.
41 *
42 * 13 3/22/99 6:26p Matt
43 * Cleaned up error handling in cfile and editor level loads.
44 *
45 * 12 1/07/99 10:51p Jeff
46 * added psglob and support to do find in files for hog files
47 *
48 * 11 11/16/98 3:49p Jason
49 * changes for manage system
50 *
51 * 10 11/11/98 2:58p Jeff
52 * added cf_ClearAllSearchPaths() function
53 *
54 * 9 10/22/98 10:48a Matt
55 * Added code to keep the library file open all the time, which will
56 * hopefully speed up file loads.
57 *
58 * 8 8/14/98 6:31p Matt
59 * Changed comment
60 *
61 * 7 7/28/98 12:27p Kevin
62 * Added CRC function
63 *
64 * 6 7/09/98 8:33p Samir
65 * Added cf_Rewind.
66 *
67 * 5 3/19/98 3:18p Samir
68 * enforce constant char* arguments when needed. done in CFILE and bitmap
69 * libraries as well as ddio.
70 *
71 * 4 2/26/98 11:01a Jason
72 * added cf_ChangeFileAttributes function
73 *
74 * 3 2/15/98 7:44p Matt
75 * Added groovy try/catch/throw error checking for cfile functions
76 *
77 * 2 12/17/97 4:09p Jason
78 * fixed compiler warning
79 *
80 * 11 5/23/97 2:27p Matt
81 * Text file newlines now handled internally.
82 * Don't look in libs for files opened for writing.
83 * Changed error message to print out errno.
84 *
85 * 10 4/03/97 4:34p Jason
86 * added CopyFileTime to the cfile, ddio libs
87 *
88 * 9 3/03/97 6:21p Matt
89 * Changed cfile functions to use D3 naming convention
90 *
91 * $NoKeywords: $
92 */
93
94 #ifndef CFILE_H
95 #define CFILE_Hcxx . . $ `cfile/cfile.h:95:9`!
96
97 #include <cmath>
98 #include <cstdint>
99 #include <cstdio>
100 #include <filesystem>
101 #include <vector>
102
103 #include "pstypes.h"cxx . . $ `<file>/lib/pstypes.h`/
104
105 struct librarycxx . . $ library#;
106
107 // The structure for a CFILE
108 struct CFILEcxx . . $ CFILE# {
109 char *namecxx . . $ CFILE#name.; // pointer to filename
110 FILEcxx . . $ FILE# *filecxx . . $ CFILE#file.; // the file itself (on disk) or the HOG
111 int32_tcxx . . $ int32_t# lib_handlecxx . . $ CFILE#lib_handle.; // the handle of the library, or -1
112 uint32_tcxx . . $ uint32_t# sizecxx . . $ CFILE#size.; // length of this file
113 uint32_tcxx . . $ uint32_t# lib_offsetcxx . . $ CFILE#lib_offset.; // offset into HOG of start of file, or 0 if on disk
114 uint32_tcxx . . $ uint32_t# positioncxx . . $ CFILE#position.; // current position in file
115 uint32_tcxx . . $ uint32_t# flagscxx . . $ CFILE#flags.; // see values below
116 };
117
118 // Defines for cfile_error
119 enum CFileErrorcxx . . $ CFileError# {
120 CFE_READINGcxx . . $ CFE_READING. = 1,
121 CFE_WRITINGcxx . . $ CFE_WRITING.,
122 };
123
124 // The structure thrown by a cfile error
125 struct cfile_errorcxx . . $ cfile_error# {
126 int read_writecxx . . $ cfile_error#read_write.; // reading or writing? See defines.
127 const char *msgcxx . . $ cfile_error#msg.; // the error message
128 CFILEcxx . . $ CFILE# *filecxx . . $ cfile_error#file.; // the file that got the error
129 };
130
131 // Flags for CFILE struct
132 enum CFileFlagscxx . . $ CFileFlags# {
133 CFF_TEXTcxx . . $ CFF_TEXT. = 1, // if this bit set, file is text
134 CFF_WRITINGcxx . . $ CFF_WRITING., // if bit set, file opened for writing
135 };
136
137 // return values for cfexist()
138 enum CFileExitStatuscxx . . $ CFileExitStatus# {
139 CFES_NOT_FOUNDcxx . . $ CFES_NOT_FOUND. = 0,
140 CFES_ON_DISKcxx . . $ CFES_ON_DISK.,
141 CFES_IN_LIBRARYcxx . . $ CFES_IN_LIBRARY.,
142 };
143
144 // See if a file is in a hog
145 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);
146
147 // Opens a HOG file. Future calls to cfopen(), etc. will look in this HOG.
148 // Parameters: libname - the path & filename of the HOG file
149 // NOTE: libname must be valid for the entire execution of the program. Therefore, it should either
150 // be a fully-specified path name, or the current directory must not change.
151 // Returns: 0 if error, else library handle that can be used to close the library
152 int cf_OpenLibrarycxx . . $ cf_OpenLibrary(293b0826c8d9cb54).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#& libname);
153
154 // Closes a library file.
155 // Parameters: handle: the handle returned by cf_OpenLibrary()
156 void cf_CloseLibrarycxx . . $ cf_CloseLibrary(d4f767463ce0a6b3).(int handle);
157
158 /**
159 * Returns fixed case file name to actual case on disk for case-sensitive filesystems (Linux).
160 * @param fname the fixed case name to map to reality
161 * @param directory optional directory to search within (default - current path)
162 * @return filename with actual case name or empty path if there no mapping in filesystem
163 * @note This function returns only filename without directory part, i.e.
164 * cf_FindRealFileNameCaseInsensitive("test/test.txt") will return only "test.txt" on success.
165 */
166 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,
167 const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &directory = "."cxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d).);
168
169 /**
170 * Add directory path into paths to look in for files. If ext_list is empty,
171 * look in this directory for all files. Otherwise, the directory will only
172 * be searched for files that match one of the listed extensions.
173 * @param path directory to add; should be existing and resolvable directory.
174 * @param ext_list list of extensions, which only searched on that directory.
175 * @return
176 * false: path is not a real directory;
177 * true: path was successfully added.
178 */
179 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 =cxx . . $ std/vector#vector(49f6e7a06ebc5aa8). {});
180
181 // Removes all search paths that have been added by cf_SetSearchPath
182 void cf_ClearAllSearchPathscxx . . $ cf_ClearAllSearchPaths(49f6e7a06ebc5aa8).();
183
184 // Opens a file for reading or writing
185 // If a path is specified, will try to open the file only in that path.
186 // If no path is specified, will look through search directories and library files.
187 // Parameters: filename - the name if the file, with or without a path
188 // mode - the standard C mode string
189 // Returns: the CFile handle, or NULL if file not opened
190 CFILEcxx . . $ CFILE# *cfopencxx . . $ cfopen(7c7b94e455f1ae56).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#& filename, const char *mode);
191
192 // Opens a file for reading in a library, given the library id.
193 // Works just like cfopen, except it assumes "rb" mode and forces the file to be
194 // opened from the given library. Returns the CFILE handle or NULL if file
195 // couldn't be found or open.
196 CFILEcxx . . $ CFILE# *cf_OpenFileInLibrarycxx . . $ cf_OpenFileInLibrary(b06e90447c91b9c9).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#& filename, int libhandle);
197
198 // Returns the length of the specified file
199 // Parameters: cfp - the file pointer returned by cfopen()
200 uint32_tcxx . . $ uint32_t# cfilelengthcxx . . $ cfilelength(e4541f75b25d0ae5).(CFILEcxx . . $ CFILE# *cfp);
201
202 // Closes an open CFILE.
203 // Parameters: cfile - the file pointer returned by cfopen()
204 void cfclosecxx . . $ cfclose(3639e799710acb68).(CFILEcxx . . $ CFILE# *cfp);
205
206 // Just like stdio fgetc(), except works on a CFILE
207 // Returns a char or EOF
208 int cfgetccxx . . $ cfgetc(413716a422df0036).(CFILEcxx . . $ CFILE# *cfp);
209
210 // Just like stdio fseek(), except works on a CFILE
211 int cfseekcxx . . $ cfseek(f9992d249f032805).(CFILEcxx . . $ CFILE# *cfp, long offset, int where);
212
213 // Just like stdio ftell(), except works on a CFILE
214 long cftellcxx . . $ cftell(65b457210a6528a1).(CFILEcxx . . $ CFILE# *cfp);
215
216 // Returns true if at EOF
217 int cfeofcxx . . $ cfeof(413716a422df0036).(CFILEcxx . . $ CFILE# *cfp);
218
219 // Tells if the file exists
220 // Returns non-zero if file exists. Also tells if the file is on disk
221 // or in a hog - See return values in cfile.h
222 int cfexistcxx . . $ cfexist(293b0826c8d9cb54).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#& filename);
223
224 // Reads the specified number of bytes from a file into the buffer
225 // DO NOT USE THIS TO READ STRUCTURES. This function is for byte
226 // data, such as a string or a bitmap of 8-bit pixels.
227 // Returns the number of bytes read.
228 // Throws an exception of type (cfile_error *) if the OS returns an error on read
229 int cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(uint8_tcxx . . $ uint8_t# *buf, int count, CFILEcxx . . $ CFILE# *cfp);
230
231 // The following functions read numeric vales from a CFILE. All values are
232 // stored in the file in Intel (little-endian) format. These functions
233 // will convert to big-endian if required.
234 // These funtions will throw an exception of if the value cannot be read,
235 // so do not call these if you don't require the data to be present.
236
237 // Read and return an integer (32 bits)
238 // Throws an exception of type (cfile_error *) if the OS returns an error on read
239 int32_tcxx . . $ int32_t# cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(CFILEcxx . . $ CFILE# *cfp, bool little_endian = true);
240
241 // Read and return a int16_t (16 bits)
242 // Throws an exception of type (cfile_error *) if the OS returns an error on read
243 int16_tcxx . . $ int16_t# cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(CFILEcxx . . $ CFILE# *cfp, bool little_endian = true);
244
245 // Read and return a byte (8 bits)
246 // Throws an exception of type (cfile_error *) if the OS returns an error on read
247 int8_tcxx . . $ int8_t# cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(CFILEcxx . . $ CFILE# *cfp);
248
249 // Read and return a float (32 bits)
250 // Throws an exception of type (cfile_error *) if the OS returns an error on read
251 float cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(CFILEcxx . . $ CFILE# *cfp);
252
253 // Read and return a double (64 bits)
254 // Throws an exception of type (cfile_error *) if the OS returns an error on read
255 double cf_ReadDoublecxx . . $ cf_ReadDouble(96e019f41f67302c).(CFILEcxx . . $ CFILE# *cfp);
256
257 // Reads a string from a CFILE. If the file is type binary, this
258 // function reads until a NULL or EOF is found. If the file is text,
259 // the function reads until a newline or EOF is found. The string is always
260 // written to the destination buffer null-terminated, without the newline.
261 // Parameters: buf - where the string is written
262 // n - the maximum string length, including the terminating 0
263 // cfp - the CFILE pointer
264 // Returns the number of bytes in the string, before the terminator
265 // Does not generate an exception on EOF
266 int cf_ReadStringcxx . . $ cf_ReadString(38cd788e4423eff6).(char *buf, size_tcxx . . $ size_t# n, CFILEcxx . . $ CFILE# *cfp);
267
268 // Writes the specified number of bytes from a file into the buffer
269 // DO NOT USE THIS TO WRITE STRUCTURES. This function is for byte
270 // data, such as a string or a bitmap of 8-bit pixels.
271 // Returns the number of bytes written.
272 // Throws an exception of type (cfile_error *) if the OS returns an error on write
273 int cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).(const uint8_tcxx . . $ uint8_t# *buf, int count, CFILEcxx . . $ CFILE# *cfp);
274
275 // Writes a null-terminated string to a file. If the file is type binary,
276 // the string is terminated in the file with a null. If the file is type
277 // text, the string is terminated with a newline.
278 // Parameters: buf - pointer to the string
279 // cfp = the CFILE pointer
280 // Returns the number of bytes written
281 // Throws an exception of type (cfile_error *) if the OS returns an error on write
282 int cf_WriteStringcxx . . $ cf_WriteString(643e1411966bd56c).(CFILEcxx . . $ CFILE# *cfp, const char *buf);
283
284 // Just like stdio fprintf(), except works on a CFILE
285 int cfprintfcxx . . $ cfprintf(4762586383887345).(CFILEcxx . . $ CFILE# *cfp, const char *format, ...);
286
287 // The following functions write numeric vales to a CFILE. All values are
288 // stored to the file in Intel (little-endian) format.
289 // All these throw an exception if there's an error on write.
290
291 // Write an integer (32 bits)
292 // Throws an exception of type (cfile_error *) if the OS returns an error on write
293 void cf_WriteIntcxx . . $ cf_WriteInt(966989f25543eb13).(CFILEcxx . . $ CFILE# *cfp, int32_tcxx . . $ int32_t# i);
294
295 // Write a int16_t (16 bits)
296 // Throws an exception of type (cfile_error *) if the OS returns an error on write
297 void cf_WriteShortcxx . . $ cf_WriteShort(d7cf1a05aca96fe5).(CFILEcxx . . $ CFILE# *cfp, int16_tcxx . . $ int16_t# s);
298
299 // Write a byte (8 bits). If the byte is a newline & the file is a text file, writes a CR/LF pair.
300 // Throws an exception of type (cfile_error *) if the OS returns an error on write
301 void cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(CFILEcxx . . $ CFILE# *cfp, int8_tcxx . . $ int8_t# b);
302
303 // Write a float (32 bits)
304 // Throws an exception of type (cfile_error *) if the OS returns an error on write
305 void cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(CFILEcxx . . $ CFILE# *cfp, float f);
306
307 // Write a double (64 bits)
308 // Throws an exception of type (cfile_error *) if the OS returns an error on write
309 void cf_WriteDoublecxx . . $ cf_WriteDouble(a93307bf66375cef).(CFILEcxx . . $ CFILE# *cfp, double d);
310
311 // Copies a file. Returns TRUE if copied ok. Returns FALSE if error opening either file.
312 // Throws an exception of type (cfile_error *) if the OS returns an error on read or write
313 // If copytime is nonzero, copies the filetime info as well
314 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 = 0);
315
316 // Checks to see if two files are different.
317 // Returns TRUE if the files are different, or FALSE if they are the same.
318 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);
319
320 // Copies the file time from one file to another
321 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);
322
323 // Changes a files attributes (ie read/write only)
324 void cf_ChangeFileAttributescxx . . $ cf_ChangeFileAttributes(34d26762b42c433a).(const char *name, int attr);
325
326 // rewinds cfile position
327 void cf_Rewindcxx . . $ cf_Rewind(3639e799710acb68).(CFILEcxx . . $ CFILE# *fp);
328
329 // Calculates a 32 bit CRC
330 uint32_tcxx . . $ uint32_t# cf_GetfileCRCcxx . . $ cf_GetfileCRC(eb3222378d8a05e0).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#& src);
331 uint32_tcxx . . $ uint32_t# cf_CalculateFileCRCcxx . . $ cf_CalculateFileCRC(e4541f75b25d0ae5).(CFILEcxx . . $ CFILE# *fp); // same as cf_GetfileCRC, except works with CFILE pointers
332
333 // the following cf_LibraryFind function are similar to the ddio_Find functions as they look
334 // for files that match the wildcard passed in, however, this is to be used for hog files.
335 bool cf_LibraryFindFirstcxx . . $ cf_LibraryFindFirst(58fb7ae714029740).(int handle, const char *wildcard, char *buffer);
336 bool cf_LibraryFindNextcxx . . $ cf_LibraryFindNext(c6b7e35ff6de5671).(char *buffer);
337 void cf_LibraryFindClosecxx . . $ cf_LibraryFindClose(49f6e7a06ebc5aa8).();
338
339 #endif