File indexing completed on 2025-02-23 05:15:21
0001 // 0002 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney 0003 // Distributed under the Boost Software License, Version 1.0. 0004 // (See accompanying file LICENSE_1_0.txt or copy at 0005 // http://www.boost.org/LICENSE_1_0.txt) 0006 // 0007 0008 #define SOCI_SQLITE3_SOURCE 0009 #include "soci/sqlite3/soci-sqlite3.h" 0010 0011 #include <algorithm> 0012 #include <cstring> 0013 0014 using namespace soci; 0015 0016 sqlite3_blob_backend::sqlite3_blob_backend(sqlite3_session_backend &session) 0017 : session_(session), buf_(0), len_(0) 0018 { 0019 } 0020 0021 sqlite3_blob_backend::~sqlite3_blob_backend() 0022 { 0023 if (buf_) 0024 { 0025 delete [] buf_; 0026 buf_ = 0; 0027 len_ = 0; 0028 } 0029 } 0030 0031 std::size_t sqlite3_blob_backend::get_len() 0032 { 0033 return len_; 0034 } 0035 0036 std::size_t sqlite3_blob_backend::read_from_start(char * buf, std::size_t toRead, std::size_t offset) 0037 { 0038 size_t r = toRead; 0039 0040 // make sure that we don't try to read 0041 // past the end of the data 0042 if (r > len_ - offset) 0043 { 0044 r = len_ - offset; 0045 } 0046 0047 memcpy(buf, buf_ + offset, r); 0048 0049 return r; 0050 } 0051 0052 0053 std::size_t sqlite3_blob_backend::write_from_start(char const * buf, std::size_t toWrite, std::size_t offset) 0054 { 0055 const char* oldBuf = buf_; 0056 std::size_t oldLen = len_; 0057 len_ = (std::max)(len_, offset + toWrite); 0058 0059 buf_ = new char[len_]; 0060 0061 if (oldBuf) 0062 { 0063 // we need to copy both old and new buffers 0064 // it is possible that the new does not 0065 // completely cover the old 0066 memcpy(buf_, oldBuf, oldLen); 0067 delete [] oldBuf; 0068 } 0069 memcpy(buf_ + offset, buf, toWrite); 0070 0071 return len_; 0072 } 0073 0074 0075 std::size_t sqlite3_blob_backend::append( 0076 char const * buf, std::size_t toWrite) 0077 { 0078 const char* oldBuf = buf_; 0079 0080 buf_ = new char[len_ + toWrite]; 0081 0082 memcpy(buf_, oldBuf, len_); 0083 0084 memcpy(buf_ + len_, buf, toWrite); 0085 0086 delete [] oldBuf; 0087 0088 len_ += toWrite; 0089 0090 return len_; 0091 } 0092 0093 0094 void sqlite3_blob_backend::trim(std::size_t newLen) 0095 { 0096 const char* oldBuf = buf_; 0097 len_ = newLen; 0098 0099 buf_ = new char[len_]; 0100 0101 memcpy(buf_, oldBuf, len_); 0102 0103 delete [] oldBuf; 0104 } 0105 0106 std::size_t sqlite3_blob_backend::set_data(char const *buf, std::size_t toWrite) 0107 { 0108 if (buf_) 0109 { 0110 delete [] buf_; 0111 buf_ = 0; 0112 len_ = 0; 0113 } 0114 return write_from_start(buf, toWrite); 0115 }