Main MRPT website > C++ reference for MRPT 1.9.9
xsstring.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef XSSTRING_H
10 #define XSSTRING_H
11 
12 #include "xstypesconfig.h"
13 #include "xsarray.h"
14 #ifndef XSENS_NO_WCHAR
15 #include <wchar.h>
16 #if !defined(XSENS_NO_STL) && defined(__cplusplus) && defined(WIN32)
17 #include <Windows.h> // required for MultiByteToWideChar
18 #endif
19 #endif // XSENS_NO_WCHAR
20 #include <string.h>
21 
22 #ifdef __cplusplus
23 #include <string>
24 #include <cstdlib>
25 #include <cstdio>
26 extern "C" {
27 #endif
28 
30 
31 #ifndef __cplusplus
32 #define XsString_INITIALIZER XSARRAY_INITIALIZER(&g_xsStringDescriptor)
34 typedef struct XsString XsString;
35 // obsolete:
36 #define XSSTRING_INITIALIZER XsString_INITIALIZER
37 #else
38 struct XsString;
39 #endif
40 
44  XsString* thisPtr, XsSize count, const char* src);
46  XsString* thisPtr, const char* src);
47 #ifndef XSENS_NO_WCHAR
49  XsString* thisPtr, const wchar_t* src);
50 #endif // XSENS_NO_WCHAR
52  const XsString* thisPtr, wchar_t* dest, XsSize size);
53 // XSTYPES_DLL_API void XsString_assignWString(XsString* thisPtr, XsWString
54 // const* src);
56 XSTYPES_DLL_API void XsString_append(XsString* thisPtr, XsString const* other);
58  XsString* thisPtr, XsSize index, XsSize count);
59 
60 #ifndef __cplusplus
61 // obsolete:
62 #define XsString_copy(thisPtr, copy) XsArray_copy(copy, thisPtr)
63 #define XsString_swap(a, b) XsArray_swap(a, b)
64 #else
65 } // extern "C"
66 #endif
67 
68 #ifdef __cplusplus
69 /* We need some special template implementations for strings to keep them
70  * 0-terminated
71 */
72 // this typedef is not _always_ interpreted correctly by doxygen, hence the
73 // occasional function where we're NOT using it.
74 // template <typename T, XsArrayDescriptor const& D, typename I>
75 // typedef XsArrayImpl<char, g_xsStringDescriptor, XsString> XsStringType; //
76 // MRPT: Converted into a struct to fix building with GCC 4.9+
77 struct XsStringType : public XsArrayImpl<char, g_xsStringDescriptor, XsString>
78 {
79  inline XsStringType() : XsArrayImpl() {}
80  inline explicit XsStringType(char* ref, XsSize sz, XsDataFlags flags)
81  : XsArrayImpl(ref, sz, flags)
82  {
83  }
84 
85  /*! \brief Returns the number of items currently in the array, excluding the
86  terminating 0
87  \returns The number of items currently in the array
88  \sa reserved \sa setSize \sa resize
89  */
90  inline XsSize size() const { return m_size ? m_size - 1 : 0; }
91  //! \copydoc XsArray_reserve
92  inline void reserve(XsSize count) { XsArray_reserve(this, count + 1); }
93  //! \brief Returns the reserved space in number of items
94  inline XsSize reserved() const { return m_reserved ? m_reserved - 1 : 0; }
95  /*! \brief indexed data access operator */
96  inline char& operator[](XsSize index)
97  {
98  assert(index < size());
99  return *ptrAt(m_data, index);
100  }
101 
102  /*! \brief Removes \a count items from the array starting at \a index.
103  * \param index The index of the first item to remove. \param count The
104  * number of items to remove. */
105  inline void erase(XsSize index, XsSize count)
106  {
108  }
109 
110  /*! \brief Insert \a count \a items at \a index in the string
111  \param items The items to insert, may not be 0 unless count is 0
112  \param index The index to use for inserting. Anything beyond the end of
113  the string (ie. -1) will
114  append to the actual end of the string.
115  \param count The number of items to insert
116  */
117  inline void insert(char const* items, XsSize index, XsSize count)
118  {
119  if (size())
120  {
121  if (index >= size()) index = size();
122  XsArray_insert(this, index, count, items);
123  }
124  else
125  XsString_assign((XsString*)this, count, items);
126  }
127 
128  /*! \brief Assign the characters in \a src to the string
129  \details This function is resizes the string to fit \a count characters
130  and copies those from \a src.
131  \param count The number of characters in src. If this value is 0 and \a
132  scr is not 0, the value is
133  determined automatically by looking through src until a
134  terminating 0 is found.
135  \param src The source string to copy from. If this is 0 and \a count is
136  not 0, the string will be
137  filled with spaces instead.
138  \sa XsString_assign
139  */
140  inline void assign(XsSize count, char const* src)
141  {
142  XsString_assign((XsString*)this, count, src);
143  }
144 
145  /*! \brief This function resizes the contained string to the desired size,
146  while retaining its contents
147  \param count The desired size of the string. This excludes the
148  terminating 0 character.
149  \sa XsString_resize
150  */
151  inline void resize(XsSize count)
152  {
153  XsString_resize((XsString*)this, count);
154  }
155 
156  /*! \brief Set the size of the array to \a count.
157  \details The contents of the array after this operation are undefined.
158  \param count The desired new size fo the array.
159  \sa XsArray_assign \sa reserve \sa resize
160  */
161  inline void setSize(XsSize count)
162  {
163  if (count != size()) XsString_assign((XsString*)this, count, 0);
164  }
165 
166  /*! \copydoc XsArray_append \sa XsArray_append */
167  inline void append(const XsStringType& other)
168  {
169  XsString_append((XsString*)this, (XsString const*)&other);
170  }
171 }; // end of struct XsStringType (MRPT)
172 
173 struct XsString : public XsStringType
174 {
175  //! \brief Constructs an XsString
176  inline explicit XsString(XsSize sz = 0, char const* src = 0)
177  : XsStringType()
178  {
179  if (sz || src) XsString_assign(this, sz, src);
180  }
181 
182  //! \brief Constructs an XsString as a copy of \a other
183  inline XsString(const XsStringType& other) : XsStringType(other) {}
184  //! \brief Constructs an XsInt64Array that references the data supplied in
185  //! \a ref
186  inline explicit XsString(
187  char* ref, XsSize sz, XsDataFlags flags = XSDF_None)
188  : XsStringType(ref, sz + 1, flags)
189  {
190  }
191 #ifndef XSENS_NOITERATOR
192  //! \brief Constructs an XsString with the list bound by the supplied
193  //! iterators \a beginIt and \a endIt
194  template <typename Iterator>
195  inline XsString(Iterator const& beginIt, Iterator const& endIt)
196  : XsStringType(beginIt, endIt)
197  {
198  }
199 #endif
200 
201  //! \brief Construct an XsString from a 0-terminated character array
202  inline XsString(char const* src) : XsStringType()
203  {
204  if (src && src[0]) XsString_assignCharArray(this, src);
205  }
206 
207 #ifndef XSENS_NO_WCHAR
208  //! \brief Construct an XsString from a 0-terminated character array
209  inline XsString(wchar_t const* src) : XsStringType()
210  {
211  if (src && src[0]) XsString_assignWCharArray(this, src);
212  }
213 #endif
214 
215 #ifndef XSENS_NO_STL
216  //! \brief Construct an XsString from a std::string
217  inline XsString(std::string const& src) : XsStringType()
218  {
219  if (!src.empty()) XsString_assign(this, src.size() + 1, src.c_str());
220  }
221 
222  //! \brief Construct an XsString from a std::wstring
223  inline XsString(std::wstring const& src) : XsStringType()
224  {
225  if (!src.empty()) XsString_assignWCharArray(this, src.c_str());
226  }
227 #endif // XSENS_NO_STL
228 
229  //! \brief Return the internal 0-terminated C-style string
230  inline char* c_str()
231  {
232  static const char nullChar = 0;
233  if (empty()) return (char*)&nullChar;
234  return begin().operator->();
235  }
236 
237  //! \brief Return the internal 0-terminated C-style string
238  inline char const* c_str() const
239  {
240  static const char nullChar = 0;
241  if (empty()) return &nullChar;
242  return begin().operator->();
243  }
244 
245 #ifndef XSENS_NO_STL
246  //! \brief Return the string as a std::string
247  std::string toStdString() const
248  {
249  if (empty()) return std::string();
250  return std::string((char const*)begin().operator->());
251  }
252 #endif // XSENS_NO_STL
253 
254  //! \brief Return \a other appended to this, without modifying this
255  XsString operator+(XsString const& other) const
256  {
257  XsString tmp;
258  tmp.reserve(size() + other.size());
259  tmp.append(*this);
260  tmp.append(other);
261  return tmp;
262  }
263 
264 #ifndef XSENS_NO_STL
265  //! \brief Return the string as a std::wstring
266  std::wstring toStdWString() const
267  {
268  if (empty()) return std::wstring();
269  size_t s = XsString_copyToWCharArray(this, nullptr, 0);
270  std::wstring w;
271  w.resize(s - 1);
272  s = XsString_copyToWCharArray(this, &w[0], s);
273  return w;
274  }
275 #endif // XSENS_NO_STL
276 
277  /*! \cond NODOXYGEN */
278  using XsStringType::operator==;
279  using XsStringType::operator!=;
280 #ifndef XSENS_NOITERATOR
281  using XsStringType::operator<<;
282 #endif
283  /*! \endcond */
284 
285  //! \brief Return true if the contents of \a str are identical to this
286  //! string
287  bool operator==(char const* str) const
288  {
289  if (!str) return empty();
290  return !strcmp(c_str(), str);
291  }
292 
293  //! \brief Return true if the contents of \a str differ from this string
294  inline bool operator!=(char const* str) const { return !(*this == str); }
295  //! \brief Append a character array to the string in a stream-like way
296  inline XsString& operator<<(char const* str)
297  {
298  if (str && str[0])
299  {
300  XsString const ref(const_cast<char*>(str), strlen(str), XSDF_None);
301  append(ref);
302  }
303  return *this;
304  }
305 
306  //! \brief Append an integer to the string in a stream-like way
307  inline XsString& operator<<(int i)
308  {
309  char buffer[32];
310  append(XsString(buffer, sprintf(buffer, "%d", i), XSDF_None));
311  return *this;
312  }
313 
314  //! \brief Append another string to the string in a stream-like way
315  inline XsString& operator<<(XsString const& s)
316  {
317  append(s);
318  return *this;
319  }
320 };
321 
322 #ifndef XSENS_NO_STL
323 namespace std
324 {
325 template <typename _CharT, typename _Traits>
326 basic_ostream<_CharT, _Traits>& operator<<(
327  basic_ostream<_CharT, _Traits>& o, XsString const& xs)
328 {
329  return (o << xs.toStdString());
330 }
331 }
332 #endif
333 
334 #endif
335 
336 #endif // file guard
XsArrayDescriptor
This object describes how to treat the data in an array.
Definition: xsarray.h:63
XsString
struct XsString XsString
Definition: xsstring.h:34
begin
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:29
mrpt::img::operator+
TColor operator+(const TColor &first, const TColor &second)
Pairwise addition of their corresponding RGBA members.
Definition: TColor.cpp:20
s
GLdouble s
Definition: glext.h:3676
XsString_copyToWCharArray
XSTYPES_DLL_API XsSize XsString_copyToWCharArray(const XsString *thisPtr, wchar_t *dest, XsSize size)
src
GLuint src
Definition: glext.h:7278
XsString_append
XSTYPES_DLL_API void XsString_append(XsString *thisPtr, XsString const *other)
mrpt::containers::operator[]
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn't exist already.
Definition: ts_hash_map.h:199
XsArray_insert
XSTYPES_DLL_API void XsArray_insert(void *thisPtr, XsSize index, XsSize count, void const *src)
w
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
XsString_assignWCharArray
XSTYPES_DLL_API void XsString_assignWCharArray(XsString *thisPtr, const wchar_t *src)
mrpt::img::operator==
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:201
XsString_assign
XSTYPES_DLL_API void XsString_assign(XsString *thisPtr, XsSize count, const char *src)
XsString_destruct
XSTYPES_DLL_API void XsString_destruct(XsString *thisPtr)
mrpt::system::os::sprintf
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
Definition: os.cpp:189
count
GLuint GLuint GLsizei count
Definition: glext.h:3528
index
GLuint index
Definition: glext.h:4054
mrpt::containers::m_size
size_t m_size
Number of elements accessed with write access so far.
Definition: ts_hash_map.h:180
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:19
xsarray.h
setSize
EIGEN_STRONG_INLINE void setSize(size_t row, size_t col)
Changes the size of matrix, maintaining its previous content as possible and padding with zeros where...
Definition: eigen_plugins.h:343
xstypesconfig.h
buffer
GLuint buffer
Definition: glext.h:3917
mrpt::img::operator!=
bool operator!=(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:208
assign
EIGEN_STRONG_INLINE void assign(const Scalar v)
Definition: eigen_plugins.h:48
XsString_assignCharArray
XSTYPES_DLL_API void XsString_assignCharArray(XsString *thisPtr, const char *src)
XsString_erase
XSTYPES_DLL_API void XsString_erase(XsString *thisPtr, XsSize index, XsSize count)
g_xsStringDescriptor
const XsArrayDescriptor XSTYPES_DLL_API g_xsStringDescriptor
XSDF_None
@ XSDF_None
No flag set.
Definition: xstypedefs.h:44
XSTYPES_DLL_API
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:9
XsString_construct
XSTYPES_DLL_API void XsString_construct(XsString *thisPtr)
XsDataFlags
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:41
empty
EIGEN_STRONG_INLINE bool empty() const
Definition: eigen_plugins.h:601
ref
GLenum GLint ref
Definition: glext.h:4050
XSARRAY_STRUCT
XSARRAY_STRUCT(XsString, char)
string
GLsizei const GLchar ** string
Definition: glext.h:4101
XsArray_reserve
XSTYPES_DLL_API void XsArray_reserve(void *thisPtr, XsSize count)
reserved
_u32 reserved
Definition: rplidar_cmd.h:3
size
GLsizeiptr size
Definition: glext.h:3923
XsString_resize
XSTYPES_DLL_API void XsString_resize(XsString *thisPtr, XsSize count)
mrpt::comms::operator<<
std::ostream & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
Definition: CInterfaceFTDI_WIN.cpp:449



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST