From 1ab177757bec9eb28482785f75fd8a4fb8a1313c Mon Sep 17 00:00:00 2001 From: Adam Borowski Date: Thu, 17 Dec 2009 12:48:02 +0100 Subject: Replace static snprintf() with make_sprintf() where the result is immediately cast to std::string. --- crawl-ref/source/clua.cc | 4 +--- crawl-ref/source/files.cc | 4 +--- crawl-ref/source/format.cc | 5 +---- crawl-ref/source/hiscores.cc | 10 ++++------ crawl-ref/source/items.cc | 15 +++++---------- crawl-ref/source/message.cc | 1 + crawl-ref/source/mon-act.cc | 7 ++----- crawl-ref/source/ouch.cc | 3 +-- crawl-ref/source/place.cc | 11 ++++------- crawl-ref/source/shopping.cc | 15 +++++---------- crawl-ref/source/stash.cc | 15 +++------------ crawl-ref/source/stuff.cc | 4 +--- crawl-ref/source/tags.cc | 9 ++------- crawl-ref/source/wiz-fsim.cc | 4 +--- 14 files changed, 32 insertions(+), 75 deletions(-) (limited to 'crawl-ref') diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc index b30e84dc1e..2cd15a4bbf 100644 --- a/crawl-ref/source/clua.cc +++ b/crawl-ref/source/clua.cc @@ -839,9 +839,7 @@ void lua_text_pattern::post_pattern(std::string &pat, std::string &fn) const std::string lua_text_pattern::new_fn_name() { - char buf[100]; - snprintf(buf, sizeof buf, "__ch_stash_search_%lu", lfndx++); - return (buf); + return (make_stringf("__ch_stash_search_%lu", lfndx++)); } bool lua_text_pattern::translate() const diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index b05bbde518..45a0664a5b 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -136,9 +136,7 @@ static void _redraw_all(void) static std::string _uid_as_string() { #ifdef MULTIUSER - char struid[20]; - snprintf( struid, sizeof struid, "-%d", static_cast(getuid()) ); - return std::string(struid); + return make_stringf("-%d", static_cast(getuid())); #else return std::string(); #endif diff --git a/crawl-ref/source/format.cc b/crawl-ref/source/format.cc index a65613adff..59fc8152bf 100644 --- a/crawl-ref/source/format.cc +++ b/crawl-ref/source/format.cc @@ -514,13 +514,10 @@ void formatted_string::clear() void formatted_string::cprintf(const char *s, ...) { - char buf[1000]; va_list args; va_start(args, s); - vsnprintf(buf, sizeof buf, s, args); + cprintf(make_stringf(s, args)); va_end(args); - - cprintf(std::string(buf)); } void formatted_string::cprintf(const std::string &s) diff --git a/crawl-ref/source/hiscores.cc b/crawl-ref/source/hiscores.cc index 81bf6479e8..cbbd3b518c 100644 --- a/crawl-ref/source/hiscores.cc +++ b/crawl-ref/source/hiscores.cc @@ -1430,10 +1430,9 @@ std::string scorefile_entry::death_place(death_desc_verbosity verbosity) const if (verbosity == DDV_ONELINE || verbosity == DDV_TERSE) { - snprintf( scratch, sizeof scratch, " (%s)", - place_name(get_packed_place(branch, dlvl, level_type), - false, true).c_str()); - return (scratch); + return (make_stringf(" (%s)", + place_name(get_packed_place(branch, dlvl, level_type), + false, true).c_str())); } if (verbose && death_type != KILLED_BY_QUITTING) @@ -2122,10 +2121,9 @@ void xlog_fields::add_field(const std::string &key, const char *format, ...) { - char buf[500]; va_list args; va_start(args, format); - vsnprintf(buf, sizeof buf, format, args); + std::string buf = make_stringf(format, args); va_end(args); fields.push_back( std::pair( key, buf ) ); diff --git a/crawl-ref/source/items.cc b/crawl-ref/source/items.cc index a167165398..a3418761d4 100644 --- a/crawl-ref/source/items.cc +++ b/crawl-ref/source/items.cc @@ -2010,7 +2010,6 @@ bool drop_item( int item_dropped, int quant_drop, bool try_offer ) static std::string _drop_menu_invstatus(const Menu *menu) { - char buf[100]; const int cap = carrying_capacity(BS_UNENCUMBERED); std::string s_newweight; @@ -2024,15 +2023,13 @@ static std::string _drop_menu_invstatus(const Menu *menu) newweight -= item_mass(*item) * se[i]->selected_qty; } - snprintf(buf, sizeof buf, ">%.0f", newweight * BURDEN_TO_AUM); - s_newweight = buf; + s_newweight = make_stringf(">%.0f", newweight * BURDEN_TO_AUM); } - snprintf(buf, sizeof buf, "(Inv: %.0f%s/%.0f aum)", + return (make_stringf("(Inv: %.0f%s/%.0f aum)", you.burden * BURDEN_TO_AUM, s_newweight.c_str(), - cap * BURDEN_TO_AUM); - return (buf); + cap * BURDEN_TO_AUM)); } static std::string _drop_menu_title(const Menu *menu, const std::string &oldt) @@ -2081,7 +2078,6 @@ mon_inv_type get_mon_equip_slot(const monsters* mon, const item_def &item) static std::string _drop_selitem_text( const std::vector *s ) { - char buf[130]; bool extraturns = false; if (s->empty()) @@ -2098,11 +2094,10 @@ static std::string _drop_selitem_text( const std::vector *s ) } } - snprintf( buf, sizeof buf, " (%lu%s turn%s)", + return (make_stringf( " (%lu%s turn%s)", (unsigned long) (s->size()), extraturns? "+" : "", - s->size() > 1? "s" : "" ); - return buf; + s->size() > 1? "s" : "" )); } std::vector items_for_multidrop; diff --git a/crawl-ref/source/message.cc b/crawl-ref/source/message.cc index d0fc3e7e9f..c2fce04242 100644 --- a/crawl-ref/source/message.cc +++ b/crawl-ref/source/message.cc @@ -413,6 +413,7 @@ int channel_to_colour( msg_channel_type channel, int param ) static void do_message_print( msg_channel_type channel, int param, const char *format, va_list argp ) { + // Is this limit intentional? char buff[200]; vsnprintf( buff, sizeof( buff ), format, argp ); buff[199] = 0; diff --git a/crawl-ref/source/mon-act.cc b/crawl-ref/source/mon-act.cc index 4e866cec38..16c025e217 100644 --- a/crawl-ref/source/mon-act.cc +++ b/crawl-ref/source/mon-act.cc @@ -1324,22 +1324,19 @@ static bool _mons_throw(struct monsters *monster, struct bolt &pbolt, // [dshaligram] When changing bolt names here, you must edit // hiscores.cc (scorefile_entry::terse_missile_cause()) to match. - char throw_buff[ITEMNAME_SIZE]; if (projected == LRET_LAUNCHED) { - snprintf(throw_buff, sizeof(throw_buff), "Shot with a%s %s by %s", + pbolt.aux_source = make_stringf("Shot with a%s %s by %s", (is_vowel(pbolt.name[0]) ? "n" : ""), pbolt.name.c_str(), monster->name(DESC_NOCAP_A).c_str()); } else { - snprintf(throw_buff, sizeof(throw_buff), "Hit by a%s %s thrown by %s", + pbolt.aux_source = make_stringf("Hit by a%s %s thrown by %s", (is_vowel(pbolt.name[0]) ? "n" : ""), pbolt.name.c_str(), monster->name(DESC_NOCAP_A).c_str()); } - pbolt.aux_source = throw_buff; - // Add everything up. pbolt.hit = baseHit + random2avg(exHitBonus, 2) + ammoHitBonus; pbolt.damage = diff --git a/crawl-ref/source/ouch.cc b/crawl-ref/source/ouch.cc index 93eaacd48b..1c7d94cf1f 100644 --- a/crawl-ref/source/ouch.cc +++ b/crawl-ref/source/ouch.cc @@ -1082,8 +1082,7 @@ void ouch(int dam, int death_source, kill_method_type death_type, std::string damage_desc; if (!see_source) { - snprintf(info, INFO_SIZE, "something (%d)", dam); - damage_desc = info; + damage_desc = make_stringf("something (%d)", dam); } else { diff --git a/crawl-ref/source/place.cc b/crawl-ref/source/place.cc index a07929b723..03fffbd370 100644 --- a/crawl-ref/source/place.cc +++ b/crawl-ref/source/place.cc @@ -114,21 +114,18 @@ std::string place_name( unsigned short place, bool long_name, if (include_number && branches[branch].depth != 1) { - char buf[200]; if (long_name) { // decapitalise 'the' if ( result.find("The") == 0 ) result[0] = 't'; - snprintf( buf, sizeof buf, "Level %d of %s", - lev, result.c_str() ); + result = make_stringf("Level %d of %s", + lev, result.c_str()); } else if (lev) - snprintf( buf, sizeof buf, "%s:%d", result.c_str(), lev ); + result = make_stringf("%s:%d", result.c_str(), lev); else - snprintf( buf, sizeof buf, "%s:$", result.c_str() ); - - result = buf; + result = make_stringf("%s:$", result.c_str()); } return result; } diff --git a/crawl-ref/source/shopping.cc b/crawl-ref/source/shopping.cc index d117dc9238..1aac684a98 100644 --- a/crawl-ref/source/shopping.cc +++ b/crawl-ref/source/shopping.cc @@ -124,7 +124,6 @@ static void _list_shop_keys(const std::string &purchasable, bool viewing, { ASSERT(total_stock > 0); - char buf[200]; const int numlines = get_number_of_lines(); cgotoxy(1, numlines - 1, GOTO_CRT); @@ -159,7 +158,7 @@ static void _list_shop_keys(const std::string &purchasable, bool viewing, pkeys = "[" + pkeys + "] Select Item to " + (viewing ? "Examine" : "Buy"); } - snprintf(buf, sizeof buf, + formatted_string fs = formatted_string::parse_string(make_stringf( "[x/Esc" #ifdef USE_TILE "/R-Click" @@ -167,9 +166,8 @@ static void _list_shop_keys(const std::string &purchasable, bool viewing, "] exit [!] %s %s%s", (viewing ? "to select items " : "to examine items"), pkeys.c_str(), - shop_list.c_str()); + shop_list.c_str())); - formatted_string fs = formatted_string::parse_string(buf); fs.cprintf("%*s", get_number_of_cols() - fs.length() - 1, ""); fs.display(); cgotoxy(1, numlines, GOTO_CRT); @@ -2526,12 +2524,9 @@ void ShoppingListMenu::draw_title() const char *verb = menu_action == ACT_EXECUTE ? "travel" : menu_action == ACT_EXAMINE ? "examine" : "delete"; - char buf[200]; - snprintf(buf, 200, - " [a-z: %s ?/!: change action]", - verb); - - draw_title_suffix(formatted_string::parse_string(buf), false); + draw_title_suffix(formatted_string::parse_string(make_stringf( + " [a-z: %s ?/!: change action]", + verb)), false); } } diff --git a/crawl-ref/source/stash.cc b/crawl-ref/source/stash.cc index cb3a461b61..147dbc67d8 100644 --- a/crawl-ref/source/stash.cc +++ b/crawl-ref/source/stash.cc @@ -887,21 +887,15 @@ void ShopInfo::add_item(const item_def &sitem, unsigned price) std::string ShopInfo::shop_item_name(const shop_item &si) const { - char shopitem[ITEMNAME_SIZE * 2]; - const unsigned long oldflags = si.item.flags; if (shoptype_identifies_stock(static_cast(this->shoptype))) const_cast(si).item.flags |= ISFLAG_IDENT_MASK; - const std::string itemname = Stash::stash_item_name(si.item); - snprintf(shopitem, sizeof shopitem, "%s (%u gold)", - itemname.c_str(), si.price); - if (oldflags != si.item.flags) const_cast(si).item.flags = oldflags; - return shopitem; + return make_stringf("%s (%u gold)", Stash::stash_item_name(si.item).c_str(), si.price); } std::string ShopInfo::shop_item_desc(const shop_item &si) const @@ -1871,12 +1865,9 @@ void StashSearchMenu::draw_title() title->quantity > 1? "es" : "", sort_style); - char buf[200]; - snprintf(buf, 200, + draw_title_suffix(formatted_string::parse_string(make_stringf( " [a-z: %s ?/!: change action /: change sort]", - menu_action == ACT_EXECUTE ? "travel" : "examine"); - - draw_title_suffix(formatted_string::parse_string(buf), false); + menu_action == ACT_EXECUTE ? "travel" : "examine")), false); } } diff --git a/crawl-ref/source/stuff.cc b/crawl-ref/source/stuff.cc index 15defe291c..2e55a337ab 100644 --- a/crawl-ref/source/stuff.cc +++ b/crawl-ref/source/stuff.cc @@ -149,15 +149,13 @@ std::string make_file_time(time_t when) { if (tm *loc = TIME_FN(&when)) { - char buf[25]; - snprintf(buf, sizeof buf, "%04d%02d%02d-%02d%02d%02d", + return make_stringf("%04d%02d%02d-%02d%02d%02d", loc->tm_year + 1900, loc->tm_mon + 1, loc->tm_mday, loc->tm_hour, loc->tm_min, loc->tm_sec); - return (buf); } return (""); } diff --git a/crawl-ref/source/tags.cc b/crawl-ref/source/tags.cc index f0d1707dd6..ab60932b4f 100644 --- a/crawl-ref/source/tags.cc +++ b/crawl-ref/source/tags.cc @@ -603,23 +603,18 @@ bool unmarshallBoolean(reader &th) // Saving the date as a string so we're not reliant on a particular epoch. std::string make_date_string( time_t in_date ) { - char buff[20]; - if (in_date <= 0) { - buff[0] = 0; - return (buff); + return (""); } struct tm *date = TIME_FN( &in_date ); - snprintf( buff, sizeof buff, + return make_stringf( "%4d%02d%02d%02d%02d%02d%s", date->tm_year + 1900, date->tm_mon, date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec, ((date->tm_isdst > 0) ? "D" : "S") ); - - return (buff); } void marshallEnumVal(writer& wr, const enum_info *ei, int val) diff --git a/crawl-ref/source/wiz-fsim.cc b/crawl-ref/source/wiz-fsim.cc index 05ea5703dc..8a9929cdc8 100644 --- a/crawl-ref/source/wiz-fsim.cc +++ b/crawl-ref/source/wiz-fsim.cc @@ -300,15 +300,13 @@ static std::string _fsim_time_string() struct tm *ltime = TIME_FN(&curr_time); if (ltime) { - char buf[100]; - snprintf(buf, sizeof buf, "%4d%02d%02d/%2d:%02d:%02d", + return make_stringf("%4d%02d%02d/%2d:%02d:%02d", ltime->tm_year + 1900, ltime->tm_mon + 1, ltime->tm_mday, ltime->tm_hour, ltime->tm_min, ltime->tm_sec); - return (buf); } return (""); } -- cgit v1.2.3-54-g00ecf