mksqlite  2.5
A MATLAB interface to SQLite
utils.hpp
Go to the documentation of this file.
1 
19 #pragma once
20 
21 //#include "config.h"
22 #include "global.hpp"
23 //#include "locale.hpp"
24 
25 /* helper functions, formard declarations */
26 #if defined( MATLAB_MEX_FILE)
27  char* utils_getString ( const mxArray* str );
28  size_t utils_elbytes ( mxClassID classID );
29  void utils_destroy_array ( mxArray *&pmxarr );
30 template<class T> void utils_free_ptr ( T *&pmxarr );
31 #endif
32  int utils_utf2latin ( const unsigned char *s, unsigned char *buffer );
33  int utils_latin2utf ( const unsigned char *s, unsigned char *buffer );
34  char* utils_strnewdup ( const char* s, int flagConvertUTF8 );
35  double utils_get_wall_time ();
36  double utils_get_cpu_time ();
37  char* utils_strlwr ( char* );
38 
39 
40 #ifdef MAIN_MODULE
41 
42 #if defined( MATLAB_MEX_FILE)
43 /* Implementations */
44 
52 char* utils_getString( const mxArray* str )
53 {
54  char* result = NULL;
55 
56  if( str && mxCHAR_CLASS == mxGetClassID( str ) )
57  {
58  size_t size = mxGetNumberOfElements( str ) + 1;
59  result = (char*)MEM_ALLOC( size, 1 );
60  if( result )
61  {
62  mxGetString( str, result, (int)size );
63  }
64  }
65 
66  return result;
67 }
68 
75 size_t utils_elbytes( mxClassID classID )
76 {
77  size_t result = 0;
78 
79  switch (classID)
80  {
81 
82  case mxCHAR_CLASS:
83  result = sizeof(mxChar);
84  break;
85 
86  case mxDOUBLE_CLASS:
87  result = sizeof(double);
88  break;
89 
90  case mxSINGLE_CLASS:
91  result = sizeof(real32_T);
92  break;
93 
94  case mxINT8_CLASS:
95  result = sizeof(int8_T);
96  break;
97 
98  case mxUINT8_CLASS:
99  result = sizeof(uint8_T);
100  break;
101 
102  case mxINT16_CLASS:
103  result = sizeof(int16_T);
104  break;
105 
106  case mxUINT16_CLASS:
107  result = sizeof(uint16_T);
108  break;
109 
110  case mxINT32_CLASS:
111  result = sizeof(int32_T);
112  break;
113 
114  case mxUINT32_CLASS:
115  result = sizeof(uint32_T);
116  break;
117 
118  default:
119  assert( false );
120  }
121 
122  return result;
123 }
124 #endif
125 
133 int utils_utf2latin( const unsigned char *s, unsigned char *buffer = NULL )
134 {
135  int cnt = 0;
136  unsigned char ch, *p = buffer ? buffer : &ch;
137 
138  if( s )
139  {
140  while( *s )
141  {
142  if( *s < 128 )
143  {
144  *p = *s++;
145  }
146  else
147  {
148  *p = ( s[0] << 6 ) | ( s[1] & 63 );
149  s += 2;
150  }
151  if( buffer )
152  {
153  p++;
154  }
155  cnt++;
156  }
157  *p = 0;
158  cnt++;
159  }
160 
161  return cnt;
162 }
163 
171 int utils_latin2utf( const unsigned char *s, unsigned char *buffer = NULL )
172 {
173  int cnt = 0;
174  unsigned char ch[2], *p = buffer ? buffer : ch;
175 
176  if( s )
177  {
178  while( *s )
179  {
180  if( *s < 128 )
181  {
182  *p++ = *s++;
183  cnt++;
184  }
185  else
186  {
187  *p++ = 128 + 64 + ( *s >> 6 );
188  *p++ = 128 + ( *s++ & 63 );
189  cnt += 2;
190  }
191  if( !buffer )
192  {
193  p = ch;
194  }
195  }
196  *p = 0;
197  cnt++;
198  }
199 
200  return cnt;
201 }
202 
203 
211 char* utils_strnewdup(const char* s, int flagConvertUTF8 )
212 {
213  char *newstr = 0;
214 
215  if( flagConvertUTF8 )
216  {
217  if( s )
218  {
219  /* get memory space needed */
220  int buflen = utils_utf2latin( (unsigned char*)s, NULL );
221 
222  newstr = (char*)MEM_ALLOC( buflen, sizeof(char) );
223  if( newstr )
224  {
225  utils_utf2latin( (unsigned char*)s, (unsigned char*)newstr );
226  }
227  }
228  }
229  else
230  {
231  if( s )
232  {
233  newstr = (char*)MEM_ALLOC( strlen(s) + 1, sizeof(char) );
234  if( newstr )
235  {
236  strcpy( newstr, s );
237  }
238  }
239 
240  }
241 
242  return newstr;
243 }
244 
245 
253 char* utils_strlwr( char* str )
254 {
255  char *p = str;
256 
257  while( p && ( *p = ::tolower(*p) ) )
258  {
259  p++;
260  }
261 
262  return str;
263 }
264 
265 
266 
294 #if defined( MATLAB_MEX_FILE)
295 
302 void utils_destroy_array( mxArray *&pmxarr )
303 {
304  if( pmxarr )
305  {
306  // Workaround to avoid MATLAB crash with persistent arrays ("Case 02098404", Lucas Lebert, MathWorks Technical Support Department
307  mxArray* tmp = pmxarr;
308  pmxarr = NULL;
309  mxDestroyArray( tmp );
310  }
311 }
312 #endif
313 
321 template <class T>
322 void utils_free_ptr( T *&pmxarr )
323 {
324  if( pmxarr )
325  {
326  T* tmp = pmxarr;
327  pmxarr = NULL;
328  MEM_FREE( tmp );
329  }
330 }
331 
332 
333 
334 // Time measuring functions
335 
346 // Windows
347 #ifdef _WIN32
348 #include <windows.h>
349 
351 {
352  LARGE_INTEGER time,freq;
353 
354  if( !QueryPerformanceFrequency( &freq ) )
355  {
356  // Handle error
357  return 0;
358  }
359 
360  if( !QueryPerformanceCounter( &time ) )
361  {
362  // Handle error
363  return 0;
364  }
365 
366  return (double)time.QuadPart / freq.QuadPart;
367 }
368 
369 
371 {
372  FILETIME a,b,c,d;
373  if( GetProcessTimes( GetCurrentProcess(), &a, &b, &c, &d ) != 0 )
374  {
375  // Returns total user time.
376  // Can be tweaked to include kernel times as well.
377  return
378  (double)( d.dwLowDateTime |
379  ( (unsigned long long)d.dwHighDateTime << 32 ) ) * 0.0000001;
380  }
381  else
382  {
383  // Handle error
384  return 0;
385  }
386 }
387 
388 // Posix/Linux
389 #else
390 #include <time.h>
391 #include <sys/time.h>
392 double utils_get_wall_time()
393 {
394  struct timeval time;
395 
396  if( gettimeofday( &time, NULL ) )
397  {
398  // Handle error
399  return 0;
400  }
401 
402  return (double)time.tv_sec + (double)time.tv_usec * .000001;
403 }
404 
405 double utils_get_cpu_time()
406 {
407  return (double)clock() / CLOCKS_PER_SEC;
408 }
409 #endif
410 
411 
412 #endif /* MAIN_MODULE */
char * utils_strlwr(char *)
Change string to lowercase (inplace)
Definition: utils.hpp:253
size_t utils_elbytes(mxClassID classID)
Get the size of one element in bytes.
Definition: utils.hpp:75
double utils_get_wall_time()
Returns current counter time in seconds.
Definition: utils.hpp:350
double utils_get_cpu_time()
Returns user mode time of current process in seconds.
Definition: utils.hpp:370
#define MEM_FREE(ptr)
standard memory free function
Definition: global.hpp:158
void utils_destroy_array(mxArray *&pmxarr)
Freeing memory allocated by mxCreateNumericMatrix() or mxCreateNumericArray().
Definition: utils.hpp:302
int utils_utf2latin(const unsigned char *s, unsigned char *buffer)
Convert UTF-8 string to char string.
Definition: utils.hpp:133
void utils_free_ptr(T *&pmxarr)
Freeing memory allocated by mxAlloc() or mxRealloc()
Definition: utils.hpp:322
char * utils_strnewdup(const char *s, int flagConvertUTF8)
duplicate a string and recode from UTF8 to char due to flag flagConvertUTF8
Definition: utils.hpp:211
int utils_latin2utf(const unsigned char *s, unsigned char *buffer)
Convert char string to UTF-8 string.
Definition: utils.hpp:171
Global definitions.
char * utils_getString(const mxArray *str)
Copy string characters into allocated memory.
Definition: utils.hpp:52
#define MEM_ALLOC(count, bytes)
standard memory allocator
Definition: global.hpp:156