mirror of https://github.com/OpenTTD/OpenTTD
Codechange: replace printf with PRINTF macros by fmt::format for scripts
parent
484883e6e1
commit
8f24901843
|
@ -76,15 +76,12 @@ void SQVM::Raise_Error(SQObjectPtr &desc)
|
||||||
|
|
||||||
SQString *SQVM::PrintObjVal(const SQObject &o)
|
SQString *SQVM::PrintObjVal(const SQObject &o)
|
||||||
{
|
{
|
||||||
char buf[NUMBER_MAX_CHAR+1];
|
|
||||||
switch(type(o)) {
|
switch(type(o)) {
|
||||||
case OT_STRING: return _string(o);
|
case OT_STRING: return _string(o);
|
||||||
case OT_INTEGER:
|
case OT_INTEGER:
|
||||||
seprintf(buf, lastof(buf), OTTD_PRINTF64, _integer(o));
|
return SQString::Create(_ss(this), fmt::format("{}", _integer(o)).c_str());
|
||||||
return SQString::Create(_ss(this), buf);
|
|
||||||
case OT_FLOAT:
|
case OT_FLOAT:
|
||||||
seprintf(buf, lastof(buf), "%.14g", _float(o));
|
return SQString::Create(_ss(this), fmt::format("{:.14g}", _float(o)).c_str());
|
||||||
return SQString::Create(_ss(this), buf);
|
|
||||||
default:
|
default:
|
||||||
return SQString::Create(_ss(this), GetTypeName(o));
|
return SQString::Create(_ss(this), GetTypeName(o));
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../../../stdafx.h"
|
#include "../../../stdafx.h"
|
||||||
|
#include "../../fmt/format.h"
|
||||||
|
|
||||||
#include "sqpcheader.h"
|
#include "sqpcheader.h"
|
||||||
#include "sqcompiler.h"
|
#include "sqcompiler.h"
|
||||||
|
@ -83,11 +84,11 @@ SQInstructionDesc g_InstrDesc[]={
|
||||||
void DumpLiteral(SQObjectPtr &o)
|
void DumpLiteral(SQObjectPtr &o)
|
||||||
{
|
{
|
||||||
switch(type(o)){
|
switch(type(o)){
|
||||||
case OT_STRING: printf("\"%s\"",_stringval(o));break;
|
case OT_STRING: fmt::print("\"{}\"",_stringval(o));break;
|
||||||
case OT_FLOAT: printf("{%f}",_float(o));break;
|
case OT_FLOAT: fmt::print("{{{}}}",_float(o));break;
|
||||||
case OT_INTEGER: printf("{" OTTD_PRINTF64 "}",_integer(o));break;
|
case OT_INTEGER: fmt::print("{{{}}}",_integer(o));break;
|
||||||
case OT_BOOL: printf("%s",_integer(o)?"true":"false");break;
|
case OT_BOOL: fmt::print(_integer(o)?"true":"false");break;
|
||||||
default: printf("(%s %p)",GetTypeName(o),(void*)_rawval(o));break; break; //shut up compiler
|
default: fmt::print("({} {})",GetTypeName(o),(size_t)(void*)_rawval(o));break; break; //shut up compiler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -262,19 +262,19 @@ bool SQVM::CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObject
|
||||||
|
|
||||||
void SQVM::ToString(const SQObjectPtr &o,SQObjectPtr &res)
|
void SQVM::ToString(const SQObjectPtr &o,SQObjectPtr &res)
|
||||||
{
|
{
|
||||||
char buf[64];
|
std::string str;
|
||||||
switch(type(o)) {
|
switch(type(o)) {
|
||||||
case OT_STRING:
|
case OT_STRING:
|
||||||
res = o;
|
res = o;
|
||||||
return;
|
return;
|
||||||
case OT_FLOAT:
|
case OT_FLOAT:
|
||||||
seprintf(buf, lastof(buf),"%g",_float(o));
|
str = fmt::format("{}",_float(o));
|
||||||
break;
|
break;
|
||||||
case OT_INTEGER:
|
case OT_INTEGER:
|
||||||
seprintf(buf, lastof(buf),OTTD_PRINTF64,_integer(o));
|
str = fmt::format("{}",_integer(o));
|
||||||
break;
|
break;
|
||||||
case OT_BOOL:
|
case OT_BOOL:
|
||||||
seprintf(buf, lastof(buf),_integer(o)?"true":"false");
|
str = _integer(o)?"true":"false";
|
||||||
break;
|
break;
|
||||||
case OT_TABLE:
|
case OT_TABLE:
|
||||||
case OT_USERDATA:
|
case OT_USERDATA:
|
||||||
|
@ -289,9 +289,9 @@ void SQVM::ToString(const SQObjectPtr &o,SQObjectPtr &res)
|
||||||
}
|
}
|
||||||
FALLTHROUGH;
|
FALLTHROUGH;
|
||||||
default:
|
default:
|
||||||
seprintf(buf, lastof(buf),"(%s : 0x%p)",GetTypeName(o),(void*)_rawval(o));
|
str = fmt::format("({} : 0x{:08X})",GetTypeName(o),(size_t)(void*)_rawval(o));
|
||||||
}
|
}
|
||||||
res = SQString::Create(_ss(this),buf);
|
res = SQString::Create(_ss(this),str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -196,7 +196,7 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||||
/* No more extra parameters, assume SQInteger are expected. */
|
/* No more extra parameters, assume SQInteger are expected. */
|
||||||
if (cur_idx >= this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
if (cur_idx >= this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
||||||
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
||||||
p += seprintf(p, lastofp, ":" OTTD_PRINTFHEX64, std::get<SQInteger>(this->param[cur_idx++]));
|
p = strecpy(p, fmt::format(":{:X}", std::get<SQInteger>(this->param[cur_idx++])).c_str(), lastofp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (prev_idx == prev_count) {
|
if (prev_idx == prev_count) {
|
||||||
|
@ -213,7 +213,7 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||||
case StringParam::STRING: {
|
case StringParam::STRING: {
|
||||||
if (!std::holds_alternative<ScriptTextRef>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a substring", name, param_count));
|
if (!std::holds_alternative<ScriptTextRef>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a substring", name, param_count));
|
||||||
int count = 0;
|
int count = 0;
|
||||||
p += seprintf(p, lastofp, ":");
|
p = strecpy(p, ":", lastofp);
|
||||||
p = std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(p, lastofp, count, seen_ids);
|
p = std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(p, lastofp, count, seen_ids);
|
||||||
if (++count != cur_param.consumes) {
|
if (++count != cur_param.consumes) {
|
||||||
ScriptLog::Error(fmt::format("{}: Parameter {} substring consumes {}, but expected {} to be consumed", name, param_count, count - 1, cur_param.consumes - 1).c_str());
|
ScriptLog::Error(fmt::format("{}: Parameter {} substring consumes {}, but expected {} to be consumed", name, param_count, count - 1, cur_param.consumes - 1).c_str());
|
||||||
|
@ -233,7 +233,7 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||||
if (cur_idx + cur_param.consumes > this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
if (cur_idx + cur_param.consumes > this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
||||||
for (int i = 0; i < cur_param.consumes; i++) {
|
for (int i = 0; i < cur_param.consumes; i++) {
|
||||||
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
||||||
p += seprintf(p, lastofp, ":" OTTD_PRINTFHEX64, std::get<SQInteger>(this->param[cur_idx++]));
|
p = strecpy(p, fmt::format(":{:X}", std::get<SQInteger>(this->param[cur_idx++])).c_str(), lastofp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,12 +74,11 @@ struct ScriptAllocator {
|
||||||
* already as then the allocation is for throwing that error in Squirrel, the
|
* already as then the allocation is for throwing that error in Squirrel, the
|
||||||
* associated stack trace information and while cleaning up the AI. */
|
* associated stack trace information and while cleaning up the AI. */
|
||||||
this->error_thrown = true;
|
this->error_thrown = true;
|
||||||
char buff[128];
|
std::string msg = fmt::format("Maximum memory allocation exceeded by {} bytes when allocating {} bytes",
|
||||||
seprintf(buff, lastof(buff), "Maximum memory allocation exceeded by " PRINTF_SIZE " bytes when allocating " PRINTF_SIZE " bytes",
|
|
||||||
this->allocated_size + requested_size - this->allocation_limit, requested_size);
|
this->allocated_size + requested_size - this->allocation_limit, requested_size);
|
||||||
/* Don't leak the rejected allocation. */
|
/* Don't leak the rejected allocation. */
|
||||||
free(p);
|
free(p);
|
||||||
throw Script_FatalError(buff);
|
throw Script_FatalError(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p == nullptr) {
|
if (p == nullptr) {
|
||||||
|
@ -93,9 +92,8 @@ struct ScriptAllocator {
|
||||||
}
|
}
|
||||||
|
|
||||||
this->error_thrown = true;
|
this->error_thrown = true;
|
||||||
char buff[64];
|
std::string msg = fmt::format("Out of memory. Cannot allocate {} bytes", requested_size);
|
||||||
seprintf(buff, lastof(buff), "Out of memory. Cannot allocate " PRINTF_SIZE " bytes", requested_size);
|
throw Script_FatalError(msg);
|
||||||
throw Script_FatalError(buff);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue