-
Notifications
You must be signed in to change notification settings - Fork 5
/
File.cpp
54 lines (45 loc) · 1.24 KB
/
File.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
#include "File.hpp"
#include "MemoryFile.hpp"
#include "PartFile.hpp"
#include "FileInputStream.hpp"
#include "Exception.hpp"
#include <string.h>
BEGIN_INANITY
bool File::Comparer::operator()(File* a, File* b) const
{
size_t asize = a->GetSize();
size_t bsize = b->GetSize();
int r = memcmp(a->GetData(), b->GetData(), asize < bsize ? asize : bsize);
return (r == 0) ? (asize < bsize) : (r < 0);
}
ptr<File> File::Slice(size_t offset, size_t size)
{
return NEW(PartFile(this, offset, size));
}
ptr<File> File::SliceFrom(size_t offset)
{
return Slice(offset, GetSize() - offset);
}
ptr<File> File::Concat(ptr<File> other)
{
size_t size = GetSize();
size_t otherSize = other->GetSize();
ptr<MemoryFile> resultFile = NEW(MemoryFile(size + otherSize));
char* resultData = (char*)resultFile->GetData();
memcpy(resultData, GetData(), size);
memcpy(resultData + size, other->GetData(), otherSize);
return resultFile;
}
bigsize_t File::GetBigSize() const
{
return GetSize();
}
void File::Read(bigsize_t offset, size_t size, void* data)
{
memcpy(data, (const uint8_t*)GetData() + offset, size);
}
ptr<InputStream> File::GetInputStream(bigsize_t offset, bigsize_t size)
{
return NEW(FileInputStream(Slice((size_t)offset, (size_t)size)));
}
END_INANITY