Skip to content

Commit

Permalink
Adding Fw::StringUtils::format string format helper
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStarch committed Jan 16, 2025
1 parent 5191501 commit 302f62f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Fw/Types/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Fw/Types/Assert.hpp>
#include <cstring>
#include <limits>
#include <Fw/Types/ExternalString.hpp>

char* Fw::StringUtils::string_copy(char* destination, const char* source, FwSizeType num) {
// Handle self-copy and 0 bytes copy
Expand Down Expand Up @@ -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);
}
16 changes: 16 additions & 0 deletions Fw/Types/StringUtils.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef FW_STRINGUTILS_HPP
#define FW_STRINGUTILS_HPP
#include <FpConfig.hpp>
#include <Fw/Types/StringBase.hpp>

namespace Fw {
namespace StringUtils {
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Utils/CRCChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <Os/File.hpp>
#include <Os/FileSystem.hpp>
#include <Utils/Hash/Hash.hpp>
#include <Fw/Types/ExternalString.hpp>
#include <Fw/Types/StringUtils.hpp>

namespace Utils {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 302f62f

Please sign in to comment.