diff --git a/configure.py b/configure.py index 121b5b0e..4f4dabe2 100644 --- a/configure.py +++ b/configure.py @@ -330,7 +330,7 @@ config.libs = [ Object(Matching, "nw4r/ut/ut_list.cpp"), Object(Matching, "nw4r/ut/ut_LinkList.cpp"), Object(Matching, "nw4r/ut/ut_binaryFileFormat.cpp"), - Object(NonMatching, "nw4r/ut/ut_CharStrmReader.cpp"), + Object(Matching, "nw4r/ut/ut_CharStrmReader.cpp"), Object(Matching, "nw4r/ut/ut_IOStream.cpp"), Object(Matching, "nw4r/ut/ut_TagProcessorBase.cpp"), Object(Matching, "nw4r/ut/ut_FileStream.cpp"), diff --git a/src/nw4r/ut/ut_CharStrmReader.cpp b/src/nw4r/ut/ut_CharStrmReader.cpp index e69de29b..81bb80fa 100644 --- a/src/nw4r/ut/ut_CharStrmReader.cpp +++ b/src/nw4r/ut/ut_CharStrmReader.cpp @@ -0,0 +1,54 @@ +#include + +namespace nw4r { +namespace ut { +namespace { + +bool IsSJISLeadByte(u8 c) { + return ((c >= 0x81) && (c < 0xA0)) || c >= 0xe0; +} + +} // namespace + +u16 CharStrmReader::ReadNextCharUTF8() { + u16 code; + + if (!(GetChar(0) & 0x80)) { + code = GetChar(0); + StepStrm(1); + } else if ((GetChar(0) & 0xe0) == 0xC0) { + code = ((GetChar(0) & 0x1F) << 6) | GetChar(1) & 0x3F; + StepStrm(2); + } else { + code = ((GetChar(0) & 0x1F) << 12) | ((GetChar(1) & 0x3F) << 6) | (GetChar(2) & 0x3F); + StepStrm(3); + } + return code; +} + +u16 CharStrmReader::ReadNextCharUTF16() { + u16 code = GetChar(0); + StepStrm(1); + return code; +} +u16 CharStrmReader::ReadNextCharCP1252() { + u16 code = GetChar(0); + StepStrm(1); + return code; +} +u16 CharStrmReader::ReadNextCharSJIS() { + u16 code = GetChar(0); + + if (IsSJISLeadByte((u8)code)) { + code = GetChar(1) | (GetChar(0) << 8); + StepStrm(2); + } else { + code = GetChar(0); + StepStrm(1); + } + return code; +} + +} // namespace ut + +} // namespace nw4r