Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "buffer.h"
23 :
24 : #include <cstring>
25 : #include <utility>
26 :
27 : #include "global/log.h"
28 :
29 : using namespace muse;
30 : using namespace muse::io;
31 :
32 2334 : Buffer Buffer::opened(const OpenMode mode, ByteArray* ba)
33 : {
34 2334 : Buffer buf(ba);
35 2334 : DO_ASSERT(buf.open(mode));
36 :
37 2334 : return buf;
38 0 : }
39 :
40 3 : Buffer Buffer::opened(const OpenMode mode, ByteArray&& ba)
41 : {
42 3 : Buffer buf(std::move(ba));
43 3 : DO_ASSERT(buf.open(mode));
44 :
45 3 : return buf;
46 0 : }
47 :
48 7 : Buffer::Buffer()
49 : {
50 7 : m_ref = &m_ba;
51 7 : }
52 :
53 2 : Buffer::Buffer(size_t size)
54 : {
55 2 : m_ba.resize(size);
56 2 : m_ref = &m_ba;
57 2 : }
58 :
59 0 : Buffer::Buffer(const uint8_t* data, size_t size)
60 : {
61 0 : m_ba = ByteArray(data, size);
62 0 : m_ref = &m_ba;
63 0 : }
64 :
65 2434 : Buffer::Buffer(ByteArray* ba)
66 2434 : : m_ref(ba)
67 : {
68 2434 : if (!m_ref) {
69 2243 : m_ref = &m_ba;
70 : }
71 2434 : }
72 :
73 9 : Buffer::Buffer(ByteArray&& ba)
74 : {
75 9 : m_ba = std::move(ba);
76 9 : m_ref = &m_ba;
77 9 : }
78 :
79 2588 : const ByteArray& Buffer::data() const
80 : {
81 2588 : return *m_ref;
82 : }
83 :
84 176 : ByteArray Buffer::doReadAll()
85 : {
86 176 : IF_ASSERT_FAILED(isOpenModeReadable()) {
87 0 : return ByteArray();
88 : }
89 :
90 176 : if (pos() != 0) {
91 3 : return IODevice::doReadAll();
92 : }
93 :
94 173 : seek(size());
95 173 : return data();
96 : }
97 :
98 2453 : bool Buffer::doOpen(OpenMode)
99 : {
100 2453 : return true;
101 : }
102 :
103 33971 : size_t Buffer::dataSize() const
104 : {
105 33971 : return m_ref->size();
106 : }
107 :
108 8724 : const uint8_t* Buffer::rawData() const
109 : {
110 8724 : return m_ref->constData();
111 : }
112 :
113 3776 : bool Buffer::resizeData(size_t size)
114 : {
115 3776 : m_ref->resize(size);
116 3776 : return true;
117 : }
118 :
119 7082 : size_t Buffer::writeData(const uint8_t* data, size_t len)
120 : {
121 7082 : std::memcpy(m_ref->data() + pos(), data, len);
122 7082 : return len;
123 : }
|