This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
file_stream.cpp
130 lines (104 loc) · 3.37 KB
/
file_stream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <QFile>
#include <taglib/tiostream.h>
class FileStream: public TagLib::IOStream {
QFile *file;
unsigned int bufferSize() const { return 1024; }
public:
FileStream(QFile *f): file(f) {}
bool readOnly() const {
return !file->isWritable();
}
bool isOpen() const {
return file->isOpen();
}
long tell() const {
return file->pos();
}
long length() {
return file->size();
}
void truncate(long length) {
file->resize(length);
}
void seek(long offset,
TagLib::IOStream::Position p = TagLib::IOStream::Beginning) {
switch (p) {
case TagLib::IOStream::Beginning:
file->seek(offset);
break;
case TagLib::IOStream::Current:
file->seek(file->pos() + offset);
break;
case TagLib::IOStream::End:
file->seek(file->size() + offset);
}
}
TagLib::FileName name() const {
return file->fileName().toStdWString().data();
}
TagLib::ByteVector readBlock(unsigned long length) {
TagLib::ByteVector bv(static_cast<unsigned int>(length));
const qint64 &l = file->read(bv.data(), length);
bv.resize(l);
return std::move(bv);
}
void writeBlock(const TagLib::ByteVector &data) {
file->write(data.data(), data.size());
}
void insert(const TagLib::ByteVector &data,
unsigned long start,
unsigned long replace) {
if (data.size() == replace) {
seek(start);
writeBlock(data);
return;
}
if (data.size() < replace) {
seek(start);
writeBlock(data);
removeBlock(start + data.size(), replace - data.size());
return;
}
unsigned long bufferLength = bufferSize();
while (data.size() - replace > bufferLength)
bufferLength += bufferSize();
long readPosition = start + replace;
long writePosition = start;
QByteArray buffer(data.data(), data.size());
QByteArray aboutToOverwrite;
aboutToOverwrite.resize(bufferLength);
while (true) {
seek(readPosition);
const qint64 &bytesRead = file->peek(aboutToOverwrite.data(), bufferLength);
aboutToOverwrite.resize(bytesRead);
readPosition += bufferLength;
if (bytesRead < bufferLength) clear();
seek(writePosition);
file->write(buffer);
if (bytesRead == 0) break;
writePosition += buffer.size();
buffer = aboutToOverwrite;
}
}
void removeBlock(unsigned long start, unsigned long length) {
unsigned long bufferLength = bufferSize();
long readPosition = start + length;
long writePosition = start;
QByteArray buffer;
buffer.resize(bufferLength);
for (qint64 bytesRead = -1; bytesRead != 0;)
{
seek(readPosition);
bytesRead = file->peek(buffer.data(), bufferLength);
readPosition += bytesRead;
if (bytesRead < buffer.size()) {
clear();
buffer.resize(bytesRead);
}
seek(writePosition);
file->write(buffer);
writePosition += bytesRead;
}
truncate(writePosition);
}
};