Main MRPT website > C++ reference for MRPT 1.5.6
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-2017, 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 
43 XSTYPES_DLL_API void XsString_assign(XsString* thisPtr, XsSize count, const char* src);
44 XSTYPES_DLL_API void XsString_assignCharArray(XsString* thisPtr, const char* src);
45 #ifndef XSENS_NO_WCHAR
46 XSTYPES_DLL_API void XsString_assignWCharArray(XsString* thisPtr, const wchar_t* src);
47 #endif // XSENS_NO_WCHAR
48 XSTYPES_DLL_API XsSize XsString_copyToWCharArray(const XsString* thisPtr, wchar_t* dest, XsSize size);
49 //XSTYPES_DLL_API void XsString_assignWString(XsString* thisPtr, XsWString const* src);
51 XSTYPES_DLL_API void XsString_append(XsString* thisPtr, XsString const* other);
53 
54 #ifndef __cplusplus
55 // obsolete:
56 #define XsString_copy(thisPtr, copy) XsArray_copy(copy, thisPtr)
57 #define XsString_swap(a, b) XsArray_swap(a, b)
58 #else
59 } // extern "C"
60 #endif
61 
62 #ifdef __cplusplus
63 /* We need some special template implementations for strings to keep them 0-terminated
64 */
65 // this typedef is not _always_ interpreted correctly by doxygen, hence the occasional function where we're NOT using it.
66 //template <typename T, XsArrayDescriptor const& D, typename I>
67 //typedef XsArrayImpl<char, g_xsStringDescriptor, XsString> XsStringType; // MRPT: Converted into a struct to fix building with GCC 4.9+
68 struct XsStringType : public XsArrayImpl<char, g_xsStringDescriptor, XsString>
69 {
70  inline XsStringType() : XsArrayImpl()
71  {
72  }
73 
74  inline explicit XsStringType(char* ref, XsSize sz, XsDataFlags flags) :
75  XsArrayImpl(ref,sz,flags)
76  {
77  }
78 
79  /*! \brief Returns the number of items currently in the array, excluding the terminating 0
80  \returns The number of items currently in the array
81  \sa reserved \sa setSize \sa resize
82  */
83  inline XsSize size() const
84  {
85  return m_size?m_size-1:0;
86  }
87 
88  //! \copydoc XsArray_reserve
89  inline void reserve(XsSize count)
90  {
91  XsArray_reserve(this, count+1);
92  }
93 
94  //! \brief Returns the reserved space in number of items
95  inline XsSize reserved() const
96  {
97  return m_reserved?m_reserved-1:0;
98  }
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. \param index The index of the first item to remove. \param count The number of items to remove. */
108  inline void erase(XsSize index, XsSize count)
109  {
110  XsString_erase((XsString*) this, index, count);
111  }
112 
113  /*! \brief Insert \a count \a items at \a index in the string
114  \param items The items to insert, may not be 0 unless count is 0
115  \param index The index to use for inserting. Anything beyond the end of the string (ie. -1) will
116  append to the actual end of the string.
117  \param count The number of items to insert
118  */
119  inline void insert(char const* items, XsSize index, XsSize count)
120  {
121  if (size())
122  {
123  if (index >= size())
124  index = size();
125  XsArray_insert(this, index, count, items);
126  }
127  else
128  XsString_assign((XsString*) this, count, items);
129  }
130 
131  /*! \brief Assign the characters in \a src to the string
132  \details This function is resizes the string to fit \a count characters and copies those from \a src.
133  \param count The number of characters in src. If this value is 0 and \a scr is not 0, the value is
134  determined automatically by looking through src until a terminating 0 is found.
135  \param src The source string to copy from. If this is 0 and \a count is not 0, the string will be
136  filled with spaces instead.
137  \sa XsString_assign
138  */
139  inline void assign(XsSize count, char const* src)
140  {
141  XsString_assign((XsString*) this, count, src);
142  }
143 
144  /*! \brief This function resizes the contained string to the desired size, while retaining its contents
145  \param count The desired size of the string. This excludes the terminating 0 character.
146  \sa XsString_resize
147  */
148  inline void resize(XsSize count)
149  {
150  XsString_resize((XsString*) this, count);
151  }
152 
153  /*! \brief Set the size of the array to \a count.
154  \details The contents of the array after this operation are undefined.
155  \param count The desired new size fo the array.
156  \sa XsArray_assign \sa reserve \sa resize
157  */
158  inline void setSize(XsSize count)
159  {
160  if (count != size())
161  XsString_assign((XsString*) this, count, 0);
162  }
163 
164  /*! \copydoc XsArray_append \sa XsArray_append */
165  inline void append(const XsStringType& other)
166  {
167  XsString_append((XsString*) this, (XsString const*) &other);
168  }
169 }; // end of struct XsStringType (MRPT)
170 
171 
172 
173 struct XsString : public XsStringType {
174  //! \brief Constructs an XsString
175  inline explicit XsString(XsSize sz = 0, char const* src = 0)
176  : XsStringType()
177  {
178  if (sz || src)
179  XsString_assign(this, sz, src);
180  }
181 
182  //! \brief Constructs an XsString as a copy of \a other
183  inline XsString(const XsStringType& other)
184  : XsStringType(other)
185  {
186  }
187 
188  //! \brief Constructs an XsInt64Array that references the data supplied in \a ref
189  inline explicit XsString(char* ref, XsSize sz, XsDataFlags flags = XSDF_None)
190  : XsStringType(ref, sz+1, flags)
191  {
192  }
193 #ifndef XSENS_NOITERATOR
194  //! \brief Constructs an XsString with the list bound by the supplied 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)
204  : XsStringType()
205  {
206  if (src && src[0])
207  XsString_assignCharArray(this, src);
208  }
209 
210 #ifndef XSENS_NO_WCHAR
211  //! \brief Construct an XsString from a 0-terminated character array
212  inline XsString(wchar_t const* src)
213  : XsStringType()
214  {
215  if (src && src[0])
216  XsString_assignWCharArray(this, src);
217  }
218 #endif
219 
220 #ifndef XSENS_NO_STL
221  //! \brief Construct an XsString from a std::string
222  inline XsString(std::string const& src)
223  : XsStringType()
224  {
225  if (!src.empty())
226  XsString_assign(this, src.size()+1, src.c_str());
227  }
228 
229  //! \brief Construct an XsString from a std::wstring
230  inline XsString(std::wstring const& src)
231  : XsStringType()
232  {
233  if (!src.empty())
234  XsString_assignWCharArray(this, src.c_str());
235  }
236 #endif // XSENS_NO_STL
237 
238  //! \brief Return the internal 0-terminated C-style string
239  inline char* c_str()
240  {
241  static const char nullChar = 0;
242  if (empty())
243  return (char*) &nullChar;
244  return begin().operator ->();
245  }
246 
247  //! \brief Return the internal 0-terminated C-style string
248  inline char const* c_str() const
249  {
250  static const char nullChar = 0;
251  if (empty())
252  return &nullChar;
253  return begin().operator ->();
254  }
255 
256 #ifndef XSENS_NO_STL
257  //! \brief Return the string as a std::string
258  std::string toStdString() const
259  {
260  if (empty())
261  return std::string();
262  return std::string((char const*)begin().operator ->());
263  }
264 #endif // XSENS_NO_STL
265 
266  //! \brief Return \a other appended to this, without modifying this
267  XsString operator + (XsString const& other) const
268  {
269  XsString tmp;
270  tmp.reserve(size()+other.size());
271  tmp.append(*this);
272  tmp.append(other);
273  return tmp;
274  }
275 
276 #ifndef XSENS_NO_STL
277  //! \brief Return the string as a std::wstring
278  std::wstring toStdWString() const
279  {
280  if (empty())
281  return std::wstring();
282  size_t s = XsString_copyToWCharArray(this, NULL, 0);
283  std::wstring w;
284  w.resize(s-1);
285  s = XsString_copyToWCharArray(this, &w[0], s);
286  return w;
287  }
288 #endif // XSENS_NO_STL
289 
290  /*! \cond NODOXYGEN */
291  using XsStringType::operator ==;
292  using XsStringType::operator !=;
293 #ifndef XSENS_NOITERATOR
294  using XsStringType::operator <<;
295 #endif
296  /*! \endcond */
297 
298  //! \brief Return true if the contents of \a str are identical to this string
299  bool operator == (char const* str) const
300  {
301  if (!str) return empty();
302  return !strcmp(c_str(), str);
303  }
304 
305  //! \brief Return true if the contents of \a str differ from this string
306  inline bool operator != (char const* str) const
307  {
308  return !(*this == str);
309  }
310 
311  //! \brief Append a character array to the string in a stream-like way
312  inline XsString& operator << (char const* str)
313  {
314  if (str && str[0])
315  {
316  XsString const ref(const_cast<char*>(str), strlen(str), XSDF_None);
317  append(ref);
318  }
319  return *this;
320  }
321 
322  //! \brief Append an integer to the string in a stream-like way
323  inline XsString& operator << (int i)
324  {
325  char buffer[32];
326  append(XsString(buffer, sprintf(buffer, "%d", i), XSDF_None));
327  return *this;
328  }
329 
330  //! \brief Append another string to the string in a stream-like way
331  inline XsString& operator << (XsString const& s)
332  {
333  append(s);
334  return *this;
335  }
336 };
337 
338 #ifndef XSENS_NO_STL
339 namespace std {
340 template<typename _CharT, typename _Traits>
341 basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& o, XsString const& xs)
342 {
343  return (o << xs.toStdString());
344 }
345 }
346 #endif
347 
348 #endif
349 
350 #endif // file guard
XSTYPES_DLL_API void XsArray_insert(void *thisPtr, XsSize index, XsSize count, void const *src)
bool operator==(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
This object describes how to treat the data in an array.
Definition: xsarray.h:51
XSARRAY_STRUCT(XsString, char)
XSTYPES_DLL_API void XsString_assignWCharArray(XsString *thisPtr, const wchar_t *src)
_u32 reserved
Definition: rplidar_cmd.h:22
XSTYPES_DLL_API void XsString_resize(XsString *thisPtr, XsSize count)
bool operator!=(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
XSTYPES_DLL_API void XsString_assign(XsString *thisPtr, XsSize count, const char *src)
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1797
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:26
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:17
GLuint src
Definition: glew.h:7126
No flag set.
Definition: xstypedefs.h:38
XSTYPES_DLL_API void XsString_append(XsString *thisPtr, XsString const *other)
XSTYPES_DLL_API XsSize XsString_copyToWCharArray(const XsString *thisPtr, wchar_t *dest, XsSize size)
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:89
GLdouble s
Definition: glew.h:1295
EIGEN_STRONG_INLINE bool empty() const
GLenum GLint ref
Definition: glew.h:1752
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:123
GLuint buffer
Definition: glew.h:1585
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...
EIGEN_STRONG_INLINE void assign(const Scalar v)
Definition: eigen_plugins.h:41
size_t m_size
Number of elements accessed with write access so far.
Definition: ts_hash_map.h:103
XSTYPES_DLL_API void XsString_erase(XsString *thisPtr, XsSize index, XsSize count)
XSTYPES_DLL_API void XsString_destruct(XsString *thisPtr)
std::ostream & operator<<(std::ostream &out, MD5 md5)
Definition: md5.cpp:419
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:9
GLsizeiptr size
Definition: glew.h:1586
XSTYPES_DLL_API void XsString_construct(XsString *thisPtr)
GLsizei const GLcharARB ** string
Definition: glew.h:3293
GLuint index
Definition: glew.h:1721
int BASE_IMPEXP sprintf(char *buf, size_t bufSize, const char *format,...) MRPT_NO_THROWS 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
GLuint GLuint GLsizei count
Definition: glew.h:1167
XSTYPES_DLL_API void XsArray_reserve(void *thisPtr, XsSize count)
struct XsString XsString
Definition: xsstring.h:34
XSTYPES_DLL_API void XsString_assignCharArray(XsString *thisPtr, const char *src)
XsArrayDescriptor const XSTYPES_DLL_API g_xsStringDescriptor
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:37



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018