F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
StringBase.cpp
Go to the documentation of this file.
1 
13 #include <Fw/Types/Assert.hpp>
14 #include <Fw/Types/StringType.hpp>
15 #include <Fw/Types/StringUtils.hpp>
16 #include <cstdarg>
17 #include <cstring>
18 
19 namespace Fw {
20 
22 
24 
25 const CHAR* StringBase::operator+=(const CHAR* src) {
26  this->appendBuff(src, static_cast<SizeType>(StringUtils::string_length(src, this->getCapacity())));
27  return this->toChar();
28 }
29 
31  this->appendBuff(src.toChar(), src.length());
32  return *this;
33 }
34 
36  return StringBase::operator+=(static_cast<const ConstStringBase&>(src));
37 }
38 
39 FormatStatus StringBase::format(const CHAR* formatString, ...) {
40  va_list args;
41  va_start(args, formatString);
42  FormatStatus status = this->vformat(formatString, args);
43  va_end(args);
44  return status;
45 }
46 
47 FormatStatus StringBase::vformat(const CHAR* formatString, va_list args) {
48  CHAR* us = const_cast<CHAR*>(this->toChar());
49  SizeType cap = this->getCapacity();
50  FW_ASSERT(us != nullptr);
51  // Needed until SizeType an FwSizeType are the same
52  static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<SizeType>::max(),
53  "String size type must fit into FwSizeType");
54  return Fw::stringFormat(us, static_cast<FwSizeType>(cap), formatString, args);
55 }
56 
57 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
58 void StringBase::toString(StringBase& text) const {
59  text = *this;
60 }
61 #endif
62 
64  if (this != &other) {
65  (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), other.toChar(), this->getCapacity());
66  }
67  return *this;
68 }
69 
71  return StringBase::operator=(static_cast<const ConstStringBase&>(other));
72 }
73 
74 // Copy constructor doesn't make sense in this virtual class as there is nothing to copy. Derived classes should
75 // call the empty constructor and then call their own copy function
76 StringBase& StringBase::operator=(const CHAR* other) { // lgtm[cpp/rule-of-two]
77  (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), other, this->getCapacity());
78  return *this;
79 }
80 
81 void StringBase::appendBuff(const CHAR* buff, SizeType size) {
82  const SizeType capacity = this->getCapacity();
83  const SizeType length = this->length();
84  FW_ASSERT(capacity > length, static_cast<FwAssertArgType>(capacity), static_cast<FwAssertArgType>(length));
85  // Subtract 1 to leave space for null terminator
86  SizeType remaining = capacity - length - 1;
87  if (size < remaining) {
88  remaining = size;
89  }
90  FW_ASSERT(remaining < capacity, static_cast<FwAssertArgType>(remaining), static_cast<FwAssertArgType>(capacity));
91  (void)strncat(const_cast<CHAR*>(this->toChar()), buff, static_cast<size_t>(remaining));
92 }
93 
95  // Get the max size of the deserialized string
96  const SizeType maxSize = this->maxLength();
97  // Initial estimate of actual size is max size
98  // This estimate is refined when calling the deserialize function below
99  SizeType actualSize = maxSize;
100  // Public interface returns const char*, but implementation needs char*
101  // So use const_cast
102  CHAR* raw = const_cast<CHAR*>(this->toChar());
103  // Deserialize length
104  // Fail if length exceeds max size (the initial value of actualSize)
105  // Otherwise deserialize length bytes and set actualSize to length
106  SerializeStatus stat = buffer.deserializeTo(reinterpret_cast<U8*>(raw), actualSize, Serialization::INCLUDE_LENGTH);
107  if (stat == FW_SERIALIZE_OK) {
108  // Deserialization succeeded: null-terminate string at actual size
109  FW_ASSERT(actualSize <= maxSize, static_cast<FwAssertArgType>(actualSize),
110  static_cast<FwAssertArgType>(maxSize));
111  raw[actualSize] = 0;
112  } else {
113  // Deserialization failed: leave string unmodified, but ensure that it
114  // is null-terminated
115  raw[maxSize] = 0;
116  }
117  return stat;
118 }
119 
120 } // namespace Fw
Serialization/Deserialization operation was successful.
SerializeStatus deserializeFrom(SerialBufferBase &buffer, Endianness mode=Endianness::BIG) override
Deserialize the contents of this object from a buffer.
Definition: StringBase.cpp:94
const CHAR * operator+=(const CHAR *src)
Concatenate a CHAR*.
Definition: StringBase.cpp:25
virtual const CHAR * toChar() const =0
Convert to a C-style char*.
FormatStatus vformat(const CHAR *formatString, va_list args)
write formatted string to buffer using va_list
Definition: StringBase.cpp:47
char CHAR
Definition: BasicTypes.h:59
SerializeStatus
forward declaration for string
virtual SerializeStatus deserializeTo(U8 &val, Endianness mode=Endianness::BIG)=0
Deserialize an 8-bit unsigned integer value.
virtual ~StringBase()
Definition: StringBase.cpp:23
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
Definition: StringUtils.cpp:7
virtual SizeType getCapacity() const =0
Return the size of the buffer.
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:39
SizeType maxLength() const
Get the maximum length of a string that the buffer can hold (which is capacity - 1) ...
A read-only abstract superclass for StringBase.
void appendBuff(const CHAR *buff, SizeType size)
Definition: StringBase.cpp:81
StringBase & operator=(const CHAR *src)
Assign CHAR*.
Definition: StringBase.cpp:76
FormatStatus stringFormat(char *destination, const FwSizeType maximumSize, const char *formatString,...)
format a c-string
Include length as first token in serialization.
virtual SizeType length() const
Get the length of the string.
Implementation of malloc based allocator.
Endianness
#define FW_ASSERT(...)
Definition: Assert.hpp:14
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
Definition: StringUtils.cpp:24
FormatStatus
status of string format calls
Definition: format.hpp:18