-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
DataIO.cpp
441 lines (332 loc) · 6.88 KB
/
DataIO.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright 2005-2014, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stefano Ceccherini (burton666@libero.it)
*/
#include <DataIO.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
BDataIO::BDataIO()
{
}
BDataIO::~BDataIO()
{
}
ssize_t
BDataIO::Read(void* buffer, size_t size)
{
return B_NOT_SUPPORTED;
}
ssize_t
BDataIO::Write(const void* buffer, size_t size)
{
return B_NOT_SUPPORTED;
}
// Private or Reserved
BDataIO::BDataIO(const BDataIO &)
{
// Copying not allowed
}
BDataIO &
BDataIO::operator=(const BDataIO &)
{
// Copying not allowed
return *this;
}
// FBC
void BDataIO::_ReservedDataIO1(){}
void BDataIO::_ReservedDataIO2(){}
void BDataIO::_ReservedDataIO3(){}
void BDataIO::_ReservedDataIO4(){}
void BDataIO::_ReservedDataIO5(){}
void BDataIO::_ReservedDataIO6(){}
void BDataIO::_ReservedDataIO7(){}
void BDataIO::_ReservedDataIO8(){}
void BDataIO::_ReservedDataIO9(){}
void BDataIO::_ReservedDataIO10(){}
void BDataIO::_ReservedDataIO11(){}
void BDataIO::_ReservedDataIO12(){}
// #pragma mark -
BPositionIO::BPositionIO()
{
}
BPositionIO::~BPositionIO()
{
}
ssize_t
BPositionIO::Read(void *buffer, size_t size)
{
off_t curPos = Position();
ssize_t result = ReadAt(curPos, buffer, size);
if (result > 0)
Seek(result, SEEK_CUR);
return result;
}
ssize_t
BPositionIO::Write(const void *buffer, size_t size)
{
off_t curPos = Position();
ssize_t result = WriteAt(curPos, buffer, size);
if (result > 0)
Seek(result, SEEK_CUR);
return result;
}
status_t
BPositionIO::SetSize(off_t size)
{
return B_ERROR;
}
status_t
BPositionIO::GetSize(off_t* size) const
{
if (!size)
return B_BAD_VALUE;
off_t currentPos = Position();
if (currentPos < 0)
return (status_t)currentPos;
*size = const_cast<BPositionIO*>(this)->Seek(0, SEEK_END);
if (*size < 0)
return (status_t)*size;
off_t pos = const_cast<BPositionIO*>(this)->Seek(currentPos, SEEK_SET);
if (pos != currentPos)
return pos < 0 ? (status_t)pos : B_ERROR;
return B_OK;
}
// FBC
extern "C" void _ReservedPositionIO1__11BPositionIO() {}
void BPositionIO::_ReservedPositionIO2(){}
void BPositionIO::_ReservedPositionIO3(){}
void BPositionIO::_ReservedPositionIO4(){}
void BPositionIO::_ReservedPositionIO5(){}
void BPositionIO::_ReservedPositionIO6(){}
void BPositionIO::_ReservedPositionIO7(){}
void BPositionIO::_ReservedPositionIO8(){}
void BPositionIO::_ReservedPositionIO9(){}
void BPositionIO::_ReservedPositionIO10(){}
void BPositionIO::_ReservedPositionIO11(){}
void BPositionIO::_ReservedPositionIO12(){}
// #pragma mark -
BMemoryIO::BMemoryIO(void *buffer, size_t length)
:
fReadOnly(false),
fBuffer(static_cast<char*>(buffer)),
fLength(length),
fBufferSize(length),
fPosition(0)
{
}
BMemoryIO::BMemoryIO(const void *buffer, size_t length)
:
fReadOnly(true),
fBuffer(const_cast<char*>(static_cast<const char*>(buffer))),
fLength(length),
fBufferSize(length),
fPosition(0)
{
}
BMemoryIO::~BMemoryIO()
{
}
ssize_t
BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size)
{
if (buffer == NULL || pos < 0)
return B_BAD_VALUE;
ssize_t sizeRead = 0;
if (pos < (off_t)fLength) {
sizeRead = min_c((off_t)size, (off_t)fLength - pos);
memcpy(buffer, fBuffer + pos, sizeRead);
}
return sizeRead;
}
ssize_t
BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size)
{
if (fReadOnly)
return B_NOT_ALLOWED;
if (buffer == NULL || pos < 0)
return B_BAD_VALUE;
ssize_t sizeWritten = 0;
if (pos < (off_t)fBufferSize) {
sizeWritten = min_c((off_t)size, (off_t)fBufferSize - pos);
memcpy(fBuffer + pos, buffer, sizeWritten);
}
if (pos + sizeWritten > (off_t)fLength)
fLength = pos + sizeWritten;
return sizeWritten;
}
off_t
BMemoryIO::Seek(off_t position, uint32 seek_mode)
{
switch (seek_mode) {
case SEEK_SET:
fPosition = position;
break;
case SEEK_CUR:
fPosition += position;
break;
case SEEK_END:
fPosition = fLength + position;
break;
default:
break;
}
return fPosition;
}
off_t
BMemoryIO::Position() const
{
return fPosition;
}
status_t
BMemoryIO::SetSize(off_t size)
{
if (fReadOnly)
return B_NOT_ALLOWED;
if (size > (off_t)fBufferSize)
return B_ERROR;
fLength = size;
return B_OK;
}
// Private or Reserved
BMemoryIO::BMemoryIO(const BMemoryIO &)
{
//Copying not allowed
}
BMemoryIO &
BMemoryIO::operator=(const BMemoryIO &)
{
//Copying not allowed
return *this;
}
// FBC
void BMemoryIO::_ReservedMemoryIO1(){}
void BMemoryIO::_ReservedMemoryIO2(){}
// #pragma mark -
BMallocIO::BMallocIO()
:
fBlockSize(256),
fMallocSize(0),
fLength(0),
fData(NULL),
fPosition(0)
{
}
BMallocIO::~BMallocIO()
{
free(fData);
}
ssize_t
BMallocIO::ReadAt(off_t pos, void *buffer, size_t size)
{
if (buffer == NULL)
return B_BAD_VALUE;
ssize_t sizeRead = 0;
if (pos < (off_t)fLength) {
sizeRead = min_c((off_t)size, (off_t)fLength - pos);
memcpy(buffer, fData + pos, sizeRead);
}
return sizeRead;
}
ssize_t
BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size)
{
if (buffer == NULL)
return B_BAD_VALUE;
size_t newSize = max_c(pos + (off_t)size, (off_t)fLength);
status_t error = B_OK;
if (newSize > fMallocSize)
error = SetSize(newSize);
if (error == B_OK) {
memcpy(fData + pos, buffer, size);
if (pos + size > fLength)
fLength = pos + size;
}
return error != B_OK ? error : size;
}
off_t
BMallocIO::Seek(off_t position, uint32 seekMode)
{
switch (seekMode) {
case SEEK_SET:
fPosition = position;
break;
case SEEK_END:
fPosition = fLength + position;
break;
case SEEK_CUR:
fPosition += position;
break;
default:
break;
}
return fPosition;
}
off_t
BMallocIO::Position() const
{
return fPosition;
}
status_t
BMallocIO::SetSize(off_t size)
{
status_t error = B_OK;
if (size == 0) {
// size == 0, free the memory
free(fData);
fData = NULL;
fMallocSize = 0;
} else {
// size != 0, see, if necessary to resize
size_t newSize = (size + fBlockSize - 1) / fBlockSize * fBlockSize;
if (size != (off_t)fMallocSize) {
// we need to resize
if (char *newData = static_cast<char*>(realloc(fData, newSize))) {
// set the new area to 0
if (newSize > fMallocSize)
memset(newData + fMallocSize, 0, newSize - fMallocSize);
fData = newData;
fMallocSize = newSize;
} else // couldn't alloc the memory
error = B_NO_MEMORY;
}
}
if (error == B_OK)
fLength = size;
return error;
}
void
BMallocIO::SetBlockSize(size_t blockSize)
{
if (blockSize == 0)
blockSize = 1;
if (blockSize != fBlockSize)
fBlockSize = blockSize;
}
const void *
BMallocIO::Buffer() const
{
return fData;
}
size_t
BMallocIO::BufferLength() const
{
return fLength;
}
// Private or Reserved
BMallocIO::BMallocIO(const BMallocIO &)
{
// copying not allowed...
}
BMallocIO &
BMallocIO::operator=(const BMallocIO &)
{
// copying not allowed...
return *this;
}
// FBC
void BMallocIO::_ReservedMallocIO1() {}
void BMallocIO::_ReservedMallocIO2() {}