diff --git a/libs/os/path.h b/libs/os/path.h index 60399705..827cb1c2 100644 --- a/libs/os/path.h +++ b/libs/os/path.h @@ -207,6 +207,21 @@ inline MatchFileExtension matchFileExtension( const char* extension, co return MatchFileExtension( extension, functor ); } +class PathExtensionless +{ +public: +const char* m_path; +PathExtensionless( const char* path ) : m_path( path ){ +} +}; + +/// \brief Writes \p path to \p ostream without .ext part. +template +TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const PathExtensionless& path ){ + ostream << StringRange( path.m_path, path_get_filename_base_end( path.m_path ) ); + return ostream; +} + class PathCleaned { public: diff --git a/radiant/map.cpp b/radiant/map.cpp index 2b315f8c..b7814aec 100644 --- a/radiant/map.cpp +++ b/radiant/map.cpp @@ -2351,7 +2351,7 @@ void map_autocaulk_selected(){ ScopeDisableScreenUpdates disableScreenUpdates( "processing", "autocaulk" ); StringOutputStream filename( 256 ); - filename << StringRange( g_map.m_name.c_str(), path_get_filename_base_end( g_map.m_name.c_str() ) ) << "_ac.map"; + filename << PathExtensionless( g_map.m_name.c_str() ) << "_ac.map"; {// write .map const Vector3 spawn( Camera_getOrigin( *g_pParentWnd->GetCamWnd() ) ); @@ -2463,7 +2463,7 @@ void map_autocaulk_selected(){ CaulkMap map; { // load filename.clear(); - filename << StringRange( g_map.m_name.c_str(), path_get_filename_base_end( g_map.m_name.c_str() ) ) << "_ac.caulk"; + filename << PathExtensionless( g_map.m_name.c_str() ) << "_ac.caulk"; TextFileInputStream file( filename.c_str() ); if( file.failed() ){ diff --git a/radiant/points.cpp b/radiant/points.cpp index 4ef6eed0..1e747963 100644 --- a/radiant/points.cpp +++ b/radiant/points.cpp @@ -174,9 +174,7 @@ void CPointfile::GenerateDisplayList(){ void Pointfile_Delete( void ){ const char* mapname = Map_Name( g_map ); - StringOutputStream name( 256 ); - name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".lin"; - file_remove( name.c_str() ); + file_remove( StringOutputStream( 256 )( PathExtensionless( mapname ), ".lin" ).c_str() ); } @@ -258,14 +256,14 @@ void Pointfile_Parse( CPointfile& pointfile ){ const char* mapname = Map_Name( g_map ); StringOutputStream name( 256 ); - name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".lin"; + name << PathExtensionless( mapname ) << ".lin"; size = LoadFile( name.c_str(), (void**)&data ); if ( size == -1 ) { globalErrorStream() << "Pointfile " << name.c_str() << " not found\n"; /* try .pts (q1) */ name.clear(); - name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".pts"; + name << PathExtensionless( mapname ) << ".pts"; size = LoadFile( name.c_str(), (void**)&data ); if ( size == -1 ) { globalErrorStream() << "Pointfile " << name.c_str() << " not found\n"; diff --git a/radiant/qe3.cpp b/radiant/qe3.cpp index 6fd7d38e..be1c82e2 100644 --- a/radiant/qe3.cpp +++ b/radiant/qe3.cpp @@ -173,15 +173,11 @@ void bsp_init(){ const char* mapname = Map_Name( g_map ); { - StringOutputStream name( 256 ); - name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".bsp"; - build_set_variable( "BspFile", name.c_str() ); + build_set_variable( "BspFile", StringOutputStream( 256 )( PathExtensionless( mapname ), ".bsp" ).c_str() ); } if( g_region_active ){ - StringOutputStream name( 256 ); - name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".reg"; - build_set_variable( "MapFile", name.c_str() ); + build_set_variable( "MapFile", StringOutputStream( 256 )( PathExtensionless( mapname ), ".reg" ).c_str() ); } else{ build_set_variable( "MapFile", mapname ); @@ -272,9 +268,7 @@ void RunBSP( const char* name ){ if ( g_region_active ) { const char* mapname = Map_Name( g_map ); - StringOutputStream name( 256 ); - name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".reg"; - Map_SaveRegion( name.c_str() ); + Map_SaveRegion( StringOutputStream( 256 )( PathExtensionless( mapname ), ".reg" ).c_str() ); } Pointfile_Delete(); diff --git a/radiant/texwindow.cpp b/radiant/texwindow.cpp index f1ae4ecc..cc6e0cc6 100644 --- a/radiant/texwindow.cpp +++ b/radiant/texwindow.cpp @@ -740,8 +740,7 @@ void operator()( const char* name ) const { }; void TextureDirectory_loadTexture( const char* directory, const char* texture ){ - StringOutputStream name( 256 ); - name << directory << StringRange( texture, path_get_filename_base_end( texture ) ); + const auto name = StringOutputStream( 256 )( directory, PathExtensionless( texture ) ); if ( texture_name_ignore( name.c_str() ) ) { return; diff --git a/radiant/xywindow.cpp b/radiant/xywindow.cpp index f28dc90f..169f2666 100644 --- a/radiant/xywindow.cpp +++ b/radiant/xywindow.cpp @@ -1384,7 +1384,7 @@ void BackgroundImage::set( const VIEWTYPE viewtype ){ free_tex(); const char *filename = background_image_dialog(); if( filename ){ - const auto filename_noext = StringOutputStream( 256 )( StringRange( filename, path_get_filename_base_end( filename ) ) ); + const auto filename_noext = StringOutputStream( 256 )( PathExtensionless( filename ) ); Image *image = QERApp_LoadImage( 0, filename_noext.c_str() ); if ( !image ) { globalErrorStream() << "Could not load texture " << filename_noext.c_str() << "\n"; diff --git a/tools/quake3/q3map2/model.cpp b/tools/quake3/q3map2/model.cpp index fa9286e2..35147500 100644 --- a/tools/quake3/q3map2/model.cpp +++ b/tools/quake3/q3map2/model.cpp @@ -244,11 +244,11 @@ void InsertModel( const char *name, int skin, int frame, m4x4_t transform, const } /* load skin file */ - auto skinfilename = StringOutputStream(99)( StringRange( name, path_get_filename_base_end( name ) ), '_', skin, ".skin" ); + auto skinfilename = StringOutputStream(99)( PathExtensionless( name ), '_', skin, ".skin" ); skinfilesize = vfsLoadFile( skinfilename.c_str(), (void**) &skinfilecontent, 0 ); if ( skinfilesize < 0 && skin != 0 ) { /* fallback to skin 0 if invalid */ - skinfilename( StringRange( name, path_get_filename_base_end( name ) ), "_0.skin" ); + skinfilename( PathExtensionless( name ), "_0.skin" ); skinfilesize = vfsLoadFile( skinfilename.c_str(), (void**) &skinfilecontent, 0 ); if ( skinfilesize >= 0 ) { Sys_Printf( "Skin %d of %s does not exist, using 0 instead\n", skin, name );