StringFormat converted to move semantics

This commit is contained in:
Arthur Ice 2014-07-08 18:57:54 -07:00 committed by Arthur Ice
parent 9a634a2056
commit 6a59b83d43
6 changed files with 27 additions and 36 deletions

View File

@ -175,14 +175,14 @@ void ImportSkillCaps(SharedDatabase *db) {
continue; continue;
} }
std::string sql;
int class_id, skill_id, level, cap; int class_id, skill_id, level, cap;
class_id = atoi(split[0].c_str()); class_id = atoi(split[0].c_str());
skill_id = atoi(split[1].c_str()); skill_id = atoi(split[1].c_str());
level = atoi(split[2].c_str()); level = atoi(split[2].c_str());
cap = atoi(split[3].c_str()); cap = atoi(split[3].c_str());
StringFormat(sql, "INSERT INTO skill_caps(class, skillID, level, cap) VALUES(%d, %d, %d, %d)", std::string sql = StringFormat("INSERT INTO skill_caps(class, skillID, level, cap) VALUES(%d, %d, %d, %d)",
class_id, skill_id, level, cap); class_id, skill_id, level, cap);
db->RunQuery(sql.c_str(), (uint32)sql.length()); db->RunQuery(sql.c_str(), (uint32)sql.length());
@ -226,7 +226,7 @@ void ImportBaseData(SharedDatabase *db) {
mana_fac = atof(split[8].c_str()); mana_fac = atof(split[8].c_str());
end_fac = atof(split[9].c_str()); end_fac = atof(split[9].c_str());
StringFormat(sql, "INSERT INTO base_data(level, class, hp, mana, end, unk1, unk2, hp_fac, " sql = StringFormat("INSERT INTO base_data(level, class, hp, mana, end, unk1, unk2, hp_fac, "
"mana_fac, end_fac) VALUES(%d, %d, %f, %f, %f, %f, %f, %f, %f, %f)", "mana_fac, end_fac) VALUES(%d, %d, %f, %f, %f, %f, %f, %f, %f, %f)",
level, class_id, hp, mana, end, unk1, unk2, hp_fac, mana_fac, end_fac); level, class_id, hp, mana, end, unk1, unk2, hp_fac, mana_fac, end_fac);

View File

@ -893,8 +893,7 @@ void Inventory::dumpItemCollection(const std::map<int16, ItemInst*> &collection)
if (!inst || !inst->GetItem()) if (!inst || !inst->GetItem())
continue; continue;
std::string slot; std::string slot = StringFormat("Slot %d: %s (%d)", it->first, it->second->GetItem()->Name, (inst->GetCharges() <= 0) ? 1 : inst->GetCharges());
StringFormat(slot, "Slot %d: %s (%d)", it->first, it->second->GetItem()->Name, (inst->GetCharges() <= 0) ? 1 : inst->GetCharges());
std::cout << slot << std::endl; std::cout << slot << std::endl;
dumpBagContents(inst, &it); dumpBagContents(inst, &it);
@ -913,8 +912,7 @@ void Inventory::dumpBagContents(ItemInst *inst, iter_inst *it) {
if (!baginst || !baginst->GetItem()) if (!baginst || !baginst->GetItem())
continue; continue;
std::string subSlot; std::string subSlot = StringFormat(" Slot %d: %s (%d)", Inventory::CalcSlotId((*it)->first, itb->first),
StringFormat(subSlot, " Slot %d: %s (%d)", Inventory::CalcSlotId((*it)->first, itb->first),
baginst->GetItem()->Name, (baginst->GetCharges() <= 0) ? 1 : baginst->GetCharges()); baginst->GetItem()->Name, (baginst->GetCharges() <= 0) ? 1 : baginst->GetCharges());
std::cout << subSlot << std::endl; std::cout << subSlot << std::endl;
} }

View File

