mksqlite  2.5
A MATLAB interface to SQLite
locale.hpp
Go to the documentation of this file.
1 
18 #pragma once
19 
20 #include "config.h"
21 //#include "global.hpp"
22 #include "svn_revision.h" /* get the SVN revision number */
23 #include <cstdarg>
24 extern "C"
25 {
26  #include "blosc/blosc.h"
27 }
28 
29 /* Localization, declaration */
30 
31 const char* getLocaleMsg ( int iMsgNr );
32 bool setLocale ( int iLang );
33 int getLocale ();
34 
35 
36 /*
37  * a poor man localization.
38  * every language have a table of messages.
39  */
40 
53 #define MSG_PURESTRING -2
54 #define MSG_NOERROR -1
55 #define MSG_HELLO 0
56 #define MSG_INVALIDDBHANDLE 1
57 #define MSG_IMPOSSIBLE 2
58 #define MSG_USAGE 3
59 #define MSG_INVALIDARG 4
60 #define MSG_CLOSINGFILES 5
61 #define MSG_CANTCOPYSTRING 6
62 #define MSG_NOOPENARG 7
63 #define MSG_NOFREESLOT 8
64 #define MSG_CANTOPEN 9
65 #define MSG_DBNOTOPEN 10
66 #define MSG_INVQUERY 11
67 #define MSG_CANTCREATEOUTPUT 12
68 #define MSG_UNKNWNDBTYPE 13
69 #define MSG_BUSYTIMEOUTFAIL 14
70 #define MSG_MSGUNIQUEWARN 15
71 #define MSG_UNEXPECTEDARG 16
72 #define MSG_MISSINGARGL 17
73 #define MSG_ERRMEMORY 18
74 #define MSG_UNSUPPVARTYPE 19
75 #define MSG_UNSUPPTBH 20
76 #define MSG_ERRPLATFORMDETECT 21
77 #define MSG_WARNDIFFARCH 22
78 #define MSG_BLOBTOOBIG 23
79 #define MSG_ERRCOMPRESSION 24
80 #define MSG_UNKCOMPRESSOR 25
81 #define MSG_ERRCOMPRARG 26
82 #define MSG_ERRCOMPRLOGMINVALS 27
83 #define MSG_ERRUNKOPENMODE 28
84 #define MSG_ERRUNKTHREADMODE 29
85 #define MSG_ERRCANTCLOSE 30
86 #define MSG_ERRCLOSEDBS 31
87 #define MSG_ERRNOTSUPPORTED 32
88 #define MSG_EXTENSION_EN 33
89 #define MSG_EXTENSION_DIS 34
90 #define MSG_EXTENSION_FAIL 35
91 #define MSG_MISSINGARG 36
92 #define MSG_MISSINGARG_CELL 37
93 #define MSG_MISSINGARG_STRUCT 38
94 #define MSG_NUMARGEXPCT 39
95 #define MSG_SINGLECELLNOTALLOWED 40
96 #define MSG_SINGLESTRUCTNOTALLOWED 41
97 #define MSG_ERRVARNAME 42
98 #define MSG_STREAMINGNEEDTYBLOBS 43
99 #define MSG_STREAMINGNOTSUPPORTED 44
100 #define MSG_RESULTTYPE 45
101 #define MSG_DBID_SUPFLOUS 46
102 #define MSG_FCNHARGEXPCT 47
103 #define MSG_LITERALARGEXPCT 48
104 #define MSG_RECURSIVECALL 49
105 #define MSG_INVALIDFUNCTION 50
106 #define MSG_ERRNULLDBID 51
107 #define MSG_ERRINTERNAL 52
108 #define MSG_ABORTED 53
109 
116 class Err
117 {
118  int m_msgId;
119  char m_shared_msg[1024];
120  const char* m_static_msg;
121  const char* m_err_id;
122  bool m_isPending;
123 
124 public:
125 
127  Err()
128  {
129  clear();
130  }
131 
132 
134  void clear()
135  {
136  m_msgId = MSG_NOERROR;
137  m_static_msg = m_shared_msg;
138  m_err_id = "";
139  *m_shared_msg = 0; // not used and thus emptied
140  m_isPending = false;
141  }
142 
143 
150  void set( const char* strMsg, const char* strId = NULL )
151  {
152  if( !strMsg )
153  {
154  clear();
155  }
156  else
157  {
158  m_msgId = MSG_PURESTRING;
159  m_static_msg = strMsg;
160  m_err_id = strId;
161  *m_shared_msg = 0; // not used and thus emptied
162  m_isPending = true;
163  }
164  }
165 
166 
172  void set( char* strMsg, const char* strId = NULL )
173  {
174  m_err_id = strId;
175 
176  if( !strMsg )
177  {
178  clear();
179  }
180  else
181  {
182  m_msgId = MSG_PURESTRING;
183  m_static_msg = m_shared_msg;
184  m_isPending = true;
185 
186  _snprintf( m_shared_msg, sizeof(m_shared_msg), "%s", strMsg );
187  }
188  }
189 
190 
197  void set( int iMessageNr, const char* strId = NULL )
198  {
199  m_msgId = iMessageNr;
200 
201  if( iMessageNr == MSG_NOERROR )
202  {
203  clear();
204  }
205  else
206  {
207  set( ::getLocaleMsg( iMessageNr ), strId );
208  }
209  }
210 
211 
218  void set_printf( int iMessageNr, const char* strId, ... )
219  {
220  va_list va;
221  va_start( va, strId );
222  const char* message = ::getLocaleMsg( iMessageNr );
223 
224  m_msgId = iMessageNr;
225  *m_shared_msg = 0;
226 
227  if( message )
228  {
229  vsnprintf( m_shared_msg, sizeof( m_shared_msg ), message, va );
230  }
231  set( m_shared_msg, strId );
232 
233  va_end( va );
234  }
235 
236 
243  void set_printf( const char* fmt, const char* strId, ... )
244  {
245  va_list va;
246  va_start( va, strId );
247 
248  m_msgId = MSG_PURESTRING;
249  *m_shared_msg = 0;
250 
251  if( fmt )
252  {
253  vsnprintf( m_shared_msg, sizeof( m_shared_msg ), fmt, va );
254  }
255  set( m_shared_msg, strId );
256 
257  va_end( va );
258  }
259 
260 
266  const char* get( const char** errId = NULL )
267  {
268  if( errId )
269  {
270  *errId = m_err_id;
271  }
272 
273  return m_static_msg;
274  }
275 
276 
278  int getMsgId()
279  {
280  return m_msgId;
281  }
282 
283 
285  bool isPending()
286  {
287  return m_isPending;
288  }
289 
290 
292  void warn( int iMessageNr )
293  {
294  mexWarnMsgTxt( ::getLocaleMsg( iMessageNr ) );
295  }
296 
297 };
298 
299 
300 
301 #ifdef MAIN_MODULE
302 
303 /* Implementations */
304 
308 static const char* messages_0[] =
309 {
310  "mksqlite Version " CONFIG_MKSQLITE_VERSION_STRING " " SVNREV ", an interface from MATLAB to SQLite\n"
311  "(c) 2008-2017 by Martin Kortmann <mail@kortmann.de>\n"
312  " Andreas Martin <andimartin@users.sourceforge.net>\n"
313  "based on SQLite Version %s - http://www.sqlite.org\n"
314  "mksqlite utilizes:\n"
315  " - DEELX perl compatible regex engine Version " DEELX_VERSION_STRING " (Sswater@gmail.com)\n"
316  " - BLOSC/LZ4 " BLOSC_VERSION_STRING " compression algorithm (Francesc Alted / Yann Collett) \n"
317  " - MD5 Message-Digest Algorithm (RFC 1321) implementation by Alexander Peslyak\n"
318  " \n",
319 
320 /* 1*/ "invalid database handle",
321 /* 2*/ "function not possible",
322 /* 3*/ "usage: mksqlite([dbid,] command [, databasefile])\n",
323 /* 4*/ "no or wrong argument",
324 /* 5*/ "mksqlite: closing open databases",
325 /* 6*/ "can\'t copy string in getstring()",
326 /* 7*/ "open without database name",
327 /* 8*/ "no free database handle available",
328 /* 9*/ "cannot open database (check access privileges and existence of database)",
329 /* 10*/ "database not open",
330 /* 11*/ "invalid query string (semicolon?)",
331 /* 12*/ "cannot create output matrix",
332 /* 13*/ "unknown SQLITE data type",
333 /* 14*/ "cannot set busy timeout",
334 /* 15*/ "could not build unique field name for %s",
335 /* 16*/ "unexpected arguments passed",
336 /* 17*/ "missing argument list",
337 /* 18*/ "memory allocation error",
338 /* 19*/ "unsupported variable type",
339 /* 20*/ "unknown/unsupported typed blob header",
340 /* 21*/ "error while detecting the type of computer you are using",
341 /* 22*/ "BLOB stored on different type of computer",
342 /* 23*/ "BLOB exceeds maximum allowed size",
343 /* 24*/ "error while compressing data",
344 /* 25*/ "unknown compressor",
345 /* 26*/ "chosen compressor accepts 'double' type only",
346 /* 27*/ "chosen compressor accepts positive values only",
347 /* 28*/ "unknown open modus (only 'ro', 'rw' or 'rwc' accepted)",
348 /* 29*/ "unknown threading mode (only 'single', 'multi' or 'serial' accepted)",
349 /* 30*/ "cannot close connection",
350 /* 31*/ "not all connections could be closed",
351 /* 32*/ "this Matlab version doesn't support this feature",
352 /* 33*/ "extension loading enabled for this db",
353 /* 34*/ "extension loading disabled for this db",
354 /* 35*/ "failed to set extension loading feature",
355 /* 36*/ "more argument(s) expected",
356 /* 37*/ "more argument(s) expected (maybe matrix argument given, instead of a cell array?)",
357 /* 38*/ "missing field in argument for SQL parameter '%s'",
358 /* 39*/ "numeric argument expected",
359 /* 40*/ "single cell argument not allowed when streaming is enabled while multiple\nSQL parameters are used or parameter wrapping is enabled, too",
360 /* 41*/ "single struct argument not allowed when streaming is enabled while multiple\nSQL parameters are used or parameter wrapping is enabled, too",
361 /* 42*/ "unable to create fieldname from column name",
362 /* 43*/ "streaming of variables needs typed BLOBs! Streaming is off",
363 /* 44*/ "streaming not supported in this MATLAB version",
364 /* 45*/ "result type is ",
365 /* 46*/ "database ID is given, but superflous!",
366 /* 47*/ "function handle expected!",
367 /* 48*/ "string argument expected!",
368 /* 49*/ "recursive application-defined functions not allowed!",
369 /* 50*/ "invalid function!",
370 /* 51*/ "dbid of 0 only allowed for commands 'open' and 'close'!",
371 /* 52*/ "Internal error!",
372 /* 53*/ "Aborted (Ctrl+C)!",
373 };
374 
375 
379 static const char* messages_1[] =
380 {
381  "mksqlite Version " CONFIG_MKSQLITE_VERSION_STRING " " SVNREV ", ein MATLAB Interface zu SQLite\n"
382  "(c) 2008-2017 by Martin Kortmann <mail@kortmann.de>\n"
383  " Andreas Martin <andimartin@users.sourceforge.net>\n"
384  "basierend auf SQLite Version %s - http://www.sqlite.org\n"
385  "mksqlite verwendet:\n"
386  " - DEELX perl kompatible regex engine Version " DEELX_VERSION_STRING " (Sswater@gmail.com)\n"
387  " - BLOSC/LZ4 " BLOSC_VERSION_STRING " zur Datenkompression (Francesc Alted / Yann Collett) \n"
388  " - MD5 Message-Digest Algorithm (RFC 1321) Implementierung von Alexander Peslyak\n"
389  " \n",
390 
391 /* 1*/ "ungueltiger Datenbankhandle",
392 /* 2*/ "Funktion nicht moeglich",
393 /* 3*/ "Verwendung: mksqlite([dbid,] Befehl [, Datenbankdatei])\n",
394 /* 4*/ "kein oder falsches Argument uebergeben",
395 /* 5*/ "mksqlite: Die noch geoeffneten Datenbanken wurden geschlossen",
396 /* 6*/ "getstring() kann keine neue Zeichenkette erstellen",
397 /* 7*/ "Open Befehl ohne Datenbanknamen",
398 /* 8*/ "kein freier Datenbankhandle verfuegbar",
399 /* 9*/ "Datenbank konnte nicht geoeffnet werden (ggf. Zugriffsrechte oder Existenz der Datenbank pruefen)",
400 /* 10*/ "Datenbank nicht geoeffnet",
401 /* 11*/ "ungueltiger query String (Semikolon?)",
402 /* 12*/ "kann Ausgabematrix nicht erstellen",
403 /* 13*/ "unbekannter SQLITE Datentyp",
404 /* 14*/ "busytimeout konnte nicht gesetzt werden",
405 /* 15*/ "konnte keinen eindeutigen Bezeichner fuer Feld %s bilden",
406 /* 16*/ "Argumentliste zu lang",
407 /* 17*/ "keine Argumentliste angegeben",
408 /* 18*/ "Fehler im Speichermanagement",
409 /* 19*/ "nicht unterstuetzter Variablentyp",
410 /* 20*/ "unbekannter oder nicht unterstuetzter typisierter BLOB Header",
411 /* 21*/ "Fehler beim Identifizieren der Computerarchitektur",
412 /* 22*/ "BLOB wurde mit abweichender Computerarchitektur erstellt",
413 /* 23*/ "BLOB ist zu gross",
414 /* 24*/ "Fehler waehrend der Kompression aufgetreten",
415 /* 25*/ "unbekannte Komprimierung",
416 /* 26*/ "gewaehlter Kompressor erlaubt nur Datentyp 'double'",
417 /* 27*/ "gewaehlter Kompressor erlaubt nur positive Werte",
418 /* 28*/ "unbekannter Zugriffmodus (nur 'ro', 'rw' oder 'rwc' moeglich)",
419 /* 29*/ "unbekannter Threadingmodus (nur 'single', 'multi' oder 'serial' moeglich)",
420 /* 30*/ "die Datenbank kann nicht geschlossen werden",
421 /* 31*/ "nicht alle Datenbanken konnten geschlossen werden",
422 /* 32*/ "Feature wird von dieser Matlab Version nicht unterstuetzt",
423 /* 33*/ "DLL Erweiterungen fuer diese db aktiviert",
424 /* 34*/ "DLL Erweiterungen fuer diese db deaktiviert",
425 /* 35*/ "Einstellung fuer DLL Erweiterungen nicht moeglich",
426 /* 36*/ "Argumentliste zu kurz",
427 /* 37*/ "Argumentliste zu kurz (moeglicherweise eine Matrix statt Cell-Array uebergeben?)",
428 /* 38*/ "Feld fuer SQL Parameter '%s' fehlt",
429 /* 39*/ "numerischer Parameter erwartet",
430 /* 40*/ "einzelnes Argument vom Typ Cell nicht erlaubt, bei aktiviertem Streaming mit\nmehreren SQL Parametern oder ebenfalls aktiviertem Parameter Wrapping",
431 /* 41*/ "einzelnes Argument vom Typ Struct nicht erlaubt, bei aktiviertem Streaming mit\nmehreren SQL Parametern oder ebenfalls aktiviertem Parameter Wrapping",
432 /* 42*/ "aus dem Spaltennamen konnte kein gueltiger Feldname erzeugt werden",
433 /* 43*/ "fuer das Streamen von Variablen sind typisierte BLOBS erforderlich! Streaming ist ausgeschaltet",
434 /* 44*/ "Streaming wird von dieser MATLAB Version nicht unterstuetzt",
435 /* 45*/ "Rueckgabetyp ist ",
436 /* 46*/ "Datenbank ID wurde angegeben, ist fuer diesen Befehl jedoch ueberfluessig! ",
437 /* 47*/ "Funktionshandle erwartet! ",
438 /* 48*/ "String Argument erwartet! ",
439 /* 49*/ "unzulaessiger rekursiver Funktionsaufruf! ",
440 /* 50*/ "ungueltige Funktion! ",
441 /* 51*/ "0 als dbid ist nur fuer die Befehle 'open' und 'close' erlaubt! ",
442 /* 52*/ "Interner Fehler! ",
443 /* 53*/ "Ausfuehrung abgebrochen (Ctrl+C)!",
444 };
445 
450 const char* STR_RESULT_TYPES[] = {
451  "array of structs", // RESULT_TYPE_ARRAYOFSTRUCTS
452  "struct of arrays", // RESULT_TYPE_STRUCTOFARRAYS
453  "matrix/cell array" // RESULT_TYPE_MATRIX
454 };
455 
456 
462 static int Language = -1;
463 
464 
468 static const char **messages[] =
469 {
470  messages_0, /* English translations */
471  messages_1 /* German translations */
472 };
473 
474 
480 const char* getLocaleMsg( int iMsgNr )
481 {
482  if(iMsgNr < 0)
483  {
484  switch( getLocale() )
485  {
486  case 1:
487  return "Unbekanner Fehler!";
488  default:
489  return "Unspecified error!";
490  }
491  }
492  else
493  {
494  return messages[Language][iMsgNr];
495  }
496 }
497 
498 
504 bool setLocale( int iLang )
505 {
506  if( iLang >=0 && iLang < sizeof(messages) / sizeof(messages[0]) )
507  {
508  Language = iLang;
509  return true;
510  } else {
511  return false;
512  }
513 }
514 
515 
520 {
521  return Language;
522 }
523 
524 #endif
char m_shared_msg[1024]
(Shared) text buffer for non-const (generated) messages
Definition: locale.hpp:119
bool setLocale(int iLang)
Sets the current locale.
Definition: locale.hpp:504
void warn(int iMessageNr)
Omits a warning with the current error message.
Definition: locale.hpp:292
void set_printf(int iMessageNr, const char *strId,...)
Set error message by identifier (translations available) with printf arguments.
Definition: locale.hpp:218
Err()
Constructor.
Definition: locale.hpp:127
static const char * messages_0[]
Message table for english translations (Language==0)
Definition: locale.hpp:308
Global configuration settings and defaults.
const char * getLocaleMsg(int iMsgNr)
Returns the translation for a defined message.
Definition: locale.hpp:480
void set_printf(const char *fmt, const char *strId,...)
Set error message by format string with arguments.
Definition: locale.hpp:243
const char * STR_RESULT_TYPES[]
Text representations.
Definition: locale.hpp:450
static const char * messages_1[]
Message table for german translations (Language=1)
Definition: locale.hpp:379
bool isPending()
Returns true, if the current error message is still not handled.
Definition: locale.hpp:285
void clear()
Reset error message.
Definition: locale.hpp:134
static const char ** messages[]
Message Tables.
Definition: locale.hpp:468
Helperclass for error message transport.
Definition: locale.hpp:116
int getLocale()
Get current locale id.
Definition: locale.hpp:519
static int Language
Number of language in use.
Definition: locale.hpp:462
const char * m_err_id
Holds the error id (for MATLAB exception handling f.e., see MSG_IDS)
Definition: locale.hpp:121
const char * m_static_msg
Holds pointer to static message text.
Definition: locale.hpp:120
#define CONFIG_MKSQLITE_VERSION_STRING
mksqlite version string
Definition: config.h:54
int getMsgId()
Get the current message identifier.
Definition: locale.hpp:278
int m_msgId
Message ID (see Message Identifiers)
Definition: locale.hpp:118
bool m_isPending
Message has still to be handled if this flag is set.
Definition: locale.hpp:122