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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019