summaryrefslogtreecommitdiff
path: root/libs/JSystem/src/JSupport/JSUFileStream.cpp
blob: c05bc5d2cbbe41c73ee8c8186e10e64889394299 (plain)
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
#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JSupport/JSUFileStream.h"
#include "JSystem/JKernel/JKRFile.h"

JSUFileInputStream::JSUFileInputStream(JKRFile* pFile) {
    mFile = pFile;
    mPosition = 0;
}

u32 JSUFileInputStream::readData(void* pBuffer, s32 length) {
    s32 lenRead = 0;
    if (mFile->isAvailable()) {
        if (mPosition + length > (u32)mFile->getFileSize()) {
            length = mFile->getFileSize() - mPosition;
        }

        if (length > 0) {
            lenRead = mFile->readData(pBuffer, length, mPosition);
            if (lenRead < 0) {
                return 0;
            } else {
                mPosition += lenRead;
            }
        }
    }

    return lenRead;
}

s32 JSUFileInputStream::seekPos(s32 pos, JSUStreamSeekFrom seekFrom) {
    s32 oldPos = mPosition;

    switch (seekFrom) {
    case JSUStreamSeekFrom_SET:
        mPosition = pos;
        break;
    case JSUStreamSeekFrom_END:
        mPosition = mFile->getFileSize() - pos;
        break;
    case JSUStreamSeekFrom_CUR:
        mPosition += pos;
        break;
    }

    if (mPosition < 0) {
        mPosition = 0;
    }

    if (mPosition > mFile->getFileSize()) {
        mPosition = mFile->getFileSize();
    }

    return mPosition - oldPos;
}

s32 JSUFileInputStream::getLength() const {
    return mFile->getFileSize();
}

s32 JSUFileInputStream::getPosition() const {
    return mPosition;
}