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 <limits>
8 #include <cstdio>
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 
18 Fw::FormatStatus Fw::stringFormat(char* destination, const FwSizeType maximumSize, const char* formatString, va_list args) {
20  // Force null termination in error cases
21  destination[0] = 0;
22  // Check format string
23  if (formatString == nullptr) {
25  }
26  // Must allow the compiler to choose the correct type for comparison
27  else if (maximumSize > std::numeric_limits<size_t>::max()) {
28  formatStatus = Fw::FormatStatus::SIZE_OVERFLOW;
29  } else {
30  PlatformIntType needed_size = vsnprintf(destination, static_cast<size_t>(maximumSize), formatString, args);
31  destination[maximumSize - 1] = 0; // Force null-termination
32  if (needed_size < 0) {
33  formatStatus = Fw::FormatStatus::OTHER_ERROR;
34  } else if (static_cast<FwSizeType>(needed_size) >= maximumSize) {
35  formatStatus = Fw::FormatStatus::OVERFLOWED;
36  }
37  }
38  return formatStatus;
39 }
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
Format overflowed.
FwSizeType overflowed the range of size_t.
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
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