/
nativefile.cpp
262 lines (219 loc) · 5.87 KB
/
nativefile.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright © 2009-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/NativeFile"
#include "de/Guard"
#include "de/math.h"
namespace de {
DENG2_PIMPL(NativeFile)
{
/// Path of the native file in the OS file system.
NativePath nativePath;
/// Input stream.
mutable QFile *in;
/// Output stream. Kept open until flush() is called.
/// (Re)opened before changing the contents of the file.
QFile *out;
/// Output file should be truncated before the next write.
bool needTruncation;
Instance(Public *i)
: Base(i)
, in(0)
, out(0)
, needTruncation(false)
{}
~Instance()
{
DENG2_ASSERT(!in);
DENG2_ASSERT(!out);
}
QFile &getInput()
{
if(!in)
{
// Reading is allowed always.
in = new QFile(nativePath);
if(!in->open(QFile::ReadOnly))
{
delete in;
in = 0;
/// @throw InputError Opening the input stream failed.
throw InputError("NativeFile::openInput", "Failed to read " + nativePath);
}
}
return *in;
}
QFile &getOutput()
{
if(!out)
{
// Are we allowed to output?
self.verifyWriteAccess();
QFile::OpenMode fileMode = QFile::ReadWrite;
if(self.mode() & Truncate)
{
if(needTruncation)
{
fileMode |= QFile::Truncate;
needTruncation = false;
}
}
out = new QFile(nativePath);
if(!out->open(fileMode))
{
delete out;
out = 0;
/// @throw OutputError Opening the output stream failed.
throw OutputError("NativeFile::output", "Failed to write " + nativePath);
}
if(self.mode() & Truncate)
{
Status st = self.status();
st.size = 0;
st.modifiedAt = Time();
self.setStatus(st);
}
}
return *out;
}
void closeInput()
{
if(in)
{
delete in;
in = 0;
}
}
void closeOutput()
{
if(out)
{
delete out;
out = 0;
}
}
};
NativeFile::NativeFile(String const &name, NativePath const &nativePath)
: ByteArrayFile(name), d(new Instance(this))
{
d->nativePath = nativePath;
}
NativeFile::~NativeFile()
{
DENG2_GUARD(this);
DENG2_FOR_AUDIENCE2(Deletion, i) i->fileBeingDeleted(*this);
audienceForDeletion().clear();
close();
deindex();
}
String NativeFile::describe() const
{
DENG2_GUARD(this);
return String("\"%1\"").arg(d->nativePath.pretty());
}
void NativeFile::close()
{
DENG2_GUARD(this);
flush();
DENG2_ASSERT(!d->out);
d->closeInput();
}
void NativeFile::flush()
{
DENG2_GUARD(this);
d->closeOutput();
DENG2_ASSERT(!d->out);
}
NativePath const &NativeFile::nativePath() const
{
DENG2_GUARD(this);
return d->nativePath;
}
void NativeFile::clear()
{
DENG2_GUARD(this);
File::clear();
Flags oldMode = mode();
setMode(Write | Truncate);
d->getOutput();
File::setMode(oldMode);
}
NativeFile::Size NativeFile::size() const
{
DENG2_GUARD(this);
return status().size;
}
void NativeFile::get(Offset at, Byte *values, Size count) const
{
DENG2_GUARD(this);
QFile &in = input();
if(at + count > size())
{
/// @throw IByteArray::OffsetError The region specified for reading extends
/// beyond the bounds of the file.
throw OffsetError("NativeFile::get", description() + ": cannot read past end of file " +
String("(%1[+%2] > %3)").arg(at).arg(count).arg(size()));
}
in.seek(at);
in.read(reinterpret_cast<char *>(values), count);
}
void NativeFile::set(Offset at, Byte const *values, Size count)
{
DENG2_GUARD(this);
QFile &out = output();
if(at > size())
{
/// @throw IByteArray::OffsetError @a at specified a position beyond the
/// end of the file.
throw OffsetError("NativeFile::set", "Cannot write past end of file");
}
out.seek(at);
out.write(reinterpret_cast<char const *>(values), count);
if(out.error() != QFile::NoError)
{
/// @throw OutputError Failure to write to the native file.
throw OutputError("NativeFile::set", "Error writing to file:" +
out.errorString());
}
// Update status.
Status st = status();
st.size = max(st.size, at + count);
st.modifiedAt = Time();
setStatus(st);
}
void NativeFile::setMode(Flags const &newMode)
{
DENG2_GUARD(this);
close();
File::setMode(newMode);
if(newMode.testFlag(Truncate))
{
d->needTruncation = true;
}
}
QFile &NativeFile::input() const
{
DENG2_GUARD(this);
return d->getInput();
}
QFile &NativeFile::output()
{
DENG2_GUARD(this);
return d->getOutput();
}
} // namespace de