diff --git a/Fw/Types/StringUtils.cpp b/Fw/Types/StringUtils.cpp index 714b9d4a05..7901ce390b 100644 --- a/Fw/Types/StringUtils.cpp +++ b/Fw/Types/StringUtils.cpp @@ -2,6 +2,7 @@ #include #include #include +#include char* Fw::StringUtils::string_copy(char* destination, const char* source, FwSizeType num) { // Handle self-copy and 0 bytes copy @@ -72,3 +73,10 @@ FwSignedSizeType Fw::StringUtils::substring_find(const CHAR* source_string, // if we make it here, no matches were found return -1; } + +void Fw::StringUtils::format(CHAR* destination, Fw::StringBase::SizeType size, const CHAR* format, ...) { + va_list args; + va_start(args, format); + Fw::ExternalString(destination, size).vformat(format, args); + va_end(args); +} \ No newline at end of file diff --git a/Fw/Types/StringUtils.hpp b/Fw/Types/StringUtils.hpp index 5213dba8a7..ab05210e1a 100644 --- a/Fw/Types/StringUtils.hpp +++ b/Fw/Types/StringUtils.hpp @@ -1,6 +1,7 @@ #ifndef FW_STRINGUTILS_HPP #define FW_STRINGUTILS_HPP #include +#include namespace Fw { namespace StringUtils { @@ -44,6 +45,21 @@ FwSizeType string_length(const CHAR* source, FwSizeType buffer_size); */ FwSignedSizeType substring_find(const CHAR* source_string, FwSizeType source_size, const CHAR* sub_string, FwSizeType sub_size); +//! \brief format a string and store into a destination buffer +//! +//! This function will format a string into a destination buffer. The destination buffer must be large enough to hold +//! the formatted string. The format delegates to the standard C library function vsnprintf underneath the hood. +//! +//! \warning if used in execution (i.e. a file path), assert FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING. +//! ``` +//! static_assert(FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING, "String formatting disabled") +//! ``` +//! \param destination: destination buffer to hold formatted string +//! \param size: size of the destination buffer. Output will be truncated to this length +//! \param format: constant format string +//! \param ...: variable arguments to to pass to the format string +void format(CHAR* destination, StringBase::SizeType size, const CHAR* format, ...); + enum StringToNumberStatus { SUCCESSFUL_CONVERSION, //!< Output should be valid NULL_INPUT, //!< A null string was supplied diff --git a/Utils/CRCChecker.cpp b/Utils/CRCChecker.cpp index 5872c7a952..705477b4df 100644 --- a/Utils/CRCChecker.cpp +++ b/Utils/CRCChecker.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include namespace Utils { @@ -91,7 +90,7 @@ namespace Utils { // open checksum file FW_ASSERT(CRC_MAX_FILENAME_SIZE > (Fw::StringUtils::string_length(fname, CRC_MAX_FILENAME_SIZE) + sizeof HASH_EXTENSION_STRING)); - Fw::ExternalString(hashFilename, CRC_MAX_FILENAME_SIZE).format("%s%s", fname, HASH_EXTENSION_STRING); + Fw::StringUtils::format(hashFilename, CRC_MAX_FILENAME_SIZE, "%s%s", fname, HASH_EXTENSION_STRING); stat = f.open(hashFilename, Os::File::OPEN_WRITE); if(stat != Os::File::OP_OK) @@ -121,7 +120,7 @@ namespace Utils { FW_ASSERT(fname != nullptr); // open checksum file FW_ASSERT(CRC_MAX_FILENAME_SIZE > (Fw::StringUtils::string_length(fname, CRC_MAX_FILENAME_SIZE) + sizeof HASH_EXTENSION_STRING)); - Fw::ExternalString(hashFilename, CRC_MAX_FILENAME_SIZE).format("%s%s", fname, HASH_EXTENSION_STRING); + Fw::StringUtils::format(hashFilename, CRC_MAX_FILENAME_SIZE, "%s%s", fname, HASH_EXTENSION_STRING); stat = f.open(hashFilename, Os::File::OPEN_READ); if(stat != Os::File::OP_OK)