Codechange: use string/fmt instead of printf for squirrel's PRINTFUNC

pull/544/head
Rubidium 1 year ago committed by rubidium42
parent 075671bcfc
commit 8b93e45e22

@ -174,7 +174,7 @@ typedef SQObject HSQOBJECT;
typedef SQInteger (*SQFUNCTION)(HSQUIRRELVM);
typedef SQInteger (*SQRELEASEHOOK)(SQUserPointer,SQInteger size);
typedef void (*SQCOMPILERERROR)(HSQUIRRELVM,const SQChar * /*desc*/,const SQChar * /*source*/,SQInteger /*line*/,SQInteger /*column*/);
typedef void (*SQPRINTFUNCTION)(HSQUIRRELVM,const SQChar * ,...);
typedef void (*SQPRINTFUNCTION)(HSQUIRRELVM,const std::string &);
typedef SQInteger (*SQWRITEFUNC)(SQUserPointer,SQUserPointer,SQInteger);
typedef SQInteger (*SQREADFUNC)(SQUserPointer,SQUserPointer,SQInteger);

@ -5,6 +5,7 @@
#include <squirrel.h>
#include <sqstdaux.h>
#include "../../fmt/format.h"
#include "../../../safeguards.h"
void sqstd_printcallstack(HSQUIRRELVM v)
@ -38,7 +39,7 @@ void sqstd_printcallstack(HSQUIRRELVM v)
src = si.source;
}
}
pf(v,"*FUNCTION [%s()] %s line [" OTTD_PRINTF64 "]\n",fn,src,si.line);
pf(v,fmt::format("*FUNCTION [{}()] {} line [{}]\n",fn,src,si.line));
level++;
}
level=0;
@ -52,56 +53,56 @@ void sqstd_printcallstack(HSQUIRRELVM v)
switch(sq_gettype(v,-1))
{
case OT_NULL:
pf(v,"[%s] NULL\n",name);
pf(v,fmt::format("[{}] NULL\n",name));
break;
case OT_INTEGER:
sq_getinteger(v,-1,&i);
pf(v,"[%s] " OTTD_PRINTF64 "\n",name,i);
pf(v,fmt::format("[{}] {}\n",name,i));
break;
case OT_FLOAT:
sq_getfloat(v,-1,&f);
pf(v,"[%s] %.14g\n",name,f);
pf(v,fmt::format("[{}] {:14g}\n",name,f));
break;
case OT_USERPOINTER:
pf(v,"[%s] USERPOINTER\n",name);
pf(v,fmt::format("[{}] USERPOINTER\n",name));
break;
case OT_STRING:
sq_getstring(v,-1,&s);
pf(v,"[%s] \"%s\"\n",name,s);
pf(v,fmt::format("[{}] \"{}\"\n",name,s));
break;
case OT_TABLE:
pf(v,"[%s] TABLE\n",name);
pf(v,fmt::format("[{}] TABLE\n",name));
break;
case OT_ARRAY:
pf(v,"[%s] ARRAY\n",name);
pf(v,fmt::format("[{}] ARRAY\n",name));
break;
case OT_CLOSURE:
pf(v,"[%s] CLOSURE\n",name);
pf(v,fmt::format("[{}] CLOSURE\n",name));
break;
case OT_NATIVECLOSURE:
pf(v,"[%s] NATIVECLOSURE\n",name);
pf(v,fmt::format("[{}] NATIVECLOSURE\n",name));
break;
case OT_GENERATOR:
pf(v,"[%s] GENERATOR\n",name);
pf(v,fmt::format("[{}] GENERATOR\n",name));
break;
case OT_USERDATA:
pf(v,"[%s] USERDATA\n",name);
pf(v,fmt::format("[{}] USERDATA\n",name));
break;
case OT_THREAD:
pf(v,"[%s] THREAD\n",name);
pf(v,fmt::format("[{}] THREAD\n",name));
break;
case OT_CLASS:
pf(v,"[%s] CLASS\n",name);
pf(v,fmt::format("[{}] CLASS\n",name));
break;
case OT_INSTANCE:
pf(v,"[%s] INSTANCE\n",name);
pf(v,fmt::format("[{}] INSTANCE\n",name));
break;
case OT_WEAKREF:
pf(v,"[%s] WEAKREF\n",name);
pf(v,fmt::format("[{}] WEAKREF\n",name));
break;
case OT_BOOL:{
sq_getbool(v,-1,&b);
pf(v,"[%s] %s\n",name,b?"true":"false");
pf(v,fmt::format("[{}] {}\n",name,b?"true":"false"));
}
break;
default: assert(0); break;
@ -119,7 +120,7 @@ static SQInteger _sqstd_aux_printerror(HSQUIRRELVM v)
const SQChar *sErr = nullptr;
if(sq_gettop(v)>=1) {
if(SQ_SUCCEEDED(sq_getstring(v,2,&sErr))) {
pf(v,"\nAN ERROR HAS OCCURRED [%s]\n",sErr);
pf(v,fmt::format("\nAN ERROR HAS OCCURRED [{}]\n",sErr));
}
else{
pf(v,"\nAN ERROR HAS OCCURRED [unknown]\n");
@ -134,7 +135,7 @@ void _sqstd_compiler_error(HSQUIRRELVM v,const SQChar *sErr,const SQChar *sSourc
{
SQPRINTFUNCTION pf = sq_getprintfunc(v);
if(pf) {
pf(v,"%s line = (" OTTD_PRINTF64 ") column = (" OTTD_PRINTF64 ") : error %s\n",sSource,line,column,sErr);
pf(v,fmt::format("{} line = ({}) column = ({}) : error {}\n",sSource,line,column,sErr));
}
}

@ -172,7 +172,7 @@ static SQInteger base_print(HSQUIRRELVM v)
const SQChar *str;
sq_tostring(v,2);
sq_getstring(v,-1,&str);
if(_ss(v)->_printfunc) _ss(v)->_printfunc(v,"%s",str);
if(_ss(v)->_printfunc) _ss(v)->_printfunc(v,str);
return 0;
}

@ -4,6 +4,8 @@
#include "../../../stdafx.h"
#include "../../fmt/format.h"
#include <squirrel.h>
#include "sqpcheader.h"
#include <stdarg.h>
@ -65,16 +67,11 @@ public:
}
NORETURN static void ThrowError(void *ud, const SQChar *s) {
SQCompiler *c = (SQCompiler *)ud;
c->Error("%s", s);
c->Error(s);
}
NORETURN void Error(const SQChar *s, ...) WARN_FORMAT(2, 3)
NORETURN void Error(const std::string &msg)
{
static SQChar temp[256];
va_list vl;
va_start(vl, s);
vseprintf(temp, lastof(temp), s, vl);
va_end(vl);
throw temp;
throw msg;
}
void Lex(){ _token = _lex.Lex();}
void PushExpState(){ _expstates.push_back(ExpState()); }
@ -120,9 +117,9 @@ public:
default:
etypename = _lex.Tok2Str(tok);
}
Error("expected '%s'", etypename);
Error(fmt::format("expected '{}'", etypename));
}
Error("expected '%c'", (char)tok);
Error(fmt::format("expected '{:c}'", tok));
}
}
SQObjectPtr ret;
@ -647,7 +644,7 @@ public:
Expect('.'); constid = Expect(TK_IDENTIFIER);
if(!_table(constant)->Get(constid,constval)) {
constval.Null();
Error("invalid constant [%s.%s]", _stringval(id),_stringval(constid));
Error(fmt::format("invalid constant [{}.{}]", _stringval(id),_stringval(constid)));
}
}
else {

@ -44,10 +44,10 @@ ScriptStorage::~ScriptStorage()
* @param error_msg Is this an error message?
* @param message The actual message text.
*/
static void PrintFunc(bool error_msg, const SQChar *message)
static void PrintFunc(bool error_msg, const std::string &message)
{
/* Convert to OpenTTD internal capable string */
ScriptController::Print(error_msg, message);
ScriptController::Print(error_msg, message.c_str());
}
ScriptInstance::ScriptInstance(const char *APIName) :

@ -208,36 +208,27 @@ size_t Squirrel::GetAllocatedMemory() const noexcept
void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
{
SQChar buf[1024];
seprintf(buf, lastof(buf), "Error %s:" OTTD_PRINTF64 "/" OTTD_PRINTF64 ": %s", source, line, column, desc);
std::string msg = fmt::format("Error {}:{}/{}: {}", source, line, column, desc);
/* Check if we have a custom print function */
Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
engine->crashed = true;
SQPrintFunc *func = engine->print_func;
if (func == nullptr) {
Debug(misc, 0, "[Squirrel] Compile error: {}", buf);
Debug(misc, 0, "[Squirrel] Compile error: {}", msg);
} else {
(*func)(true, buf);
(*func)(true, msg);
}
}
void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s)
{
va_list arglist;
SQChar buf[1024];
va_start(arglist, s);
vseprintf(buf, lastof(buf), s, arglist);
va_end(arglist);
/* Check if we have a custom print function */
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
if (func == nullptr) {
fprintf(stderr, "%s", buf);
fprintf(stderr, "%s", s.c_str());
} else {
(*func)(true, buf);
(*func)(true, s);
}
}
@ -248,14 +239,13 @@ void Squirrel::RunError(HSQUIRRELVM vm, const SQChar *error)
sq_setprintfunc(vm, &Squirrel::ErrorPrintFunc);
/* Check if we have a custom print function */
SQChar buf[1024];
seprintf(buf, lastof(buf), "Your script made an error: %s\n", error);
std::string msg = fmt::format("Your script made an error: {}\n", error);
Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
SQPrintFunc *func = engine->print_func;
if (func == nullptr) {
fprintf(stderr, "%s", buf);
fprintf(stderr, "%s", msg.c_str());
} else {
(*func)(true, buf);
(*func)(true, msg);
}
/* Print below the error the stack, so the users knows what is happening */
@ -279,22 +269,14 @@ SQInteger Squirrel::_RunError(HSQUIRRELVM vm)
return 0;
}
void Squirrel::PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
void Squirrel::PrintFunc(HSQUIRRELVM vm, const std::string &s)
{
va_list arglist;
SQChar buf[1024];
va_start(arglist, s);
vseprintf(buf, lastof(buf) - 2, s, arglist);
va_end(arglist);
strecat(buf, "\n", lastof(buf));
/* Check if we have a custom print function */
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
if (func == nullptr) {
printf("%s", buf);
printf("%s", s.c_str());
} else {
(*func)(false, buf);
(*func)(false, s);
}
}

@ -24,7 +24,7 @@ class Squirrel {
friend class ScriptAllocatorScope;
private:
typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
typedef void (SQPrintFunc)(bool error_msg, const std::string &message);
HSQUIRRELVM vm; ///< The VirtualMachine instance for squirrel
void *global_pointer; ///< Can be set by who ever initializes Squirrel
@ -63,12 +63,12 @@ protected:
/**
* If a user runs 'print' inside a script, this function gets the params.
*/
static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...) WARN_FORMAT(2, 3);
static void PrintFunc(HSQUIRRELVM vm, const std::string &s);
/**
* If an error has to be print, this function is called.
*/
static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...) WARN_FORMAT(2, 3);
static void ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s);
public:
Squirrel(const char *APIName);

Loading…
Cancel
Save