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