Use RAIIPHYSFS_File to manage PHYSFS_File

Fixes a few leaks on error paths.
This commit is contained in:
Kp 2015-01-17 18:31:42 +00:00
parent 68d705a85a
commit f7815810e8
40 changed files with 182 additions and 522 deletions

View file

@ -67,7 +67,6 @@ static std::vector<uint8_t> current_music_hndlbuf;
int mix_play_file(const char *filename, int loop, void (*hook_finished_track)())
{
SDL_RWops *rw = NULL;
PHYSFS_file *filehandle = NULL;
char full_path[PATH_MAX];
const char *fptr;
unsigned int bufsize = 0;
@ -116,15 +115,13 @@ int mix_play_file(const char *filename, int loop, void (*hook_finished_track)())
// still nothin'? Let's open via PhysFS in case it's located inside an archive
if (!current_music)
{
filehandle = PHYSFS_openRead(filename);
if (filehandle)
if (RAIIPHYSFS_File filehandle{PHYSFS_openRead(filename)})
{
unsigned len = PHYSFS_fileLength(filehandle);
current_music_hndlbuf.resize(len);
bufsize = PHYSFS_read(filehandle, &current_music_hndlbuf[0], sizeof(char), len);
rw = SDL_RWFromConstMem(&current_music_hndlbuf[0], bufsize*sizeof(char));
current_music.reset(Mix_LoadMUS_RW(rw), rw);
PHYSFS_close(filehandle);
}
}

View file

@ -438,6 +438,36 @@ static inline void PHYSFSX_readMatrix(const char *func, const unsigned line, vms
#define PHYSFSX_contfile_init PHYSFSX_addRelToSearchPath
#define PHYSFSX_contfile_close PHYSFSX_removeRelFromSearchPath
class PHYSFS_File_deleter
{
public:
int operator()(PHYSFS_File *fp) const
{
return PHYSFS_close(fp);
}
};
class RAIIPHYSFS_File : public std::unique_ptr<PHYSFS_File, PHYSFS_File_deleter>
{
typedef std::unique_ptr<PHYSFS_File, PHYSFS_File_deleter> base_t;
public:
DXX_INHERIT_CONSTRUCTORS(RAIIPHYSFS_File, base_t);
using base_t::operator bool;
operator PHYSFS_File *() const { return get(); }
int close()
{
/* Like reset(), but returns result */
int r = get_deleter()(get());
if (r)
release();
return r;
}
template <typename T>
bool operator==(T) const = delete;
template <typename T>
bool operator!=(T) const = delete;
};
typedef char file_extension_t[5];
int PHYSFSX_checkMatchingExtension(const file_extension_t *exts, const char *filename) __attribute_nonnull();
extern int PHYSFSX_addRelToSearchPath(const char *relname, int add_to_end);
@ -452,8 +482,8 @@ extern char **PHYSFSX_findFiles(const char *path, const file_extension_t *exts)
extern char **PHYSFSX_findabsoluteFiles(const char *path, const char *realpath, const file_extension_t *exts) __attribute_nonnull();
extern PHYSFS_sint64 PHYSFSX_getFreeDiskSpace();
extern int PHYSFSX_exists(const char *filename, int ignorecase);
extern PHYSFS_file *PHYSFSX_openReadBuffered(const char *filename);
extern PHYSFS_file *PHYSFSX_openWriteBuffered(const char *filename);
RAIIPHYSFS_File PHYSFSX_openReadBuffered(const char *filename);
RAIIPHYSFS_File PHYSFSX_openWriteBuffered(const char *filename);
extern void PHYSFSX_addArchiveContent();
extern void PHYSFSX_removeArchiveContent();

View file

@ -51,59 +51,51 @@ hmp_file::~hmp_file()
std::unique_ptr<hmp_file> hmp_open(const char *filename) {
int data, num_tracks, tempo;
char buf[256];
PHYSFS_file *fp;
auto fp = PHYSFSX_openReadBuffered(filename);
if (!(fp = PHYSFSX_openReadBuffered(filename)))
if (!fp)
return NULL;
std::unique_ptr<hmp_file> hmp(new hmp_file{});
if ((PHYSFS_read(fp, buf, 1, 8) != 8) || (memcmp(buf, "HMIMIDIP", 8)))
{
PHYSFS_close(fp);
return NULL;
}
if (PHYSFSX_fseek(fp, 0x30, SEEK_SET))
{
PHYSFS_close(fp);
return NULL;
}
if (PHYSFS_read(fp, &num_tracks, 4, 1) != 1)
{
PHYSFS_close(fp);
return NULL;
}
if ((num_tracks < 1) || (num_tracks > HMP_TRACKS))
{
PHYSFS_close(fp);
return NULL;
}
hmp->num_trks = num_tracks;
if (PHYSFSX_fseek(fp, 0x38, SEEK_SET))
{
PHYSFS_close(fp);
return NULL;
}
if (PHYSFS_read(fp, &tempo, 4, 1) != 1)
{
PHYSFS_close(fp);
return NULL;
}
hmp->tempo = INTEL_INT(tempo);
if (PHYSFSX_fseek(fp, 0x308, SEEK_SET))
{
PHYSFS_close(fp);
return NULL;
}
for (int i = 0; i < num_tracks; i++) {
if ((PHYSFSX_fseek(fp, 4, SEEK_CUR)) || (PHYSFS_read(fp, &data, 4, 1) != 1))
{
PHYSFS_close(fp);
return NULL;
}
@ -114,13 +106,11 @@ std::unique_ptr<hmp_file> hmp_open(const char *filename) {
/* finally, read track data */
if ((PHYSFSX_fseek(fp, 4, SEEK_CUR)) || (PHYSFS_read(fp, hmp->trks[i].data.get(), data, 1) != 1))
{
PHYSFS_close(fp);
return NULL;
}
hmp->trks[i].loop_set = 0;
}
hmp->filesize = PHYSFS_fileLength(fp);
PHYSFS_close(fp);
return hmp;
}

View file

@ -19,7 +19,6 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#include <stdlib.h>
#include <string.h>
#include <physfs.h>
#include "event.h"
#include "maths.h"
@ -31,6 +30,7 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#include "ui.h"
#include "window.h"
#include "u_mem.h"
#include "physfsx.h"
#include "compiler-make_unique.h"
@ -203,23 +203,18 @@ static int browser_handler(UI_DIALOG *dlg,const d_event &event, browser *b)
if (!PHYSFS_isDirectory(b->filename))
{
PHYSFS_file *TempFile;
TempFile = PHYSFS_openRead(b->filename);
if (TempFile)
if (RAIIPHYSFS_File{PHYSFS_openRead(b->filename)})
{
// Looks like a valid filename that already exists!
PHYSFS_close(TempFile);
ui_close_dialog(dlg);
return 1;
}
// File doesn't exist, but can we create it?
TempFile = PHYSFS_openWrite(b->filename);
if (TempFile)
if (RAIIPHYSFS_File TempFile{PHYSFS_openWrite(b->filename)})
{
TempFile.reset();
// Looks like a valid filename!
PHYSFS_close(TempFile);
PHYSFS_delete(b->filename);
ui_close_dialog(dlg);
return 1;

View file

@ -360,11 +360,10 @@ UI_KEYPAD::UI_KEYPAD() :
void ui_pad_read( int n, const char * filename )
{
PHYSFS_file * infile;
int linenumber = 0;
int keycode, functionnumber;
infile = PHYSFSX_openReadBuffered( filename );
auto infile = PHYSFSX_openReadBuffered(filename);
if (!infile) {
Warning( "Couldn't find %s\n", filename );
return;
@ -468,5 +467,4 @@ void ui_pad_read( int n, const char * filename )
kpn.numkeys++;
}
}
PHYSFS_close(infile);
}

View file

@ -738,7 +738,6 @@ void menubar_init( const char * file )
{
int i,j, np;
int aw, w, h;
PHYSFS_file * infile;
char buf1[200];
char buf2[200];
int menu, item;
@ -751,8 +750,7 @@ void menubar_init( const char * file )
for (i=0; i < MAXMENUS; i++ )
for (j=0; j< MAXITEMS; j++ )
Menu[i].Item[j].Hotkey = -1;
infile = PHYSFSX_openReadBuffered( file );
auto infile = PHYSFSX_openReadBuffered(file);
if (!infile) return;
@ -867,10 +865,7 @@ void menubar_init( const char * file )
num_menus = menu+1;
}
Menu[0].w = 700;
PHYSFS_close( infile );
}
void menubar_hide()

View file

@ -251,7 +251,6 @@ static void ab_load(int skip, const char * filename, array<bitmap_index, MAX_BIT
int ds_load(int skip, const char * filename ) {
int i;
PHYSFS_file * cfp;
digi_sound n;
char fname[20];
char rawname[100];
@ -270,14 +269,11 @@ int ds_load(int skip, const char * filename ) {
return i;
}
cfp = PHYSFSX_openReadBuffered(rawname);
if (cfp)
if (auto cfp = PHYSFSX_openReadBuffered(rawname))
{
n.length = PHYSFS_fileLength( cfp );
MALLOC( n.data, ubyte, n.length );
PHYSFS_read( cfp, n.data, 1, n.length );
PHYSFS_close(cfp);
n.bits = 8;
n.freq = 11025;
} else {
@ -326,7 +322,6 @@ static int get_int()
int gamedata_read_tbl(int pc_shareware)
{
std::string dest_bm;
PHYSFS_file * InfoFile;
int i, have_bin_tbl;
ObjType[0] = OL_PLAYER;
@ -374,7 +369,7 @@ int gamedata_read_tbl(int pc_shareware)
// Open BITMAPS.TBL for reading.
have_bin_tbl = 0;
InfoFile = PHYSFSX_openReadBuffered("BITMAPS.TBL");
auto InfoFile = PHYSFSX_openReadBuffered("BITMAPS.TBL");
if (!InfoFile)
{
InfoFile = PHYSFSX_openReadBuffered("BITMAPS.BIN");
@ -543,8 +538,6 @@ int gamedata_read_tbl(int pc_shareware)
NumTextures = texture_count;
Num_tmaps = tmap_count;
PHYSFS_close( InfoFile );
Assert(N_robot_types == Num_robot_ais); //should be one ai info per robot
verify_textures();

View file

@ -238,11 +238,11 @@ static int load_pigpog(const d_fname &pogname)
grs_bitmap *bmp;
digi_sound *snd;
ubyte *p;
PHYSFS_file *f;
auto f = PHYSFSX_openReadBuffered(pogname);
int i, j, rc = -1;
unsigned int x = 0;
if (!(f = PHYSFSX_openReadBuffered(pogname)))
if (!f)
return -1; // pog file doesn't exist
i = PHYSFSX_readInt(f);
@ -251,7 +251,6 @@ static int load_pigpog(const d_fname &pogname)
std::unique_ptr<custom_info[]> ci;
if (load_pog(f, i, x, num_custom, ci) && load_pig1(f, i, x, num_custom, ci))
{
PHYSFS_close(f);
return rc;
}
@ -272,7 +271,6 @@ static int load_pigpog(const d_fname &pogname)
if (!MALLOC(p, ubyte, j))
{
PHYSFS_close(f);
return rc;
}
@ -308,7 +306,6 @@ static int load_pigpog(const d_fname &pogname)
if (PHYSFS_read(f, p, 1, j) < 1)
{
PHYSFS_close(f);
return rc;
}
@ -321,7 +318,6 @@ static int load_pigpog(const d_fname &pogname)
j = cip->width;
if (!MALLOC(p, ubyte, j))
{
PHYSFS_close(f);
return rc;
}
@ -346,16 +342,12 @@ static int load_pigpog(const d_fname &pogname)
if (PHYSFS_read(f, p, j, 1) < 1)
{
PHYSFS_close(f);
return rc;
}
}
cip++;
}
rc = 0;
PHYSFS_close(f);
return rc;
}
@ -452,21 +444,21 @@ static void load_hxm(const d_fname &hxmname)
{
unsigned int repl_num;
int i;
PHYSFS_file *f;
auto f = PHYSFSX_openReadBuffered(hxmname);
int n_items;
if (!(f = PHYSFSX_openReadBuffered(hxmname)))
if (!f)
return; // hxm file doesn't exist
if (PHYSFSX_readInt(f) != 0x21584d48) /* HMX! */
{
PHYSFS_close(f); // invalid hxm file
// invalid hxm file
return;
}
if (PHYSFSX_readInt(f) != 1)
{
PHYSFS_close(f); // unknown version
// unknown version
return;
}
@ -485,7 +477,6 @@ static void load_hxm(const d_fname &hxmname)
{
if (!(read_d2_robot_info(f, &Robot_info[repl_num])))
{
PHYSFS_close(f);
return;
}
}
@ -529,7 +520,6 @@ static void load_hxm(const d_fname &hxmname)
if (PHYSFS_read(f, pm->model_data, pm->model_data_size, 1) < 1)
{
pm->model_data.reset();
PHYSFS_close(f);
return;
}
@ -551,8 +541,6 @@ static void load_hxm(const d_fname &hxmname)
ObjBitmaps[repl_num].index = PHYSFSX_readShort(f);
}
}
PHYSFS_close(f);
}
// undo customized items

View file

@ -273,7 +273,6 @@ static void ab_load(int skip, const char * filename, bitmap_index bmp[], unsigne
int ds_load(int skip, const char * filename ) {
int i;
PHYSFS_file * cfp;
digi_sound n;
char fname[20];
char rawname[100];
@ -291,14 +290,11 @@ int ds_load(int skip, const char * filename ) {
if (i!=255) {
return i;
}
cfp = PHYSFSX_openReadBuffered(rawname);
if (cfp)
if (auto cfp = PHYSFSX_openReadBuffered(rawname))
{
n.length = PHYSFS_fileLength( cfp );
MALLOC( n.data, ubyte, n.length );
PHYSFS_read( cfp, n.data, 1, n.length );
PHYSFS_close(cfp);
n.bits = 8;
n.freq = 11025;
} else {
@ -372,12 +368,11 @@ static int get_texture(char *name)
// If no editor, properties_read_cmp() is called.
int gamedata_read_tbl(int pc_shareware)
{
PHYSFS_file * InfoFile;
int i, have_bin_tbl;
// Open BITMAPS.TBL for reading.
have_bin_tbl = 0;
InfoFile = PHYSFSX_openReadBuffered("BITMAPS.TBL");
auto InfoFile = PHYSFSX_openReadBuffered("BITMAPS.TBL");
if (!InfoFile)
{
InfoFile = PHYSFSX_openReadBuffered("BITMAPS.BIN");
@ -604,9 +599,7 @@ int gamedata_read_tbl(int pc_shareware)
Num_tmaps = tmap_count;
Textures[NumTextures++].index = 0; //entry for bogus tmap
PHYSFS_close( InfoFile );
InfoFile.reset();
Assert(N_robot_types == Num_robot_ais); //should be one ai info per robot
verify_textures();
@ -2101,10 +2094,9 @@ static void tmap_info_write(PHYSFS_file *fp, const tmap_info &ti)
void bm_write_all(PHYSFS_file *fp)
{
unsigned i,t;
PHYSFS_file *tfile;
int s=0;
tfile = PHYSFSX_openWriteBuffered("hamfile.lst");
auto tfile = PHYSFSX_openWriteBuffered("hamfile.lst");
t = NumTextures-1; //don't save bogus texture
PHYSFS_write( fp, &t, sizeof(int), 1 );
@ -2201,21 +2193,14 @@ void bm_write_all(PHYSFS_file *fp)
PHYSFSX_printf(tfile, "Num_reactors = %d, Reactors array = %d\n", Num_reactors, (int) sizeof(*Reactors)*Num_reactors);
PHYSFS_write( fp, &Marker_model_num, sizeof(Marker_model_num), 1);
PHYSFS_close(tfile);
bm_write_extra_robots();
}
void bm_write_extra_robots()
{
PHYSFS_file *fp;
u_int32_t t;
int i;
fp = PHYSFSX_openWriteBuffered("robots.ham");
auto fp = PHYSFSX_openWriteBuffered("robots.ham");
t = 0x5848414d; /* 'XHAM' */
PHYSFS_write( fp, &t, sizeof(int), 1);
t = 1; //version
@ -2261,6 +2246,4 @@ void bm_write_extra_robots()
PHYSFS_write( fp, &ObjBitmapPtrs[N_D2_OBJBITMAPPTRS], sizeof(ushort), t);
PHYSFS_write( fp, ObjBitmapPtrs, sizeof(ushort), t);
PHYSFS_close(fp);
}

View file

@ -548,7 +548,6 @@ static int init_subtitles(const char *filename)
{
if (!filename)
return 0;
PHYSFS_file *ifile;
int size,read_count;
char *p;
int have_binary = 0;
@ -558,7 +557,7 @@ static int init_subtitles(const char *filename)
if (!GameCfg.MovieSubtitles)
return 0;
ifile = PHYSFSX_openReadBuffered(filename); //try text version
auto ifile = PHYSFSX_openReadBuffered(filename); //try text version
if (!ifile) { //no text version, try binary version
char filename2[FILENAME_LEN];
@ -574,9 +573,7 @@ static int init_subtitles(const char *filename)
MALLOC (subtitle_raw_data, char, size+1);
read_count = PHYSFS_read(ifile, subtitle_raw_data, 1, size);
PHYSFS_close(ifile);
ifile.reset();
subtitle_raw_data[size] = 0;
if (read_count != size) {

View file

@ -1021,7 +1021,6 @@ grs_font_ptr gr_init_font( const char * fontname )
{
unsigned char * ptr;
int nchars;
PHYSFS_file *fontfile;
char file_id[4];
int datasize; //size up to (but not including) palette
@ -1034,7 +1033,7 @@ grs_font_ptr gr_init_font( const char * fontname )
strncpy(&f.filename[0], fontname, FILENAME_LEN);
fontfile = PHYSFSX_openReadBuffered(fontname);
auto fontfile = PHYSFSX_openReadBuffered(fontname);
if (!fontfile) {
con_printf(CON_VERBOSE, "Can't open font file %s", fontname);
@ -1100,9 +1099,7 @@ grs_font_ptr gr_init_font( const char * fontname )
decode_data(font->ft_data, ptr - font->ft_data, colormap, freq );
}
PHYSFS_close(fontfile);
fontfile.reset();
//set curcanv vars
grd_curcanv->cv_font = font.get();
@ -1123,7 +1120,6 @@ grs_font_ptr gr_init_font( const char * fontname )
void gr_remap_font( grs_font *font, const char * fontname, uint8_t *font_data )
{
int nchars;
PHYSFS_file *fontfile;
char file_id[4];
int datasize; //size up to (but not including) palette
unsigned char *ptr;
@ -1131,7 +1127,7 @@ void gr_remap_font( grs_font *font, const char * fontname, uint8_t *font_data )
if (! (font->ft_flags & FT_COLOR))
return;
fontfile = PHYSFSX_openReadBuffered(fontname);
auto fontfile = PHYSFSX_openReadBuffered(fontname);
if (!fontfile)
Error( "Can't open font file %s", fontname );
@ -1192,9 +1188,6 @@ void gr_remap_font( grs_font *font, const char * fontname, uint8_t *font_data )
decode_data(font->ft_data, ptr - font->ft_data, colormap, freq );
}
PHYSFS_close(fontfile);
#ifdef OGL
gr_free_bitmap_data(font->ft_parent_bitmap);
ogl_init_font(font);

View file

@ -123,10 +123,9 @@ void gr_copy_palette(palette_array_t &gr_palette, const palette_array_t &pal)
void gr_use_palette_table(const char * filename )
{
PHYSFS_file *fp;
int fsize;
fp = PHYSFSX_openReadBuffered( filename );
auto fp = PHYSFSX_openReadBuffered(filename);
#if defined(DXX_BUILD_DESCENT_I)
#define FAILURE_FORMAT "Can't open palette file <%s>"
#elif defined(DXX_BUILD_DESCENT_II)
@ -147,7 +146,7 @@ void gr_use_palette_table(const char * filename )
(void)fsize;
PHYSFS_read( fp, &gr_palette[0], sizeof(gr_palette[0]), gr_palette.size() );
PHYSFS_read( fp, gr_fade_table, 256*34, 1 );
PHYSFS_close(fp);
fp.reset();
// This is the TRANSPARENCY COLOR
range_for (auto &i, gr_fade_table)

View file

@ -92,14 +92,13 @@ static int PCXHeader_read_n(PCXHeader *ph, int n, PHYSFS_file *fp)
int bald_guy_load(const char * filename, grs_bitmap * bmp,int bitmap_type ,palette_array_t &palette )
{
PCXHeader header;
PHYSFS_file * PCXfile;
int i, count, fsize;
ubyte data, c, xor_value;
ubyte *p;
unsigned int row, xsize;
unsigned int col, ysize;
PCXfile = PHYSFSX_openReadBuffered( filename );
auto PCXfile = PHYSFSX_openReadBuffered(filename);
if ( !PCXfile )
return PCX_ERROR_OPENING;
@ -121,8 +120,7 @@ int bald_guy_load(const char * filename, grs_bitmap * bmp,int bitmap_type ,palet
bguy_data[i] = c;
xor_value--;
}
PHYSFS_close(PCXfile);
PCXfile.reset();
p = bguy_data;
memcpy( &header, p, sizeof(PCXHeader) );
p += sizeof(PCXHeader);
@ -182,20 +180,18 @@ int bald_guy_load(const char * filename, grs_bitmap * bmp,int bitmap_type ,palet
struct PCX_PHYSFS_file
{
PHYSFS_file *PCXfile;
RAIIPHYSFS_File PCXfile;
};
static int pcx_read_bitmap_file(struct PCX_PHYSFS_file *const pcxphysfs, grs_bitmap &bmp,int bitmap_type ,palette_array_t &palette);
int pcx_read_bitmap(const char * filename, grs_bitmap &bmp, int bitmap_type, palette_array_t &palette )
{
struct PCX_PHYSFS_file pcxphysfs;
int result;
pcxphysfs.PCXfile = PHYSFSX_openReadBuffered( filename );
PCX_PHYSFS_file pcxphysfs{PHYSFSX_openReadBuffered(filename)};
if (!pcxphysfs.PCXfile)
return PCX_ERROR_OPENING;
result = pcx_read_bitmap_file(&pcxphysfs, bmp, bitmap_type, palette);
PHYSFS_close(pcxphysfs.PCXfile);
return result;
}
@ -293,7 +289,6 @@ int pcx_write_bitmap(const char * filename, grs_bitmap * bmp, palette_array_t &p
int retval;
ubyte data;
PCXHeader header{};
PHYSFS_file *PCXfile;
header.Manufacturer = 10;
header.Encoding = 1;
@ -304,19 +299,17 @@ int pcx_write_bitmap(const char * filename, grs_bitmap * bmp, palette_array_t &p
header.Ymax = bmp->bm_h-1;
header.BytesPerLine = bmp->bm_w;
PCXfile = PHYSFSX_openWriteBuffered(filename);
auto PCXfile = PHYSFSX_openWriteBuffered(filename);
if ( !PCXfile )
return PCX_ERROR_OPENING;
if (PHYSFS_write(PCXfile, &header, PCXHEADER_SIZE, 1) != 1)
{
PHYSFS_close(PCXfile);
return PCX_ERROR_WRITING;
}
for (uint_fast32_t i=0; i<bmp->bm_h; i++ ) {
if (!pcx_encode_line( &bmp->get_bitmap_data()[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
PHYSFS_close(PCXfile);
return PCX_ERROR_WRITING;
}
}
@ -325,7 +318,6 @@ int pcx_write_bitmap(const char * filename, grs_bitmap * bmp, palette_array_t &p
data = 12;
if (PHYSFS_write(PCXfile, &data, 1, 1) != 1)
{
PHYSFS_close(PCXfile);
return PCX_ERROR_WRITING;
}
@ -342,13 +334,9 @@ int pcx_write_bitmap(const char * filename, grs_bitmap * bmp, palette_array_t &p
diminish_palette(palette);
if (retval !=1) {
PHYSFS_close(PCXfile);
return PCX_ERROR_WRITING;
}
PHYSFS_close(PCXfile);
return PCX_ERROR_NONE;
}
// returns number of bytes written into outBuff, 0 if failed

View file

@ -1035,7 +1035,6 @@ struct TGA_header
//if we got really spiffy, we could optionally link in libpng or something, and use that.
static void write_bmp(char *savename,unsigned w,unsigned h)
{
PHYSFS_file* TGAFile;
TGA_header TGA;
GLbyte HeightH,HeightL,WidthH,WidthL;
RAIIdubyte buf;
@ -1050,7 +1049,8 @@ static void write_bmp(char *savename,unsigned w,unsigned h)
*(buf + pixel * 3 + 2) = *(rgbaBuf + pixel * 4);
}
if (!(TGAFile = PHYSFSX_openWriteBuffered(savename)))
auto TGAFile = PHYSFSX_openWriteBuffered(savename);
if (!TGAFile)
{
con_printf(CON_URGENT,"Could not create TGA file to dump screenshot!");
return;
@ -1081,7 +1081,6 @@ static void write_bmp(char *savename,unsigned w,unsigned h)
TGA.header[5] = 0;
PHYSFS_write(TGAFile,&TGA,sizeof(TGA_header),1);
PHYSFS_write(TGAFile,buf,w*h*3*sizeof(unsigned char),1);
PHYSFS_close(TGAFile);
}
void save_screen_shot(int automap_flag)

View file

@ -975,7 +975,6 @@ static array<d_fname, MAX_TEXTURES> current_tmap_list;
// values in the headers.
static int med_save_group( const char *filename, const group::vertex_array_type_t &vertex_ids, const group::segment_array_type_t &segment_ids)
{
PHYSFS_file * SaveFile;
int header_offset, editor_offset, vertex_offset, segment_offset, texture_offset;
char ErrorMessage[100];
int i, j;
@ -983,7 +982,7 @@ static int med_save_group( const char *filename, const group::vertex_array_type_
segment tseg;
vms_vector tvert;
SaveFile = PHYSFSX_openWriteBuffered( filename );
auto SaveFile = PHYSFSX_openWriteBuffered(filename);
if (!SaveFile)
{
sprintf( ErrorMessage, "ERROR: Unable to open %s\n", filename );
@ -1094,10 +1093,7 @@ static int med_save_group( const char *filename, const group::vertex_array_type_
PHYSFS_write( SaveFile, &group_fileinfo, sizeof(group_fileinfo), 1);
//==================== CLOSE THE FILE =============================
PHYSFS_close(SaveFile);
return 0;
}
static array<d_fname, MAX_TEXTURES> old_tmap_list;
@ -1115,9 +1111,7 @@ static int med_load_group( const char *filename, group::vertex_array_type_t &ver
char *temptr;
int i, j;
segment tseg;
PHYSFS_file * LoadFile;
LoadFile = PHYSFSX_openReadBuffered( filename );
auto LoadFile = PHYSFSX_openReadBuffered(filename);
if (!LoadFile)
{
sprintf( ErrorMessage, "ERROR: Unable to open %s\n", filename );
@ -1161,7 +1155,6 @@ static int med_load_group( const char *filename, group::vertex_array_type_t &ver
if (ui_messagebox( -2, -2, 2, ErrorMessage, "Forget it", "Try anyway" )==1)
{
PHYSFS_close( LoadFile );
return 1;
}
@ -1324,7 +1317,7 @@ static int med_load_group( const char *filename, group::vertex_array_type_t &ver
//======================== CLOSE FILE ==============================
PHYSFS_close( LoadFile );
LoadFile.reset();
//========================= UPDATE VARIABLES ======================

View file

@ -192,9 +192,7 @@ static int med_load_situation(char * filename)
// -----------------------------------------------------------------------------
static int med_save_situation(char * filename)
{
PHYSFS_file * SaveFile;
SaveFile = PHYSFSX_openWriteBuffered( filename );
auto SaveFile = PHYSFSX_openWriteBuffered(filename);
if (!SaveFile) {
char ErrorMessage[200];
@ -216,9 +214,6 @@ static int med_save_situation(char * filename)
PHYSFSX_printf(SaveFile, "%8x %8x %8x\n",(unsigned int) ConsoleObject->orient.uvec.x,(unsigned int) ConsoleObject->orient.uvec.y,(unsigned int) ConsoleObject->orient.uvec.z);
PHYSFSX_printf(SaveFile, "%8x %8x %8x\n",(unsigned int) ConsoleObject->orient.fvec.x,(unsigned int) ConsoleObject->orient.fvec.y,(unsigned int) ConsoleObject->orient.fvec.z);
PHYSFSX_printf(SaveFile, "%i\n", ConsoleObject->segnum);
PHYSFS_close( SaveFile);
return 1;
}

View file

@ -275,7 +275,6 @@ static int (*KeyFunction[2048])();
static void medkey_init()
{
PHYSFS_file * keyfile;
char keypress[100];
int key;
int i; //, size;
@ -285,8 +284,7 @@ static void medkey_init()
for (i=0; i<2048; i++ )
KeyFunction[i] = NULL;
keyfile = PHYSFSX_openReadBuffered( "GLOBAL.KEY" );
if (keyfile)
if (auto keyfile = PHYSFSX_openReadBuffered("GLOBAL.KEY"))
{
PHYSFSX_gets_line_t<200> line_buffer;
while (PHYSFSX_fgets(line_buffer, keyfile))
@ -302,7 +300,6 @@ static void medkey_init()
Error( "Bad key %s in GLOBAL.KEY!", keypress );
}
}
PHYSFS_close(keyfile);
}
}

View file

@ -332,10 +332,9 @@ static short convert_to_d1_tmap_num(short tmap_num)
int med_save_mine(const char * filename)
{
PHYSFS_file *SaveFile;
char ErrorMessage[256];
SaveFile = PHYSFSX_openWriteBuffered( filename );
auto SaveFile = PHYSFSX_openWriteBuffered(filename);
if (!SaveFile)
{
#if 0 //ndef __linux__
@ -354,10 +353,7 @@ int med_save_mine(const char * filename)
save_mine_data(SaveFile);
//==================== CLOSE THE FILE =============================
PHYSFS_close(SaveFile);
return 0;
}
// -----------------------------------------------------------------------------

View file

@ -404,10 +404,9 @@ static void bm_free_extra_models()
//type==1 means 1.1, type==2 means 1.2 (with weapons)
void bm_read_extra_robots(const char *fname,int type)
{
PHYSFS_file *fp;
int t,i,version;
fp = PHYSFSX_openReadBuffered(fname);
auto fp = PHYSFSX_openReadBuffered(fname);
if (!fp)
{
Error("Failed to open HAM file \"%s\"", fname);
@ -475,22 +474,18 @@ void bm_read_extra_robots(const char *fname,int type)
Error("Too many object bitmap pointers (%d) in <%s>. Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
ObjBitmapPtrs[i] = PHYSFSX_readShort(fp);
PHYSFS_close(fp);
}
int Robot_replacements_loaded = 0;
void load_robot_replacements(const d_fname &level_name)
{
PHYSFS_file *fp;
int t,i,j;
char ifile_name[FILENAME_LEN];
change_filename_extension(ifile_name, level_name, ".HXM" );
fp = PHYSFSX_openReadBuffered(ifile_name);
auto fp = PHYSFSX_openReadBuffered(ifile_name);
if (!fp) //no robot replacement file
return;
@ -548,8 +543,6 @@ void load_robot_replacements(const d_fname &level_name)
Error("Object bitmap pointer (%d) out of range in (%s). Range = [0..%d].",i,static_cast<const char *>(level_name),MAX_OBJ_BITMAPS-1);
ObjBitmapPtrs[i] = PHYSFSX_readShort(fp);
}
PHYSFS_close(fp);
Robot_replacements_loaded = 1;
}
@ -623,7 +616,6 @@ static grs_bitmap *bm_load_extra_objbitmap(const char *name)
int load_exit_models()
{
PHYSFS_file *exit_hamfile;
int start_num;
bm_free_extra_models();
@ -640,10 +632,8 @@ int load_exit_models()
con_printf(CON_NORMAL, "Can't load exit models!");
return 0;
}
exit_hamfile = PHYSFSX_openReadBuffered("exit.ham");
if (exit_hamfile) {
if (auto exit_hamfile = PHYSFSX_openReadBuffered("exit.ham"))
{
exit_modelnum = N_polygon_models++;
destroyed_exit_modelnum = N_polygon_models++;
polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
@ -654,9 +644,6 @@ int load_exit_models()
polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
PHYSFS_close(exit_hamfile);
} else if (PHYSFSX_exists("exit01.pof",1) && PHYSFSX_exists("exit01d.pof",1)) {
exit_modelnum = load_polygon_model("exit01.pof", 3, start_num, NULL);
@ -667,12 +654,10 @@ int load_exit_models()
ogl_cache_polymodel_textures(destroyed_exit_modelnum);
#endif
}
else if (PHYSFSX_exists(D1_PIGFILE,1))
else if (auto exit_hamfile = PHYSFSX_openReadBuffered(D1_PIGFILE))
{
int offset, offset2;
int hamsize;
exit_hamfile = PHYSFSX_openReadBuffered(D1_PIGFILE);
hamsize = PHYSFS_fileLength(exit_hamfile);
switch (hamsize) { //total hack for loading models
case D1_PIGSIZE:
@ -703,8 +688,6 @@ int load_exit_models()
PHYSFSX_fseek(exit_hamfile, offset2, SEEK_SET);
polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
PHYSFS_close(exit_hamfile);
} else {
con_printf(CON_NORMAL, "Can't load exit models!");
return 0;

View file

@ -81,8 +81,6 @@ struct Cfg GameCfg;
int ReadConfigFile()
{
PHYSFS_file *infile;
// set defaults
GameCfg.DigiVolume = 8;
GameCfg.MusicVolume = 8;
@ -133,7 +131,7 @@ int ReadConfigFile()
GameCfg.Grabinput = 1;
infile = PHYSFSX_openReadBuffered("descent.cfg");
auto infile = PHYSFSX_openReadBuffered("descent.cfg");
if (!infile)
{
return 1;
@ -215,9 +213,6 @@ int ReadConfigFile()
else if (cmp(lb, eq, GrabinputStr))
convert_integer(GameCfg.Grabinput, value);
}
PHYSFS_close(infile);
if ( GameCfg.DigiVolume > 8 ) GameCfg.DigiVolume = 8;
if ( GameCfg.MusicVolume > 8 ) GameCfg.MusicVolume = 8;
@ -229,10 +224,9 @@ int ReadConfigFile()
int WriteConfigFile()
{
PHYSFS_file *infile;
GameCfg.GammaLevel = gr_palette_get_gamma();
infile = PHYSFSX_openWriteBuffered("descent.cfg");
auto infile = PHYSFSX_openWriteBuffered("descent.cfg");
if (!infile)
{
return 1;
@ -268,8 +262,5 @@ int WriteConfigFile()
PHYSFSX_printf(infile, "%s=%i\n", MultisampleStr, GameCfg.Multisample);
PHYSFSX_printf(infile, "%s=%i\n", FPSIndicatorStr, GameCfg.FPSIndicator);
PHYSFSX_printf(infile, "%s=%i\n", GrabinputStr, GameCfg.Grabinput);
PHYSFS_close(infile);
return 0;
}

View file

@ -31,7 +31,7 @@
#include "dxxsconf.h"
#include "compiler-array.h"
static PHYSFS_file *gamelog_fp=NULL;
static RAIIPHYSFS_File gamelog_fp;
static array<console_buffer, CON_LINES_MAX> con_buffer;
static int con_state = CON_STATE_CLOSED, con_scroll_offset = 0, con_size = 0;
@ -282,17 +282,14 @@ void con_showup(void)
static void con_close(void)
{
if (gamelog_fp)
PHYSFS_close(gamelog_fp);
gamelog_fp = NULL;
gamelog_fp.reset();
}
void con_init(void)
{
con_buffer = {};
if (GameArg.DbgSafelog)
gamelog_fp = PHYSFS_openWrite("gamelog.txt");
gamelog_fp.reset(PHYSFS_openWrite("gamelog.txt"));
else
gamelog_fp = PHYSFSX_openWriteBuffered("gamelog.txt");
atexit(con_close);

View file

@ -69,7 +69,7 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
struct credits : ignore_window_pointer_t
{
PHYSFS_file * file;
RAIIPHYSFS_File file;
int have_bin_file;
array<PHYSFSX_gets_line_t<80>, NUM_LINES> buffer;
int buffer_line;
@ -203,12 +203,10 @@ static window_event_result credits_handler(window *wind,const d_event &event, cr
case EVENT_WINDOW_CLOSE:
gr_free_bitmap_data(cr->backdrop);
PHYSFS_close(cr->file);
songs_set_volume(GameCfg.MusicVolume);
songs_play_song( SONG_TITLE, 1 );
delete cr;
std::default_delete<credits>()(cr);
break;
default:
break;
}
@ -254,7 +252,6 @@ void credits_show(const char *credits_filename)
pcx_error = pcx_read_bitmap(STARS_BACKGROUND, cr->backdrop, BM_LINEAR,backdrop_palette);
if (pcx_error != PCX_ERROR_NONE) {
PHYSFS_close(cr->file);
return;
}

View file

@ -586,8 +586,6 @@ void write_game_text_file(const char *filename)
{
char my_filename[128];
int namelen;
PHYSFS_file * my_file;
Errors_in_mine = 0;
namelen = strlen(filename);
@ -599,8 +597,7 @@ void write_game_text_file(const char *filename)
strcpy(my_filename, filename);
strcpy( &my_filename[namelen-4], ".txm");
my_file = PHYSFSX_openWriteBuffered( my_filename );
auto my_file = PHYSFSX_openWriteBuffered(my_filename);
if (!my_file) {
gr_palette_load(gr_palette);
nm_messagebox( NULL, 1, "Ok", "ERROR: Unable to open %s\nErrno = %i", my_filename, errno);
@ -638,13 +635,6 @@ void write_game_text_file(const char *filename)
// ---------- Show keyed walls ----------
write_key_text(my_file);
{
int r;
r = PHYSFS_close(my_file);
if (!r)
Int3();
}
}
#if defined(DXX_BUILD_DESCENT_II)
@ -1022,10 +1012,7 @@ int Last_dump_level = NUM_ADAM_LEVELS-1;
static void say_totals_all(void)
{
int i;
PHYSFS_file *my_file;
my_file = PHYSFSX_openWriteBuffered( "levels.all" );
auto my_file = PHYSFSX_openWriteBuffered("levels.all");
if (!my_file) {
gr_palette_load(gr_palette);
nm_messagebox( NULL, 1, "Ok", "ERROR: Unable to open levels.all\nErrno=%i", errno );
@ -1049,8 +1036,6 @@ static void say_totals_all(void)
say_totals(my_file, Adam_level_names[i]);
}
#endif
PHYSFS_close(my_file);
}
static void dump_used_textures_level(PHYSFS_file *my_file, int level_num)
@ -1076,7 +1061,6 @@ static void dump_used_textures_level(PHYSFS_file *my_file, int level_num)
// ----------------------------------------------------------------------------
void dump_used_textures_all(void)
{
PHYSFS_file *my_file;
int i;
#if defined(DXX_BUILD_DESCENT_I)
int temp_tmap_buf[MAX_TEXTURES];
@ -1093,7 +1077,7 @@ void dump_used_textures_all(void)
say_totals_all();
my_file = PHYSFSX_openWriteBuffered( "textures.dmp" );
auto my_file = PHYSFSX_openWriteBuffered("textures.dmp");
if (!my_file) {
gr_palette_load(gr_palette);
@ -1152,8 +1136,6 @@ say_totals_all();
PHYSFSX_printf(my_file, "\nUnused textures in all (including registered) mines:\n");
say_unused_tmaps(my_file, perm_tmap_buf);
PHYSFS_close(my_file);
}
#endif

View file

@ -1364,7 +1364,6 @@ void load_endlevel_data(int level_num)
{
d_fname filename;
char *p;
PHYSFS_file *ifile;
int var;
int exit_side = 0;
int have_binary = 0;
@ -1387,7 +1386,7 @@ try_again:
Error("Error converting filename <%s> for endlevel data\n",static_cast<const char *>(filename));
#endif
ifile = PHYSFSX_openReadBuffered(filename);
auto ifile = PHYSFSX_openReadBuffered(filename);
if (!ifile) {
@ -1450,7 +1449,6 @@ try_again:
con_printf(CON_DEBUG, "Can't load exit terrain from file %s: IFF error: %s",
p, iff_errormsg(iff_error));
endlevel_data_loaded = 0; // won't be able to play endlevel sequence
PHYSFS_close(ifile);
return;
}
@ -1486,7 +1484,6 @@ try_again:
con_printf(CON_DEBUG, "Can't load exit satellite from file %s: IFF error: %s",
p, iff_errormsg(iff_error));
endlevel_data_loaded = 0; // won't be able to play endlevel sequence
PHYSFS_close(ifile);
return;
}
@ -1577,9 +1574,5 @@ try_again:
}
PHYSFS_close(ifile);
endlevel_data_loaded = 1;
}

View file

@ -1185,7 +1185,6 @@ int load_level(const char * filename_passed)
#ifdef EDITOR
int use_compiled_level=1;
#endif
PHYSFS_file * LoadFile;
char filename[PATH_MAX];
int sig, minedata_offset, gamedata_offset;
int mine_err, game_err;
@ -1230,7 +1229,7 @@ int load_level(const char * filename_passed)
if (!PHYSFSX_exists(filename,1))
sprintf(filename,"%s%s",MISSION_DIR,filename_passed);
LoadFile = PHYSFSX_openReadBuffered( filename );
auto LoadFile = PHYSFSX_openReadBuffered(filename);
if (!LoadFile) {
#ifdef EDITOR
@ -1358,7 +1357,6 @@ int load_level(const char * filename_passed)
#endif
if (mine_err == -1) { //error!!
PHYSFS_close(LoadFile);
return 2;
}
@ -1366,14 +1364,11 @@ int load_level(const char * filename_passed)
game_err = load_game_data(LoadFile);
if (game_err == -1) { //error!!
PHYSFS_close(LoadFile);
return 3;
}
//======================== CLOSE FILE =============================
PHYSFS_close( LoadFile );
LoadFile.reset();
#if defined(DXX_BUILD_DESCENT_II)
set_ambient_sound_flags();
#endif
@ -1663,7 +1658,6 @@ static int save_game_data(PHYSFS_file *SaveFile)
// Save game
static int save_level_sub(const char * filename, int compiled_version)
{
PHYSFS_file * SaveFile;
char temp_filename[PATH_MAX];
int minedata_offset=0,gamedata_offset=0;
@ -1692,7 +1686,7 @@ static int save_level_sub(const char * filename, int compiled_version)
change_filename_extension(temp_filename, filename, "." D1X_LEVEL_FILE_EXTENSION);
}
SaveFile = PHYSFSX_openWriteBuffered(temp_filename);
auto SaveFile = PHYSFSX_openWriteBuffered(temp_filename);
if (!SaveFile)
{
gr_palette_load(gr_palette);
@ -1795,7 +1789,6 @@ static int save_level_sub(const char * filename, int compiled_version)
#endif
//==================== CLOSE THE FILE =============================
PHYSFS_close(SaveFile);
// if ( !compiled_version )
{

View file

@ -599,16 +599,12 @@ static int iff_parse_bitmap(PHYSFS_file *ifile, grs_bitmap *bm, int bitmap_type,
int iff_read_bitmap(const char *ifilename,grs_bitmap *bm,int bitmap_type,palette_array_t *palette)
{
int ret; //return code
PHYSFS_file *ifile;
ifile = PHYSFSX_openReadBuffered(ifilename);
auto ifile = PHYSFSX_openReadBuffered(ifilename);
if (!ifile)
return IFF_NO_FILE;
bm->bm_data = NULL;
ret = iff_parse_bitmap(ifile,bm,bitmap_type,palette,NULL);
PHYSFS_close(ifile);
return ret;
}
@ -617,15 +613,11 @@ int iff_read_bitmap(const char *ifilename,grs_bitmap *bm,int bitmap_type,palette
int iff_read_into_bitmap(const char *ifilename, grs_bitmap *bm, palette_array_t *palette)
{
int ret; //return code
PHYSFS_file *ifile;
ifile = PHYSFSX_openReadBuffered(ifilename);
auto ifile = PHYSFSX_openReadBuffered(ifilename);
if (!ifile)
return IFF_NO_FILE;
ret = iff_parse_bitmap(ifile,bm,bm->bm_type,palette,NULL);
PHYSFS_close(ifile);
return ret;
}
@ -881,7 +873,6 @@ static int write_pbm(PHYSFS_file *ofile,iff_bitmap_header *bitmap_header,int com
//returns error codes - see IFF.H.
int iff_write_bitmap(const char *ofilename,grs_bitmap *bm,palette_array_t *palette)
{
PHYSFS_file *ofile;
iff_bitmap_header bmheader;
int ret;
int compression_on;
@ -919,14 +910,11 @@ int iff_write_bitmap(const char *ofilename,grs_bitmap *bm,palette_array_t *palet
//open file and write
ofile = PHYSFS_openWrite(ofilename);
RAIIPHYSFS_File ofile{PHYSFS_openWrite(ofilename)};
if (!ofile)
return IFF_NO_FILE;
ret = write_pbm(ofile,&bmheader,compression_on);
PHYSFS_close(ofile);
return ret;
}
@ -935,13 +923,12 @@ int iff_write_bitmap(const char *ofilename,grs_bitmap *bm,palette_array_t *palet
int iff_read_animbrush(const char *ifilename,array<std::unique_ptr<grs_bitmap>, MAX_BITMAPS_PER_BRUSH> &bm_list,unsigned *n_bitmaps,palette_array_t &palette)
{
int ret = IFF_NO_ERROR; //return code
PHYSFS_file *ifile;
int sig,form_len;
long form_type;
*n_bitmaps=0;
ifile = PHYSFSX_openReadBuffered(ifilename);
auto ifile = PHYSFSX_openReadBuffered(ifilename);
if (!ifile)
return IFF_NO_FILE;
@ -985,11 +972,7 @@ int iff_read_animbrush(const char *ifilename,array<std::unique_ptr<grs_bitmap>,
ret = IFF_UNKNOWN_FORM;
done:
PHYSFS_close(ifile);
return ret;
}
//text for error messges

View file

@ -1076,10 +1076,9 @@ static window_event_result kconfig_key_command(window *wind,const d_event &event
}
return window_event_result::handled;
#ifdef TABLE_CREATION
case KEY_F12: {
PHYSFS_file * fp;
fp = PHYSFSX_openWriteBuffered( "kconfig.cod" );
case KEY_F12:
if (auto fp = PHYSFSX_openWriteBuffered("kconfig.cod"))
{
PHYSFSX_printf( fp, "const ubyte DefaultKeySettings[3][MAX_CONTROLS] = {\n" );
for (unsigned i=0; i<3; i++ ) {
PHYSFSX_printf( fp, "{0x%2x", PlayerCfg.KeySettings[i][0] );
@ -1093,8 +1092,6 @@ static window_event_result kconfig_key_command(window *wind,const d_event &event
print_create_table_items(fp, "joystick", kcl_joystick, kc_joystick);
print_create_table_items(fp, "mouse", kcl_mouse, kc_mouse);
print_create_table_items(fp, "rebirth", kcl_rebirth, kc_rebirth);
PHYSFS_close(fp);
}
return window_event_result::handled;
#endif

View file

@ -342,10 +342,8 @@ static int read_mission_file(mission_list &mission_list, const char *filename, e
{
char filename2[100];
snprintf(filename2, sizeof(filename2), "%s%s", location == ML_MISSIONDIR ? MISSION_DIR : "", filename);
PHYSFS_file *mfile;
mfile = PHYSFSX_openReadBuffered(filename2);
if (mfile) {
if (auto mfile = PHYSFSX_openReadBuffered(filename2))
{
char *p;
char temp[PATH_MAX], *ext;
@ -400,7 +398,6 @@ static int read_mission_file(mission_list &mission_list, const char *filename, e
mission->mission_name.copy_if(p, mission->mission_name.size() - 1);
}
else {
PHYSFS_close(mfile);
mission_list.pop_back();
return 0;
}
@ -418,9 +415,6 @@ static int read_mission_file(mission_list &mission_list, const char *filename, e
}
}
}
PHYSFS_close(mfile);
return 1;
}
@ -706,7 +700,6 @@ static void record_briefing(d_fname &f, array<char, PATH_MAX> &buf)
//Returns true if mission loaded ok, else false.
static int load_mission(const mle *mission)
{
PHYSFS_file *mfile;
char buf[PATH_MAX], *v;
#if defined(DXX_BUILD_DESCENT_II)
@ -785,9 +778,8 @@ static int load_mission(const mle *mission)
PHYSFSEXT_locateCorrectCase(buf);
mfile = PHYSFSX_openReadBuffered(buf);
if (!mfile)
{
auto mfile = PHYSFSX_openReadBuffered(buf);
if (!mfile) {
Current_mission.reset();
return 0; //error!
}
@ -922,9 +914,7 @@ static int load_mission(const mle *mission)
#endif
}
PHYSFS_close(mfile);
mfile.reset();
if (Last_level <= 0) {
Current_mission.reset(); //no valid mission loaded
return 0;

View file

@ -4877,7 +4877,6 @@ void init_hoard_data()
int orb_w,orb_h;
int icon_w,icon_h;
palette_array_t palette;
PHYSFS_file *ifile;
ubyte *bitmap_data1;
int i,save_pos;
int bitmap_num=Hoard_bm_idx=Num_bitmap_files;
@ -4885,7 +4884,7 @@ void init_hoard_data()
if (!first_time)
free_hoard_data();
ifile = PHYSFSX_openReadBuffered("hoard.ham");
auto ifile = PHYSFSX_openReadBuffered("hoard.ham");
if (!ifile)
Error("can't open <hoard.ham>");
@ -4999,8 +4998,6 @@ void init_hoard_data()
Sounds[SOUND_YOU_GOT_ORB+i] = Num_sound_files+i;
AltSounds[SOUND_YOU_GOT_ORB+i] = Sounds[SOUND_YOU_GOT_ORB+i];
}
PHYSFS_close(ifile);
if (first_time)
atexit(free_hoard_data);
@ -5013,7 +5010,6 @@ void save_hoard_data(void)
grs_bitmap icon;
unsigned nframes;
palette_array_t palette;
PHYSFS_file *ofile;
int iff_error;
unsigned i;
static const char sounds[][13] = {"selforb.raw","selforb.r22", //SOUND_YOU_GOT_ORB
@ -5021,7 +5017,7 @@ void save_hoard_data(void)
"enemyorb.raw","enemyorb.r22", //SOUND_OPPONENT_GOT_ORB
"OPSCORE1.raw","OPSCORE1.r22"}; //SOUND_OPPONENT_HAS_SCORED
ofile = PHYSFSX_openWriteBuffered("hoard.ham");
auto ofile = PHYSFSX_openWriteBuffered("hoard.ham");
array<std::unique_ptr<grs_bitmap>, MAX_BITMAPS_PER_BRUSH> bm;
iff_error = iff_read_animbrush("orb.abm",bm,&nframes,palette);
@ -5052,22 +5048,17 @@ void save_hoard_data(void)
}
(void)iff_error;
for (i=0;i<sizeof(sounds)/sizeof(*sounds);i++) {
PHYSFS_file *ifile;
range_for (auto &i, sounds)
if (RAIIPHYSFS_File ifile{PHYSFS_openRead(i)})
{
int size;
ifile = PHYSFS_openRead(sounds[i]);
Assert(ifile != NULL);
size = PHYSFS_fileLength(ifile);
RAIIdubyte buf;
MALLOC(buf, ubyte, size);
PHYSFS_read(ifile, buf, size, 1);
PHYSFS_writeULE32(ofile, size);
PHYSFS_write(ofile, buf, size, 1);
PHYSFS_close(ifile);
}
PHYSFS_close(ofile);
}
#endif
#endif

View file

@ -3896,12 +3896,12 @@ int newdemo_swap_endian(const char *filename)
else
return 0;
infile = PHYSFSX_openReadBuffered(inpath);
infile = PHYSFSX_openReadBuffered(inpath).release();
if (infile==NULL)
goto read_error;
nd_playback_v_demosize = PHYSFS_fileLength(infile); // should be exactly the same size
outfile = PHYSFSX_openWriteBuffered(DEMO_FILENAME);
outfile = PHYSFSX_openWriteBuffered(DEMO_FILENAME).release();
if (outfile==NULL)
{
PHYSFS_close(infile);
@ -3958,13 +3958,12 @@ read_error:
void newdemo_strip_frames(char *outname, int bytes_to_strip)
{
PHYSFS_file *outfile;
char *buf;
int read_elems, bytes_back;
int trailer_start, loc1, loc2, stop_loc, bytes_to_read;
short last_frame_length;
outfile = PHYSFSX_openWriteBuffered(outname);
auto outfile = PHYSFSX_openWriteBuffered(outname);
if (!outfile) {
nm_messagebox( NULL, 1, TXT_OK, "Can't open output file" );
newdemo_stop_playback();
@ -3973,7 +3972,6 @@ void newdemo_strip_frames(char *outname, int bytes_to_strip)
MALLOC(buf, char, BUF_SIZE);
if (buf == NULL) {
nm_messagebox( NULL, 1, TXT_OK, "Can't malloc output buffer" );
PHYSFS_close(outfile);
newdemo_stop_playback();
return;
}
@ -4008,7 +4006,6 @@ void newdemo_strip_frames(char *outname, int bytes_to_strip)
PHYSFS_seek(outfile, stop_loc);
PHYSFS_seek(outfile, PHYSFS_tell(infile) + 1);
PHYSFS_write(outfile, &last_frame_length, 2, 1);
PHYSFS_close(outfile);
newdemo_stop_playback();
}

View file

@ -142,7 +142,7 @@ ushort GameBitmapXlat[MAX_BITMAP_FILES];
int piggy_page_flushed = 0;
PHYSFS_file * Piggy_fp = NULL;
static RAIIPHYSFS_File Piggy_fp;
ubyte bogus_data[64*64];
ubyte bogus_bitmap_initialized=0;
@ -404,9 +404,9 @@ int piggy_find_sound(const char *name)
static void piggy_close_file()
{
if ( Piggy_fp ) {
PHYSFS_close( Piggy_fp );
Piggy_fp = NULL;
if (Piggy_fp)
{
Piggy_fp.reset();
#if defined(DXX_BUILD_DESCENT_II)
Current_pigfile[0] = 0;
#endif
@ -648,8 +648,8 @@ void piggy_init_pigfile(const char *filename)
pig_id = PHYSFSX_readInt(Piggy_fp);
pig_version = PHYSFSX_readInt(Piggy_fp);
if (pig_id != PIGFILE_ID || pig_version != PIGFILE_VERSION) {
PHYSFS_close(Piggy_fp); //out of date pig
Piggy_fp = NULL; //..so pretend it's not here
Piggy_fp.reset(); //out of date pig
//..so pretend it's not here
}
}
@ -755,8 +755,8 @@ void piggy_new_pigfile(char *pigname)
pig_id = PHYSFSX_readInt(Piggy_fp);
pig_version = PHYSFSX_readInt(Piggy_fp);
if (pig_id != PIGFILE_ID || pig_version != PIGFILE_VERSION) {
PHYSFS_close(Piggy_fp); //out of date pig
Piggy_fp = NULL; //..so pretend it's not here
Piggy_fp.reset(); //out of date pig
//..so pretend it's not here
}
}
@ -960,12 +960,11 @@ void piggy_new_pigfile(char *pigname)
int read_hamfile()
{
PHYSFS_file * ham_fp = NULL;
int ham_id;
int sound_offset = 0;
int shareware = 0;
ham_fp = PHYSFSX_openReadBuffered(DEFAULT_HAMFILE_REGISTERED);
auto ham_fp = PHYSFSX_openReadBuffered(DEFAULT_HAMFILE_REGISTERED);
if (!ham_fp)
{
@ -1041,7 +1040,6 @@ int read_hamfile()
if (!justonce)
{
PHYSFS_close(ham_fp);
return 1;
}
justonce = 0;
@ -1068,16 +1066,11 @@ int read_hamfile()
}
SoundBits = make_unique<ubyte[]>(sbytes + 16);
}
PHYSFS_close(ham_fp);
return 1;
}
static int read_sndfile()
{
PHYSFS_file * snd_fp = NULL;
int snd_id,snd_version;
int N_sounds;
int sound_start;
@ -1088,7 +1081,7 @@ static int read_sndfile()
char temp_name_read[16];
int sbytes = 0;
snd_fp = PHYSFSX_openReadBuffered(DEFAULT_SNDFILE);
auto snd_fp = PHYSFSX_openReadBuffered(DEFAULT_SNDFILE);
if (!snd_fp)
return 0;
@ -1096,7 +1089,6 @@ static int read_sndfile()
snd_id = PHYSFSX_readInt(snd_fp);
snd_version = PHYSFSX_readInt(snd_fp);
if (snd_id != SNDFILE_ID || snd_version != SNDFILE_VERSION) {
PHYSFS_close(snd_fp); //out of date sound file
return 0;
}
@ -1119,8 +1111,6 @@ static int read_sndfile()
sbytes += sndh.length;
}
SoundBits = make_unique<ubyte[]>(sbytes + 16);
PHYSFS_close(snd_fp);
return 1;
}
@ -1195,17 +1185,14 @@ void piggy_read_sounds(int pc_shareware)
{
// Read Mac sounds converted to RAW format (too messy to read them directly from the resource fork code-wise)
char soundfile[32] = "Sounds/sounds.array";
PHYSFS_file *array = PHYSFSX_openReadBuffered(soundfile); // hack for Mac Demo
if (array)
// hack for Mac Demo
if (auto array = PHYSFSX_openReadBuffered(soundfile))
{
if (PHYSFS_read(array, Sounds, MAX_SOUNDS, 1) != 1) // make the 'Sounds' index array match with the sounds we're about to read in
{
con_printf(CON_URGENT,"Warning: Can't read Sounds/sounds.array: %s", PHYSFS_getLastError());
PHYSFS_close(array);
return;
}
PHYSFS_close(array);
}
else if (PHYSFSX_fsize(DEFAULT_PIGFILE_REGISTERED) == D1_MAC_SHARE_PIGSIZE)
{
@ -1268,13 +1255,12 @@ void piggy_read_sounds(int pc_shareware)
#elif defined(DXX_BUILD_DESCENT_II)
void piggy_read_sounds(void)
{
PHYSFS_file * fp = NULL;
ubyte * ptr;
int i, sbytes;
ptr = SoundBits.get();
sbytes = 0;
fp = PHYSFSX_openReadBuffered(DEFAULT_SNDFILE);
auto fp = PHYSFSX_openReadBuffered(DEFAULT_SNDFILE);
if (!fp)
return;
@ -1295,9 +1281,6 @@ void piggy_read_sounds(void)
snd->data = (ubyte *) -1;
}
}
PHYSFS_close(fp);
}
#endif
@ -1495,13 +1478,11 @@ void piggy_load_level_data()
static void piggy_write_pigfile(const char *filename)
{
PHYSFS_file *pig_fp;
int bitmap_data_start, data_offset;
DiskBitmapHeader bmh;
int org_offset;
char subst_name[32];
int i;
PHYSFS_file *fp1,*fp2;
char tname[FILENAME_LEN];
for (i=0; i < Num_bitmap_files; i++ ) {
@ -1512,7 +1493,7 @@ static void piggy_write_pigfile(const char *filename)
piggy_close_file();
pig_fp = PHYSFSX_openWriteBuffered( filename ); //open PIG file
auto pig_fp = PHYSFSX_openWriteBuffered(filename); //open PIG file
Assert(pig_fp);
write_int(PIGFILE_ID,pig_fp);
@ -1527,9 +1508,9 @@ static void piggy_write_pigfile(const char *filename)
data_offset = bitmap_data_start;
change_filename_extension(tname,filename,"lst");
fp1 = PHYSFSX_openWriteBuffered( tname );
auto fp1 = PHYSFSX_openWriteBuffered(tname);
change_filename_extension(tname,filename,"all");
fp2 = PHYSFSX_openWriteBuffered( tname );
auto fp2 = PHYSFSX_openWriteBuffered(tname);
for (i=1; i < Num_bitmap_files; i++ ) {
int *size;
@ -1597,14 +1578,7 @@ static void piggy_write_pigfile(const char *filename)
bmh.avg_color=GameBitmaps[i].avg_color;
PHYSFS_write(pig_fp, &bmh, sizeof(DiskBitmapHeader), 1); // Mark as a bitmap
}
PHYSFS_close(pig_fp);
PHYSFSX_printf( fp1, " Dumped %d assorted bitmaps.\n", Num_bitmap_files );
PHYSFS_close(fp1);
PHYSFS_close(fp2);
}
static void write_int(int i, PHYSFS_file *file)
@ -1740,17 +1714,14 @@ static void free_bitmap_replacements()
void load_bitmap_replacements(const char *level_name)
{
char ifile_name[FILENAME_LEN];
PHYSFS_file *ifile;
int i;
//first, free up data allocated for old bitmaps
free_bitmap_replacements();
change_filename_extension(ifile_name, level_name, ".POG" );
ifile = PHYSFSX_openReadBuffered(ifile_name);
if (ifile) {
if (auto ifile = PHYSFSX_openReadBuffered(ifile_name))
{
int id,version,n_bitmaps;
int bitmap_data_size;
@ -1758,7 +1729,6 @@ void load_bitmap_replacements(const char *level_name)
version = PHYSFSX_readInt(ifile);
if (id != MAKE_SIG('G','O','P','D') || version != 1) {
PHYSFS_close(ifile);
return;
}
@ -1798,10 +1768,7 @@ void load_bitmap_replacements(const char *level_name)
grs_bitmap *bm = &GameBitmaps[indices[i]];
gr_set_bitmap_data(*bm, &Bitmap_replacement_data[(size_t) bm->bm_data]);
}
PHYSFS_close(ifile);
last_palette_loaded_pig[0]= 0; //force pig re-load
texmerge_flush(); //for re-merging with new textures
}
}
@ -1811,11 +1778,10 @@ void load_bitmap_replacements(const char *level_name)
*/
static int get_d1_colormap( palette_array_t &d1_palette, array<color_t, 256> &colormap )
{
PHYSFS_file * palette_file = PHYSFSX_openReadBuffered(D1_PALETTE);
auto palette_file = PHYSFSX_openReadBuffered(D1_PALETTE);
if (!palette_file || PHYSFS_fileLength(palette_file) != 9472)
return -1;
PHYSFS_read( palette_file, &d1_palette[0], sizeof(d1_palette[0]), d1_palette.size() );
PHYSFS_close( palette_file );
array<unsigned, 256> freq;
build_colormap_good( d1_palette, colormap, freq );
// don't change transparencies:
@ -1942,11 +1908,10 @@ static void read_d1_tmap_nums_from_hog(PHYSFS_file *d1_pig)
#define LINEBUF_SIZE 600
int reading_textures = 0;
short texture_count = 0;
PHYSFS_file * bitmaps;
int bitmaps_tbl_is_binary = 0;
int i;
bitmaps = PHYSFSX_openReadBuffered ("bitmaps.tbl");
auto bitmaps = PHYSFSX_openReadBuffered("bitmaps.tbl");
if (!bitmaps) {
bitmaps = PHYSFSX_openReadBuffered ("bitmaps.bin");
bitmaps_tbl_is_binary = 1;
@ -2009,7 +1974,6 @@ static void read_d1_tmap_nums_from_hog(PHYSFS_file *d1_pig)
arg = strtok (NULL, equal_space);
}
}
PHYSFS_close (bitmaps);
}
/* If the given d1_index is the index of a bitmap we have to load
@ -2030,7 +1994,6 @@ static short d2_index_for_d1_index(short d1_index)
#define D1_BITMAPS_SIZE 300000
void load_d1_bitmap_replacements()
{
PHYSFS_file * d1_Piggy_fp;
DiskBitmapHeader bmh;
int pig_data_start, bitmap_header_start, bitmap_data_start;
int N_bitmaps;
@ -2040,7 +2003,7 @@ void load_d1_bitmap_replacements()
char *p;
int pigsize;
d1_Piggy_fp = PHYSFSX_openReadBuffered( D1_PIGFILE );
auto d1_Piggy_fp = PHYSFSX_openReadBuffered(D1_PIGFILE);
#define D1_PIG_LOAD_FAILED "Failed loading " D1_PIGFILE
if (!d1_Piggy_fp) {
@ -2124,9 +2087,6 @@ void load_d1_bitmap_replacements()
}
}
}
PHYSFS_close(d1_Piggy_fp);
last_palette_loaded_pig[0]= 0; //force pig re-load
texmerge_flush(); //for re-merging with new textures
@ -2145,15 +2105,12 @@ bitmap_index read_extra_bitmap_d1_pig(const char *name)
bitmap_num.index = 0;
{
PHYSFS_file *d1_Piggy_fp;
DiskBitmapHeader bmh;
int pig_data_start, bitmap_header_start, bitmap_data_start;
int i, N_bitmaps;
palette_array_t d1_palette;
int pigsize;
d1_Piggy_fp = PHYSFSX_openReadBuffered(D1_PIGFILE);
auto d1_Piggy_fp = PHYSFSX_openReadBuffered(D1_PIGFILE);
if (!d1_Piggy_fp)
{
Warning(D1_PIG_LOAD_FAILED);
@ -2210,8 +2167,6 @@ bitmap_index read_extra_bitmap_d1_pig(const char *name)
}
bitmap_read_d1( n, d1_Piggy_fp, bitmap_data_start, &bmh, 0, d1_palette, colormap );
PHYSFS_close(d1_Piggy_fp);
}
n->avg_color = 0; //compute_average_pixel(n);

View file

@ -302,13 +302,11 @@ static const char *splitword(char *line, char c)
static int read_player_dxx(const char *filename)
{
PHYSFS_file *f;
int rc = 0;
plyr_read_stats();
f = PHYSFSX_openReadBuffered(filename);
auto f = PHYSFSX_openReadBuffered(filename);
if(!f || PHYSFS_eof(f))
return errno;
@ -505,9 +503,6 @@ static int read_player_dxx(const char *filename)
}
}
}
PHYSFS_close(f);
return rc;
}
@ -548,15 +543,10 @@ static void plyr_read_stats_v(int *k, int *d)
{
char filename[PATH_MAX];
int k1=-1,k2=0,d1=-1,d2=0;
PHYSFS_file *f;
*k=0;*d=0;//in case the file doesn't exist.
memset(filename, '\0', PATH_MAX);
snprintf(filename,sizeof(filename),PLAYER_EFFECTIVENESS_FILENAME_FORMAT,static_cast<const char *>(Players[Player_num].callsign));
f = PHYSFSX_openReadBuffered(filename);
if(f)
if (auto f = PHYSFSX_openReadBuffered(filename))
{
PHYSFSX_gets_line_t<256> line;
if(!PHYSFS_eof(f))
@ -593,9 +583,6 @@ static void plyr_read_stats_v(int *k, int *d)
*k=0;*d=0;
}
}
if(f)
PHYSFS_close(f);
}
static void plyr_read_stats()
@ -608,12 +595,9 @@ void plyr_save_stats()
int kills = PlayerCfg.NetlifeKills,deaths = PlayerCfg.NetlifeKilled, neg, i;
char filename[PATH_MAX];
unsigned char buf[16],buf2[16],a;
PHYSFS_file *f;
memset(filename, '\0', PATH_MAX);
snprintf(filename,sizeof(filename),PLAYER_EFFECTIVENESS_FILENAME_FORMAT,static_cast<const char *>(Players[Player_num].callsign));
f = PHYSFSX_openWriteBuffered(filename);
auto f = PHYSFSX_openWriteBuffered(filename);
if(!f)
return; //broken!
@ -677,22 +661,18 @@ void plyr_save_stats()
i+='A';
PHYSFSX_printf(f,"%c%s %c%s\n",i,buf,i,buf2);
PHYSFS_close(f);
}
#endif
static int write_player_dxx(const char *filename)
{
PHYSFS_file *fout;
int rc=0;
char tempfile[PATH_MAX];
strcpy(tempfile,filename);
tempfile[strlen(tempfile)-4]=0;
strcat(tempfile,".pl$");
fout=PHYSFSX_openWriteBuffered(tempfile);
auto fout = PHYSFSX_openWriteBuffered(tempfile);
if (!fout && GameArg.SysUsePlayersDir)
{
PHYSFS_mkdir(PLAYER_DIRECTORY_STRING("")); //try making directory
@ -765,8 +745,7 @@ static int write_player_dxx(const char *filename)
PHYSFSX_printf(fout,"plx version=%s\n", VERSION);
PHYSFSX_printf(fout,END_TEXT "\n");
PHYSFSX_printf(fout,END_TEXT "\n");
PHYSFS_close(fout);
fout.reset();
if(rc==0)
{
PHYSFS_delete(filename);
@ -783,7 +762,6 @@ static int write_player_dxx(const char *filename)
int read_player_file()
{
char filename[PATH_MAX];
PHYSFS_file *file;
#if defined(DXX_BUILD_DESCENT_I)
int shareware_file = -1;
int player_file_size;
@ -799,9 +777,7 @@ int read_player_file()
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.plr"), static_cast<const char *>(Players[Player_num].callsign));
if (!PHYSFSX_exists(filename,0))
return ENOENT;
file = PHYSFSX_openReadBuffered(filename);
auto file = PHYSFSX_openReadBuffered(filename);
if (!file)
goto read_player_file_failed;
@ -836,14 +812,12 @@ int read_player_file()
if (id!=SAVE_FILE_ID) {
nm_messagebox(TXT_ERROR, 1, TXT_OK, "Invalid player file");
PHYSFS_close(file);
return -1;
}
#if defined(DXX_BUILD_DESCENT_I)
if (saved_game_version < COMPATIBLE_SAVED_GAME_VERSION || player_struct_version < COMPATIBLE_PLAYER_STRUCT_VERSION) {
nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
PHYSFS_close(file);
return -1;
}
@ -888,7 +862,6 @@ int read_player_file()
if (shareware_file == -1) {
nm_messagebox(TXT_ERROR, 1, TXT_OK, "Error invalid or unknown\nplayerfile-size");
PHYSFS_close(file);
return -1;
}
@ -926,7 +899,6 @@ int read_player_file()
if (player_file_version < COMPATIBLE_PLAYER_FILE_VERSION) {
nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_ERROR_PLR_VERSION);
PHYSFS_close(file);
return -1;
}
@ -1080,9 +1052,6 @@ int read_player_file()
}
#endif
if (!PHYSFS_close(file))
goto read_player_file_failed;
#if defined(DXX_BUILD_DESCENT_II)
if (rewrite_it)
write_player_file();
@ -1097,9 +1066,6 @@ int read_player_file()
read_player_file_failed:
nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", "Error reading PLR file", PHYSFS_getLastError());
if (file)
PHYSFS_close(file);
return -1;
}
@ -1165,7 +1131,6 @@ int get_highest_level(void)
void write_player_file()
{
char filename[PATH_MAX];
PHYSFS_file *file;
int errno_ret;
if ( Newdemo_state == ND_STATE_PLAYBACK )
@ -1176,8 +1141,7 @@ void write_player_file()
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.plx"), static_cast<const char *>(Players[Player_num].callsign));
write_player_dxx(filename);
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.plr"), static_cast<const char *>(Players[Player_num].callsign));
file = PHYSFSX_openWriteBuffered(filename);
auto file = PHYSFSX_openWriteBuffered(filename);
if (!file)
return;
@ -1194,20 +1158,17 @@ void write_player_file()
//write higest level info
if ((PHYSFS_write( file, PlayerCfg.HighestLevels, sizeof(hli), PlayerCfg.NHighestLevels) != PlayerCfg.NHighestLevels)) {
errno_ret = errno;
PHYSFS_close(file);
return;
}
if (PHYSFS_write( file, saved_games,sizeof(saved_games),1) != 1) {
errno_ret = errno;
PHYSFS_close(file);
return;
}
range_for (auto &i, PlayerCfg.NetworkMessageMacro)
if (PHYSFS_write(file, i.data(), i.size(), 1) != 1) {
errno_ret = errno;
PHYSFS_close(file);
return;
}
@ -1236,7 +1197,7 @@ void write_player_file()
}
}
if (!PHYSFS_close(file))
if (!file.close())
errno_ret = errno;
if (errno_ret != EZERO) {
@ -1318,7 +1279,7 @@ void write_player_file()
PHYSFSX_writeString(file, buf); // Write out current joystick for player.
}
if (!PHYSFS_close(file))
if (!file.close())
goto write_player_file_failed;
return;
@ -1327,7 +1288,7 @@ void write_player_file()
nm_messagebox(TXT_ERROR, 1, TXT_OK, "%s\n\n%s", TXT_ERROR_WRITING_PLR, PHYSFS_getLastError());
if (file)
{
PHYSFS_close(file);
file.reset();
PHYSFS_delete(filename); //delete bogus file
}
#endif
@ -1351,14 +1312,12 @@ static int get_lifetime_checksum (int a,int b)
void read_netgame_profile(netgame_info *ng)
{
char filename[PATH_MAX];
PHYSFS_file *file;
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.ngp"), static_cast<const char *>(Players[Player_num].callsign));
if (!PHYSFSX_exists(filename,0))
return;
file = PHYSFSX_openReadBuffered(filename);
auto file = PHYSFSX_openReadBuffered(filename);
if (!file)
return;
@ -1418,19 +1377,14 @@ void read_netgame_profile(netgame_info *ng)
convert_integer(ng->Tracker, value);
#endif
}
PHYSFS_close(file);
}
// write values from netgame_info to ngp file
void write_netgame_profile(netgame_info *ng)
{
char filename[PATH_MAX];
PHYSFS_file *file;
snprintf(filename, sizeof(filename), PLAYER_DIRECTORY_STRING("%.8s.ngp"), static_cast<const char *>(Players[Player_num].callsign));
file = PHYSFSX_openWriteBuffered(filename);
auto file = PHYSFSX_openWriteBuffered(filename);
if (!file)
return;
@ -1458,6 +1412,4 @@ void write_netgame_profile(netgame_info *ng)
PHYSFSX_printf(file, TrackerStr "=0\n");
#endif
PHYSFSX_printf(file, NGPVersionStr "=%s\n",VERSION);
PHYSFS_close(file);
}

View file

@ -264,12 +264,11 @@ static void align_polygon_model_data(polymodel *pm)
//reads a binary file containing a 3d model
static polymodel *read_model_file(polymodel *pm,const char *filename,robot_info *r)
{
PHYSFS_file *ifile;
short version;
int id,len, next_chunk;
ubyte model_buf[MODEL_BUF_SIZE];
ifile = PHYSFSX_openReadBuffered(filename);
auto ifile = PHYSFSX_openReadBuffered(filename);
if (!ifile)
Error("Can't open file <%s>",filename);
@ -277,8 +276,7 @@ static polymodel *read_model_file(polymodel *pm,const char *filename,robot_info
Pof_addr = 0;
Pof_file_end = PHYSFS_read(ifile, model_buf, 1, PHYSFS_fileLength(ifile));
PHYSFS_close(ifile);
ifile.reset();
id = pof_read_int(model_buf);
if (id!=0x4f505350) /* 'OPSP' */
@ -431,13 +429,12 @@ static polymodel *read_model_file(polymodel *pm,const char *filename,robot_info
int read_model_guns(const char *filename,array<vms_vector, MAX_CONTROLCEN_GUNS> &gun_points, array<vms_vector, MAX_CONTROLCEN_GUNS> &gun_dirs);
int read_model_guns(const char *filename,array<vms_vector, MAX_CONTROLCEN_GUNS> &gun_points, array<vms_vector, MAX_CONTROLCEN_GUNS> &gun_dirs)
{
PHYSFS_file *ifile;
short version;
int id,len;
int n_guns=0;
ubyte model_buf[MODEL_BUF_SIZE];
ifile = PHYSFSX_openReadBuffered(filename);
auto ifile = PHYSFSX_openReadBuffered(filename);
if (!ifile)
Error("Can't open file <%s>",filename);
@ -445,7 +442,7 @@ int read_model_guns(const char *filename,array<vms_vector, MAX_CONTROLCEN_GUNS>
Pof_addr = 0;
Pof_file_end = PHYSFS_read(ifile, model_buf, 1, PHYSFS_fileLength(ifile));
PHYSFS_close(ifile);
ifile.reset();
id = pof_read_int(model_buf);

View file

@ -50,6 +50,7 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#include "text.h"
#include "strutil.h"
#include "rbaudio.h"
#include "physfsx.h"
#ifdef OGL
#include "ogl_init.h"
@ -89,13 +90,12 @@ struct all_scores
static void scores_read(all_scores *scores)
{
PHYSFS_file *fp;
int fsize;
// clear score array...
*scores = {};
fp = PHYSFS_openRead(SCORES_FILENAME);
RAIIPHYSFS_File fp{PHYSFS_openRead(SCORES_FILENAME)};
if (!fp)
{
// No error message needed, code will work without a scores file
@ -119,13 +119,10 @@ static void scores_read(all_scores *scores)
fsize = PHYSFS_fileLength(fp);
if ( fsize != sizeof(all_scores) ) {
PHYSFS_close(fp);
return;
}
// Read 'em in...
PHYSFS_read(fp, scores, sizeof(all_scores), 1);
PHYSFS_close(fp);
if ( (scores->version!=VERSION_NUMBER)||(scores->signature[0]!='D')||(scores->signature[1]!='H')||(scores->signature[2]!='S') ) {
*scores = {};
return;
@ -134,9 +131,7 @@ static void scores_read(all_scores *scores)
static void scores_write(all_scores *scores)
{
PHYSFS_file *fp;
fp = PHYSFS_openWrite(SCORES_FILENAME);
RAIIPHYSFS_File fp{PHYSFS_openWrite(SCORES_FILENAME)};
if (!fp)
{
nm_messagebox( TXT_WARNING, 1, TXT_OK, "%s\n'%s'", TXT_UNABLE_TO_OPEN, SCORES_FILENAME );
@ -148,7 +143,6 @@ static void scores_write(all_scores *scores)
scores->signature[2]='S';
scores->version = VERSION_NUMBER;
PHYSFS_write(fp, scores,sizeof(all_scores), 1);
PHYSFS_close(fp);
}
static void int_to_string( int number, char *dest )

View file

@ -75,15 +75,13 @@ void songs_set_volume(int volume)
static void songs_init()
{
int i = 0;
PHYSFS_file * fp = NULL;
Songs_initialized = 0;
if (BIMSongs != NULL)
d_free(BIMSongs);
if (!fp) // try dxx-r.sng - a songfile specifically for dxx which level authors CAN use (dxx does not care if descent.sng contains MP3/OGG/etc. as well) besides the normal descent.sng containing files other versions of the game cannot play. this way a mission can contain a DOS-Descent compatible OST (hmp files) as well as a OST using MP3, OGG, etc.
fp = PHYSFSX_openReadBuffered( "dxx-r.sng" );
// try dxx-r.sng - a songfile specifically for dxx which level authors CAN use (dxx does not care if descent.sng contains MP3/OGG/etc. as well) besides the normal descent.sng containing files other versions of the game cannot play. this way a mission can contain a DOS-Descent compatible OST (hmp files) as well as a OST using MP3, OGG, etc.
auto fp = PHYSFSX_openReadBuffered("dxx-r.sng");
if (!fp) // try to open regular descent.sng
fp = PHYSFSX_openReadBuffered( "descent.sng" );
@ -154,8 +152,7 @@ static void songs_init()
Num_bim_songs = i;
Songs_initialized = 1;
if (fp)
PHYSFS_close(fp);
fp.reset();
if (GameArg.SndNoMusic)
GameCfg.MusicType = MUSIC_TYPE_NONE;

View file

@ -638,7 +638,6 @@ int state_quick_item = -1;
*/
static int state_get_savegame_filename(char * fname, char * dsc, const char * caption, int blind_save)
{
PHYSFS_file * fp;
int i, choice, version, nsaves;
newmenu_item m[NUM_SAVES+1];
char filename[NUM_SAVES][PATH_MAX];
@ -652,8 +651,8 @@ static int state_get_savegame_filename(char * fname, char * dsc, const char * ca
for (i=0;i<NUM_SAVES; i++ ) {
snprintf(filename[i], sizeof(filename[i]), PLAYER_DIRECTORY_STRING("%.8s.%cg%x"), static_cast<const char *>(Players[Player_num].callsign), (Game_mode & GM_MULTI_COOP)?'m':'s', i );
valid = 0;
fp = PHYSFSX_openReadBuffered(filename[i]);
if ( fp ) {
if (auto fp = PHYSFSX_openReadBuffered(filename[i]))
{
//Read id
PHYSFS_read(fp, id, sizeof(char) * 4, 1);
if ( !memcmp( id, dgss_id, 4 )) {
@ -684,7 +683,6 @@ static int state_get_savegame_filename(char * fname, char * dsc, const char * ca
valid = 1;
}
}
PHYSFS_close(fp);
}
if (!valid) {
strcpy( desc[i], TXT_EMPTY );
@ -740,17 +738,12 @@ int state_get_restore_file(char * fname, int blind_save)
static int copy_file(const char *old_file, const char *new_file)
{
int buf_size;
PHYSFS_file *in_file, *out_file;
out_file = PHYSFS_openWrite(new_file);
if (!out_file)
return -1;
in_file = PHYSFS_openRead(old_file);
RAIIPHYSFS_File in_file{PHYSFS_openRead(old_file)};
if (!in_file)
return -2;
RAIIPHYSFS_File out_file{PHYSFS_openWrite(new_file)};
if (!out_file)
return -1;
buf_size = PHYSFS_fileLength(in_file);
RAIIdmem<sbyte> buf;
@ -775,13 +768,7 @@ static int copy_file(const char *old_file, const char *new_file)
if (PHYSFS_write(out_file, buf, 1, bytes_read) < bytes_read)
Error("Cannot write to file <%s>: %s", new_file, PHYSFS_getLastError());
}
if (!PHYSFS_close(in_file))
{
PHYSFS_close(out_file);
return -3;
}
if (!PHYSFS_close(out_file))
if (!out_file.close())
return -4;
return 0;
@ -892,7 +879,6 @@ int state_save_all(int secret_save, const char *filename_override, int blind_sav
int state_save_all_sub(const char *filename, const char *desc)
{
int i;
PHYSFS_file *fp;
char mission_filename[9];
#ifdef OGL
GLint gl_draw_buffer;
@ -904,7 +890,7 @@ int state_save_all_sub(const char *filename, const char *desc)
Int3();
#endif
fp = PHYSFSX_openWriteBuffered(filename);
auto fp = PHYSFSX_openWriteBuffered(filename);
if ( !fp ) {
nm_messagebox(NULL, 1, TXT_OK, "Error writing savegame.\nPossibly out of disk\nspace.");
start_time();
@ -1182,11 +1168,7 @@ int state_save_all_sub(const char *filename, const char *desc)
PHYSFS_write(fp, &Netgame.numconnected, sizeof(ubyte), 1);
PHYSFS_write(fp, &Netgame.level_time, sizeof(int), 1);
}
PHYSFS_close(fp);
start_time();
return 1;
}
@ -1292,7 +1274,6 @@ int state_restore_all(int in_game, int secret_restore, const char *filename_over
int state_restore_all_sub(const char *filename, int secret_restore)
{
int version,i, j, coop_player_got[MAX_PLAYERS], coop_org_objnum = Players[Player_num].objnum;
PHYSFS_file *fp;
int swap = 0; // if file is not endian native, have to swap all shorts and ints
int current_level;
char mission[16];
@ -1313,13 +1294,12 @@ int state_restore_all_sub(const char *filename, int secret_restore)
Int3();
#endif
fp = PHYSFSX_openReadBuffered(filename);
auto fp = PHYSFSX_openReadBuffered(filename);
if ( !fp ) return 0;
//Read id
PHYSFS_read(fp, id, sizeof(char) * 4, 1);
if ( memcmp( id, dgss_id, 4 )) {
PHYSFS_close(fp);
return 0;
}
@ -1333,7 +1313,6 @@ int state_restore_all_sub(const char *filename, int secret_restore)
}
if (version < STATE_COMPATIBLE_VERSION) {
PHYSFS_close(fp);
return 0;
}
@ -1345,7 +1324,6 @@ int state_restore_all_sub(const char *filename, int secret_restore)
PHYSFS_read(fp, &saved_callsign, sizeof(char)*CALLSIGN_LEN+1, 1);
if (!(saved_callsign == Players[Player_num].callsign)) // check the callsign of the palyer who saved this state. It MUST match. If we transferred this savegame from pilot A to pilot B, others won't be able to restore us. So bail out here if this is the case.
{
PHYSFS_close(fp);
return 0;
}
}
@ -1368,7 +1346,6 @@ int state_restore_all_sub(const char *filename, int secret_restore)
if (!load_mission_by_name( mission )) {
nm_messagebox( NULL, 1, "Ok", "Error!\nUnable to load mission\n'%s'\n", mission );
PHYSFS_close(fp);
return 0;
}
@ -1817,9 +1794,6 @@ int state_restore_all_sub(const char *filename, int secret_restore)
Viewer = ConsoleObject = &Objects[Players[Player_num].objnum]; // make sure Viewer and ConsoleObject are set up (which we skipped by not using InitPlayerObject but we need since objects changed while loading)
special_reset_objects(); // since we juggeled around with objects to remap coop players rebuild the index of free objects
}
PHYSFS_close(fp);
if (Game_wind)
if (!window_is_visible(Game_wind))
window_set_visible(Game_wind, 1);
@ -1831,7 +1805,6 @@ int state_restore_all_sub(const char *filename, int secret_restore)
int state_get_game_id(const char *filename)
{
int version;
PHYSFS_file *fp;
int swap = 0; // if file is not endian native, have to swap all shorts and ints
char id[5];
callsign_t saved_callsign;
@ -1844,13 +1817,12 @@ int state_get_game_id(const char *filename)
if (!(Game_mode & GM_MULTI_COOP))
return 0;
fp = PHYSFSX_openReadBuffered(filename);
auto fp = PHYSFSX_openReadBuffered(filename);
if ( !fp ) return 0;
//Read id
PHYSFS_read(fp, id, sizeof(char) * 4, 1);
if ( memcmp( id, dgss_id, 4 )) {
PHYSFS_close(fp);
return 0;
}
@ -1864,7 +1836,6 @@ int state_get_game_id(const char *filename)
}
if (version < STATE_COMPATIBLE_VERSION) {
PHYSFS_close(fp);
return 0;
}

View file

@ -99,8 +99,6 @@ void decode_text(char *buf, int len)
//load all the text strings for Descent
void load_text()
{
PHYSFS_file *tfile;
PHYSFS_file *ifile;
int len,i, have_binary = 0;
char *tptr;
const char *filename="descent.tex";
@ -219,11 +217,11 @@ void load_text()
if (GameArg.DbgAltTex)
filename = GameArg.DbgAltTex;
tfile = PHYSFSX_openReadBuffered(filename);
auto tfile = PHYSFSX_openReadBuffered(filename);
if (!tfile)
{
filename="descent.txb";
ifile = PHYSFSX_openReadBuffered(filename);
auto ifile = PHYSFSX_openReadBuffered(filename);
if (!ifile)
{
Error("Cannot open file DESCENT.TEX or DESCENT.TXB");
@ -238,8 +236,6 @@ void load_text()
PHYSFS_read(ifile,text,1,len);
text[len]=0;
//end edit -MM
PHYSFS_close(ifile);
} else {
int c;
char * p;
@ -257,8 +253,6 @@ void load_text()
} while ( c!=EOF );
*p=0;
//end edit -MM
PHYSFS_close(tfile);
}
for (i=0,tptr=text.get();i<N_TEXT_STRINGS;i++) {

View file

@ -270,15 +270,11 @@ void show_titles(void)
}
{ //show bundler movie or screens
PHYSFS_file *movie_handle;
played=MOVIE_NOT_PLAYED; //default is not played
//check if OEM movie exists, so we don't stop the music if it doesn't
movie_handle = PHYSFS_openRead("oem.mve");
if (movie_handle)
if (RAIIPHYSFS_File{PHYSFS_openRead("oem.mve")})
{
PHYSFS_close(movie_handle);
played = PlayMovie(NULL, "oem.mve",0);
song_playing = 0; //movie will kill sound
}
@ -520,7 +516,6 @@ static void briefing_init(briefing *br, short level_num)
// Load Descent briefing text.
static int load_screen_text(const d_fname &filename, std::unique_ptr<char[]> &buf)
{
PHYSFS_file *tfile;
int len, have_binary = 0;
auto e = end(filename);
auto ext = std::find(begin(filename), e, '.');
@ -529,7 +524,7 @@ static int load_screen_text(const d_fname &filename, std::unique_ptr<char[]> &bu
if (!d_stricmp(&*ext, ".txb"))
have_binary = 1;
tfile = PHYSFSX_openReadBuffered(filename);
auto tfile = PHYSFSX_openReadBuffered(filename);
if (!tfile)
return (0);
@ -544,7 +539,6 @@ static int load_screen_text(const d_fname &filename, std::unique_ptr<char[]> &bu
x--;
}
#endif
PHYSFS_close(tfile);
if (have_binary)
decode_text(buf.get(), len);

View file

@ -68,10 +68,8 @@ struct Arg GameArg;
static void AppendIniArgs(void)
{
PHYSFS_file *f;
f = PHYSFSX_openReadBuffered(INI_FILENAME);
if(f) {
if (auto f = PHYSFSX_openReadBuffered(INI_FILENAME))
{
PHYSFSX_gets_line_t<1024> line;
while(!PHYSFS_eof(f) && Args.size() < MAX_ARGS && PHYSFSX_fgets(line, f))
{
@ -83,7 +81,6 @@ static void AppendIniArgs(void)
Args.push_back(token);
}
}
PHYSFS_close(f);
}
}

View file

@ -270,20 +270,14 @@ int PHYSFSX_removeRelFromSearchPath(const char *relname)
int PHYSFSX_fsize(const char *hogname)
{
PHYSFS_file *fp;
char hogname2[PATH_MAX];
int size;
snprintf(hogname2, sizeof(hogname2), "%s", hogname);
PHYSFSEXT_locateCorrectCase(hogname2);
fp = PHYSFS_openRead(hogname2);
if (!fp)
return -1;
size = PHYSFS_fileLength(fp);
PHYSFS_close(fp);
return size;
if (RAIIPHYSFS_File fp{PHYSFS_openRead(hogname2)})
return PHYSFS_fileLength(fp);
return -1;
}
void PHYSFSX_listSearchPathContent()
@ -487,9 +481,8 @@ int PHYSFSX_exists(const char *filename, int ignorecase)
}
//Open a file for reading, set up a buffer
PHYSFS_file *PHYSFSX_openReadBuffered(const char *filename)
RAIIPHYSFS_File PHYSFSX_openReadBuffered(const char *filename)
{
PHYSFS_file *fp;
PHYSFS_uint64 bufSize;
char filename2[PATH_MAX];
@ -502,30 +495,26 @@ PHYSFS_file *PHYSFSX_openReadBuffered(const char *filename)
snprintf(filename2, sizeof(filename2), "%s", filename);
PHYSFSEXT_locateCorrectCase(filename2);
fp = PHYSFS_openRead(filename2);
RAIIPHYSFS_File fp{PHYSFS_openRead(filename2)};
if (!fp)
return NULL;
return nullptr;
bufSize = PHYSFS_fileLength(fp);
while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times
return fp;
}
//Open a file for writing, set up a buffer
PHYSFS_file *PHYSFSX_openWriteBuffered(const char *filename)
RAIIPHYSFS_File PHYSFSX_openWriteBuffered(const char *filename)
{
PHYSFS_file *fp;
PHYSFS_uint64 bufSize = 1024*1024; // hmm, seems like an OK size.
fp = PHYSFS_openWrite(filename);
RAIIPHYSFS_File fp{PHYSFS_openWrite(filename)};
if (!fp)
return NULL;
return nullptr;
while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
bufSize /= 2;
return fp;
}