SOCI Logo Get SOCI at SourceForge.net. Fast, secure and Free Open Source software downloads

blob.cpp

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
00003 // Distributed under the Boost Software License, Version 1.0.
00004 // (See accompanying file LICENSE_1_0.txt or copy at
00005 // http://www.boost.org/LICENSE_1_0.txt)
00006 //
00007 
00008 #include "soci-sqlite3.h"
00009 #include <cstring>
00010 
00011 using namespace soci;
00012 
00013 sqlite3_blob_backend::sqlite3_blob_backend(sqlite3_session_backend &session)
00014     : session_(session), buf_(0), len_(0)
00015 {
00016 }
00017 
00018 sqlite3_blob_backend::~sqlite3_blob_backend()
00019 {
00020     if (buf_)
00021     {
00022         delete [] buf_;
00023         buf_ = 0;
00024         len_ = 0;
00025     }
00026 }
00027 
00028 std::size_t sqlite3_blob_backend::get_len()
00029 {
00030     return len_;
00031 }
00032 
00033 std::size_t sqlite3_blob_backend::read(
00034     std::size_t offset, char * buf, std::size_t toRead)
00035 {
00036     size_t r = toRead;
00037 
00038     // make sure that we don't try to read
00039     // past the end of the data
00040     if (r > len_ - offset)
00041     {
00042         r = len_ - offset;
00043     }
00044 
00045     memcpy(buf, buf_ + offset, r);
00046 
00047     return r;
00048 }
00049 
00050 
00051 std::size_t sqlite3_blob_backend::write(
00052     std::size_t offset, char const * buf,
00053     std::size_t toWrite)
00054 {
00055     const char* oldBuf = buf_;
00056     std::size_t oldLen = len_;
00057     len_ = (std::max)(len_, offset + toWrite);
00058 
00059     buf_ = new char[len_];
00060 
00061     if (oldBuf)
00062     {
00063         // we need to copy both old and new buffers
00064         // it is possible that the new does not
00065         // completely cover the old
00066         memcpy(buf_, oldBuf, oldLen);
00067         delete [] oldBuf;
00068     }
00069     memcpy(buf_ + offset, buf, len_);
00070 
00071     return len_;
00072 }
00073 
00074 
00075 std::size_t sqlite3_blob_backend::append(
00076     char const * buf, std::size_t toWrite)
00077 {
00078     const char* oldBuf = buf_;
00079 
00080     buf_ = new char[len_ + toWrite];
00081 
00082     memcpy(buf_, oldBuf, len_);
00083 
00084     memcpy(buf_ + len_, buf, toWrite);
00085 
00086     delete [] oldBuf;
00087 
00088     len_ += toWrite;
00089 
00090     return len_;
00091 }
00092 
00093 
00094 void sqlite3_blob_backend::trim(std::size_t newLen)
00095 {
00096     const char* oldBuf = buf_;
00097     len_ = newLen;
00098 
00099     buf_ = new char[len_];
00100 
00101     memcpy(buf_, oldBuf, len_);
00102 
00103     delete [] oldBuf;
00104 }
00105 
00106 std::size_t sqlite3_blob_backend::set_data(char const *buf, std::size_t toWrite)
00107 {
00108     if (buf_)
00109     {
00110         delete [] buf_;
00111         buf_ = 0;
00112         len_ = 0;
00113     }
00114     return write(0, buf, toWrite);
00115 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
SourceForge Logo

Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1