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) 2025 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 : #pragma once
23 :
24 : #include <cstdint>
25 : #include <string>
26 : #include <map>
27 :
28 : #include "global/types/bytearray.h"
29 :
30 : namespace muse::io {
31 : class IODevice
32 : {
33 : public:
34 :
35 : enum OpenMode {
36 : Unknown, ReadOnly, WriteOnly, ReadWrite, Append
37 : };
38 :
39 7915 : IODevice() = default;
40 7903 : virtual ~IODevice() = default;
41 :
42 : bool open(OpenMode m = ReadOnly);
43 : void close();
44 :
45 : bool isOpen() const;
46 : OpenMode openMode() const;
47 : bool isReadable() const;
48 : bool isWriteable() const;
49 :
50 : size_t size() const;
51 : size_t pos() const;
52 :
53 : bool seek(size_t pos);
54 :
55 : size_t read(uint8_t* data, size_t len);
56 : ByteArray read(size_t count);
57 : ByteArray readAll();
58 :
59 : const uint8_t* readData();
60 :
61 : size_t write(const uint8_t* data, size_t len);
62 : size_t write(const ByteArray& ba);
63 :
64 : #ifndef NO_QT_SUPPORT
65 : size_t write(const QByteArray& ba);
66 : #endif
67 :
68 : const std::string& meta(const std::string& key) const;
69 : void setMeta(const std::string& key, const std::string& val);
70 :
71 : bool hasError() const;
72 : int error() const;
73 : const std::string& errorString() const;
74 :
75 : protected:
76 :
77 : virtual bool doOpen(OpenMode m) = 0;
78 8520 : virtual void doClose() {}
79 : virtual ByteArray doReadAll();
80 : virtual size_t dataSize() const = 0;
81 : virtual const uint8_t* rawData() const = 0;
82 : virtual bool resizeData(size_t size) = 0;
83 : virtual size_t writeData(const uint8_t* data, size_t len) = 0;
84 :
85 : bool isOpenModeReadable() const;
86 : bool isOpenModeWriteable() const;
87 :
88 : void setError(int error, const std::string& errorString);
89 :
90 : private:
91 :
92 : const uint8_t* cdataOffsetted() const;
93 :
94 : OpenMode m_mode = OpenMode::Unknown;
95 : size_t m_pos = 0;
96 : std::map<std::string, std::string> m_meta;
97 :
98 : int m_error = 0;
99 : std::string m_errorString;
100 : };
101 : }
|