diff --git a/module/Base/Shared.cpp b/module/Base/Shared.cpp index cb69873f..18e9b319 100644 --- a/module/Base/Shared.cpp +++ b/module/Base/Shared.cpp @@ -941,6 +941,56 @@ bool BuildFormatString(String & out, StackStrF & fmt, Uint32 arg, const String & return true; } +// ------------------------------------------------------------------------------------------------ +size_t PrintToStrF(String & out, CSStr str, ...) +{ + // Initialize the variable argument list + va_list args; + va_start(args, str); + // Forward to the actual implementation + const size_t r = PrintToStrFv(out, str, args); + // Finalize the variable argument list + va_end(args); + // Return result + return r; +} +// ------------------------------------------------------------------------------------------------ +size_t PrintToStrFv(String & out, CSStr str, va_list vl) +{ + va_list args; + // Backup original size to revert back if necessary + const size_t size = out.size(); + // The estimated buffer required + ssize_t len = 256; +begin: + // Do not modify the original va_list + va_copy(args, vl); + // Reserve the necessary space + out.resize(size + static_cast< size_t >(len), '\0'); + // Attempt to generate the specified string + int res = std::vsnprintf(&out[0] + size, len, str, args); + // Do we need more space? + if (res >= len) + { + // Adjust to required size + len = res + 1; + // Try again + goto begin; + } + // Did the format failed? + else if (res < 0) + { + // Discard changes + out.resize(size); + } + else + { + // Discard extra characters + out.resize(size + static_cast< size_t >(res)); + } + // Return the amount of written characters + return static_cast< size_t >(res); +} // ------------------------------------------------------------------------------------------------ void SqThrowLastF(CSStr msg, ...) { diff --git a/module/Base/Shared.hpp b/module/Base/Shared.hpp index 3ce2fb0d..a8f8741f 100644 --- a/module/Base/Shared.hpp +++ b/module/Base/Shared.hpp @@ -175,4 +175,14 @@ template < typename T > inline void SqSetDelimiter(SQInteger c) */ bool BuildFormatString(String & out, StackStrF & fmt, Uint32 arg, const String & spec); +/* ------------------------------------------------------------------------------------------------ + * Append a formatted string to a string container. +*/ +size_t PrintToStrF(String & out, CSStr str, ...); + +/* ------------------------------------------------------------------------------------------------ + * Append a formatted string to a string container. +*/ +size_t PrintToStrFv(String & out, CSStr str, va_list vl); + } // Namespace:: SqMod