From f3c9e9912f9fc63c5273a65beb4340171c6d3b00 Mon Sep 17 00:00:00 2001 From: Bradley Bell Date: Sun, 15 Jun 2003 04:14:53 +0000 Subject: [PATCH] implemented cfile wrappers for writing and other modes besides "rb" (mostly taken from freespace2) --- ChangeLog | 6 ++ cfile/cfile.c | 177 +++++++++++++++++++++++++++++++++++++++++++++--- include/cfile.h | 45 +++++++++--- 3 files changed, 209 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7b46fe12..0a92f6a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2003-06-14 Bradley Bell + + * include/cfile.h, cfile/cfile.c: implemented cfile wrappers for + writing and other modes besides "rb" (mostly taken from + freespace2) + 2003-06-09 Bradley Bell * 2d/palette.c, main/kludge.c: gr_copy_palette not really a diff --git a/cfile/cfile.c b/cfile/cfile.c index 7d6c8b9b..e102f6ed 100644 --- a/cfile/cfile.c +++ b/cfile/cfile.c @@ -1,4 +1,4 @@ -/* $Id: cfile.c,v 1.11 2003-04-14 18:34:40 btb Exp $ */ +/* $Id: cfile.c,v 1.12 2003-06-15 04:14:53 btb Exp $ */ /* THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO @@ -135,6 +135,13 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. #include "cfile.h" #include "byteswap.h" +struct CFILE { + FILE *file; + int size; + int lib_offset; + int raw_position; +}; + typedef struct hogfile { char name[13]; int offset; @@ -405,6 +412,29 @@ int cfile_use_descent1_hogfile( char * name ) } } + +// cfeof() Tests for end-of-file on a stream +// +// returns a nonzero value after the first read operation that attempts to read +// past the end of the file. It returns 0 if the current position is not end of file. +// There is no error return. + +int cfeof(CFILE *cfile) +{ + Assert(cfile != NULL); + + Assert(cfile->file != NULL); + + return (cfile->raw_position >= cfile->size); +} + + +int cferror(CFILE *cfile) +{ + return ferror(cfile->file); +} + + int cfexist( char * filename ) { int length; @@ -433,16 +463,37 @@ int cfexist( char * filename ) } +// Deletes a file. +int cfile_delete(char *filename) +{ + return remove(filename); +} + + +// Rename a file. +int cfile_rename(char *oldname, char *newname) +{ + return rename(oldname, newname); +} + + +// Make a directory. +int cfile_mkdir(char *pathname) +{ +#if defined(__WINDOWS__) || defined(__MINGW32__) + return mkdir(pathname); +#else + return mkdir(pathname, 0755); +#endif +} + + CFILE * cfopen(char * filename, char * mode ) { int length; FILE * fp; CFILE *cfile; - if (stricmp( mode, "rb")) { - Error( "cfiles can only be opened with mode==rb\n" ); - } - if (filename[0] != '\x01') { #ifdef MACINTOSH char mac_path[255]; @@ -461,6 +512,8 @@ CFILE * cfopen(char * filename, char * mode ) fp = cfile_find_libfile(filename, &length ); if ( !fp ) return NULL; // No file found + if (stricmp(mode, "rb")) + Error("mode must be rb for files in hog.\n"); cfile = d_malloc ( sizeof(CFILE) ); if ( cfile == NULL ) { fclose(fp); @@ -490,6 +543,41 @@ int cfilelength( CFILE *fp ) return fp->size; } + +// cfwrite() writes to the file +// +// returns: number of full elements actually written +// +// +int cfwrite(void *buf, int elsize, int nelem, CFILE *cfile) +{ + Assert(cfile != NULL); + Assert(buf != NULL); + Assert(elsize > 0); + + Assert(cfile->file != NULL); + Assert(cfile->lib_offset == 0); + + return fwrite(buf, elsize, nelem, cfile->file); +} + + +// cfputc() writes a character to a file +// +// returns: success ==> returns character written +// error ==> EOF +// +int cfputc(int c, CFILE *cfile) +{ + Assert(cfile != NULL); + + Assert(cfile->file != NULL); + Assert(cfile->lib_offset == 0); + + return fputc(c, cfile->file); +} + + int cfgetc( CFILE * fp ) { int c; @@ -505,6 +593,23 @@ int cfgetc( CFILE * fp ) return c; } + +// cfputs() writes a string to a file +// +// returns: success ==> non-negative value +// error ==> EOF +// +int cfputs(char *str, CFILE *cfile) +{ + Assert(cfile != NULL); + Assert(str != NULL); + + Assert(cfile->file != NULL); + + return fputs(str, cfile->file); +} + + char * cfgets( char * buf, size_t n, CFILE * fp ) { char * t = buf; @@ -581,11 +686,14 @@ int cfseek( CFILE *fp, long int offset, int where ) return c; } -void cfclose( CFILE * fp ) -{ - fclose(fp->file); +int cfclose(CFILE *fp) +{ + int result; + + result = fclose(fp->file); d_free(fp); - return; + + return result; } // routines to read basic data types from CFILE's. Put here to @@ -666,3 +774,54 @@ void cfile_read_matrix(vms_matrix *m,CFILE *file) cfile_read_vector(&m->fvec,file); } + +void cfile_read_string(char *buf, int n, CFILE *file) +{ + char c; + + do { + c = (char)cfile_read_byte(file); + if (n > 0) + { + *buf++ = c; + n--; + } + } while (c != 0); +} + + +// equivalent write functions of above read functions follow + +int cfile_write_int(int i, CFILE *file) +{ + i = INTEL_INT(i); + return cfwrite(&i, sizeof(i), 1, file); +} + + +int cfile_write_short(short s, CFILE *file) +{ + s = INTEL_SHORT(s); + return cfwrite(&s, sizeof(s), 1, file); +} + + +int cfile_write_byte(byte b, CFILE *file) +{ + return cfwrite(&b, sizeof(b), 1, file); +} + + +int cfile_write_string(char *buf, CFILE *file) +{ + int len; + + if ((!buf) || (buf && !buf[0])) + return cfile_write_byte(0, file); + + len = strlen(buf); + if (!cfwrite(buf, len, 1, file)) + return 0; + + return cfile_write_byte(0, file); // write out NULL termination +} diff --git a/include/cfile.h b/include/cfile.h index 4346d5e1..4243280b 100644 --- a/include/cfile.h +++ b/include/cfile.h @@ -1,4 +1,4 @@ -/* $Id: cfile.h,v 1.8 2003-04-12 00:11:46 btb Exp $ */ +/* $Id: cfile.h,v 1.9 2003-06-15 04:14:53 btb Exp $ */ /* THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO @@ -66,12 +66,7 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. #include "maths.h" #include "vecmat.h" -typedef struct CFILE { - FILE *file; - int size; - int lib_offset; - int raw_position; -} CFILE; +typedef struct CFILE CFILE; //Specify the name of the hogfile. Returns 1 if hogfile found & had files int cfile_init(char *hogname); @@ -81,14 +76,35 @@ int cfile_size(char *hogname); CFILE * cfopen(char * filename, char * mode); int cfilelength( CFILE *fp ); // Returns actual size of file... size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp ); -void cfclose( CFILE * cfile ); +int cfclose(CFILE *cfile); int cfgetc( CFILE * fp ); int cfseek( CFILE *fp, long int offset, int where ); int cftell( CFILE * fp ); char * cfgets( char * buf, size_t n, CFILE * fp ); +int cfeof(CFILE *cfile); +int cferror(CFILE *cfile); + int cfexist( char * filename ); // Returns true if file exists on disk (1) or in hog (2). +// Deletes a file. +int cfile_delete(char *filename); + +// Rename a file. +int cfile_rename(char *oldname, char *newname); + +// Make a directory +int cfile_mkdir(char *pathname); + +// cfwrite() writes to the file +int cfwrite(void *buf, int elsize, int nelem, CFILE *cfile); + +// cfputc() writes a character to a file +int cfputc(int c, CFILE *cfile); + +// cfputs() writes a string to a file +int cfputs(char *str, CFILE *cfile); + // Allows files to be gotten from an alternate hog file. // Passing NULL disables this. // Returns 1 if hogfile found (& contains file), else 0. @@ -118,7 +134,16 @@ void cfile_read_vector(vms_vector *v, CFILE *file); void cfile_read_angvec(vms_angvec *v, CFILE *file); void cfile_read_matrix(vms_matrix *v, CFILE *file); -extern char AltHogDir[]; -extern char AltHogdir_initialized; +// Reads variable length, null-termined string. Will only read up +// to n characters. +void cfile_read_string(char *buf, int n, CFILE *file); + +// functions for writing cfiles +int cfile_write_int(int i, CFILE *file); +int cfile_write_short(short s, CFILE *file); +int cfile_write_byte(byte u, CFILE *file); + +// writes variable length, null-termined string. +int cfile_write_string(char *buf, CFILE *file); #endif -- 2.39.2