From 43e7b4bd4597857b8bcd1c6b89ab568f2b322777 Mon Sep 17 00:00:00 2001 From: hikerstk Date: Wed, 28 Jan 2009 21:49:28 +0000 Subject: [PATCH] Removed all snprintf statements and constants for maximum message sizes. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@3065 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/challenges/challenge.cpp | 26 +++++++++++------- src/file_manager.cpp | 1 - src/gui/char_sel.cpp | 10 +++---- src/gui/config_controls.cpp | 4 --- src/gui/config_display.cpp | 32 +++++++++++----------- src/gui/display_res_confirm.cpp | 9 +++---- src/gui/display_res_confirm.hpp | 10 +++---- src/gui/grand_prix_ending.cpp | 10 +++---- src/gui/player_controls.cpp | 15 ++++------- src/lisp/parser.cpp | 48 +++++++++++++-------------------- src/utils/translation.hpp | 5 ---- 11 files changed, 70 insertions(+), 100 deletions(-) diff --git a/src/challenges/challenge.cpp b/src/challenges/challenge.cpp index fed5978b9..db06b2a73 100755 --- a/src/challenges/challenge.cpp +++ b/src/challenges/challenge.cpp @@ -26,10 +26,8 @@ #include "tracks/track.hpp" #include "tracks/track_manager.hpp" #include "utils/translation.hpp" +#include "utils/string_utils.hpp" -#if defined(WIN32) && !defined(__CYGWIN__) -# define snprintf _snprintf -#endif //----------------------------------------------------------------------------- void Challenge::addUnlockTrackReward(const std::string &track_name) @@ -93,7 +91,7 @@ const std::string Challenge::getUnlockedMessage() const // add line break if we are showing multiple messages if(n>0) unlocked_message+='\n'; - char message[128]; + std::string message; // write message depending on feature type switch(m_feature[n].type) @@ -101,24 +99,34 @@ const std::string Challenge::getUnlockedMessage() const case UNLOCK_TRACK: { // {} avoids compiler warning Track* track = track_manager->getTrack( m_feature[n].name ); - snprintf(message, 127, _("New track '%s'\nnow available"), _(track->getName()) ); + message = StringUtils::insert_values( + _("New track '%s'\nnow available"), + _(track->getName()) ); break; } case UNLOCK_MODE: - snprintf(message, 127, _("New game mode\n'%s'\nnow available"), m_feature[n].user_name.c_str() ); + message = StringUtils::insert_values( + _("New game mode\n'%s'\nnow available"), + m_feature[n].user_name); break; case UNLOCK_GP: { std::string gp_user_name = grand_prix_manager->getGrandPrix(m_feature[n].name)->getName(); - snprintf(message, 127, _("New Grand Prix '%s'\nnow available"), gp_user_name.c_str() ); + message = StringUtils::insert_values( + _("New Grand Prix '%s'\nnow available"), + gp_user_name); break; } case UNLOCK_DIFFICULTY: - snprintf(message, 127, _("New difficulty\n'%s'\nnow available"), m_feature[n].user_name.c_str() ); + message = StringUtils::insert_values( + _("New difficulty\n'%s'\nnow available"), + m_feature[n].user_name); break; case UNLOCK_KART: const KartProperties *kp=kart_properties_manager->getKart(m_feature[n].name ); - snprintf(message, 127, _("New kart\n'%s'\nnow available"), kp->getName().c_str()); + message = StringUtils::insert_values( + _("New kart\n'%s'\nnow available"), + kp->getName()); break; } // switch unlocked_message += message; diff --git a/src/file_manager.cpp b/src/file_manager.cpp index 2e38ca766..43576d398 100644 --- a/src/file_manager.cpp +++ b/src/file_manager.cpp @@ -30,7 +30,6 @@ # ifndef __CYGWIN__ # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) // Some portabilty defines -# define snprintf _snprintf # define access _access # define F_OK 04 # endif diff --git a/src/gui/char_sel.cpp b/src/gui/char_sel.cpp index faf65ebd3..8c94dd828 100644 --- a/src/gui/char_sel.cpp +++ b/src/gui/char_sel.cpp @@ -33,10 +33,7 @@ #include "karts/kart_properties_manager.hpp" #include "network/network_manager.hpp" #include "utils/translation.hpp" - -#if defined(WIN32) && !defined(__CYGWIN__) -# define snprintf _snprintf -#endif +#include "utils/string_utils.hpp" enum WidgetTokens { @@ -108,9 +105,8 @@ CharSel::CharSel(int whichPlayer) if (m_player_index < (int)kart_properties_manager->getNumSelectedKarts()) kart_properties_manager->removeLastSelectedKart(); - char heading[MAX_MESSAGE_LENGTH]; - snprintf(heading, sizeof(heading), _("Player %d, choose a driver"), - m_player_index + 1); + std::string heading = StringUtils::insert_values( _("Player %d, choose a driver"), + m_player_index + 1); widget_manager->addTitleWgt( WTOK_TITLE, 100, 10, heading ); widget_manager->hideWgtRect(WTOK_TITLE); widget_manager->breakLine(); diff --git a/src/gui/config_controls.cpp b/src/gui/config_controls.cpp index ce1c4464a..bd70aab74 100644 --- a/src/gui/config_controls.cpp +++ b/src/gui/config_controls.cpp @@ -19,10 +19,6 @@ #include "config_controls.hpp" -#if defined(WIN32) && !defined(__CYGWIN__) -# define snprintf _snprintf -#endif - #include "gui/menu_manager.hpp" #include "gui/widget_manager.hpp" #include "utils/translation.hpp" diff --git a/src/gui/config_display.cpp b/src/gui/config_display.cpp index 669b00275..2ad9d9ebe 100644 --- a/src/gui/config_display.cpp +++ b/src/gui/config_display.cpp @@ -18,11 +18,8 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "config_display.hpp" -#include -#if defined(WIN32) && !defined(__CYGWIN__) -# define snprintf _snprintf -#endif +#include #include "main_loop.hpp" #include "widget_manager.hpp" @@ -30,7 +27,7 @@ #include "menu_manager.hpp" #include "sdldrv.hpp" #include "utils/translation.hpp" - +#include "utils/string_utils.hpp" enum WidgetTokens { @@ -84,9 +81,9 @@ ConfigDisplay::ConfigDisplay() widget_manager->addTextButtonWgt(WTOK_NEXT_BACKGROUND, 60, 7, _("Next background")); widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 60, 2 ); - char msg [MAX_MESSAGE_LENGTH]; //I18N: displays current resolution - snprintf( msg, MAX_MESSAGE_LENGTH, _("Current: %dx%d"), m_curr_width, m_curr_height ); + std::string msg = StringUtils::insert_values(_("Current: %dx%d"), + m_curr_width, m_curr_height); widget_manager->addTextWgt( WTOK_CURRENT_RES, 60, 7, msg); widget_manager->hideWgtRect(WTOK_CURRENT_RES); @@ -176,10 +173,11 @@ void ConfigDisplay::select() } else { - char msg [MAX_MESSAGE_LENGTH]; - snprintf (msg, MAX_MESSAGE_LENGTH, "Current: %dx%d", - user_config->m_width, user_config->m_height); - widget_manager->setWgtText(WTOK_CURRENT_RES, msg); + //I18N: displays current resolution + std::string s = StringUtils::insert_values(_("Current: %dx%d"), + user_config->m_width, + user_config->m_height); + widget_manager->setWgtText(WTOK_CURRENT_RES, s); if ( isBlacklisted( user_config->m_width, user_config->m_height )) @@ -357,9 +355,9 @@ void ConfigDisplay::getScreenModes() void ConfigDisplay::changeApplyButton() { // change Apply button text - char msg [MAX_MESSAGE_LENGTH]; - snprintf(msg, MAX_MESSAGE_LENGTH, _("Apply %dx%d"), - m_sizes[m_curr_res].first,m_sizes[m_curr_res].second); + std::string msg = StringUtils::insert_values(_("Apply %dx%d"), + m_sizes[m_curr_res].first, + m_sizes[m_curr_res].second); widget_manager->setWgtText(WTOK_APPLY_RES, msg); widget_manager->activateWgt(WTOK_APPLY_RES); @@ -391,9 +389,9 @@ bool ConfigDisplay::isBlacklisted(int width, int height) void ConfigDisplay::showBlacklistButtons() { //change Apply button to Blacklisted button - char msg [MAX_MESSAGE_LENGTH]; - snprintf(msg, MAX_MESSAGE_LENGTH, _("%dx%d Blacklisted"), - m_sizes[m_curr_res].first,m_sizes[m_curr_res].second); + std::string msg = StringUtils::insert_values(_("%dx%d Blacklisted"), + m_sizes[m_curr_res].first, + m_sizes[m_curr_res].second); widget_manager->setWgtText(WTOK_APPLY_RES, msg); widget_manager->deactivateWgt(WTOK_APPLY_RES); diff --git a/src/gui/display_res_confirm.cpp b/src/gui/display_res_confirm.cpp index 2e41d4b58..8000eb454 100644 --- a/src/gui/display_res_confirm.cpp +++ b/src/gui/display_res_confirm.cpp @@ -26,11 +26,7 @@ #include "gui/menu_manager.hpp" #include "gui/widget_manager.hpp" #include "utils/translation.hpp" - - -#if defined(WIN32) && !defined(__CYGWIN__) -# define snprintf _snprintf -#endif +#include "utils/string_utils.hpp" enum WidgetTokens { @@ -107,7 +103,8 @@ void DisplayResConfirm::countdown() if (m_counter > 1) { m_counter--; - snprintf(m_count, MAX_MESSAGE_LENGTH, _("Confirm Resolution Within %d Seconds"), m_counter); + m_count = StringUtils::insert_values(_("Confirm Resolution Within %d Seconds"), + m_counter); widget_manager->setWgtText( WTOK_TITLE, m_count ); } else diff --git a/src/gui/display_res_confirm.hpp b/src/gui/display_res_confirm.hpp index 42f5b5640..b5eb6dff4 100644 --- a/src/gui/display_res_confirm.hpp +++ b/src/gui/display_res_confirm.hpp @@ -17,8 +17,8 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#ifndef HEADER_DISPLAY_RES_CONFIRM_H -#define HEADER_DISPLAY_RES_CONFIRM_H +#ifndef HEADER_DISPLAY_RES_CONFIRM_HPP +#define HEADER_DISPLAY_RES_CONFIRM_HPP #include "base_gui.hpp" @@ -35,9 +35,9 @@ public: void handle(GameAction ga, int value); private: - char m_count[60]; - int m_counter; - const bool FROM_WINDOW; + std::string m_count; + int m_counter; + const bool FROM_WINDOW; SDL_TimerID m_timer; }; diff --git a/src/gui/grand_prix_ending.cpp b/src/gui/grand_prix_ending.cpp index f8116ac1a..94b0cf7df 100644 --- a/src/gui/grand_prix_ending.cpp +++ b/src/gui/grand_prix_ending.cpp @@ -21,9 +21,6 @@ #include #include -#if defined(WIN32) && !defined(__CYGWIN__) -# define snprintf _snprintf -#endif #include @@ -41,7 +38,7 @@ #include "karts/kart_properties.hpp" #include "karts/kart_properties_manager.hpp" #include "utils/translation.hpp" -#include "graphics/scene.hpp" +#include "utils/string_utils.hpp" enum WidgetTokens { @@ -80,9 +77,8 @@ GrandPrixEnd::GrandPrixEnd() } } - static char output[MAX_MESSAGE_LENGTH]; - snprintf(output, sizeof(output), - _("The winner is %s!"),race_manager->getKartName(winner_kart_id).c_str()); // FIXME - uses inner-name and not user name + std::string output = StringUtils::insert_values(_("The winner is %s!"), + race_manager->getKartName(winner_kart_id)); widget_manager->addWgt( WTOK_TITLE, 60, 10); widget_manager->showWgtRect(WTOK_TITLE); widget_manager->showWgtText(WTOK_TITLE); diff --git a/src/gui/player_controls.cpp b/src/gui/player_controls.cpp index 8bb86f36f..4e700b007 100644 --- a/src/gui/player_controls.cpp +++ b/src/gui/player_controls.cpp @@ -20,15 +20,13 @@ #include "gui/player_controls.hpp" #include -#if defined(WIN32) && !defined(__CYGWIN__) -# define snprintf _snprintf -#endif #include "sdldrv.hpp" #include "user_config.hpp" #include "gui/menu_manager.hpp" #include "gui/widget_manager.hpp" #include "utils/translation.hpp" +#include "utils/string_utils.hpp" enum WidgetTokens { @@ -81,14 +79,11 @@ const char *sKartAction2String[KA_LAST+1] = PlayerControls::PlayerControls(int whichPlayer): m_player_index(whichPlayer), m_grab_input(false) -{ - // This is quite difficult to convert to using ustringstream, since the - // position of %s in the string might vary from language to language. - char heading[MAX_MESSAGE_LENGTH]; - snprintf(heading, sizeof(heading), _("Choose your controls, %s"), - user_config->m_player[m_player_index].getName().c_str() ); +{ + std::string s = StringUtils::insert_values( _("Choose your controls, %s"), + user_config->m_player[m_player_index].getName()); - widget_manager->addTitleWgt( WTOK_TITLE, 60, 7, heading ); + widget_manager->addTitleWgt( WTOK_TITLE, 60, 7, s); widget_manager->hideWgtRect(WTOK_TITLE); widget_manager->breakLine(); diff --git a/src/lisp/parser.cpp b/src/lisp/parser.cpp index efd4b9467..2b30d06e8 100644 --- a/src/lisp/parser.cpp +++ b/src/lisp/parser.cpp @@ -25,10 +25,6 @@ #include "lisp/lisp.hpp" #include "utils/translation.hpp" -#if defined(WIN32) && !defined(__CYGWIN__) -# define snprintf _snprintf -#endif - namespace lisp { @@ -51,11 +47,9 @@ namespace lisp std::ifstream in(filename.c_str()); if(!in.good()) { - char msg[MAX_ERROR_MESSAGE_LENGTH]; - snprintf(msg, sizeof(msg), - "Couldn't open file '%s'.", - filename.c_str()); - throw std::runtime_error(msg); + std::ostringstream msg; + msg << "Couldn't open file '" << filename << "'."; + throw std::runtime_error(msg.str()); } return parse(in); } @@ -89,19 +83,17 @@ namespace lisp { case Lexer::TOKEN_EOF: { - char msg[MAX_ERROR_MESSAGE_LENGTH]; - snprintf(msg, sizeof(msg), - "Parse Error at line %d: Unexpected EOF.", - m_lexer->getLineNumber()); - throw std::runtime_error(msg); + std::ostringstream msg; + msg << "Parse Error at line " << m_lexer->getLineNumber() + << ": Unexpected EOF."; + throw std::runtime_error(msg.str()); } case Lexer::TOKEN_CLOSE_PAREN: { - char msg[MAX_ERROR_MESSAGE_LENGTH]; - snprintf(msg, sizeof(msg), - "Parse Error at line %d: Unexpected ')'.", - m_lexer->getLineNumber()); - throw std::runtime_error(msg); + std::ostringstream msg; + msg << "Parse Error at line " << m_lexer->getLineNumber() + << ": Unexpected ')'."; + throw std::runtime_error(msg.str()); } case Lexer::TOKEN_TRANSLATION: { @@ -110,11 +102,10 @@ namespace lisp Lisp* next=read(); if(next->getType()!=Lisp::TYPE_STRING) { - char msg[MAX_ERROR_MESSAGE_LENGTH]; - snprintf(msg, sizeof(msg), - "Parse Error at line %d: No string inside translation.", - m_lexer->getLineNumber()); - throw std::runtime_error(msg); + std::ostringstream msg; + msg << "Parse Error at line " << m_lexer->getLineNumber() + << ": No string inside translation."; + throw std::runtime_error(msg.str()); } const char* trans=_(next->m_v.m_string); const size_t LEN = strlen(trans) + 1; @@ -133,11 +124,10 @@ namespace lisp if(m_token != Lexer::TOKEN_CLOSE_PAREN) { - char msg[MAX_ERROR_MESSAGE_LENGTH]; - snprintf(msg, sizeof(msg), - "Parse Error at line %d: Expected ')'.", - m_lexer->getLineNumber()); - throw std::runtime_error(msg); + std::ostringstream msg; + msg << "Parse Error at line " << m_lexer->getLineNumber() + << ": Expected ')'."; + throw std::runtime_error(msg.str()); } break; case Lexer::TOKEN_SYMBOL: diff --git a/src/utils/translation.hpp b/src/utils/translation.hpp index 675c9fc08..95f1706b3 100644 --- a/src/utils/translation.hpp +++ b/src/utils/translation.hpp @@ -35,11 +35,6 @@ # define N_(String) String #endif -// This length is used for all translated error messages. -#define MAX_ERROR_MESSAGE_LENGTH 160 - -// This length is used for all normal messages -#define MAX_MESSAGE_LENGTH 80 class Translations { public: