libsqlite3x  2007.10.18
sqlite3x_exception.cpp
1 /*
2  Copyright (C) 2004-2005 Cory Nelson
3 
4  This software is provided 'as-is', without any express or implied
5  warranty. In no event will the authors be held liable for any damages
6  arising from the use of this software.
7 
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it
10  freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16  2. Altered source versions must be plainly marked as such, and must not be
17  misrepresented as being the original software.
18  3. This notice may not be removed or altered from any source distribution.
19 
20  CVS Info :
21  $Author: sgbeal $
22  $Date: 2007/02/26 21:33:39 $
23  $Revision: 1.6 $
24 */
25 
26 #include <sqlite3.h>
27 #include "sqlite3x.hpp"
28 #include <cstdarg> // varargs handling
29 #include <limits> // std::max()
30 #include <cstring> // strlen()
31 #include <cstdio> // vsnprintf()
32 #include <vector>
33 namespace sqlite3x {
34 
35  database_error::~database_error() throw() {}
36 
38  : m_what( "sqlite3_connection["+con.name()+"]: "+con.errormsg() )
39  {
40  }
41 
42  char const * database_error::what() const throw()
43  {
44  return this->m_what.c_str();
45  }
46 
47  database_error::database_error(const char *format,...)
48  {
49  const int buffsz = static_cast<int>( std::max( (size_t) 2048, strlen(format) * 2 ) );
50  std::vector<char> buffer( buffsz, '\0' );
51  va_list vargs;
52  va_start ( vargs, format );
53  int size = vsnprintf(&buffer[0], buffsz, format, vargs);
54  va_end( vargs );
55  if (size > (buffsz-1))
56  {
57  // replace tail of msg with "..."
58  size = buffsz-1;
59  for( int i = buffsz-4; i < buffsz-1; ++i )
60  {
61  buffer[i] = '.';
62  }
63  }
64  buffer[size] = '\0';
65  this->m_what = std::string( &buffer[0], &buffer[0]+size );
66  }
67 }
virtual char const * what() const
Returns this object&#39;s error string.
database_error(const char *format,...)
Takes a format specifier compatible with printf.
This namespace encapsulates a C++ API wrapper for sqlite3 databases.
Definition: sqlite3x.hpp:120
Represents a connection to an sqlite3 database.
Definition: sqlite3x.hpp:148