F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
snprintf_format.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title format.cpp
3 // \author mstarch
4 // \brief cpp file for c-string format function as a implementation using snprintf
5 // ======================================================================
6 #include <Fw/Types/format.hpp>
7 #include <cstdio>
8 #include <limits>
9 
10 Fw::FormatStatus Fw::stringFormat(char* destination, const FwSizeType maximumSize, const char* formatString, ...) {
11  va_list args;
12  va_start(args, formatString);
13  FormatStatus status = Fw::stringFormat(destination, maximumSize, formatString, args);
14  va_end(args);
15  return status;
16 }
17 
19  const FwSizeType maximumSize,
20  const char* formatString,
21  va_list args) {
23  // Check destination pointer
24  if (destination == nullptr || maximumSize == 0) {
26  }
27  // Force null termination in error cases
28  destination[0] = 0;
29  // Check format string
30  if (formatString == nullptr) {
32  }
33  // Must allow the compiler to choose the correct type for comparison
34  else if (maximumSize > std::numeric_limits<size_t>::max()) {
35  formatStatus = Fw::FormatStatus::SIZE_OVERFLOW;
36  } else {
37  // Format string is intentionally a runtime parameter; suppressing static analysis warning
38  int needed_size = vsnprintf(destination, static_cast<size_t>(maximumSize), formatString, args); // NOLINT
39  destination[maximumSize - 1] = 0; // Force null-termination
40  if (needed_size < 0) {
41  formatStatus = Fw::FormatStatus::OTHER_ERROR;
42  } else if (static_cast<FwSizeType>(needed_size) >= maximumSize) {
43  formatStatus = Fw::FormatStatus::OVERFLOWED;
44  }
45  }
46  return formatStatus;
47 }
PlatformSizeType FwSizeType
Format overflowed.
FwSizeType overflowed the range of size_t.
Format provided invalid format string.
An error was returned from an underlying call.
FormatStatus stringFormat(char *destination, const FwSizeType maximumSize, const char *formatString,...)
format a c-string
FormatStatus
status of string format calls
Definition: format.hpp:18