From a333eaee1c5b8b20ec59411f4878f8fc5bdfa584 Mon Sep 17 00:00:00 2001 From: Antoine Fontaine Date: Tue, 23 Mar 2021 13:19:35 +0100 Subject: [PATCH] radiant: replace StringBuffer with std::string --- libs/cmdlib.h | 2 +- libs/cmdlib/cmdlib.cpp | 12 ++-- libs/fs_path.h | 19 ++---- plugins/entity/rotation.h | 7 ++- plugins/vfspk3/vfs.cpp | 10 +-- radiant/build.cpp | 126 +++++++++++++++----------------------- radiant/eclass_def.cpp | 2 +- radiant/map.cpp | 56 ++++++++--------- radiant/mru.cpp | 7 +-- radiant/qe3.cpp | 19 +++--- 10 files changed, 115 insertions(+), 145 deletions(-) diff --git a/libs/cmdlib.h b/libs/cmdlib.h index abdc15a6..e901486d 100644 --- a/libs/cmdlib.h +++ b/libs/cmdlib.h @@ -44,7 +44,7 @@ // if the spawn was fine // TODO TTimo add functionality to track the process until it dies -bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole, bool waitfor ); +bool Q_Exec( const char *cmd, const char *cmdline, const char *execdir, bool bCreateConsole, bool waitfor ); // some easy portability crap diff --git a/libs/cmdlib/cmdlib.cpp b/libs/cmdlib/cmdlib.cpp index 9bfd2436..25c5d9ad 100644 --- a/libs/cmdlib/cmdlib.cpp +++ b/libs/cmdlib/cmdlib.cpp @@ -40,7 +40,7 @@ #include #include -bool Q_Exec( const char *cmd, char *cmdline, const char *, bool, bool waitfor ){ +bool Q_Exec( const char *cmd, const char *cmdline, const char *, bool, bool waitfor ){ char fullcmd[2048]; char *pCmd; pid_t pid; @@ -91,7 +91,7 @@ bool Q_Exec( const char *cmd, char *cmdline, const char *, bool, bool waitfor ){ #include // NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess -bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole, bool waitfor ){ +bool Q_Exec( const char *cmd, const char *cmdline, const char *execdir, bool bCreateConsole, bool waitfor ){ PROCESS_INFORMATION ProcessInformation; STARTUPINFO startupinfo = {0}; DWORD dwCreationFlags; @@ -102,14 +102,12 @@ bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateCo else{ dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS; } - const char *pCmd; - char *pCmdline; - pCmd = cmd; + const char *pCmd = cmd; if ( pCmd ) { while ( *pCmd == ' ' ) pCmd++; } - pCmdline = cmdline; + char *pCmdline = strdup(cmdline); if ( pCmdline ) { while ( *pCmdline == ' ' ) pCmdline++; @@ -130,8 +128,10 @@ bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateCo if ( waitfor ) { WaitForSingleObject( ProcessInformation.hProcess, INFINITE ); } + free(pCmdline); return true; } + free(pCmdline); return false; } diff --git a/libs/fs_path.h b/libs/fs_path.h index 2baf297e..ee945814 100644 --- a/libs/fs_path.h +++ b/libs/fs_path.h @@ -22,17 +22,15 @@ #if !defined( INCLUDED_FS_PATH_H ) #define INCLUDED_FS_PATH_H -#include "stream/stringstream.h" - /// \brief A unix-style path string which can be modified at runtime. /// /// - Maintains a path ending in a path-separator. /// - Provides a limited STL-style interface to push and pop file or directory names at the end of the path. class UnixPath { -StringBuffer m_string; +std::string m_string; -void check_separator(){ +void ensure_separator(){ if ( !empty() && m_string.back() != '/' ) { m_string.push_back( '/' ); } @@ -42,7 +40,7 @@ public: /// \brief Constructs with the directory \p root. UnixPath( const char* root ) : m_string( root ){ - check_separator(); + ensure_separator(); } bool empty() const { @@ -55,17 +53,12 @@ const char* c_str() const { /// \brief Appends the directory \p name. void push( const char* name ){ - m_string.push_string( name ); - check_separator(); -} -/// \brief Appends the directory [\p first, \p last). -void push( const char* first, const char* last ){ - m_string.push_range( first, last ); - check_separator(); + m_string += name; + ensure_separator(); } /// \brief Appends the filename \p name. void push_filename( const char* name ){ - m_string.push_string( name ); + m_string += name; } /// \brief Removes the last directory or filename appended. void pop(){ diff --git a/plugins/entity/rotation.h b/plugins/entity/rotation.h index 4c566d96..d6b00d71 100644 --- a/plugins/entity/rotation.h +++ b/plugins/entity/rotation.h @@ -24,11 +24,12 @@ #include "ientity.h" -#include "stream/stringstream.h" #include "math/quaternion.h" #include "generic/callback.h" #include "stringio.h" +#include + #include "angle.h" typedef float Float9[9]; @@ -58,7 +59,7 @@ inline void write_rotation( const Float9 rotation, Entity* entity, const char* k } else { - StringOutputStream value( 256 ); + std::ostringstream value; value << rotation[0] << ' ' << rotation[1] << ' ' << rotation[2] << ' ' @@ -68,7 +69,7 @@ inline void write_rotation( const Float9 rotation, Entity* entity, const char* k << rotation[6] << ' ' << rotation[7] << ' ' << rotation[8]; - entity->setKeyValue( key, value.c_str() ); + entity->setKeyValue( key, value.str().c_str() ); } } inline void read_rotation( Float9 rotation, const char* value ){ diff --git a/plugins/vfspk3/vfs.cpp b/plugins/vfspk3/vfs.cpp index 5871f991..703a956e 100644 --- a/plugins/vfspk3/vfs.cpp +++ b/plugins/vfspk3/vfs.cpp @@ -129,8 +129,8 @@ static void FixDOSName( char *src ){ } const _QERArchiveTable* GetArchiveTable( ArchiveModules& archiveModules, const char* ext ){ - StringOutputStream tmp( 16 ); - tmp << LowerCase( ext ); + std::string tmp = ext; + transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower); return archiveModules.findModule( tmp.c_str() ); } @@ -393,9 +393,9 @@ static void LoadDpkPakWithDeps( const char* pakname ){ if (pakname == NULL) { // load DEPS from game pack - StringOutputStream baseDirectory( 256 ); - const char* basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue( "basegame" ); - baseDirectory << GlobalRadiant().getGameToolsPath() << basegame << '/'; + std::string baseDirectory( GlobalRadiant().getGameToolsPath() ); + baseDirectory += GlobalRadiant().getRequiredGameDescriptionKeyValue( "basegame" ); + baseDirectory += '/'; arc = AddDpkDir( baseDirectory.c_str() ); depsFile = arc->openTextFile( "DEPS" ); } else { diff --git a/radiant/build.cpp b/radiant/build.cpp index 980a7fe9..4e5ca186 100644 --- a/radiant/build.cpp +++ b/radiant/build.cpp @@ -41,12 +41,12 @@ void build_set_variable( const char* name, const char* value ){ g_build_variables[name] = value; } -const char* build_get_variable( const char* name ){ - Variables::iterator i = g_build_variables.find( name ); +const char* build_get_variable( const std::string& name ){ + Variables::iterator i = g_build_variables.find( name.c_str() ); if ( i != g_build_variables.end() ) { return ( *i ).second.c_str(); } - globalErrorStream() << "undefined build variable: " << makeQuoted( name ) << "\n"; + globalErrorStream() << "undefined build variable: " << makeQuoted( name.c_str() ) << "\n"; return ""; } @@ -57,104 +57,80 @@ class Evaluatable { public: virtual ~Evaluatable() = default; -virtual void evaluate( StringBuffer& output ) = 0; +virtual std::string evaluate() = 0; virtual void exportXML( XMLImporter& importer ) = 0; }; class VariableString : public Evaluatable { -CopiedString m_string; +std::string m_string; public: VariableString() : m_string(){ } -VariableString( const char* string ) : m_string( string ){ +VariableString( std::string string ) : m_string( std::move(string) ){ } const char* c_str() const { return m_string.c_str(); } -void setString( const char* string ){ +void setString( const std::string& string ){ m_string = string; } - -void evaluate( StringBuffer& output ){ +std::string evaluate(){ // replace ".[ExecutableType]" with "[ExecutableExt]" { - StringBuffer output; - const char pattern[] = ".[ExecutableType]"; - const char *string = m_string.c_str(); - for ( const char *i = string; *i != '\0'; i++ ) - { - if ( strncmp( i, pattern, sizeof( pattern ) - 1 ) == 0 ) - { - output.push_string("[ExecutableExt]"); - // minus 1 because \0, minus 1 because i++ in a replacement - i += sizeof( pattern ) - 2; - } - else - { - output.push_back(*i); - } + size_t pos; + const std::string pattern = ".[ExecutableType]"; + while ( ( pos = m_string.find(pattern) ) != std::string::npos ) { + m_string.replace(pos, pattern.length(), "[ExecutableExt]"); } - setString(output.c_str()); } // add missing [ExtraQ3map2Args] if "[RadiantPath]q3map2[ExecutableExt]" { - const char pattern[] = "\"[RadiantPath]q3map2[ExecutableExt]\""; - const char extra[] = "[ExtraQ3map2Args]"; - const char *string = m_string.c_str(); - if ( strstr( string, pattern ) != NULL && strstr( string, extra ) == NULL ) + size_t pos; + const std::string pattern = "\"[RadiantPath]q3map2[ExecutableExt]\""; + const std::string extra = "[ExtraQ3map2Args]"; + if ( ( pos = m_string.find(pattern) ) != std::string::npos + && m_string.find(extra) == std::string::npos ) { - StringBuffer output; - for ( const char *i = string; *i != '\0'; i++ ) - { - if ( strncmp( i, pattern, sizeof( pattern ) - 1 ) == 0 ) - { - output.push_string(pattern); - output.push_string(" "); - output.push_string(extra); - // minus 1 because \0, no replacement - i += strlen( pattern ) - 1; - } - else - { - output.push_back(*i); - } - } - setString(output.c_str()); + m_string.insert(pos + pattern.size(), " "); + m_string.insert(pos + pattern.size() + 1, extra); } } - StringBuffer variable; + std::string output; + std::string variable_name; bool in_variable = false; - for ( const char* i = m_string.c_str(); *i != '\0'; ++i ) + for ( const char c : m_string ) { if ( !in_variable ) { - switch ( *i ) + switch ( c ) { case '[': in_variable = true; break; default: - output.push_back( *i ); + output += c; break; } } else { - switch ( *i ) + switch ( c ) { case ']': in_variable = false; - output.push_string( build_get_variable( variable.c_str() ) ); - variable.clear(); + output += build_get_variable( variable_name ); + variable_name.clear(); break; default: - variable.push_back( *i ); + variable_name += c; break; } } } + + return output; } void exportXML( XMLImporter& importer ){ importer << c_str(); @@ -172,12 +148,12 @@ Conditional( VariableString* test ) : m_test( test ){ delete m_test; delete m_result; } -void evaluate( StringBuffer& output ){ - StringBuffer buffer; - m_test->evaluate( buffer ); - if ( !string_empty( buffer.c_str() ) ) { - m_result->evaluate( output ); +std::string evaluate(){ + std::string result = m_test->evaluate(); + if ( result.empty() ) { + return result; } + return m_result->evaluate(); } void exportXML( XMLImporter& importer ){ StaticElement conditionElement( "cond" ); @@ -203,11 +179,13 @@ public: void push_back( Evaluatable* evaluatable ){ m_evaluatables.push_back( evaluatable ); } -void evaluate( StringBuffer& output ){ +std::string evaluate(){ + std::string result; for ( Evaluatables::iterator i = m_evaluatables.begin(); i != m_evaluatables.end(); ++i ) { - ( *i )->evaluate( output ); + result += ( *i )->evaluate(); } + return result; } void exportXML( XMLImporter& importer ){ for ( Evaluatables::iterator i = m_evaluatables.begin(); i != m_evaluatables.end(); ++i ) @@ -229,16 +207,16 @@ virtual void popElement( const char* name ) = 0; class VariableStringXMLConstructor : public XMLElementParser { -StringBuffer m_buffer; +std::string m_buffer; VariableString& m_variableString; public: VariableStringXMLConstructor( VariableString& variableString ) : m_variableString( variableString ){ } ~VariableStringXMLConstructor(){ - m_variableString.setString( m_buffer.c_str() ); + m_variableString.setString( std::move(m_buffer) ); } std::size_t write( const char* buffer, std::size_t length ){ - m_buffer.push_range( buffer, buffer + length ); + m_buffer.append( buffer, length ); return length; } XMLElementParser& pushElement( const XMLElement& element ){ @@ -251,16 +229,16 @@ void popElement( const char* name ){ class ConditionalXMLConstructor : public XMLElementParser { -StringBuffer m_buffer; +std::string m_buffer; Conditional& m_conditional; public: ConditionalXMLConstructor( Conditional& conditional ) : m_conditional( conditional ){ } ~ConditionalXMLConstructor(){ - m_conditional.m_result = new VariableString( m_buffer.c_str() ); + m_conditional.m_result = new VariableString( std::move( m_buffer ) ); } std::size_t write( const char* buffer, std::size_t length ){ - m_buffer.push_range( buffer, buffer + length ); + m_buffer.append( buffer, length ); return length; } XMLElementParser& pushElement( const XMLElement& element ){ @@ -273,7 +251,7 @@ void popElement( const char* name ){ class ToolXMLConstructor : public XMLElementParser { -StringBuffer m_buffer; +std::string m_buffer; Tool& m_tool; ConditionalXMLConstructor* m_conditional; public: @@ -283,7 +261,7 @@ ToolXMLConstructor( Tool& tool ) : m_tool( tool ){ flush(); } std::size_t write( const char* buffer, std::size_t length ){ - m_buffer.push_range( buffer, buffer + length ); + m_buffer.append( buffer, length ); return length; } XMLElementParser& pushElement( const XMLElement& element ){ @@ -308,7 +286,7 @@ void popElement( const char* name ){ void flush(){ if ( !m_buffer.empty() ) { - m_tool.push_back( new VariableString( m_buffer.c_str() ) ); + m_tool.push_back( new VariableString( std::move( m_buffer ) ) ); m_buffer.clear(); } } @@ -523,8 +501,7 @@ void project_verify( Project& project, Tools& tools ){ void build_run( const char* name, CommandListener& listener ){ for ( Tools::iterator i = g_build_tools.begin(); i != g_build_tools.end(); ++i ) { - StringBuffer output; - ( *i ).second.evaluate( output ); + std::string output = ( *i ).second.evaluate(); build_set_variable( ( *i ).first.c_str(), output.c_str() ); } @@ -534,8 +511,7 @@ void build_run( const char* name, CommandListener& listener ){ Build& build = ( *i ).second; for ( Build::iterator j = build.begin(); j != build.end(); ++j ) { - StringBuffer output; - ( *j ).evaluate( output ); + std::string output = ( *j ).evaluate(); listener.execute( output.c_str() ); } } @@ -806,7 +782,7 @@ gboolean project_selection_changed( ui::TreeSelection selection, ui::ListStore s return FALSE; } -gboolean commands_cell_edited(ui::CellRendererText cell, gchar* path_string, gchar* new_text, ui::ListStore store ){ +gboolean commands_cell_edited(ui::CellRendererText cell, const gchar* path_string, const gchar* new_text, ui::ListStore store ){ if ( g_current_build == 0 ) { return FALSE; } diff --git a/radiant/eclass_def.cpp b/radiant/eclass_def.cpp index 8637b480..b2ab48ce 100644 --- a/radiant/eclass_def.cpp +++ b/radiant/eclass_def.cpp @@ -313,7 +313,7 @@ void Eclass_ScanFile( EntityClassCollector& collector, const char *filename ){ } state = eParseDefault; const char* quakeEd = "QUAKED"; const char* p = 0; - StringBuffer buffer; + std::string buffer; SingleCharacterInputStream bufferedInput( inputFile ); for (;; ) { diff --git a/radiant/map.cpp b/radiant/map.cpp index de6c6d13..36b77ae1 100644 --- a/radiant/map.cpp +++ b/radiant/map.cpp @@ -1578,55 +1578,55 @@ tryDecompile: const char *type = GlobalRadiant().getGameDescriptionKeyValue( "q3map2_type" ); int n = string_length( path_get_extension( filename ) ); if ( n && ( extension_equal( path_get_extension( filename ), "bsp" ) || extension_equal( path_get_extension( filename ), "map" ) ) ) { - StringBuffer output; - output.push_string( AppPath_get() ); - output.push_string( "q3map2" ); - output.push_string( GDEF_OS_EXE_EXT ); - - output.push_string( " -v -game " ); - output.push_string( ( type && *type ) ? type : "quake3" ); - output.push_string( " -fs_basepath \"" ); - output.push_string( EnginePath_get() ); - output.push_string( "\" -fs_homepath \"" ); - output.push_string( g_qeglobals.m_userEnginePath.c_str() ); - output.push_string( "\"" ); + std::string output; + output += AppPath_get(); + output += "q3map2"; + output += GDEF_OS_EXE_EXT; + + output += " -v -game "; + output += ( type && *type ) ? type : "quake3"; + output += " -fs_basepath \""; + output += EnginePath_get(); + output += "\" -fs_homepath \""; + output += g_qeglobals.m_userEnginePath.c_str(); + output += "\""; // extra pakpaths for ( int i = 0; i < g_pakPathCount; i++ ) { if ( g_strcmp0( g_strPakPath[i].c_str(), "") ) { - output.push_string( " -fs_pakpath \"" ); - output.push_string( g_strPakPath[i].c_str() ); - output.push_string( "\"" ); + output += " -fs_pakpath \""; + output += g_strPakPath[i].c_str(); + output += "\""; } } // extra switches if ( g_disableEnginePath ) { - output.push_string( " -fs_nobasepath " ); + output += " -fs_nobasepath "; } if ( g_disableHomePath ) { - output.push_string( " -fs_nohomepath " ); + output += " -fs_nohomepath "; } - output.push_string( " -fs_game " ); - output.push_string( gamename_get() ); - output.push_string( " -convert -format " ); - output.push_string( Brush::m_type == eBrushTypeQuake3BP ? "map_bp" : "map" ); + output += " -fs_game "; + output += gamename_get(); + output += " -convert -format "; + output += Brush::m_type == eBrushTypeQuake3BP ? "map_bp" : "map"; if ( extension_equal( path_get_extension( filename ), "map" ) ) { - output.push_string( " -readmap " ); + output += " -readmap "; } - output.push_string( " \"" ); - output.push_string( filename ); - output.push_string( "\"" ); + output += " \""; + output += filename; + output += "\""; // run Q_Exec( NULL, output.c_str(), NULL, false, true ); // rebuild filename as "filenamewithoutext_converted.map" - output.clear(); - output.push_range( filename, filename + string_length( filename ) - ( n + 1 ) ); - output.push_string( "_converted.map" ); + output = ""; + output.append( filename, string_length( filename ) - ( n + 1 ) ); + output += "_converted.map"; filename = output.c_str(); // open diff --git a/radiant/mru.cpp b/radiant/mru.cpp index 798e7f5e..640fd391 100644 --- a/radiant/mru.cpp +++ b/radiant/mru.cpp @@ -27,7 +27,6 @@ #include "os/file.h" #include "generic/callback.h" -#include "stream/stringstream.h" #include "convert.h" #include "gtkutil/menu.h" @@ -51,10 +50,10 @@ inline const char* MRU_GetText( std::size_t index ){ class EscapedMnemonic { private: - StringBuffer m_buffer; + std::string m_buffer; public: - EscapedMnemonic( std::size_t capacity ) : m_buffer( capacity ){ + EscapedMnemonic() : m_buffer(){ m_buffer.push_back( '_' ); } const char* c_str() const { @@ -83,7 +82,7 @@ inline EscapedMnemonic& operator<<( EscapedMnemonic& ostream, const T& t ){ void MRU_updateWidget( std::size_t index, const char *filename ){ - EscapedMnemonic mnemonic( 64 ); + EscapedMnemonic mnemonic; mnemonic << Unsigned( index + 1 ) << "- " << filename; gtk_label_set_text_with_mnemonic( GTK_LABEL( gtk_bin_get_child( GTK_BIN( MRU_items[index] ) ) ), mnemonic.c_str() ); } diff --git a/radiant/qe3.cpp b/radiant/qe3.cpp index e5881bb0..3b015aa4 100644 --- a/radiant/qe3.cpp +++ b/radiant/qe3.cpp @@ -191,7 +191,7 @@ void bsp_init(){ // ".[ExecutableType]" is replaced by "[ExecutableExt]" const char *exe_ext = GDEF_OS_EXE_EXT; build_set_variable( "ExecutableType", exe_ext[0] == '\0' ? exe_ext : exe_ext + 1 ); - + build_set_variable( "ExecutableExt", GDEF_OS_EXE_EXT ); build_set_variable( "RadiantPath", AppPath_get() ); build_set_variable( "EnginePath", EnginePath_get() ); @@ -201,30 +201,31 @@ void bsp_init(){ build_set_variable( "GameName", gamename_get() ); - StringBuffer ExtraQ3map2Args; + std::string ExtraQ3map2Args; // extra pakpaths for ( int i = 0; i < g_pakPathCount; i++ ) { if ( g_strcmp0( g_strPakPath[i].c_str(), "") ) { - ExtraQ3map2Args.push_string( " -fs_pakpath \"" ); - ExtraQ3map2Args.push_string( g_strPakPath[i].c_str() ); - ExtraQ3map2Args.push_string( "\"" ); + ExtraQ3map2Args += " -fs_pakpath \""; + ExtraQ3map2Args += g_strPakPath[i].c_str(); + ExtraQ3map2Args += "\""; } } // extra switches if ( g_disableEnginePath ) { - ExtraQ3map2Args.push_string( " -fs_nobasepath " ); + ExtraQ3map2Args += " -fs_nobasepath "; } if ( g_disableHomePath ) { - ExtraQ3map2Args.push_string( " -fs_nohomepath " ); + ExtraQ3map2Args += " -fs_nohomepath "; } build_set_variable( "ExtraQ3map2Args", ExtraQ3map2Args.c_str() ); const char* mapname = Map_Name( g_map ); - StringOutputStream name( 256 ); - name << StringRange( mapname, path_get_filename_base_end( mapname ) ) << ".bsp"; + std::string name; + name.append( mapname, path_get_filename_base_end( mapname ) - mapname ); + name += ".bsp"; build_set_variable( "MapFile", mapname ); build_set_variable( "BspFile", name.c_str() ); -- 2.39.2