1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/utilities.hpp"
#include "gate/utilities.h"
#include <exception>

namespace gate
{

    template class GATE_CORE_CPP_API Array<String>;
    template class GATE_CORE_CPP_API ArrayList<String>;
    template class GATE_CORE_CPP_API Set<String>;
    template class GATE_CORE_CPP_API Map<String, String>;


    namespace util
    {
        String printByteSize(uint64_t bytes)
        {
            char buffer[256];
            size_t used = gate_util_print_byte_size(bytes, buffer, sizeof(buffer) - 16);
            return String(buffer, used);
        }
        char const* printResult(result_t resultCode)
        {
            return gate_result_text(resultCode);
        }


        String printException(Throwable const& xcpt, bool_t addResultCode, bool_t addErrorCode)
        {
            StringBuilder builder;
            char const* msg = xcpt.getMessage();
            size_t msg_len = gate_str_length(msg);

            if (msg_len > 0)
            {
                builder.append(msg);
            }
            else
            {
                addResultCode = true;
            }

            if (addResultCode)
            {
                result_t resultCode = xcpt.getResult();
                if (GATE_FAILED(resultCode))
                {
                    if (builder.length() != 0)
                    {
                        builder.appendNewLine();
                    }
                    builder.append("Result code #");
                    builder.appendHex(gate_uint16_t(resultCode));
                    builder.append(" ", 1);
                    builder.append(printResult(resultCode));
                }
            }
            if (addErrorCode)
            {
                uint32_t errorCode = static_cast<uint32_t>(xcpt.getErrorCode());
                if (errorCode != 0)
                {
                    if (builder.length() != 0)
                    {
                        builder.appendNewLine();
                    }
                    builder.append("Error code #");
                    builder.appendHex(errorCode);
                }
            }

            return builder.toString();
        }

        String printCurrentException(bool_t addResultCode, bool_t addErrorCode)
        {
            static String const msgUnknownException("Unknown Exception");

            try
            {
#if __cplusplus >= 201103L
                std::exception_ptr xcpt_ptr = std::current_exception();
                if (xcpt_ptr)
                    std::rethrow_exception(xcpt_ptr);
#else
                // pre C++11 cannot check if exceptions are raised, 
                // std::terminate() is called, if this function if called from outside a catch-block
                throw;
#endif
            }
            catch (Throwable const& throwable)
            {
                return printException(throwable, addResultCode, addErrorCode);
            }
            catch (std::exception const& xcpt)
            {
                return String(xcpt.what());
            }
            catch (int num)
            {
                StringBuilder builder;
                builder << "Numeric exception code #" << static_cast<int32_t>(num);
                return builder.toString();
            }
            catch (...)
            {
                return msgUnknownException;
            }
            return String();	// no exception was thrown
        }


        String printDuration(uint64_t seconds, bool_t shortFormat)
        {
            char buffer[1024];
            size_t buffer_used = gate_util_print_duration(seconds, buffer, sizeof(buffer), shortFormat);
            return String(buffer, buffer_used);
        }

        ArrayList<String> convertStringArray(gate_array_t const& arr)
        {
            gate_string_t const* ptr;
            size_t len = gate_array_length(&arr);
            ArrayList<String> ret(len);
            for (size_t ndx = 0; ndx != len; ++ndx)
            {
                ptr = (gate_string_t const*)gate_array_get(&arr, ndx);
                if (ptr)
                {
                    ret.add(String::duplicate(*ptr));
                }
            }
            return ret;
        }

        ArrayList<String> convertStringArray(gate_arraylist_t const& arr)
        {
            gate_string_t const* ptr;
            size_t len = gate_arraylist_length(arr);
            ArrayList<String> ret(len);
            for (size_t ndx = 0; ndx != len; ++ndx)
            {
                ptr = (gate_string_t const*)gate_arraylist_get(arr, ndx);
                if (ptr)
                {
                    ret.add(String::duplicate(*ptr));
                }
            }
            return ret;
        }

        ArrayList<String> createStringArray(gate_array_t& arr)
        {
            ArrayList<String> ret;
            try
            {
                ret = convertStringArray(arr);
            }
            catch (...)
            {
                gate_array_release(&arr);
                throw;
            }
            return ret;
        }

        ArrayList<String> createStringArray(gate_arraylist_t& arr)
        {
            ArrayList<String> ret;
            try
            {
                ret = convertStringArray(arr);
            }
            catch (...)
            {
                gate_arraylist_release(arr);
                arr = NULL;
                throw;
            }
            return ret;
        }

        ArrayList<String> splitString(String const& text, String const& separator, size_t maxCount)
        {
            gate_arraylist_t arr = gate_util_string_split(text.c_impl(), separator.c_impl(), maxCount);
            if (!arr)
            {
                GATEXX_RAISE_ERROR(results::OutOfMemory);
            }
            return createStringArray(arr);
        }



        gate_arraylist_t convertStringArray(Array<String> const& arr)
        {
            gate_arraylist_t lst = gate_util_stringarray_create();

            if (lst != NULL)
            {
                for (Array<String>::const_iterator it(arr.begin()), itend(arr.end()); it != itend; ++it)
                {
                    gate_string_t const* ptr = it->c_impl();
                    if (NULL == gate_arraylist_add(lst, ptr))
                    {
                        gate_arraylist_release(lst);
                        lst = NULL;
                        break;
                    }
                }
            }
            return lst;
        }

        gate_arraylist_t convertStringArray(ArrayList<String> const& arr)
        {
            gate_arraylist_t lst = gate_util_stringarray_create();

            if (lst != NULL)
            {
                for (ArrayList<String>::const_iterator it(arr.begin()), itend(arr.end()); it != itend; ++it)
                {
                    gate_string_t const* ptr = it->c_impl();
                    if (NULL == gate_arraylist_add(lst, ptr))
                    {
                        gate_arraylist_release(lst);
                        lst = NULL;
                        break;
                    }
                }
            }
            return lst;
        }

        size_t convertStringArray(Array<String> const& arr, gate_string_t* strarr, gate_size_t strarr_max)
        {
            size_t count = 0;
            for (ArrayList<String>::const_iterator it(arr.begin()), itend(arr.end()); (it != itend) && (strarr_max != 0); ++it)
            {
                gate_string_t const* ptr = it->c_impl();
                gate_string_duplicate(strarr, ptr);
                ++count;
                ++strarr;
                --strarr_max;
            }
            return count;
        }

        void releaseStringArray(gate_string_t* strarr, gate_size_t strarrlength)
        {
            while (strarrlength-- > 0)
            {
                gate_string_release(strarr);
                ++strarr;
            }
        }


    }	//	end of namespace util;

}	//	end of namespace gate