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_H
96
97 #include <cmath>
98 #include <cstdint>
99 #include <cstdio>
100 #include <filesystem>
101 #include <vector>
102
103 #include "pstypes.h"
104
105 struct library;
106
107 // The structure for a CFILE
108 struct CFILE {
109 char *name; // pointer to filename
110 FILE *file; // the file itself (on disk) or the HOG
111 int32_t lib_handle; // the handle of the library, or -1
112 uint32_t size; // length of this file
113 uint32_t lib_offset; // offset into HOG of start of file, or 0 if on disk
114 uint32_t position; // current position in file
115 uint32_t flags; // see values below
116 };
117
118 // Defines for cfile_error
119 enum CFileError {
120 CFE_READING = 1,
121 CFE_WRITING,
122 };
123
124 // The structure thrown by a cfile error
125 struct cfile_error {
126 int read_write; // reading or writing? See defines.
127 const char *msg; // the error message
128 CFILE *file; // the file that got the error
129 };
130
131 // Flags for CFILE struct
132 enum CFileFlags {
133 CFF_TEXT = 1, // if this bit set, file is text
134 CFF_WRITING, // if bit set, file opened for writing
135 };
136
137 // return values for cfexist()
138 enum CFileExitStatus {
139 CFES_NOT_FOUND = 0,
140 CFES_ON_DISK,
141 CFES_IN_LIBRARY,
142 };
143
144 // See if a file is in a hog
145 bool cf_IsFileInHog(const std::filesystem::path& filename, const std::filesystem::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_OpenLibrary(const std::filesystem::path& libname);
153
154 // Closes a library file.
155 // Parameters: handle: the handle returned by cf_OpenLibrary()
156 void cf_CloseLibrary(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 std::filesystem::path cf_FindRealFileNameCaseInsensitive(const std::filesystem::path &fname,
167 const std::filesystem::path &directory = ".");
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_SetSearchPath(const std::filesystem::path& path, const std::vector<std::filesystem::path>& ext_list = {});
180
181 // Removes all search paths that have been added by cf_SetSearchPath
182 void cf_ClearAllSearchPaths();
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 CFILE *cfopen(const std::filesystem::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 CFILE *cf_OpenFileInLibrary(const std::filesystem::path& filename, int libhandle);
197
198 // Returns the length of the specified file
199 // Parameters: cfp - the file pointer returned by cfopen()
200 uint32_t cfilelength(CFILE *cfp);
201
202 // Closes an open CFILE.
203 // Parameters: cfile - the file pointer returned by cfopen()
204 void cfclose(CFILE *cfp);
205
206 // Just like stdio fgetc(), except works on a CFILE
207 // Returns a char or EOF
208 int cfgetc(CFILE *cfp);
209
210 // Just like stdio fseek(), except works on a CFILE
211 int cfseek(CFILE *cfp, long offset, int where);
212
213 // Just like stdio ftell(), except works on a CFILE
214 long cftell(CFILE *cfp);
215
216 // Returns true if at EOF
217 int cfeof(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 cfexist(const std::filesystem::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_ReadBytes(uint8_t *buf, int count, 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_t cf_ReadInt(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_t cf_ReadShort(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_t cf_ReadByte(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_ReadFloat(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_ReadDouble(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_ReadString(char *buf, size_t n, 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_WriteBytes(const uint8_t *buf, int count, 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_WriteString(CFILE *cfp, const char *buf);
283
284 // Just like stdio fprintf(), except works on a CFILE
285 int cfprintf(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_WriteInt(CFILE *cfp, 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_WriteShort(CFILE *cfp, 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_WriteByte(CFILE *cfp, 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_WriteFloat(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_WriteDouble(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_CopyFile(const std::filesystem::path &dest, const std::filesystem::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_Diff(const std::filesystem::path &a, const std::filesystem::path &b);
319
320 // Copies the file time from one file to another
321 void cf_CopyFileTime(const std::filesystem::path &dest, const std::filesystem::path &src);
322
323 // Changes a files attributes (ie read/write only)
324 void cf_ChangeFileAttributes(const char *name, int attr);
325
326 // rewinds cfile position
327 void cf_Rewind(CFILE *fp);
328
329 // Calculates a 32 bit CRC
330 uint32_t cf_GetfileCRC(const std::filesystem::path& src);
331 uint32_t cf_CalculateFileCRC(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_LibraryFindFirst(int handle, const char *wildcard, char *buffer);
336 bool cf_LibraryFindNext(char *buffer);
337 void cf_LibraryFindClose();
338
339 #endif