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  // Force null termination in error cases
24  destination[0] = 0;
25  // Check format string
26  if (formatString == nullptr) {
28  }
29  // Must allow the compiler to choose the correct type for comparison
30  else if (maximumSize > std::numeric_limits<size_t>::max()) {
31  formatStatus = Fw::FormatStatus::SIZE_OVERFLOW;
32  } else {
33  int needed_size = vsnprintf(destination, static_cast<size_t>(maximumSize), formatString, args);
34  destination[maximumSize - 1] = 0; // Force null-termination
35  if (needed_size < 0) {
36  formatStatus = Fw::FormatStatus::OTHER_ERROR;
37  } else if (static_cast<FwSizeType>(needed_size) >= maximumSize) {
38  formatStatus = Fw::FormatStatus::OVERFLOWED;
39  }
40  }
41  return formatStatus;
42 }
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