Descent3/cfile/cfile.h

313 lines
11 KiB
C
Raw Normal View History

2024-04-16 03:43:29 +00:00
/*
* $Logfile: /DescentIII/Main/lib/CFILE.H $
* $Revision: 16 $
* $Date: 10/18/99 1:27p $
* $Author: Kevin $
*
* Functions for reading & writing files. Includes code to read from libraries.
*
* $Log: /DescentIII/Main/lib/CFILE.H $
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 16 10/18/99 1:27p Kevin
* Added cf_IsFileInHog
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 15 9/14/99 7:49p Jeff
* added cf_OpenFileInLibrary() to force a file to be opened from a
* specific library. Added a way to get a crc of a file given it's CFILE
* *.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 14 5/20/99 5:32p Matt
* Store a lib handle, instead of a lib pointer, in the cfile struct.
* This will keep us from using a pointer to lib that's already been
* closed.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 13 3/22/99 6:26p Matt
* Cleaned up error handling in cfile and editor level loads.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 12 1/07/99 10:51p Jeff
* added psglob and support to do find in files for hog files
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 11 11/16/98 3:49p Jason
* changes for manage system
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 10 11/11/98 2:58p Jeff
* added cf_ClearAllSearchPaths() function
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 9 10/22/98 10:48a Matt
* Added code to keep the library file open all the time, which will
* hopefully speed up file loads.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 8 8/14/98 6:31p Matt
* Changed comment
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 7 7/28/98 12:27p Kevin
* Added CRC function
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 6 7/09/98 8:33p Samir
* Added cf_Rewind.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 5 3/19/98 3:18p Samir
* enforce constant char* arguments when needed. done in CFILE and bitmap
* libraries as well as ddio.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 4 2/26/98 11:01a Jason
* added cf_ChangeFileAttributes function
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 3 2/15/98 7:44p Matt
* Added groovy try/catch/throw error checking for cfile functions
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 2 12/17/97 4:09p Jason
* fixed compiler warning
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 11 5/23/97 2:27p Matt
* Text file newlines now handled internally.
* Don't look in libs for files opened for writing.
* Changed error message to print out errno.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 10 4/03/97 4:34p Jason
* added CopyFileTime to the cfile, ddio libs
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 9 3/03/97 6:21p Matt
* Changed cfile functions to use D3 naming convention
*
* $NoKeywords: $
*/
#ifndef CFILE_H
#define CFILE_H
#include <cmath>
#include <cstdint>
#include <cstdio>
2024-04-16 03:43:29 +00:00
#include "pstypes.h"
struct library;
2024-04-16 18:56:40 +00:00
// The structure for a CFILE
2024-04-16 03:43:29 +00:00
typedef struct CFILE {
2024-04-16 18:56:40 +00:00
char *name; // pointer to filename
FILE *file; // the file itself (on disk) or the HOG
int lib_handle; // the handle of the library, or -1
int size; // length of this file
int lib_offset; // offset into HOG of start of file, or 0 if on disk
int position; // current position in file
int flags; // see values below
2024-04-16 03:43:29 +00:00
} CFILE;
2024-04-16 18:56:40 +00:00
// Defines for cfile_error
enum CFileError {
CFE_READING = 1,
CFE_WRITING,
};
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// The structure thrown by a cfile error
2024-04-16 03:43:29 +00:00
typedef struct {
2024-04-16 18:56:40 +00:00
int read_write; // reading or writing? See defines.
const char *msg; // the error message
2024-04-16 18:56:40 +00:00
CFILE *file; // the file that got the error
2024-04-16 03:43:29 +00:00
} cfile_error;
2024-04-16 18:56:40 +00:00
// Flags for CFILE struct
enum CFileFlags {
CFF_TEXT = 1, // if this bit set, file is text
CFF_WRITING, // if bit set, file opened for writing
};
// return values for cfexist()
enum CFileExitStatus {
CFES_NOT_FOUND = 0,
CFES_ON_DISK,
CFES_IN_LIBRARY,
};
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// See if a file is in a hog
bool cf_IsFileInHog(const char *filename, const char *hogname);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Opens a HOG file. Future calls to cfopen(), etc. will look in this HOG.
// Parameters: libname - the path & filename of the HOG file
// NOTE: libname must be valid for the entire execution of the program. Therefore, it should either
2024-04-16 03:43:29 +00:00
// be a fully-specified path name, or the current directory must not change.
2024-04-16 18:56:40 +00:00
// Returns: 0 if error, else library handle that can be used to close the library
2024-04-16 03:43:29 +00:00
int cf_OpenLibrary(const char *libname);
2024-04-16 18:56:40 +00:00
// Closes a library file.
// Parameters: handle: the handle returned by cf_OpenLibrary()
2024-04-16 03:43:29 +00:00
void cf_CloseLibrary(int handle);
#ifdef __LINUX__
// Maps fixed case file name to actual case on disk
// Parameters: directory: optional directory to search within (can be NULL)
// filename: the fixed case name to map to reality
// new_filename: buffer to store mapped name, must be at least _MAX_PATH bytes
// Returns: false if error, true if translated
bool cf_FindRealFileNameCaseInsenstive(const char *directory, const char *filename, char *new_filename);
#endif
2024-04-16 18:56:40 +00:00
// Specify a directory to look in for files
// Variable arguments is a NULL-terminated list of extensions
// If no extensions are specified, look in this directory for all files.
// Otherwise, the directory will only be searched for files that match
2024-04-16 18:56:40 +00:00
// one of the listed extensions.
int cf_SetSearchPath(const char *path, ...);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Removes all search paths that have been added by cf_SetSearchPath
void cf_ClearAllSearchPaths();
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Opens a file for reading or writing
// If a path is specified, will try to open the file only in that path.
// If no path is specified, will look through search directories and library files.
// Parameters: filename - the name if the file, with or without a path
2024-04-16 03:43:29 +00:00
// mode - the standard C mode string
2024-04-16 18:56:40 +00:00
// Returns: the CFile handle, or NULL if file not opened
2024-04-16 03:43:29 +00:00
CFILE *cfopen(const char *filename, const char *mode);
// Opens a file for reading in a library, given the library id.
2024-04-16 18:56:40 +00:00
// Works just like cfopen, except it assumes "rb" mode and forces the file to be
2024-04-16 03:43:29 +00:00
// opened from the given library. Returns the CFILE handle or NULL if file
// couldn't be found or open.
2024-04-16 18:56:40 +00:00
CFILE *cf_OpenFileInLibrary(const char *filename, int libhandle);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Returns the length of the specified file
// Parameters: cfp - the file pointer returned by cfopen()
int cfilelength(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Closes an open CFILE.
// Parameters: cfile - the file pointer returned by cfopen()
void cfclose(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Just like stdio fgetc(), except works on a CFILE
// Returns a char or EOF
int cfgetc(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Just like stdio fseek(), except works on a CFILE
int cfseek(CFILE *cfp, long int offset, int where);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Just like stdio ftell(), except works on a CFILE
int cftell(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Returns true if at EOF
2024-04-16 03:43:29 +00:00
int cfeof(CFILE *cfp);
// Tells if the file exists
// Returns non-zero if file exists. Also tells if the file is on disk
// or in a hog - See return values in cfile.h
2024-04-16 18:56:40 +00:00
int cfexist(const char *filename);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Reads the specified number of bytes from a file into the buffer
// DO NOT USE THIS TO READ STRUCTURES. This function is for byte
// data, such as a string or a bitmap of 8-bit pixels.
// Returns the number of bytes read.
// Throws an exception of type (cfile_error *) if the OS returns an error on read
2024-04-16 03:43:29 +00:00
int cf_ReadBytes(ubyte *buf, int count, CFILE *cfp);
2024-04-16 18:56:40 +00:00
// The following functions read numeric vales from a CFILE. All values are
2024-04-16 03:43:29 +00:00
// stored in the file in Intel (little-endian) format. These functions
// will convert to big-endian if required.
2024-04-16 18:56:40 +00:00
// These funtions will throw an exception of if the value cannot be read,
// so do not call these if you don't require the data to be present.
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Read and return an integer (32 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
int32_t cf_ReadInt(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Read and return a short (16 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
int16_t cf_ReadShort(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Read and return a byte (8 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
int8_t cf_ReadByte(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Read and return a float (32 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
float cf_ReadFloat(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Read and return a double (64 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
double cf_ReadDouble(CFILE *cfp);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Reads a string from a CFILE. If the file is type binary, this
// function reads until a NULL or EOF is found. If the file is text,
// the function reads until a newline or EOF is found. The string is always
// written to the destination buffer null-terminated, without the newline.
// Parameters: buf - where the string is written
2024-04-16 03:43:29 +00:00
// n - the maximum string length, including the terminating 0
// cfp - the CFILE pointer
2024-04-16 18:56:40 +00:00
// Returns the number of bytes in the string, before the terminator
// Does not generate an exception on EOF
int cf_ReadString(char *buf, size_t n, CFILE *cfp);
// Writes the specified number of bytes from a file into the buffer
// DO NOT USE THIS TO WRITE STRUCTURES. This function is for byte
// data, such as a string or a bitmap of 8-bit pixels.
// Returns the number of bytes written.
// Throws an exception of type (cfile_error *) if the OS returns an error on write
2024-04-16 03:43:29 +00:00
int cf_WriteBytes(const ubyte *buf, int count, CFILE *cfp);
2024-04-16 18:56:40 +00:00
// Writes a null-terminated string to a file. If the file is type binary,
// the string is terminated in the file with a null. If the file is type
// text, the string is terminated with a newline.
// Parameters: buf - pointer to the string
2024-04-16 03:43:29 +00:00
// cfp = the CFILE pointer
2024-04-16 18:56:40 +00:00
// Returns the number of bytes written
// Throws an exception of type (cfile_error *) if the OS returns an error on write
2024-04-16 03:43:29 +00:00
int cf_WriteString(CFILE *cfp, const char *buf);
2024-04-16 18:56:40 +00:00
// Just like stdio fprintf(), except works on a CFILE
int cfprintf(CFILE *cfp, const char *format, ...);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// The following functions write numeric vales to a CFILE. All values are
// stored to the file in Intel (little-endian) format.
2024-04-16 03:43:29 +00:00
// All these throw an exception if there's an error on write.
2024-04-16 18:56:40 +00:00
// Write an integer (32 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on write
void cf_WriteInt(CFILE *cfp, int32_t i);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Write a short (16 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on write
void cf_WriteShort(CFILE *cfp, int16_t s);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Write a byte (8 bits). If the byte is a newline & the file is a text file, writes a CR/LF pair.
// Throws an exception of type (cfile_error *) if the OS returns an error on write
void cf_WriteByte(CFILE *cfp, int8_t b);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Write a float (32 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on write
void cf_WriteFloat(CFILE *cfp, float_t f);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Write a double (64 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on write
void cf_WriteDouble(CFILE *cfp, double_t d);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Copies a file. Returns TRUE if copied ok. Returns FALSE if error opening either file.
// Throws an exception of type (cfile_error *) if the OS returns an error on read or write
2024-04-16 03:43:29 +00:00
// If copytime is nonzero, copies the filetime info as well
2024-04-16 18:56:40 +00:00
bool cf_CopyFile(char *dest, const char *src, int copytime = 0);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Checks to see if two files are different.
// Returns TRUE if the files are different, or FALSE if they are the same.
bool cf_Diff(const char *a, const char *b);
2024-04-16 03:43:29 +00:00
// Copies the file time from one file to another
2024-04-16 18:56:40 +00:00
void cf_CopyFileTime(char *dest, const char *src);
2024-04-16 03:43:29 +00:00
// Changes a files attributes (ie read/write only)
2024-04-16 18:56:40 +00:00
void cf_ChangeFileAttributes(const char *name, int attr);
2024-04-16 03:43:29 +00:00
// rewinds cfile position
2024-04-16 03:43:29 +00:00
void cf_Rewind(CFILE *fp);
// Calculates a 32 bit CRC
2024-04-16 18:56:40 +00:00
unsigned int cf_GetfileCRC(char *src);
unsigned int cf_CalculateFileCRC(CFILE *fp); // same as cf_GetfileCRC, except works with CFILE pointers
2024-04-16 03:43:29 +00:00
// the following cf_LibraryFind function are similar to the ddio_Find functions as they look
// for files that match the wildcard passed in, however, this is to be used for hog files.
2024-04-16 18:56:40 +00:00
bool cf_LibraryFindFirst(int handle, const char *wildcard, char *buffer);
2024-04-16 03:43:29 +00:00
bool cf_LibraryFindNext(char *buffer);
void cf_LibraryFindClose();
2024-04-16 03:43:29 +00:00
#endif