From ba497e413917f2c890c20ae12c34056025b6a8d5 Mon Sep 17 00:00:00 2001 From: Garux Date: Tue, 9 Feb 2021 18:05:06 +0300 Subject: [PATCH] SafeOpenWrite, SafeOpenRead funcs use --- tools/quake3/common/cmdlib.cpp | 16 +++++-------- tools/quake3/common/cmdlib.h | 4 ++-- tools/quake3/q3map2/brush.cpp | 6 +---- tools/quake3/q3map2/bsp.cpp | 5 +--- tools/quake3/q3map2/convert_ase.cpp | 19 +++++---------- tools/quake3/q3map2/convert_map.cpp | 14 ++++------- tools/quake3/q3map2/convert_obj.cpp | 31 ++++++++----------------- tools/quake3/q3map2/exportents.cpp | 16 +++---------- tools/quake3/q3map2/leakfile.cpp | 5 +--- tools/quake3/q3map2/light_trace.cpp | 5 +--- tools/quake3/q3map2/lightmaps_ydnar.cpp | 5 +--- tools/quake3/q3map2/prtfile.cpp | 5 +--- tools/quake3/q3map2/shaders.cpp | 2 +- tools/quake3/q3map2/surface_extra.cpp | 13 +++-------- tools/quake3/q3map2/vis.cpp | 11 ++------- 15 files changed, 43 insertions(+), 114 deletions(-) diff --git a/tools/quake3/common/cmdlib.cpp b/tools/quake3/common/cmdlib.cpp index cd769f56..0914337c 100644 --- a/tools/quake3/common/cmdlib.cpp +++ b/tools/quake3/common/cmdlib.cpp @@ -409,25 +409,21 @@ int Q_filelength( FILE *f ){ } -FILE *SafeOpenWrite( const char *filename ){ - FILE *f; - - f = fopen( filename, "wb" ); +FILE *SafeOpenWrite( const char *filename, const char *mode ){ + FILE *f = fopen( filename, mode ); if ( !f ) { - Error( "Error opening %s: %s",filename,strerror( errno ) ); + Error( "Error opening %s: %s", filename, strerror( errno ) ); } return f; } -FILE *SafeOpenRead( const char *filename ){ - FILE *f; - - f = fopen( filename, "rb" ); +FILE *SafeOpenRead( const char *filename, const char *mode ){ + FILE *f = fopen( filename, mode ); if ( !f ) { - Error( "Error opening %s: %s",filename,strerror( errno ) ); + Error( "Error opening %s: %s", filename, strerror( errno ) ); } return f; diff --git a/tools/quake3/common/cmdlib.h b/tools/quake3/common/cmdlib.h index 69ee7c86..60acf5b9 100644 --- a/tools/quake3/common/cmdlib.h +++ b/tools/quake3/common/cmdlib.h @@ -175,8 +175,8 @@ __attribute__( ( noreturn ) ) #endif ; -FILE *SafeOpenWrite( const char *filename ); -FILE *SafeOpenRead( const char *filename ); +FILE *SafeOpenWrite( const char *filename, const char *mode = "wb" ); +FILE *SafeOpenRead( const char *filename, const char *mode = "rb" ); void SafeRead( FILE *f, void *buffer, int count ); void SafeWrite( FILE *f, const void *buffer, int count ); diff --git a/tools/quake3/q3map2/brush.cpp b/tools/quake3/q3map2/brush.cpp index 9a6cf429..8388af10 100644 --- a/tools/quake3/q3map2/brush.cpp +++ b/tools/quake3/q3map2/brush.cpp @@ -607,7 +607,6 @@ vec_t BrushVolume( brush_t *brush ){ */ void WriteBSPBrushMap( const char *name, brush_t *list ){ - FILE *f; side_t *s; int i; winding_t *w; @@ -617,10 +616,7 @@ void WriteBSPBrushMap( const char *name, brush_t *list ){ Sys_Printf( "Writing %s\n", name ); /* open the map file */ - f = fopen( name, "wb" ); - if ( f == NULL ) { - Error( "Can't write %s\b", name ); - } + FILE *f = SafeOpenWrite( name ); fprintf( f, "{\n\"classname\" \"worldspawn\"\n" ); diff --git a/tools/quake3/q3map2/bsp.cpp b/tools/quake3/q3map2/bsp.cpp index 65b24d86..3a79c9d0 100644 --- a/tools/quake3/q3map2/bsp.cpp +++ b/tools/quake3/q3map2/bsp.cpp @@ -40,10 +40,7 @@ static void autocaulk_write(){ auto filename = StringOutputStream( 256 )( source, ".caulk" ); Sys_Printf( "writing %s\n", filename.c_str() ); - FILE* file = fopen( filename, "w" ); - if ( !file ) { - Error( "Error opening %s", filename.c_str() ); - } + FILE* file = SafeOpenWrite( filename, "wt" ); int fslime = 16; ApplySurfaceParm( "slime", &fslime, NULL, NULL ); diff --git a/tools/quake3/q3map2/convert_ase.cpp b/tools/quake3/q3map2/convert_ase.cpp index de7475ed..6deb7e3e 100644 --- a/tools/quake3/q3map2/convert_ase.cpp +++ b/tools/quake3/q3map2/convert_ase.cpp @@ -337,7 +337,6 @@ int ConvertBSPToASE( char *bspName ){ bspShader_t *shader; bspModel_t *model; entity_t *e; - char name[ 1024 ], base[ 1024 ], dirname[ 1024 ]; int lmIndices[ numBSPShaders ]; @@ -345,25 +344,19 @@ int ConvertBSPToASE( char *bspName ){ Sys_Printf( "--- Convert BSP to ASE ---\n" ); /* create the ase filename from the bsp name */ - strcpy( dirname, bspName ); - StripExtension( dirname ); - strcpy( name, bspName ); - path_set_extension( name, ".ase" ); - Sys_Printf( "writing %s\n", name ); - - ExtractFileBase( bspName, base ); + auto dirname = StringOutputStream( 256 )( PathExtensionless( bspName ) ); + auto name = StringOutputStream( 256 )( dirname, ".ase" ); + Sys_Printf( "writing %s\n", name.c_str() ); + auto base = StringOutputStream( 64 )( PathFilename( bspName ) ); /* open it */ - f = fopen( name, "wb" ); - if ( f == NULL ) { - Error( "Open failed on %s\n", name ); - } + f = SafeOpenWrite( name ); /* print header */ fprintf( f, "*3DSMAX_ASCIIEXPORT\t200\r\n" ); fprintf( f, "*COMMENT\t\"Generated by Q3Map2 (ydnar) -convert -format ase\"\r\n" ); fprintf( f, "*SCENE\t{\r\n" ); - fprintf( f, "\t*SCENE_FILENAME\t\"%s.bsp\"\r\n", base ); + fprintf( f, "\t*SCENE_FILENAME\t\"%s.bsp\"\r\n", base.c_str() ); fprintf( f, "\t*SCENE_FIRSTFRAME\t0\r\n" ); fprintf( f, "\t*SCENE_LASTFRAME\t100\r\n" ); fprintf( f, "\t*SCENE_FRAMESPEED\t30\r\n" ); diff --git a/tools/quake3/q3map2/convert_map.cpp b/tools/quake3/q3map2/convert_map.cpp index 9a5a00f6..3eb509b0 100644 --- a/tools/quake3/q3map2/convert_map.cpp +++ b/tools/quake3/q3map2/convert_map.cpp @@ -999,22 +999,16 @@ int ConvertBSPToMap_Ext( char *bspName, bool brushPrimitives ){ bspModel_t *model; entity_t *e; const char *value; - char name[ 1024 ]; - /* note it */ Sys_Printf( "--- Convert BSP to MAP ---\n" ); - /* create the bsp filename from the bsp name */ - strcpy( name, bspName ); - path_set_extension( name, "_converted.map" ); - Sys_Printf( "writing %s\n", name ); + /* create map filename from the bsp name */ + auto name = StringOutputStream( 256 )( PathExtensionless( bspName ), "_converted.map" ); + Sys_Printf( "writing %s\n", name.c_str() ); /* open it */ - f = fopen( name, "wb" ); - if ( f == NULL ) { - Error( "Open failed on %s\n", name ); - } + f = SafeOpenWrite( name ); /* print header */ fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" ); diff --git a/tools/quake3/q3map2/convert_obj.cpp b/tools/quake3/q3map2/convert_obj.cpp index ad9015b7..dc71baa0 100644 --- a/tools/quake3/q3map2/convert_obj.cpp +++ b/tools/quake3/q3map2/convert_obj.cpp @@ -302,7 +302,6 @@ int ConvertBSPToOBJ( char *bspName ){ bspModel_t *model; entity_t *e; const char *key; - char name[ 1024 ], base[ 1024 ], mtlname[ 1024 ], dirname[ 1024 ]; int lmIndices[ numBSPShaders ]; @@ -310,31 +309,21 @@ int ConvertBSPToOBJ( char *bspName ){ Sys_Printf( "--- Convert BSP to OBJ ---\n" ); /* create the ase filename from the bsp name */ - strcpy( dirname, bspName ); - StripExtension( dirname ); - strcpy( name, bspName ); - path_set_extension( name, ".obj" ); - Sys_Printf( "writing %s\n", name ); - strcpy( mtlname, bspName ); - path_set_extension( mtlname, ".mtl" ); - Sys_Printf( "writing %s\n", mtlname ); - - ExtractFileBase( bspName, base ); + auto dirname = StringOutputStream( 256 )( PathExtensionless( bspName ) ); + auto name = StringOutputStream( 256 )( dirname, ".obj" ); + Sys_Printf( "writing %s\n", name.c_str() ); + auto mtlname = StringOutputStream( 256 )( dirname, ".mtl" ); + Sys_Printf( "writing %s\n", mtlname.c_str() ); + auto base = StringOutputStream( 64 )( PathFilename( bspName ) ); /* open it */ - f = fopen( name, "wb" ); - if ( f == NULL ) { - Error( "Open failed on %s\n", name ); - } - fmtl = fopen( mtlname, "wb" ); - if ( fmtl == NULL ) { - Error( "Open failed on %s\n", mtlname ); - } + f = SafeOpenWrite( name ); + fmtl = SafeOpenWrite( mtlname ); /* print header */ - fprintf( f, "o %s\r\n", base ); + fprintf( f, "o %s\r\n", base.c_str() ); fprintf( f, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" ); - fprintf( f, "mtllib %s.mtl\r\n", base ); + fprintf( f, "mtllib %s.mtl\r\n", base.c_str() ); fprintf( fmtl, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" ); if ( lightmapsAsTexcoord ) { diff --git a/tools/quake3/q3map2/exportents.cpp b/tools/quake3/q3map2/exportents.cpp index f7296f27..474ad556 100644 --- a/tools/quake3/q3map2/exportents.cpp +++ b/tools/quake3/q3map2/exportents.cpp @@ -46,16 +46,9 @@ */ void ExportEntities( void ){ - char filename[ 1024 ]; - FILE *file; - /* note it */ Sys_FPrintf( SYS_VRB, "--- ExportEntities ---\n" ); - /* do some path mangling */ - strcpy( filename, source ); - path_set_extension( filename, ".ent" ); - /* sanity check */ if ( bspEntData == NULL || bspEntDataSize == 0 ) { Sys_Warning( "No BSP entity data. aborting...\n" ); @@ -63,13 +56,10 @@ void ExportEntities( void ){ } /* write it */ - Sys_Printf( "Writing %s\n", filename ); + auto filename = StringOutputStream( 256 )( PathExtensionless( source ), ".ent" ); + Sys_Printf( "Writing %s\n", filename.c_str() ); Sys_FPrintf( SYS_VRB, "(%d bytes)\n", bspEntDataSize ); - file = fopen( filename, "w" ); - - if ( file == NULL ) { - Error( "Unable to open %s for writing", filename ); - } + FILE *file = SafeOpenWrite( filename, "wt" ); fprintf( file, "%s\n", bspEntData ); fclose( file ); diff --git a/tools/quake3/q3map2/leakfile.cpp b/tools/quake3/q3map2/leakfile.cpp index fed1d8ec..76b885d2 100644 --- a/tools/quake3/q3map2/leakfile.cpp +++ b/tools/quake3/q3map2/leakfile.cpp @@ -71,10 +71,7 @@ xmlNodePtr LeakFile( tree_t *tree ){ // write the points to the file // auto filename = StringOutputStream( 256 )( source, ".lin" ); - linefile = fopen( filename, "w" ); - if ( !linefile ) { - Error( "Couldn't open %s\n", filename.c_str() ); - } + linefile = SafeOpenWrite( filename, "wt" ); xml_node = xmlNewNode( NULL, (const xmlChar*)"polyline" ); diff --git a/tools/quake3/q3map2/light_trace.cpp b/tools/quake3/q3map2/light_trace.cpp index 0234677f..4536741a 100644 --- a/tools/quake3/q3map2/light_trace.cpp +++ b/tools/quake3/q3map2/light_trace.cpp @@ -1281,10 +1281,7 @@ void SetupTraceNodes( void ){ strcpy( filename, source ); path_set_extension( filename, ".lin" ); Sys_Printf( "Opening light trace file %s...\n", filename ); - file = fopen( filename, "w" ); - if ( file == NULL ) { - Error( "Error opening %s for writing", filename ); - } + file = SafeOpenWrite( filename, "wt" ); /* walk node list */ for ( i = 0; i < numTraceWindings; i++ ) diff --git a/tools/quake3/q3map2/lightmaps_ydnar.cpp b/tools/quake3/q3map2/lightmaps_ydnar.cpp index a905a6f5..9c7bb2a5 100644 --- a/tools/quake3/q3map2/lightmaps_ydnar.cpp +++ b/tools/quake3/q3map2/lightmaps_ydnar.cpp @@ -81,10 +81,7 @@ void WriteTGA24( char *filename, byte *data, int width, int height, bool flip ){ } /* write it and free the buffer */ - file = fopen( filename, "wb" ); - if ( file == NULL ) { - Error( "Unable to open %s for writing", filename ); - } + file = SafeOpenWrite( filename ); /* flip vertically? */ if ( flip ) { diff --git a/tools/quake3/q3map2/prtfile.cpp b/tools/quake3/q3map2/prtfile.cpp index 85e9ba6d..dde87aba 100644 --- a/tools/quake3/q3map2/prtfile.cpp +++ b/tools/quake3/q3map2/prtfile.cpp @@ -372,10 +372,7 @@ void WritePortalFile( tree_t *tree ){ // write the file auto filename = StringOutputStream( 256 )( source, ".prt" ); Sys_Printf( "writing %s\n", filename.c_str() ); - pf = fopen( filename, "w" ); - if ( !pf ) { - Error( "Error opening %s", filename.c_str() ); - } + pf = SafeOpenWrite( filename, "wt" ); fprintf( pf, "%s\n", PORTALFILE ); fprintf( pf, "%i\n", num_visclusters ); diff --git a/tools/quake3/q3map2/shaders.cpp b/tools/quake3/q3map2/shaders.cpp index 6f863dd0..d2538270 100644 --- a/tools/quake3/q3map2/shaders.cpp +++ b/tools/quake3/q3map2/shaders.cpp @@ -361,7 +361,7 @@ void WriteMapShaderFile( void ){ Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile.c_str() ); /* open shader file */ - file = fopen( mapShaderFile.c_str(), "w" ); + file = fopen( mapShaderFile.c_str(), "wt" ); if ( file == NULL ) { Sys_Warning( "Unable to open map shader file %s for writing\n", mapShaderFile.c_str() ); return; diff --git a/tools/quake3/q3map2/surface_extra.cpp b/tools/quake3/q3map2/surface_extra.cpp index 18afa4c4..7bdfa3f1 100644 --- a/tools/quake3/q3map2/surface_extra.cpp +++ b/tools/quake3/q3map2/surface_extra.cpp @@ -194,12 +194,9 @@ void GetSurfaceExtraLightmapAxis( int num, vec3_t lightmapAxis ){ */ void WriteSurfaceExtraFile( const char *path ){ - char srfPath[ 1024 ]; - FILE *sf; surfaceExtra_t *se; int i; - /* dummy check */ if ( strEmptyOrNull( path ) ) { return; @@ -209,13 +206,9 @@ void WriteSurfaceExtraFile( const char *path ){ Sys_Printf( "--- WriteSurfaceExtraFile ---\n" ); /* open the file */ - strcpy( srfPath, path ); - path_set_extension( srfPath, ".srf" ); - Sys_Printf( "Writing %s\n", srfPath ); - sf = fopen( srfPath, "w" ); - if ( sf == NULL ) { - Error( "Error opening %s for writing", srfPath ); - } + auto srfPath = StringOutputStream( 256 )( PathExtensionless( path ), ".srf" ); + Sys_Printf( "Writing %s\n", srfPath.c_str() ); + FILE *sf = SafeOpenWrite( srfPath, "wt" ); /* lap through the extras list */ for ( i = -1; i < numSurfaceExtras; i++ ) diff --git a/tools/quake3/q3map2/vis.cpp b/tools/quake3/q3map2/vis.cpp index a2835015..b735f542 100644 --- a/tools/quake3/q3map2/vis.cpp +++ b/tools/quake3/q3map2/vis.cpp @@ -797,15 +797,11 @@ void WriteFloat( FILE *f, vec_t v ); void WritePortals( char *filename ){ int i, j, num; - FILE *pf; vportal_t *p; fixedWinding_t *w; // write the file - pf = fopen( filename, "w" ); - if ( !pf ) { - Error( "Error opening %s", filename ); - } + FILE *pf = SafeOpenWrite( filename, "wt" ); num = 0; for ( j = 0; j < numportals * 2; j++ ) @@ -888,10 +884,7 @@ void LoadPortals( char *name ){ } else { - f = fopen( name, "r" ); - if ( !f ) { - Error( "LoadPortals: couldn't read %s\n",name ); - } + f = SafeOpenRead( name, "rt" ); } if ( fscanf( f, "%79s\n%i\n%i\n%i\n", magic, &portalclusters, &numportals, &numfaces ) != 4 ) {