xsstringarray.c
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32 
33 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #include "xsstringarray.h"
66 #include "xsstring.h"
67 
75 {
76  sizeof(XsString),
77  XSEXPCASTITEMSWAP XsArray_swap,
78  XSEXPCASTITEMMAKE XsString_construct,
79  XSEXPCASTITEMCOPY XsArray_copyConstruct,
80  XSEXPCASTITEMMAKE XsArray_destruct,
81  XSEXPCASTITEMCOPY XsArray_copy,
82  XSEXPCASTITEMCOMP XsArray_compare,
83  0
84 };
85 
89 void XsStringArray_construct(XsStringArray* thisPtr, XsSize count, XsString const* src)
90 {
91  XsArray_construct(thisPtr, &g_xsStringArrayDescriptor, count, src);
92 }
93 
102 void XsStringArray_fromSplicedString(struct XsStringArray* thisPtr, struct XsString const* src, struct XsString const* separators)
103 {
104  XsString s;
106  XsArray_destruct(thisPtr);
107  if (src->m_size > 0)
108  {
109  // check against 1 because an empty string can either be size 0 or size 1 (just the null-terminator)
110  if (separators->m_size <= 1)
111  {
112  // no separator
113  XsArray_insert(thisPtr, 0, 1, src);
114  }
115  else
116  {
117  char const* sep = (char const*) separators->m_data;
118  char const* idx = (char const*) src->m_data;
119  char const* newIdx = strpbrk(idx, sep);
120  while (newIdx && *idx)
121  {
122  if (newIdx != idx)
123  {
124  XsString_assign(&s, (XsSize)(newIdx - idx), idx);
125  XsArray_insert(thisPtr, thisPtr->m_size, 1, &s);
126  }
127  idx = newIdx + 1;
128  newIdx = strpbrk(idx, sep);
129  }
130  if (*idx)
131  {
133  XsArray_insert(thisPtr, thisPtr->m_size, 1, &s);
134  }
135  }
136  }
138 }
139 
145 void XsStringArray_join(struct XsStringArray const* thisPtr, struct XsString* result, struct XsString const* separator)
146 {
147  // determine required buffer size
148  XsSize i;
149  XsSize chars = (thisPtr->m_size ? (thisPtr->m_size - 1) : 0) * (separator->m_size ? separator->m_size - 1 : 0);
150  for (i = 0; i < thisPtr->m_size; ++i)
151  {
152  XsSize sz = ((const XsString*)XsArray_at(thisPtr, i))->m_size;
153  chars += (sz ? sz - 1 : 0);
154  }
155 
156  XsArray_destruct(result);
157  if (chars)
158  {
159  XsArray_reserve(result, chars + 1);
160  for (i = 0; i < thisPtr->m_size; ++i)
161  {
162  const XsString* s = (const XsString*)XsArray_at(thisPtr, i);
163  if (s->m_size > 1)
164  {
165  if (result->m_size > 1)
166  XsString_append(result, separator);
167  XsString_append(result, s);
168  }
169  }
170  }
171 }
XsArrayDescriptor
This object describes how to treat the data in an array.
Definition: xsarray.h:103
XsArray::XsArray_construct
void XsArray_construct(void *thisPtr, XsArrayDescriptor const *const descriptor, XsSize count, void const *src)
Initializes the XsArray with space for count items and copies them from src.
Definition: xsarray.c:102
XsString
struct XsString XsString
Definition: xsstring.h:87
xsstring.h
XsString_destruct
void XsString_destruct(XsString *thisPtr)
Clears and frees memory allocated by the XsArray.
Definition: xsstring.c:134
XsArray::XsArray_compare
int XsArray_compare(void const *a, void const *b)
Returns non-zero if the lists are different, 0 if they're equal.
Definition: xsarray.c:479
XsStringArray_construct
void XsStringArray_construct(XsStringArray *thisPtr, XsSize count, XsString const *src)
Definition: xsstringarray.c:89
s
XmlRpcServer s
XsArray::XsArray_destruct
void XsArray_destruct(void *thisPtr)
Clears and frees memory allocated by the XsArray.
Definition: xsarray.c:157
xsstringarray.h
XsArray::XsArray_copy
void XsArray_copy(void *thisPtr, void const *src)
Copy the contents of src to thisArray.
Definition: xsarray.c:305
XsStringArray
A list of XsString values.
XsString_append
void XsString_append(XsString *thisPtr, XsString const *other)
This function concatenates the other to this.
Definition: xsstring.c:257
XsStringArray::XsStringArray_fromSplicedString
void XsStringArray_fromSplicedString(struct XsStringArray *thisPtr, struct XsString const *src, struct XsString const *separators)
Splice the supplied string and put the resulting substrings in the string array.
Definition: xsstringarray.c:102
XsArray::XsArray_copyConstruct
void XsArray_copyConstruct(void *thisPtr, void const *src)
Initializes the XsArray with a copy of src.
Definition: xsarray.c:144
XsString_construct
void XsString_construct(XsString *thisPtr)
Initializes the XsString object as an empty string.
Definition: xsstring.c:126
XsArray::XsArray_swap
void XsArray_swap(void *a, void *b)
Swap the contents of a with those of b.
Definition: xsarray.c:408
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:74
XsArray::XsArray_insert
void XsArray_insert(void *thisPtr, XsSize index, XsSize count, void const *src)
Insert count items from src at index in the array.
Definition: xsarray.c:372
XsArray::XsArray_reserve
void XsArray_reserve(void *thisPtr, XsSize count)
Reserves space for count items.
Definition: xsarray.c:256
g_xsStringArrayDescriptor
const XsArrayDescriptor g_xsStringArrayDescriptor
Descriptor for XsStringArray.
Definition: xsstringarray.c:74
XsString_assignCharArray
void XsString_assignCharArray(XsString *thisPtr, const char *src)
This function determines the size of src and copies the contents to the object.
Definition: xsstring.c:172
XsArray::XsArray_at
void const * XsArray_at(void const *thisPtr, XsSize index)
Returns a pointer to the item at the supplied index or a null pointer if it is out of bounds.
Definition: xsarray.c:625
XsStringArray::XsStringArray_join
void XsStringArray_join(struct XsStringArray const *thisPtr, struct XsString *result, struct XsString const *separator)
Join the string array into a single string, inserting separator between substrings.
Definition: xsstringarray.c:145
XsString_assign
void XsString_assign(XsString *thisPtr, XsSize count, const char *src)
Reinitializes the XsArray with space for count items and copies them from src.
Definition: xsstring.c:142
XsString
A 0-terminated managed string of characters.


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20