@ -38,8 +38,9 @@
// original source: // original source:
// https://github.com/facebook/folly/blob/master/folly/String.cpp // https://github.com/facebook/folly/blob/master/folly/String.cpp
// //
void vStringFormat(std::string& output, const char* format, va_list args) const std::string vStringFormat(const char* format, va_list args)
{ {
std::string output;
va_list tmpargs; va_list tmpargs;
va_copy(tmpargs,args); va_copy(tmpargs,args);
@ -48,11 +49,8 @@ void vStringFormat(std::string& output, const char* format, va_list args)
if (characters_used < 0) { if (characters_used < 0) {
// Looks like we have an invalid format string. // Looks like we have an invalid format string.
// error out. // return empty string.
std::string errorMessage("Invalid format string; snprintf returned negative with format string: "); return "";
errorMessage.append(format);
throw std::runtime_error(errorMessage);
} }
else if ((unsigned int)characters_used > output.capacity()) { else if ((unsigned int)characters_used > output.capacity()) {
output.resize(characters_used+1); output.resize(characters_used+1);
@ -62,12 +60,10 @@ void vStringFormat(std::string& output, const char* format, va_list args)
if (characters_used < 0) { if (characters_used < 0) {
// We shouldn't have a format error by this point, but I can't imagine what error we // We shouldn't have a format error by this point, but I can't imagine what error we
// could have by this point. Still, error out and report it. // could have by this point. Still, return empty string;
std::string errorMessage("Invalid format string or unknown vsnprintf error; vsnprintf returned negative with format string: "); return "";
errorMessage.append(format);
throw std::runtime_error(errorMessage);
} }
return std::move(output);
} }
else { else {
output.resize(characters_used + 1); output.resize(characters_used + 1);
@ -78,24 +74,23 @@ void vStringFormat(std::string& output, const char* format, va_list args)
if (characters_used < 0) { if (characters_used < 0) {
// We shouldn't have a format error by this point, but I can't imagine what error we // We shouldn't have a format error by this point, but I can't imagine what error we
// could have by this point. still error out and report it. // could have by this point. Still, return empty string;
std::string errorMessage("Invalid format string or unknown vsnprintf error; vsnprintf returned negative with format string: "); return "";
errorMessage.append(format);
throw std::runtime_error(errorMessage);
} }
return std::move(output);
} }
} }
void StringFormat(std::string& output, const char* format, ...) const std::string StringFormat(const char* format, ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
vStringFormat(output,format,args); std::string output = vStringFormat(format,args);
va_end(args); va_end(args);
return std::move(output);
} }
// normal strncpy doesnt put a null term on copied strings, this one does // normal strncpy doesnt put a null term on copied strings, this one does
// ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_strncpy_wcsncpy.asp // ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_strncpy_wcsncpy.asp
char* strn0cpy(char* dest, const char* source, uint32 size) { char* strn0cpy(char* dest, const char* source, uint32 size) {

View File

@ -19,11 +19,12 @@
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <cstdarg> #include <cstdarg>
#include <string.h>
#include "types.h" #include "types.h"
void vStringFormat(std::string& output, const char* format, va_list args); const std::string vStringFormat(const char* format, va_list args);
void StringFormat(std::string& output, const char* format, ...); const std::string StringFormat(const char* format, ...);
std::string EscapeString(const std::string &s); std::string EscapeString(const std::string &s);
const char *MakeLowerString(const char *source); const char *MakeLowerString(const char *source);

View File

@ -33,9 +33,7 @@ void log_message(LogType type, const char *fmt, ...) {
} }
void log_messageVA(LogType type, const char *fmt, va_list args) { void log_messageVA(LogType type, const char *fmt, va_list args) {
std::string prefix_buffer; std::string prefix_buffer = StringFormat("[%s] ", log_type_info[type].name);
StringFormat(prefix_buffer, "[%s] ", log_type_info[type].name);
LogFile->writePVA(EQEMuLog::Debug, prefix_buffer.c_str(), fmt, args); LogFile->writePVA(EQEMuLog::Debug, prefix_buffer.c_str(), fmt, args);
} }

View File

@ -12,8 +12,7 @@
void log_message_clientVA(LogType type, Client *who, const char *fmt, va_list args) { void log_message_clientVA(LogType type, Client *who, const char *fmt, va_list args) {
std::string prefix_buffer; std::string prefix_buffer = StringFormat("[%s] %s: ", log_type_info[type].name, who->GetAccountName());
StringFormat(prefix_buffer,"[%s] %s: ", log_type_info[type].name, who->GetAccountName());
LogFile->writePVA(EQEMuLog::Debug, prefix_buffer.c_str(), fmt, args); LogFile->writePVA(EQEMuLog::Debug, prefix_buffer.c_str(), fmt, args);
} }
@ -31,11 +30,11 @@ void log_message_zoneVA(LogType type, ZoneServer *who, const char *fmt, va_list
const char *zone_name=who->GetZoneName(); const char *zone_name=who->GetZoneName();
if (zone_name == nullptr) if (zone_name == nullptr)
StringFormat(zone_tag,"[%d]", who->GetID()); zone_tag = StringFormat("[%d]", who->GetID());
else else
StringFormat(zone_tag,"[%d] [%s]",who->GetID(),zone_name); zone_tag = StringFormat("[%d] [%s]",who->GetID(),zone_name);
StringFormat(prefix_buffer, "[%s] %s ", log_type_info[type].name, zone_tag.c_str()); prefix_buffer = StringFormat("[%s] %s ", log_type_info[type].name, zone_tag.c_str());
LogFile->writePVA(EQEMuLog::Debug, prefix_buffer.c_str(), fmt, args); LogFile->writePVA(EQEMuLog::Debug, prefix_buffer.c_str(), fmt, args);
} }