-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.hpp
412 lines (350 loc) · 9.43 KB
/
utils.hpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* <!-- mksqlite: A MATLAB interface to SQLite -->
*
* @file utils.hpp
* @brief Utilities used in all files.
* @details Common utilities.
* (freeing mex memory, utf<->latin conversion, time measurement)
* @see http://note.sonots.com/Mex/Matrix.html
* @authors Martin Kortmann <mail@kortmann.de>,
* Andreas Martin <andimartin@users.sourceforge.net>
* @version 2.14
* @date 2008-2024
* @copyright Distributed under BSD-2
* @pre
* @warning
* @bug
*/
#pragma once
//#include "config.h"
#include "global.hpp"
//#include "locale.hpp"
/* helper functions, formard declarations */
#if defined( MATLAB_MEX_FILE)
char* utils_getString ( const mxArray* str );
size_t utils_elbytes ( mxClassID classID );
void utils_destroy_array ( mxArray *&pmxarr );
template<class T> void utils_free_ptr ( T *&pmxarr );
#endif
int utils_utf2latin ( const unsigned char *s, unsigned char *buffer );
int utils_latin2utf ( const unsigned char *s, unsigned char *buffer );
char* utils_strnewdup ( const char* s, int flagConvertUTF8 );
double utils_get_wall_time ();
double utils_get_cpu_time ();
char* utils_strlwr ( char* );
#ifdef MAIN_MODULE
#if defined( MATLAB_MEX_FILE)
/* Implementations */
/**
* @brief Copy string characters into allocated memory
* @details Not to use with multibyte strings!
*
* @param str MATLAB char array
* @return Pointer to allocated string
*/
char* utils_getString( const mxArray* str )
{
char* result = NULL;
if( str && mxCHAR_CLASS == mxGetClassID( str ) )
{
size_t size = mxGetNumberOfElements( str ) + 1;
result = (char*)MEM_ALLOC( size, 1 );
if( result )
{
mxGetString( str, result, (int)size );
}
}
return result;
}
/**
* @brief Get the size of one element in bytes
*
* @param [in] classID class ID
* @returns size of one element in bytes
*/
size_t utils_elbytes( mxClassID classID )
{
size_t result = 0;
switch (classID)
{
case mxCHAR_CLASS:
result = sizeof(mxChar);
break;
case mxDOUBLE_CLASS:
result = sizeof(double);
break;
case mxSINGLE_CLASS:
result = sizeof(real32_T);
break;
case mxINT8_CLASS:
result = sizeof(int8_T);
break;
case mxUINT8_CLASS:
result = sizeof(uint8_T);
break;
case mxINT16_CLASS:
result = sizeof(int16_T);
break;
case mxUINT16_CLASS:
result = sizeof(uint16_T);
break;
case mxINT32_CLASS:
result = sizeof(int32_T);
break;
case mxUINT32_CLASS:
result = sizeof(uint32_T);
break;
default:
assert( false );
}
return result;
}
#endif
/**
* @brief Convert UTF-8 string to char string
*
* @param [in] s input string UTF8 encoded
* @param [out] buffer optional pointer to where the string should be written (NULL allowed)
* @returns always the count of bytes written (or needed) to convert input string (including NUL)
*/
int utils_utf2latin( const unsigned char *s, unsigned char *buffer = NULL )
{
int cnt = 0;
unsigned char ch, *p = buffer ? buffer : &ch;
if( s )
{
while( *s )
{
if( *s < 128 )
{
*p = *s++;
}
else
{
*p = ( s[0] << 6 ) | ( s[1] & 63 );
s += 2;
}
if( buffer )
{
p++;
}
cnt++;
}
*p = 0;
cnt++;
}
return cnt;
}
/**
* @brief Convert char string to UTF-8 string
*
* @param [in] s input string
* @param [out] buffer optional pointer to where the string should be written (NULL allowed)
* @returns always the count of bytes written (or needed) to convert input string (including NUL)
*/
int utils_latin2utf( const unsigned char *s, unsigned char *buffer = NULL )
{
int cnt = 0;
unsigned char ch[2], *p = buffer ? buffer : ch;
if( s )
{
while( *s )
{
if( *s < 128 )
{
*p++ = *s++;
cnt++;
}
else
{
*p++ = 128 + 64 + ( *s >> 6 );
*p++ = 128 + ( *s++ & 63 );
cnt += 2;
}
if( !buffer )
{
p = ch;
}
}
*p = 0;
cnt++;
}
return cnt;
}
/**
* @brief duplicate a string and recode from UTF8 to char due to flag \p flagConvertUTF8
*
* @param [in] s input string
* @param [in] flagConvertUTF8 String \p s expected UTF8 encoded, if flag is set
* @returns pointer to created duplicate (allocator @ref MEM_ALLOC) and must be freed with @ref MEM_FREE
*/
char* utils_strnewdup(const char* s, int flagConvertUTF8 )
{
char *newstr = 0;
if( flagConvertUTF8 )
{
if( s )
{
/* get memory space needed */
int buflen = utils_utf2latin( (unsigned char*)s, NULL );
newstr = (char*)MEM_ALLOC( buflen, sizeof(char) );
if( newstr )
{
utils_utf2latin( (unsigned char*)s, (unsigned char*)newstr );
}
}
}
else
{
if( s )
{
newstr = (char*)MEM_ALLOC( strlen(s) + 1, sizeof(char) );
if( newstr )
{
strcpy( newstr, s );
}
}
}
return newstr;
}
/**
* @brief Change string to lowercase (inplace)
*
* @param str String
*
* @return String
*/
char* utils_strlwr( char* str )
{
char *p = str;
while( p && ( *p = ::tolower(*p) ) )
{
p++;
}
return str;
}
/**
* @file
* @note
* <HR>
* From the Matlab documentation: \n
*
* mxDestroyArray deallocates the memory occupied by the specified mxArray.
* This includes: \n
* - Characteristics fields of the mxArray, such as size, (m and n), and type.
* - Associated data arrays, such as pr and pi for complex arrays, and ir and jc for sparse arrays.
* - Fields of structure arrays.
* - Cells of cell arrays.
*
* @note
* Do not call mxDestroyArray on an mxArray: \n
* - you return in a left-side argument of a MEX-file.
* - returned by the mxGetField or mxGetFieldByNumber functions.
* - returned by the mxGetCell function.
*
* @note
* <HR>
* Poorly the documentation is missing the issue, whether a NULL pointer may
* be passed or not. For sure a self implementation will be used.
*/
#if defined( MATLAB_MEX_FILE)
/**
* @brief Freeing memory allocated by mxCreateNumericMatrix() or mxCreateNumericArray().
*
* @param [in] pmxarr Memory pointer or NULL
*
* Memory pointer \p pmxarr is set to NULL after deallocation.
*/
void utils_destroy_array( mxArray *&pmxarr )
{
if( pmxarr )
{
// Workaround to avoid MATLAB crash with persistent arrays ("Case 02098404", Lucas Lebert, MathWorks Technical Support Department
mxArray* tmp = pmxarr;
pmxarr = NULL;
mxDestroyArray( tmp );
}
}
#endif
/**
* @brief Freeing memory allocated by mxAlloc() or mxRealloc()
*
* @param [in] pmxarr Memory pointer or NULL
*
* Memory pointer \p pmxarr is set to NULL after deallocation.
*/
template <class T>
void utils_free_ptr( T *&pmxarr )
{
if( pmxarr )
{
T* tmp = pmxarr;
pmxarr = NULL;
MEM_FREE( tmp );
}
}
// Time measuring functions
/**
* @fn utils_get_wall_time
* @brief Returns current counter time in seconds
* @returns Time in seconds
*
* @fn utils_get_cpu_time
* @brief Returns user mode time of current process in seconds
* @returns Time in seconds
*/
// Windows
#ifdef _WIN32
#include <windows.h>
double utils_get_wall_time()
{
LARGE_INTEGER time,freq;
if( !QueryPerformanceFrequency( &freq ) )
{
// Handle error
return 0;
}
if( !QueryPerformanceCounter( &time ) )
{
// Handle error
return 0;
}
return (double)time.QuadPart / freq.QuadPart;
}
double utils_get_cpu_time()
{
FILETIME a,b,c,d;
if( GetProcessTimes( GetCurrentProcess(), &a, &b, &c, &d ) != 0 )
{
// Returns total user time.
// Can be tweaked to include kernel times as well.
return
(double)( d.dwLowDateTime |
( (unsigned long long)d.dwHighDateTime << 32 ) ) * 0.0000001;
}
else
{
// Handle error
return 0;
}
}
// Posix/Linux
#else
#include <time.h>
#include <sys/time.h>
double utils_get_wall_time()
{
struct timeval time;
if( gettimeofday( &time, NULL ) )
{
// Handle error
return 0;
}
return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
double utils_get_cpu_time()
{
return (double)clock() / CLOCKS_PER_SEC;
}
#endif
#endif /* MAIN_MODULE */