SafeOpenWrite, SafeOpenRead funcs use

This commit is contained in:
Garux 2021-02-09 18:05:06 +03:00
parent 37c11990cb
commit ba497e4139
15 changed files with 43 additions and 114 deletions

View File

@ -409,25 +409,21 @@ int Q_filelength( FILE *f ){
} }
FILE *SafeOpenWrite( const char *filename ){ FILE *SafeOpenWrite( const char *filename, const char *mode ){
FILE *f; FILE *f = fopen( filename, mode );
f = fopen( filename, "wb" );
if ( !f ) { if ( !f ) {
Error( "Error opening %s: %s",filename,strerror( errno ) ); Error( "Error opening %s: %s", filename, strerror( errno ) );
} }
return f; return f;
} }
FILE *SafeOpenRead( const char *filename ){ FILE *SafeOpenRead( const char *filename, const char *mode ){
FILE *f; FILE *f = fopen( filename, mode );
f = fopen( filename, "rb" );
if ( !f ) { if ( !f ) {
Error( "Error opening %s: %s",filename,strerror( errno ) ); Error( "Error opening %s: %s", filename, strerror( errno ) );
} }
return f; return f;

View File

@ -175,8 +175,8 @@ __attribute__( ( noreturn ) )
#endif #endif
; ;
FILE *SafeOpenWrite( const char *filename ); FILE *SafeOpenWrite( const char *filename, const char *mode = "wb" );
FILE *SafeOpenRead( const char *filename ); FILE *SafeOpenRead( const char *filename, const char *mode = "rb" );
void SafeRead( FILE *f, void *buffer, int count ); void SafeRead( FILE *f, void *buffer, int count );
void SafeWrite( FILE *f, const void *buffer, int count ); void SafeWrite( FILE *f, const void *buffer, int count );

View File

@ -607,7 +607,6 @@ vec_t BrushVolume( brush_t *brush ){
*/ */
void WriteBSPBrushMap( const char *name, brush_t *list ){ void WriteBSPBrushMap( const char *name, brush_t *list ){
FILE *f;
side_t *s; side_t *s;
int i; int i;
winding_t *w; winding_t *w;
@ -617,10 +616,7 @@ void WriteBSPBrushMap( const char *name, brush_t *list ){
Sys_Printf( "Writing %s\n", name ); Sys_Printf( "Writing %s\n", name );
/* open the map file */ /* open the map file */
f = fopen( name, "wb" ); FILE *f = SafeOpenWrite( name );
if ( f == NULL ) {
Error( "Can't write %s\b", name );
}
fprintf( f, "{\n\"classname\" \"worldspawn\"\n" ); fprintf( f, "{\n\"classname\" \"worldspawn\"\n" );

View File

@ -40,10 +40,7 @@ static void autocaulk_write(){
auto filename = StringOutputStream( 256 )( source, ".caulk" ); auto filename = StringOutputStream( 256 )( source, ".caulk" );
Sys_Printf( "writing %s\n", filename.c_str() ); Sys_Printf( "writing %s\n", filename.c_str() );
FILE* file = fopen( filename, "w" ); FILE* file = SafeOpenWrite( filename, "wt" );
if ( !file ) {
Error( "Error opening %s", filename.c_str() );
}
int fslime = 16; int fslime = 16;
ApplySurfaceParm( "slime", &fslime, NULL, NULL ); ApplySurfaceParm( "slime", &fslime, NULL, NULL );

View File

@ -337,7 +337,6 @@ int ConvertBSPToASE( char *bspName ){
bspShader_t *shader; bspShader_t *shader;
bspModel_t *model; bspModel_t *model;
entity_t *e; entity_t *e;
char name[ 1024 ], base[ 1024 ], dirname[ 1024 ];
int lmIndices[ numBSPShaders ]; int lmIndices[ numBSPShaders ];
@ -345,25 +344,19 @@ int ConvertBSPToASE( char *bspName ){
Sys_Printf( "--- Convert BSP to ASE ---\n" ); Sys_Printf( "--- Convert BSP to ASE ---\n" );
/* create the ase filename from the bsp name */ /* create the ase filename from the bsp name */
strcpy( dirname, bspName ); auto dirname = StringOutputStream( 256 )( PathExtensionless( bspName ) );
StripExtension( dirname ); auto name = StringOutputStream( 256 )( dirname, ".ase" );
strcpy( name, bspName ); Sys_Printf( "writing %s\n", name.c_str() );
path_set_extension( name, ".ase" ); auto base = StringOutputStream( 64 )( PathFilename( bspName ) );
Sys_Printf( "writing %s\n", name );
ExtractFileBase( bspName, base );
/* open it */ /* open it */
f = fopen( name, "wb" ); f = SafeOpenWrite( name );
if ( f == NULL ) {
Error( "Open failed on %s\n", name );
}
/* print header */ /* print header */
fprintf( f, "*3DSMAX_ASCIIEXPORT\t200\r\n" ); fprintf( f, "*3DSMAX_ASCIIEXPORT\t200\r\n" );
fprintf( f, "*COMMENT\t\"Generated by Q3Map2 (ydnar) -convert -format ase\"\r\n" ); fprintf( f, "*COMMENT\t\"Generated by Q3Map2 (ydnar) -convert -format ase\"\r\n" );
fprintf( f, "*SCENE\t{\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_FIRSTFRAME\t0\r\n" );
fprintf( f, "\t*SCENE_LASTFRAME\t100\r\n" ); fprintf( f, "\t*SCENE_LASTFRAME\t100\r\n" );
fprintf( f, "\t*SCENE_FRAMESPEED\t30\r\n" ); fprintf( f, "\t*SCENE_FRAMESPEED\t30\r\n" );

View File

@ -999,22 +999,16 @@ int ConvertBSPToMap_Ext( char *bspName, bool brushPrimitives ){
bspModel_t *model; bspModel_t *model;
entity_t *e; entity_t *e;
const char *value; const char *value;
char name[ 1024 ];
/* note it */ /* note it */
Sys_Printf( "--- Convert BSP to MAP ---\n" ); Sys_Printf( "--- Convert BSP to MAP ---\n" );
/* create the bsp filename from the bsp name */ /* create map filename from the bsp name */
strcpy( name, bspName ); auto name = StringOutputStream( 256 )( PathExtensionless( bspName ), "_converted.map" );
path_set_extension( name, "_converted.map" ); Sys_Printf( "writing %s\n", name.c_str() );
Sys_Printf( "writing %s\n", name );
/* open it */ /* open it */
f = fopen( name, "wb" ); f = SafeOpenWrite( name );
if ( f == NULL ) {
Error( "Open failed on %s\n", name );
}
/* print header */ /* print header */
fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" ); fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );

View File

@ -302,7 +302,6 @@ int ConvertBSPToOBJ( char *bspName ){
bspModel_t *model; bspModel_t *model;
entity_t *e; entity_t *e;
const char *key; const char *key;
char name[ 1024 ], base[ 1024 ], mtlname[ 1024 ], dirname[ 1024 ];
int lmIndices[ numBSPShaders ]; int lmIndices[ numBSPShaders ];
@ -310,31 +309,21 @@ int ConvertBSPToOBJ( char *bspName ){
Sys_Printf( "--- Convert BSP to OBJ ---\n" ); Sys_Printf( "--- Convert BSP to OBJ ---\n" );
/* create the ase filename from the bsp name */ /* create the ase filename from the bsp name */
strcpy( dirname, bspName ); auto dirname = StringOutputStream( 256 )( PathExtensionless( bspName ) );
StripExtension( dirname ); auto name = StringOutputStream( 256 )( dirname, ".obj" );
strcpy( name, bspName ); Sys_Printf( "writing %s\n", name.c_str() );
path_set_extension( name, ".obj" ); auto mtlname = StringOutputStream( 256 )( dirname, ".mtl" );
Sys_Printf( "writing %s\n", name ); Sys_Printf( "writing %s\n", mtlname.c_str() );
strcpy( mtlname, bspName ); auto base = StringOutputStream( 64 )( PathFilename( bspName ) );
path_set_extension( mtlname, ".mtl" );
Sys_Printf( "writing %s\n", mtlname );
ExtractFileBase( bspName, base );
/* open it */ /* open it */
f = fopen( name, "wb" ); f = SafeOpenWrite( name );
if ( f == NULL ) { fmtl = SafeOpenWrite( mtlname );
Error( "Open failed on %s\n", name );
}
fmtl = fopen( mtlname, "wb" );
if ( fmtl == NULL ) {
Error( "Open failed on %s\n", mtlname );
}
/* print header */ /* 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, "# 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" ); fprintf( fmtl, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
if ( lightmapsAsTexcoord ) { if ( lightmapsAsTexcoord ) {

View File

@ -46,16 +46,9 @@
*/ */
void ExportEntities( void ){ void ExportEntities( void ){
char filename[ 1024 ];
FILE *file;
/* note it */ /* note it */
Sys_FPrintf( SYS_VRB, "--- ExportEntities ---\n" ); Sys_FPrintf( SYS_VRB, "--- ExportEntities ---\n" );
/* do some path mangling */
strcpy( filename, source );
path_set_extension( filename, ".ent" );
/* sanity check */ /* sanity check */
if ( bspEntData == NULL || bspEntDataSize == 0 ) { if ( bspEntData == NULL || bspEntDataSize == 0 ) {
Sys_Warning( "No BSP entity data. aborting...\n" ); Sys_Warning( "No BSP entity data. aborting...\n" );
@ -63,13 +56,10 @@ void ExportEntities( void ){
} }
/* write it */ /* 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 ); Sys_FPrintf( SYS_VRB, "(%d bytes)\n", bspEntDataSize );
file = fopen( filename, "w" ); FILE *file = SafeOpenWrite( filename, "wt" );
if ( file == NULL ) {
Error( "Unable to open %s for writing", filename );
}
fprintf( file, "%s\n", bspEntData ); fprintf( file, "%s\n", bspEntData );
fclose( file ); fclose( file );

View File

@ -71,10 +71,7 @@ xmlNodePtr LeakFile( tree_t *tree ){
// write the points to the file // write the points to the file
// //
auto filename = StringOutputStream( 256 )( source, ".lin" ); auto filename = StringOutputStream( 256 )( source, ".lin" );
linefile = fopen( filename, "w" ); linefile = SafeOpenWrite( filename, "wt" );
if ( !linefile ) {
Error( "Couldn't open %s\n", filename.c_str() );
}
xml_node = xmlNewNode( NULL, (const xmlChar*)"polyline" ); xml_node = xmlNewNode( NULL, (const xmlChar*)"polyline" );

View File

@ -1281,10 +1281,7 @@ void SetupTraceNodes( void ){
strcpy( filename, source ); strcpy( filename, source );
path_set_extension( filename, ".lin" ); path_set_extension( filename, ".lin" );
Sys_Printf( "Opening light trace file %s...\n", filename ); Sys_Printf( "Opening light trace file %s...\n", filename );
file = fopen( filename, "w" ); file = SafeOpenWrite( filename, "wt" );
if ( file == NULL ) {
Error( "Error opening %s for writing", filename );
}
/* walk node list */ /* walk node list */
for ( i = 0; i < numTraceWindings; i++ ) for ( i = 0; i < numTraceWindings; i++ )

View File

@ -81,10 +81,7 @@ void WriteTGA24( char *filename, byte *data, int width, int height, bool flip ){
} }
/* write it and free the buffer */ /* write it and free the buffer */
file = fopen( filename, "wb" ); file = SafeOpenWrite( filename );
if ( file == NULL ) {
Error( "Unable to open %s for writing", filename );
}
/* flip vertically? */ /* flip vertically? */
if ( flip ) { if ( flip ) {

View File

@ -372,10 +372,7 @@ void WritePortalFile( tree_t *tree ){
// write the file // write the file
auto filename = StringOutputStream( 256 )( source, ".prt" ); auto filename = StringOutputStream( 256 )( source, ".prt" );
Sys_Printf( "writing %s\n", filename.c_str() ); Sys_Printf( "writing %s\n", filename.c_str() );
pf = fopen( filename, "w" ); pf = SafeOpenWrite( filename, "wt" );
if ( !pf ) {
Error( "Error opening %s", filename.c_str() );
}
fprintf( pf, "%s\n", PORTALFILE ); fprintf( pf, "%s\n", PORTALFILE );
fprintf( pf, "%i\n", num_visclusters ); fprintf( pf, "%i\n", num_visclusters );

View File

@ -361,7 +361,7 @@ void WriteMapShaderFile( void ){
Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile.c_str() ); Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile.c_str() );
/* open shader file */ /* open shader file */
file = fopen( mapShaderFile.c_str(), "w" ); file = fopen( mapShaderFile.c_str(), "wt" );
if ( file == NULL ) { if ( file == NULL ) {
Sys_Warning( "Unable to open map shader file %s for writing\n", mapShaderFile.c_str() ); Sys_Warning( "Unable to open map shader file %s for writing\n", mapShaderFile.c_str() );
return; return;

View File

@ -194,12 +194,9 @@ void GetSurfaceExtraLightmapAxis( int num, vec3_t lightmapAxis ){
*/ */
void WriteSurfaceExtraFile( const char *path ){ void WriteSurfaceExtraFile( const char *path ){
char srfPath[ 1024 ];
FILE *sf;
surfaceExtra_t *se; surfaceExtra_t *se;
int i; int i;
/* dummy check */ /* dummy check */
if ( strEmptyOrNull( path ) ) { if ( strEmptyOrNull( path ) ) {
return; return;
@ -209,13 +206,9 @@ void WriteSurfaceExtraFile( const char *path ){
Sys_Printf( "--- WriteSurfaceExtraFile ---\n" ); Sys_Printf( "--- WriteSurfaceExtraFile ---\n" );
/* open the file */ /* open the file */
strcpy( srfPath, path ); auto srfPath = StringOutputStream( 256 )( PathExtensionless( path ), ".srf" );
path_set_extension( srfPath, ".srf" ); Sys_Printf( "Writing %s\n", srfPath.c_str() );
Sys_Printf( "Writing %s\n", srfPath ); FILE *sf = SafeOpenWrite( srfPath, "wt" );
sf = fopen( srfPath, "w" );
if ( sf == NULL ) {
Error( "Error opening %s for writing", srfPath );
}
/* lap through the extras list */ /* lap through the extras list */
for ( i = -1; i < numSurfaceExtras; i++ ) for ( i = -1; i < numSurfaceExtras; i++ )

View File

@ -797,15 +797,11 @@ void WriteFloat( FILE *f, vec_t v );
void WritePortals( char *filename ){ void WritePortals( char *filename ){
int i, j, num; int i, j, num;
FILE *pf;
vportal_t *p; vportal_t *p;
fixedWinding_t *w; fixedWinding_t *w;
// write the file // write the file
pf = fopen( filename, "w" ); FILE *pf = SafeOpenWrite( filename, "wt" );
if ( !pf ) {
Error( "Error opening %s", filename );
}
num = 0; num = 0;
for ( j = 0; j < numportals * 2; j++ ) for ( j = 0; j < numportals * 2; j++ )
@ -888,10 +884,7 @@ void LoadPortals( char *name ){
} }
else else
{ {
f = fopen( name, "r" ); f = SafeOpenRead( name, "rt" );
if ( !f ) {
Error( "LoadPortals: couldn't read %s\n",name );
}
} }
if ( fscanf( f, "%79s\n%i\n%i\n%i\n", magic, &portalclusters, &numportals, &numfaces ) != 4 ) { if ( fscanf( f, "%79s\n%i\n%i\n%i\n", magic, &portalclusters, &numportals, &numfaces ) != 4 ) {