From 6703b25d34a98958ec59cd4a1fa91c113e837003 Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Thu, 16 May 2024 18:21:30 +0300 Subject: [PATCH] Minor fixes to cfile --- cfile/cfile.cpp | 83 ++++++++++++++++++------------------------------- cfile/cfile.h | 48 +++++++++++++++++++--------- 2 files changed, 64 insertions(+), 67 deletions(-) diff --git a/cfile/cfile.cpp b/cfile/cfile.cpp index 064dfb00..d14664a4 100644 --- a/cfile/cfile.cpp +++ b/cfile/cfile.cpp @@ -16,13 +16,12 @@ * along with this program. If not, see . */ - -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #ifndef __LINUX__ // Non-Linux Build Includes #include @@ -42,15 +41,15 @@ // Library structures typedef struct { char name[PSFILENAME_LEN + 1]; // just the filename part - int offset; // offset into library file - int length; // length of this file - ulong timestamp; // time and date of file - int flags; // misc flags + uint32_t offset; // offset into library file + uint32_t length; // length of this file + uint32_t timestamp; // time and date of file + uint32_t flags; // misc flags } library_entry; typedef struct library { char name[_MAX_PATH]; // includes path + filename - int nfiles; + uint32_t nfiles; library_entry *entries; struct library *next; int handle; // identifier for this lib @@ -76,7 +75,7 @@ int N_paths = 0; #define MAX_EXTENSIONS 100 ext_entry extensions[MAX_EXTENSIONS]; int N_extensions; -library *Libraries = NULL; +library *Libraries = nullptr; int lib_handle = 0; // Structure thrown on disk error @@ -105,7 +104,8 @@ static CFILE *open_file_in_lib(const char *filename); int cf_OpenLibrary(const char *libname) { FILE *fp; char id[HOG_TAG_LEN]; - int i, offset; + int i; + uint32_t offset; library *lib; static int first_time = 1; tHogHeader header; @@ -252,11 +252,11 @@ int cf_SetSearchPath(const char *path, ...) { va_list exts; va_start(exts, path); const char *ext = va_arg(exts, const char *); - if (ext == NULL) + if (ext == nullptr) paths[N_paths].specific = 0; else { paths[N_paths].specific = 1; - while (ext != NULL) { + while (ext != nullptr) { if (N_extensions >= MAX_EXTENSIONS) { va_end(exts); return 0; @@ -308,8 +308,7 @@ CFILE *cf_OpenFileInLibrary(const char *filename, int libhandle) { } // now do a binary search for the file entry - int i; - int first = 0, last = lib->nfiles - 1, c, found = 0; + int i, first = 0, last = lib->nfiles - 1, c, found = 0; do { i = (first + last) / 2; @@ -785,7 +784,7 @@ got_file:; // Returns the length of the specified file // Parameters: cfp - the file pointer returned by cfopen() -int cfilelength(CFILE *cfp) { return cfp->size; } +uint32_t cfilelength(CFILE *cfp) { return cfp->size; } // Closes an open CFILE. // Parameters: cfile - the file pointer returned by cfopen() @@ -854,8 +853,9 @@ int cfgetc(CFILE *cfp) { return c; } // Just like stdio fseek(), except works on a CFILE -int cfseek(CFILE *cfp, long int offset, int where) { - int c, goal_position; +int cfseek(CFILE *cfp, long offset, int where) { + int c; + long goal_position; switch (where) { case SEEK_SET: goal_position = offset; @@ -875,7 +875,7 @@ int cfseek(CFILE *cfp, long int offset, int where) { } // Just like stdio ftell(), except works on a CFILE -int cftell(CFILE *cfp) { return cfp->position; } +long cftell(CFILE *cfp) { return cfp->position; } // Returns true if at EOF int cfeof(CFILE *cfp) { return (cfp->position >= cfp->size); } @@ -947,11 +947,9 @@ int16_t cf_ReadShort(CFILE *cfp) { // 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) { - int i; - i = cfgetc(cfp); - if (i == EOF) - ThrowCFileError(CFE_READING, cfp, cfeof(cfp) ? eof_error : strerror(errno)); - return (int8_t)i; + int8_t i; + cf_ReadBytes((ubyte *)&i, sizeof(i), cfp); + return i; } // Read and return a float (32 bits) // Throws an exception of type (cfile_error *) if the OS returns an error on read @@ -965,17 +963,7 @@ float cf_ReadFloat(CFILE *cfp) { double cf_ReadDouble(CFILE *cfp) { double f; cf_ReadBytes((ubyte *)&f, sizeof(f), cfp); -#ifdef OUTRAGE_BIG_ENDIAN - { - double t; - int *sp = (int *)&f; - int *dp = (int *)&t; - dp[0] = SWAPINT(sp[1]); - dp[1] = SWAPINT(sp[0]); - f = t; - } -#endif - return f; + return D3::convert_le(f); } // 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, @@ -988,7 +976,7 @@ double cf_ReadDouble(CFILE *cfp) { // Does not generate an exception on EOF int cf_ReadString(char *buf, size_t n, CFILE *cfp) { int c; - uint count; + int count; char *bp; if (n == 0) return -1; @@ -1083,25 +1071,16 @@ void cf_WriteByte(CFILE *cfp, int8_t b) { // 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) { +void cf_WriteFloat(CFILE *cfp, float f) { float t = INTEL_FLOAT(f); cf_WriteBytes((ubyte *)&t, sizeof(t), cfp); } // 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) { -#ifdef OUTRAGE_BIG_ENDIAN - { - double t; - int *sp = (int *)&d; - int *dp = (int *)&t; - dp[0] = SWAPINT(sp[1]); - dp[1] = SWAPINT(sp[0]); - d = t; - } -#endif - cf_WriteBytes((ubyte *)&d, sizeof(d), cfp); +void cf_WriteDouble(CFILE *cfp, double d) { + auto t = D3::convert_le(d); + cf_WriteBytes((ubyte *)&t, sizeof(t), cfp); } // Copies a file. Returns TRUE if copied ok. Returns FALSE if error opening either file. diff --git a/cfile/cfile.h b/cfile/cfile.h index 2f89a0dc..89a3e95e 100644 --- a/cfile/cfile.h +++ b/cfile/cfile.h @@ -1,4 +1,22 @@ /* + * Descent 3 + * Copyright (C) 2024 Parallax Software + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + +--- HISTORICAL COMMENTS FOLLOW --- + * $Logfile: /DescentIII/Main/lib/CFILE.H $ * $Revision: 16 $ * $Date: 10/18/99 1:27p $ @@ -86,13 +104,13 @@ struct library; // The structure for a CFILE typedef struct CFILE { - 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 + char *name; // pointer to filename + FILE *file; // the file itself (on disk) or the HOG + int32_t lib_handle; // the handle of the library, or -1 + uint32_t size; // length of this file + uint32_t lib_offset; // offset into HOG of start of file, or 0 if on disk + uint32_t position; // current position in file + uint32_t flags; // see values below } CFILE; // Defines for cfile_error @@ -103,9 +121,9 @@ enum CFileError { // The structure thrown by a cfile error typedef struct { - int read_write; // reading or writing? See defines. - const char *msg; // the error message - CFILE *file; // the file that got the error + int read_write; // reading or writing? See defines. + const char *msg; // the error message + CFILE *file; // the file that got the error } cfile_error; // Flags for CFILE struct @@ -170,7 +188,7 @@ CFILE *cf_OpenFileInLibrary(const char *filename, int libhandle); // Returns the length of the specified file // Parameters: cfp - the file pointer returned by cfopen() -int cfilelength(CFILE *cfp); +uint32_t cfilelength(CFILE *cfp); // Closes an open CFILE. // Parameters: cfile - the file pointer returned by cfopen() @@ -181,10 +199,10 @@ void cfclose(CFILE *cfp); int cfgetc(CFILE *cfp); // Just like stdio fseek(), except works on a CFILE -int cfseek(CFILE *cfp, long int offset, int where); +int cfseek(CFILE *cfp, long offset, int where); // Just like stdio ftell(), except works on a CFILE -int cftell(CFILE *cfp); +long cftell(CFILE *cfp); // Returns true if at EOF int cfeof(CFILE *cfp); @@ -275,11 +293,11 @@ void cf_WriteByte(CFILE *cfp, int8_t b); // 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); +void cf_WriteFloat(CFILE *cfp, float f); // 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); +void cf_WriteDouble(CFILE *cfp, double d); // 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