Audacity 3.2.0
Blob.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: Blob.cpp
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7#include "Blob.h"
8
9#include <algorithm>
10#include <cassert>
11#include <utility>
12
13#include <sqlite3.h>
14
16{
17Blob::Blob(sqlite3_blob* blob) noexcept
18 : mBlob { blob }
19{
20 assert(mBlob != nullptr);
21}
22
23Blob::~Blob() noexcept
24{
25 if (mBlob != nullptr)
26 {
27 sqlite3_blob_close(mBlob);
28 mBlob = nullptr;
29 }
30}
31
32Blob::Blob(Blob&& rhs) noexcept
33{
34 *this = std::move(rhs);
35}
36
37Blob& Blob::operator=(Blob&& rhs) noexcept
38{
39 std::swap(mBlob, rhs.mBlob);
40 return *this;
41}
42
43int64_t Blob::Size() const noexcept
44{
45 if (mBlob == nullptr)
46 return 0;
47
48 return sqlite3_blob_bytes(mBlob);
49}
50
51int64_t
52Blob::Read(void* buffer, int64_t offset, int64_t bufferSize) const noexcept
53{
54 if (mBlob == nullptr)
55 return 0;
56
57 const int readSize = std::min<int>(bufferSize, Size() - offset);
58
59 if (bufferSize <= 0)
60 return 0;
61
62 if (
63 SQLITE_OK !=
64 sqlite3_blob_read(mBlob, buffer, readSize, static_cast<int>(offset)))
65 return 0;
66
67 return readSize;
68}
69
70int64_t
71Blob::Write(const void* buffer, int64_t offset, int64_t bufferSize) noexcept
72{
73 if (mBlob == nullptr)
74 return 0;
75
76 const int writeSize = std::min<int>(bufferSize, Size() - offset);
77
78 if (bufferSize <= 0)
79 return 0;
80
81 if (
82 SQLITE_OK !=
83 sqlite3_blob_write(mBlob, buffer, writeSize, static_cast<int>(offset)))
84 return 0;
85
86 return writeSize;
87}
88
89} // namespace audacity::sqlite
A class representing a BLOB in a SQLite database.
Definition: Blob.h:18
Blob & operator=(const Blob &)=delete
sqlite3_blob * mBlob
Definition: Blob.h:61
int64_t Size() const noexcept
Returns the size of the BLOB.
Definition: Blob.cpp:43
~Blob() noexcept
Definition: Blob.cpp:23
Blob(sqlite3_blob *blob) noexcept
Definition: Blob.cpp:17
int64_t Read(void *buffer, int64_t offset, int64_t bufferSize) const noexcept
Reads up to bufferSize bytes from the BLOB into the buffer at the given offset.
Definition: Blob.cpp:52
int64_t Write(const void *buffer, int64_t offset, int64_t bufferSize) noexcept
Writes up to bufferSize bytes from the buffer into the BLOB at the given offset.
Definition: Blob.cpp:71
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:634
SizeType< float > Size
Alias for SizeType<float>
Definition: Size.h:174