summaryrefslogtreecommitdiff
path: root/libs/JSystem/src/JSupport/JSUOutputStream.cpp
blob: d9f4a7810028afa34e89345413210030aab505c0 (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
64
65
#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JSupport/JSUOutputStream.h"
#include "JSystem/JSupport/JSURandomOutputStream.h"
#ifdef __REVOLUTION_SDK__
#include <revolution.h>
#else
#include <dolphin.h>
#endif
#include <cstring>

JSUOutputStream::~JSUOutputStream() {
    if (!isGood()) {
        OS_REPORT("JSUOutputStream: occur error.\n");
    }
}

s32 JSUOutputStream::write(const void* buffer, s32 numBytes) {
    s32 bytesWrote = writeData(buffer, numBytes);
    if (bytesWrote != numBytes) {
        setState(IOS_STATE_1);
    }
    return bytesWrote;
}

void JSUOutputStream::write(const char* str) {
    if (str == NULL) {
        u16 spA = 0;
        if (writeData(&spA, sizeof(spA)) != sizeof(spA)) {
            setState(IOS_STATE_1);
        }
    } else {
        int len = strlen(str);
        if (len >= 0x10000) {
            setState(IOS_STATE_2);
        } else {
            u16 sp8 = len;
            if (writeData(&sp8, sizeof(sp8)) != sizeof(sp8) || writeData(str, len) != len) {
                setState(IOS_STATE_1);
            }
        }
    }    
}

s32 JSUOutputStream::skip(s32 count, s8 param_1) {
    s32 skipCount = 0;
    for (; skipCount < count; skipCount++) {
        if (writeData(&param_1, sizeof(param_1)) != sizeof(param_1)) {
            setState(IOS_STATE_1);
            break;
        }
    }

    return skipCount;
}

s32 JSURandomOutputStream::seek(s32 param_0, JSUStreamSeekFrom param_1) {
    s32 seekResult = seekPos(param_0, param_1);
    clrState(IOS_STATE_1);
    return seekResult;
}

s32 JSURandomOutputStream::getAvailable() const {
    return getLength() - getPosition();
}