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 *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;

View File

@ -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 );

View File

@ -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" );

View File

@ -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 );

View File

@ -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" );

View File

@ -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" );

View File

@ -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 ) {

View File

@ -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 );

View File

@ -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" );

View File

@ -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++ )

View File

@ -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 ) {

View File

@ -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 );

View File

@ -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;

View File

@ -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++ )

View File

@ -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 ) {