summaryrefslogtreecommitdiff
path: root/src/nw4r/ut/ut_CharStrmReader.cpp
blob: f57dba0dd3aae52440ddf2ef3adb4718b027a96e (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
// ported from https://github.com/kiwi515/ogws/blob/master/src/nw4r/ut/ut_CharStrmReader.cpp

#include "nw4r/ut/ut_CharStrmReader.h"

namespace nw4r {
namespace ut {
namespace {

bool IsSJISLeadByte(u8 c) {
    return ((c >= 0x81) && (c < 0xA0)) || c >= 0xe0;
}

} // namespace

u16 CharStrmReader::ReadNextCharUTF8() {
    u16 code;

    if (!(GetChar<u8>(0) & 0x80)) {
        code = GetChar<u8>(0);
        StepStrm<u8>(1);
    } else if ((GetChar<u8>(0) & 0xe0) == 0xC0) {
        code = ((GetChar<u8>(0) & 0x1F) << 6) | GetChar<u8>(1) & 0x3F;
        StepStrm<u8>(2);
    } else {
        code = ((GetChar<u8>(0) & 0x1F) << 12) | ((GetChar<u8>(1) & 0x3F) << 6) | (GetChar<u8>(2) & 0x3F);
        StepStrm<u8>(3);
    }
    return code;
}

u16 CharStrmReader::ReadNextCharUTF16() {
    u16 code = GetChar<u16>(0);
    StepStrm<u16>(1);
    return code;
}
u16 CharStrmReader::ReadNextCharCP1252() {
    u16 code = GetChar<u8>(0);
    StepStrm<u8>(1);
    return code;
}
u16 CharStrmReader::ReadNextCharSJIS() {
    u16 code = GetChar<u8>(0);

    if (IsSJISLeadByte((u8)code)) {
        code = GetChar<u8>(1) | (GetChar<u8>(0) << 8);
        StepStrm<u8>(2);
    } else {
        code = GetChar<u8>(0);
        StepStrm<u8>(1);
    }
    return code;
}

} // namespace ut

} // namespace nw4r