backends/sqlite3/blob.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #include "soci-sqlite3.h"
9 #include <algorithm>
10 #include <cstring>
11 
12 using namespace soci;
13 
15  : session_(session), buf_(0), len_(0)
16 {
17 }
18 
20 {
21  if (buf_)
22  {
23  delete [] buf_;
24  buf_ = 0;
25  len_ = 0;
26  }
27 }
28 
30 {
31  return len_;
32 }
33 
35  std::size_t offset, char * buf, std::size_t toRead)
36 {
37  size_t r = toRead;
38 
39  // make sure that we don't try to read
40  // past the end of the data
41  if (r > len_ - offset)
42  {
43  r = len_ - offset;
44  }
45 
46  memcpy(buf, buf_ + offset, r);
47 
48  return r;
49 }
50 
51 
53  std::size_t offset, char const * buf,
54  std::size_t toWrite)
55 {
56  const char* oldBuf = buf_;
57  std::size_t oldLen = len_;
58  len_ = (std::max)(len_, offset + toWrite);
59 
60  buf_ = new char[len_];
61 
62  if (oldBuf)
63  {
64  // we need to copy both old and new buffers
65  // it is possible that the new does not
66  // completely cover the old
67  memcpy(buf_, oldBuf, oldLen);
68  delete [] oldBuf;
69  }
70  memcpy(buf_ + offset, buf, toWrite);
71 
72  return len_;
73 }
74 
75 
77  char const * buf, std::size_t toWrite)
78 {
79  const char* oldBuf = buf_;
80 
81  buf_ = new char[len_ + toWrite];
82 
83  memcpy(buf_, oldBuf, len_);
84 
85  memcpy(buf_ + len_, buf, toWrite);
86 
87  delete [] oldBuf;
88 
89  len_ += toWrite;
90 
91  return len_;
92 }
93 
94 
95 void sqlite3_blob_backend::trim(std::size_t newLen)
96 {
97  const char* oldBuf = buf_;
98  len_ = newLen;
99 
100  buf_ = new char[len_];
101 
102  memcpy(buf_, oldBuf, len_);
103 
104  delete [] oldBuf;
105 }
106 
107 std::size_t sqlite3_blob_backend::set_data(char const *buf, std::size_t toWrite)
108 {
109  if (buf_)
110  {
111  delete [] buf_;
112  buf_ = 0;
113  len_ = 0;
114  }
115  return write(0, buf, toWrite);
116 }
sqlite3_blob_backend(sqlite3_session_backend &session)
virtual std::size_t append(char const *buf, std::size_t toWrite)
virtual std::size_t read(std::size_t offset, char *buf, std::size_t toRead)
std::size_t set_data(char const *buf, std::size_t toWrite)
virtual void trim(std::size_t newLen)
virtual std::size_t write(std::size_t offset, char const *buf, std::size_t toWrite)


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40