libsqlite3x
2007.10.18
|
00001 /* 00002 Copyright (C) 2004-2005 Cory Nelson 00003 00004 This software is provided 'as-is', without any express or implied 00005 warranty. In no event will the authors be held liable for any damages 00006 arising from the use of this software. 00007 00008 Permission is granted to anyone to use this software for any purpose, 00009 including commercial applications, and to alter it and redistribute it 00010 freely, subject to the following restrictions: 00011 00012 1. The origin of this software must not be misrepresented; you must not 00013 claim that you wrote the original software. If you use this software 00014 in a product, an acknowledgment in the product documentation would be 00015 appreciated but is not required. 00016 2. Altered source versions must be plainly marked as such, and must not be 00017 misrepresented as being the original software. 00018 3. This notice may not be removed or altered from any source distribution. 00019 00020 CVS Info : 00021 $Author: sgbeal $ 00022 $Date: 2007/02/26 21:33:39 $ 00023 $Revision: 1.6 $ 00024 */ 00025 00026 #include <sqlite3.h> 00027 #include "sqlite3x.hpp" 00028 #include <cstdarg> // varargs handling 00029 #include <limits> // std::max() 00030 #include <cstring> // strlen() 00031 #include <cstdio> // vsnprintf() 00032 #include <vector> 00033 namespace sqlite3x { 00034 00035 database_error::~database_error() throw() {} 00036 00037 database_error::database_error(sqlite3_connection &con) 00038 : m_what( "sqlite3_connection["+con.name()+"]: "+con.errormsg() ) 00039 { 00040 } 00041 00042 char const * database_error::what() const throw() 00043 { 00044 return this->m_what.c_str(); 00045 } 00046 00047 database_error::database_error(const char *format,...) 00048 { 00049 const int buffsz = static_cast<int>( std::max( (size_t) 2048, strlen(format) * 2 ) ); 00050 std::vector<char> buffer( buffsz, '\0' ); 00051 va_list vargs; 00052 va_start ( vargs, format ); 00053 int size = vsnprintf(&buffer[0], buffsz, format, vargs); 00054 va_end( vargs ); 00055 if (size > (buffsz-1)) 00056 { 00057 // replace tail of msg with "..." 00058 size = buffsz-1; 00059 for( int i = buffsz-4; i < buffsz-1; ++i ) 00060 { 00061 buffer[i] = '.'; 00062 } 00063 } 00064 buffer[size] = '\0'; 00065 this->m_what = std::string( &buffer[0], &buffer[0]+size ); 00066 } 00067 }