GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_utilities.cpp
Date: 2025-09-22 22:57:09
Exec Total Coverage
Lines: 29 118 24.6%
Functions: 4 14 28.6%
Branches: 14 124 11.3%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/utilities.hpp"
30 #include "gate/utilities.h"
31 #include <exception>
32
33 namespace gate
34 {
35
36 template class GATE_CORE_CPP_API Array<String>;
37 template class GATE_CORE_CPP_API ArrayList<String>;
38 template class GATE_CORE_CPP_API Set<String>;
39 template class GATE_CORE_CPP_API Map<String, String>;
40
41
42 namespace util
43 {
44 ✗ String printByteSize(uint64_t bytes)
45 {
46 char buffer[256];
47 ✗ size_t used = gate_util_print_byte_size(bytes, buffer, sizeof(buffer) - 16);
48 ✗ return String(buffer, used);
49 }
50 ✗ char const* printResult(result_t resultCode)
51 {
52 ✗ return gate_result_text(resultCode);
53 }
54
55
56 ✗ String printException(Throwable const& xcpt, bool_t addResultCode, bool_t addErrorCode)
57 {
58 ✗ StringBuilder builder;
59 ✗ char const* msg = xcpt.getMessage();
60 ✗ size_t msg_len = gate_str_length(msg);
61
62 ✗ if (msg_len > 0)
63 {
64 ✗ builder.append(msg);
65 }
66 else
67 {
68 ✗ addResultCode = true;
69 }
70
71 ✗ if (addResultCode)
72 {
73 ✗ result_t resultCode = xcpt.getResult();
74 ✗ if (GATE_FAILED(resultCode))
75 {
76 ✗ if (builder.length() != 0)
77 {
78 ✗ builder.appendNewLine();
79 }
80 ✗ builder.append("Result code #");
81 ✗ builder.appendHex(gate_uint16_t(resultCode));
82 ✗ builder.append(" ", 1);
83 ✗ builder.append(printResult(resultCode));
84 }
85 }
86 ✗ if (addErrorCode)
87 {
88 ✗ uint32_t errorCode = static_cast<uint32_t>(xcpt.getErrorCode());
89 ✗ if (errorCode != 0)
90 {
91 ✗ if (builder.length() != 0)
92 {
93 ✗ builder.appendNewLine();
94 }
95 ✗ builder.append("Error code #");
96 ✗ builder.appendHex(errorCode);
97 }
98 }
99
100 ✗ return builder.toString();
101 }
102
103 ✗ String printCurrentException(bool_t addResultCode, bool_t addErrorCode)
104 {
105 ✗ static StaticString const msgUnknownException = "Unknown Exception";
106
107 try
108 {
109 #if __cplusplus >= 201103L
110 ✗ std::exception_ptr xcpt_ptr = std::current_exception();
111 ✗ if (xcpt_ptr)
112 ✗ std::rethrow_exception(xcpt_ptr);
113 #else
114 // pre C++11 cannot check if exceptions are raised,
115 // std::terminate() is called, if this function if called from outside a catch-block
116 throw;
117 #endif
118 }
119 ✗ catch (Throwable const& throwable)
120 {
121 ✗ return printException(throwable, addResultCode, addErrorCode);
122 }
123 ✗ catch (std::exception const& xcpt)
124 {
125 ✗ return String(xcpt.what());
126 }
127 ✗ catch (int num)
128 {
129 ✗ StringBuilder builder;
130 ✗ builder << "Numeric exception code #" << static_cast<int32_t>(num);
131 ✗ return builder.toString();
132 }
133 ✗ catch (...)
134 {
135 ✗ return msgUnknownException;
136 }
137 ✗ return String(); // no exception was thrown
138 }
139
140
141 ✗ String printDuration(uint64_t seconds, bool_t shortFormat)
142 {
143 char buffer[1024];
144 ✗ size_t buffer_used = gate_util_print_duration(seconds, buffer, sizeof(buffer), shortFormat);
145 ✗ return String(buffer, buffer_used);
146 }
147
148 ✗ ArrayList<String> convertStringArray(gate_array_t const& arr)
149 {
150 gate_string_t const* ptr;
151 ✗ size_t len = gate_array_length(&arr);
152 ✗ ArrayList<String> ret(len);
153 ✗ for (size_t ndx = 0; ndx != len; ++ndx)
154 {
155 ✗ ptr = (gate_string_t const*)gate_array_get(&arr, ndx);
156 ✗ if (ptr)
157 {
158 ✗ ret.add(String::duplicate(*ptr));
159 }
160 }
161 ✗ return ret;
162 }
163
164 4 ArrayList<String> convertStringArray(gate_arraylist_t const& arr)
165 {
166 gate_string_t const* ptr;
167 4 size_t len = gate_arraylist_length(arr);
168 4 ArrayList<String> ret(len);
169
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 for (size_t ndx = 0; ndx != len; ++ndx)
170 {
171
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 ptr = (gate_string_t const*)gate_arraylist_get(arr, ndx);
172
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (ptr)
173 {
174
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 ret.add(String::duplicate(*ptr));
175 }
176 }
177 4 return ret;
178 }
179
180 ✗ ArrayList<String> createStringArray(gate_array_t& arr)
181 {
182 ✗ ArrayList<String> ret;
183 try
184 {
185 ✗ ret = convertStringArray(arr);
186 }
187 ✗ catch (...)
188 {
189 ✗ gate_array_release(&arr);
190 ✗ throw;
191 }
192 ✗ return ret;
193 }
194
195 ✗ ArrayList<String> createStringArray(gate_arraylist_t& arr)
196 {
197 ✗ ArrayList<String> ret;
198 try
199 {
200 ✗ ret = convertStringArray(arr);
201 }
202 ✗ catch (...)
203 {
204 ✗ gate_arraylist_release(arr);
205 ✗ arr = NULL;
206 ✗ throw;
207 }
208 ✗ return ret;
209 }
210
211 ✗ ArrayList<String> splitString(String const& text, String const& separator, size_t maxCount)
212 {
213 ✗ gate_arraylist_t arr = gate_util_string_split(text.c_impl(), separator.c_impl(), maxCount);
214 ✗ if (!arr)
215 {
216 ✗ GATEXX_RAISE_ERROR(results::OutOfMemory);
217 }
218 ✗ return createStringArray(arr);
219 }
220
221
222
223 ✗ gate_arraylist_t convertStringArray(Array<String> const& arr)
224 {
225 ✗ gate_arraylist_t lst = gate_util_stringarray_create();
226
227 ✗ if (lst != NULL)
228 {
229 ✗ for (Array<String>::const_iterator it(arr.begin()), itend(arr.end()); it != itend; ++it)
230 {
231 ✗ gate_string_t const* ptr = it->c_impl();
232 ✗ if (NULL == gate_arraylist_add(lst, ptr))
233 {
234 ✗ gate_arraylist_release(lst);
235 ✗ lst = NULL;
236 ✗ break;
237 }
238 }
239 }
240 ✗ return lst;
241 }
242
243 1 gate_arraylist_t convertStringArray(ArrayList<String> const& arr)
244 {
245 1 gate_arraylist_t lst = gate_util_stringarray_create();
246
247
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (lst != NULL)
248 {
249
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
3 for (ArrayList<String>::const_iterator it(arr.begin()), itend(arr.end()); it != itend; ++it)
250 {
251 2 gate_string_t const* ptr = it->c_impl();
252
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_arraylist_add(lst, ptr))
253 {
254 ✗ gate_arraylist_release(lst);
255 ✗ lst = NULL;
256 ✗ break;
257 }
258 }
259 }
260 1 return lst;
261 }
262
263 5 size_t convertStringArray(Array<String> const& arr, gate_string_t* strarr, gate_size_t strarr_max)
264 {
265 5 size_t count = 0;
266
3/4
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
26 for (ArrayList<String>::const_iterator it(arr.begin()), itend(arr.end()); (it != itend) && (strarr_max != 0); ++it)
267 {
268 21 gate_string_t const* ptr = it->c_impl();
269 21 gate_string_duplicate(strarr, ptr);
270 21 ++count;
271 21 ++strarr;
272 21 --strarr_max;
273 }
274 5 return count;
275 }
276
277 6 void releaseStringArray(gate_string_t* strarr, gate_size_t strarrlength)
278 {
279
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 while (strarrlength-- > 0)
280 {
281 2 gate_string_release(strarr);
282 2 ++strarr;
283 }
284 4 }
285
286
287 } // end of namespace util;
288
289 } // end of namespace gate
290