From f048950d20f4bdfbe381b960e66a871685cb6882 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 16 Jun 2017 02:23:24 +0300 Subject: [PATCH] Implement a formatted Squirrel error throwing utility. --- shared/Base/Utility.hpp | 5 +++++ shared/Base/Utility.inl | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/shared/Base/Utility.hpp b/shared/Base/Utility.hpp index 39b7185d..3bd55477 100644 --- a/shared/Base/Utility.hpp +++ b/shared/Base/Utility.hpp @@ -98,6 +98,11 @@ CSStr ToStrF(CCStr str, ...); */ CSStr ToStringF(CCStr str, ...); +/* ------------------------------------------------------------------------------------------------ + * Generate a formatted string and throw it as a squirrel exception. +*/ +SQRESULT SqThrowErrorF(HSQUIRRELVM vm, CCStr str, ...); + /* ------------------------------------------------------------------------------------------------ * Simple function to check whether the specified string can be considered as a boolean value */ diff --git a/shared/Base/Utility.inl b/shared/Base/Utility.inl index 64d34663..c12e095a 100644 --- a/shared/Base/Utility.inl +++ b/shared/Base/Utility.inl @@ -208,6 +208,23 @@ CSStr ToStringF(CSStr str, ...) return b.Get< SQChar >(); } +// ------------------------------------------------------------------------------------------------ +SQRESULT SqThrowErrorF(HSQUIRRELVM vm, CCStr str, ...) +{ + // Prepare the arguments list + va_list args; + va_start(args, str); + // Write the requested contents + if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0) + { + return sq_throwerror(vm, _SC("Formatting error occurred while throwing a script error")); + } + // Finalize the argument list + va_end(args); + // Throw the resulted string + return sq_throwerror(vm, g_Buffer); +} + // ------------------------------------------------------------------------------------------------ bool SToB(CSStr str) {