F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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, static_cast<SizeType>(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  va_list args;
58  va_start(args, formatString);
59  this->vformat(formatString, args);
60  va_end(args);
61 }
62 
63 void 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  FW_ASSERT(formatString != nullptr);
68 #if FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING
69  (void) vsnprintf(us, cap, formatString, args);
70 #else
71  *this = formatString;
72 #endif
73  // Force null terminate
74  us[cap - 1] = 0;
75 }
76 
77 bool StringBase::operator!=(const StringBase& other) const {
78  return !operator==(other);
79 }
80 
81 bool StringBase::operator!=(const CHAR* other) const {
82  return !operator==(other);
83 }
84 
85 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
86 void StringBase::toString(StringBase& text) const {
87  text = *this;
88 }
89 #endif
90 
91 #ifdef BUILD_UT
92 std::ostream& operator<<(std::ostream& os, const StringBase& str) {
93  os << str.toChar();
94  return os;
95 }
96 #endif
97 
99  if (this != &other) {
100  (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), other.toChar(), this->getCapacity());
101  }
102  return *this;
103 }
104 
105 // Copy constructor doesn't make sense in this virtual class as there is nothing to copy. Derived classes should
106 // call the empty constructor and then call their own copy function
107 StringBase& StringBase::operator=(const CHAR* other) { // lgtm[cpp/rule-of-two]
108  (void)Fw::StringUtils::string_copy(const_cast<char*>(this->toChar()), other, this->getCapacity());
109  return *this;
110 }
111 
112 void StringBase::appendBuff(const CHAR* buff, SizeType size) {
113  const SizeType capacity = this->getCapacity();
114  const SizeType length = this->length();
115  FW_ASSERT(capacity > length, static_cast<FwAssertArgType>(capacity), static_cast<FwAssertArgType>(length));
116  // Subtract 1 to leave space for null terminator
117  SizeType remaining = capacity - length - 1;
118  if (size < remaining) {
119  remaining = size;
120  }
121  FW_ASSERT(remaining < capacity, static_cast<FwAssertArgType>(remaining), static_cast<FwAssertArgType>(capacity));
122  (void)strncat(const_cast<CHAR*>(this->toChar()), buff, remaining);
123 }
124 
126  const SizeType length = static_cast<SizeType>(StringUtils::string_length(this->toChar(), this->getCapacity()));
127  FW_ASSERT(length <= this->maxLength(), static_cast<FwAssertArgType>(length),
128  static_cast<FwAssertArgType>(this->maxLength()));
129  return length;
130 }
131 
133  const SizeType capacity = this->getCapacity();
134  FW_ASSERT(capacity > 0, static_cast<FwAssertArgType>(capacity));
135  return capacity - 1;
136 }
137 
139  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + this->length();
140 }
141 
143  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + static_cast<SizeType>(FW_MIN(this->length(), maxLength));
144 }
145 
147  return buffer.serialize(reinterpret_cast<const U8*>(this->toChar()), this->length());
148 }
149 
151  const FwSizeType len = FW_MIN(maxLength, this->length());
152  // Serialize length and then bytes
153  return buffer.serialize(reinterpret_cast<const U8*>(this->toChar()), len, Serialization::INCLUDE_LENGTH);
154 }
155 
157  // Get the max size of the deserialized string
158  const SizeType maxSize = this->maxLength();
159  // Initial estimate of actual size is max size
160  // This estimate is refined when calling the deserialize function below
161  SizeType actualSize = maxSize;
162  // Public interface returns const char*, but implementation needs char*
163  // So use const_cast
164  CHAR* raw = const_cast<CHAR*>(this->toChar());
165  // Deserialize length
166  // Fail if length exceeds max size (the initial value of actualSize)
167  // Otherwise deserialize length bytes and set actualSize to length
168  SerializeStatus stat = buffer.deserialize(reinterpret_cast<U8*>(raw), actualSize, Serialization::INCLUDE_LENGTH);
169  if (stat == FW_SERIALIZE_OK) {
170  // Deserialization succeeded: null-terminate string at actual size
171  FW_ASSERT(actualSize <= maxSize, static_cast<FwAssertArgType>(actualSize),
172  static_cast<FwAssertArgType>(maxSize));
173  raw[actualSize] = 0;
174  } else {
175  // Deserialization failed: leave string unmodified, but ensure that it
176  // is null-terminated
177  raw[maxSize] = 0;
178  }
179  return stat;
180 }
181 } // namespace Fw
#define FW_ASSERT(...)
Definition: Assert.hpp:14
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.h:72
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
char CHAR
Definition: BasicTypes.h:32
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
U16 FwSizeStoreType
Definition: FpConfig.h:59
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
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:77
NATIVE_UINT_TYPE SizeType
Definition: StringBase.hpp:26
virtual ~StringBase()
Definition: StringBase.cpp:24
SizeType serializedTruncatedSize(FwSizeType maxLength) const
Definition: StringBase.cpp:142
virtual SerializeStatus deserialize(SerializeBufferBase &buffer)
deserialization function
Definition: StringBase.cpp:156
void format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:56
SizeType length() const
Get length of string.
Definition: StringBase.cpp:125
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 (which is capacity - 1)
Definition: StringBase.cpp:132
void appendBuff(const CHAR *buff, SizeType size)
Definition: StringBase.cpp:112
virtual SerializeStatus serialize(SerializeBufferBase &buffer) const
serialization function
Definition: StringBase.cpp:146
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:107
SizeType serializedSize() const
Definition: StringBase.cpp:138
void vformat(const CHAR *formatString, va_list args)
write formatted string to buffer using va_list
Definition: StringBase.cpp:63
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
Definition: StringUtils.cpp:23
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
Definition: StringUtils.cpp:6
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.