aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/deprecated/datafile/datafile.cpp
blob: ff93f11c6b74bfb49de6641782b37f0d9f79e824 (plain) (blame)
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
#include "datafile.h"

void TDataFileBase::DoLoad(const char* fname, int loadMode) {
    Destroy();
    TFile f(fname, RdOnly);
    DoLoad(f, loadMode, nullptr, 0);
}

void TDataFileBase::DoLoad(TFile& f, int loadMode, void* hdrPtr, size_t hdrSize) {
    if (hdrPtr) {
        if (loadMode & DLM_EXACT_SIZE && f.GetLength() != (i64)Length)
            throw yexception() << f.GetName() << " size does not match its header value";
    } else {
        Length = f.GetLength();
        hdrSize = 0;
    }
    if ((loadMode & DLM_LD_TYPE_MASK) == DLM_READ) {
        MemData = TVector<char>(Length);
        memcpy(MemData.begin(), hdrPtr, hdrSize);
        f.Load(MemData.begin() + hdrSize, Length - hdrSize);
        Start = MemData.begin();
    } else {
        FileData.init(f);
        if (FileData.getSize() < Length)
            throw yexception() << f.GetName() << " is smaller than what its header value says";
        if ((loadMode & DLM_LD_TYPE_MASK) == DLM_MMAP_PRC)
            FileData.precharge();
        Start = (const char*)FileData.getData();
    }
}

void TDataFileBase::Destroy() {
    TVector<char>().swap(MemData);
    FileData.term();
    Start = nullptr;
    Length = 0;
}

void TDataFileBase::Precharge() const {
    if (Length && Start == (char*)FileData.getData())
        FileData.precharge();
}