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 
35 bool StringBase::operator==(const StringBase& other) const {
36  SizeType len = this->length();
37  if (len != other.length()) {
38  return false;
39  } else {
40  return this->operator==(other.toChar());
41  }
42 }
43 
44 bool StringBase::operator==(const CHAR* other) const {
45  const CHAR* const us = this->toChar();
46  if ((us == nullptr) or (other == nullptr)) {
47  return false;
48  }
49 
50  const SizeType capacity = this->getCapacity();
51  const size_t result = static_cast<size_t>(strncmp(us, other, capacity));
52  return (result == 0);
53 }
54 
55 FormatStatus StringBase::format(const CHAR* formatString, ...) {
56  va_list args;
57  va_start(args, formatString);
58  FormatStatus status = this->vformat(formatString, args);
59  va_end(args);
60  return status;
61 }
62 
63 FormatStatus StringBase::vformat(const CHAR* formatString, va_list args) {
64  CHAR* us = const_cast<CHAR*>(this->toChar());
65  SizeType cap = this->getCapacity();
66  FW_ASSERT(us != nullptr);
67  // Needed until SizeType an FwSizeType are the same
68  static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<SizeType>::max(),
69  "String size type must fit into FwSizeType");
70  return Fw::stringFormat(us, static_cast<FwSizeType>(cap), formatString, args);
71 }
72 
73 bool StringBase::operator!=(const StringBase& other) const {
74  return !operator==(other);
75 }
76 
77 bool StringBase::operator!=(const CHAR* other) const {
78  return !operator==(other);
79 }
80 
81 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
82 void StringBase::toString(StringBase& text) const {
83  text = *this;
84 }
85 #endif
86 
87 #ifdef BUILD_UT
88 std::ostream& operator<<(std::ostream& os, const StringBase& str) {
89  os << str.toChar();
90  return os;
91 }
92 #endif
93 
95  if (this != &other) {
96  (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), other.toChar(), this->getCapacity());
97  }
98  return *this;
99 }
100 
101 // Copy constructor doesn't make sense in this virtual class as there is nothing to copy. Derived classes should
102 // call the empty constructor and then call their own copy function
103 StringBase& StringBase::operator=(const CHAR* other) { // lgtm[cpp/rule-of-two]
104  (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), other, this->getCapacity());
105  return *this;
106 }
107 
108 void StringBase::appendBuff(const CHAR* buff, SizeType size) {
109  const SizeType capacity = this->getCapacity();
110  const SizeType length = this->length();
111  FW_ASSERT(capacity > length, static_cast<FwAssertArgType>(capacity), static_cast<FwAssertArgType>(length));
112  // Subtract 1 to leave space for null terminator
113  SizeType remaining = capacity - length - 1;
114  if (size < remaining) {
115  remaining = size;
116  }
117  FW_ASSERT(remaining < capacity, static_cast<FwAssertArgType>(remaining), static_cast<FwAssertArgType>(capacity));
118  (void)strncat(const_cast<CHAR*>(this->toChar()), buff, remaining);
119 }
120 
122  const SizeType length = static_cast<SizeType>(StringUtils::string_length(this->toChar(), this->getCapacity()));
123  FW_ASSERT(length <= this->maxLength(), static_cast<FwAssertArgType>(length),
124  static_cast<FwAssertArgType>(this->maxLength()));
125  return length;
126 }
127 
129  const SizeType capacity = this->getCapacity();
130  FW_ASSERT(capacity > 0, static_cast<FwAssertArgType>(capacity));
131  return capacity - 1;
132 }
133 
135  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + this->length();
136 }
137 
139  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + static_cast<SizeType>(FW_MIN(this->length(), maxLength));
140 }
141 
143  return buffer.serialize(reinterpret_cast<const U8*>(this->toChar()), this->length());
144 }
145 
147  const FwSizeType len = FW_MIN(maxLength, this->length());
148  // Serialize length and then bytes
149  return buffer.serialize(reinterpret_cast<const U8*>(this->toChar()), len, Serialization::INCLUDE_LENGTH);
150 }
151 
153  // Get the max size of the deserialized string
154  const SizeType maxSize = this->maxLength();
155  // Initial estimate of actual size is max size
156  // This estimate is refined when calling the deserialize function below
157  SizeType actualSize = maxSize;
158  // Public interface returns const char*, but implementation needs char*
159  // So use const_cast
160  CHAR* raw = const_cast<CHAR*>(this->toChar());
161  // Deserialize length
162  // Fail if length exceeds max size (the initial value of actualSize)
163  // Otherwise deserialize length bytes and set actualSize to length
164  SerializeStatus stat = buffer.deserialize(reinterpret_cast<U8*>(raw), actualSize, Serialization::INCLUDE_LENGTH);
165  if (stat == FW_SERIALIZE_OK) {
166  // Deserialization succeeded: null-terminate string at actual size
167  FW_ASSERT(actualSize <= maxSize, static_cast<FwAssertArgType>(actualSize),
168  static_cast<FwAssertArgType>(maxSize));
169  raw[actualSize] = 0;
170  } else {
171  // Deserialization failed: leave string unmodified, but ensure that it
172  // is null-terminated
173  raw[maxSize] = 0;
174  }
175  return stat;
176 }
177 } // namespace Fw
Serialization/Deserialization operation was successful.
NATIVE_UINT_TYPE SizeType
Definition: StringBase.hpp:27
NATIVE_UINT_TYPE SizeType
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
const CHAR * operator+=(const CHAR *src)
Concatenate a CHAR*.
Definition: StringBase.cpp:25
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
FormatStatus vformat(const CHAR *formatString, va_list args)
write formatted string to buffer using va_list
Definition: StringBase.cpp:63
SizeType serializedTruncatedSize(FwSizeType maxLength) const
Definition: StringBase.cpp:138
char CHAR
Definition: BasicTypes.h:32
SerializeStatus
forward declaration for string
SizeType maxLength() const
Get the maximum length of a string that the buffer can hold (which is capacity - 1) ...
Definition: StringBase.cpp:128
bool operator!=(const StringBase &other) const
Inequality with StringBase.
Definition: StringBase.cpp:73
virtual ~StringBase()
Definition: StringBase.cpp:23
Include length as first token in serialization.
U16 FwSizeStoreType
Definition: FpConfig.h:59
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.h:72
bool operator==(const StringBase &other) const
Check for equality with StringBase.
Definition: StringBase.cpp:35
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
Definition: StringUtils.cpp:7
SizeType length() const
Get length of string.
Definition: StringBase.cpp:121
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:55
SizeType serializedSize() const
Definition: StringBase.cpp:134
void appendBuff(const CHAR *buff, SizeType size)
Definition: StringBase.cpp:108
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
StringBase & operator=(const CHAR *src)
Assign CHAR*.
Definition: StringBase.cpp:103
virtual SerializeStatus deserialize(SerializeBufferBase &buffer)
deserialization function
Definition: StringBase.cpp:152
FormatStatus stringFormat(char *destination, const FwSizeType maximumSize, const char *formatString,...)
format a c-string
virtual SerializeStatus serialize(SerializeBufferBase &buffer) const
serialization function
Definition: StringBase.cpp:142
virtual SizeType getCapacity() const =0
return size of buffer
#define FW_ASSERT(...)
Definition: Assert.hpp:14
virtual const CHAR * toChar() const =0
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