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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include "stdafx.h"
#include "Utf16Text.h"
#include "Core/Utf.h"
namespace storm {
Utf16Input::Utf16Input(IStream *src, Bool byteSwap) : src(src), byteSwap(byteSwap), pos(0) {}
Utf16Input::Utf16Input(IStream *src, Bool byteSwap, Buffer start) : src(src), byteSwap(byteSwap), pos(0) {
buf = buffer(engine(), max(Nat(bufSize), start.filled()));
buf.filled(start.filled());
memcpy(buf.dataPtr(), start.dataPtr(), start.filled());
}
void Utf16Input::close() {
src->close();
}
Char Utf16Input::readChar() {
using namespace utf16;
nat16 ch = readCh();
if (leading(ch)) {
nat16 t = readCh();
if (trailing(t))
return Char(assemble(ch, t));
else
return Char('?');
} else if (!trailing(ch)) {
return Char(Nat(ch));
} else {
return Char('?');
}
}
nat16 Utf16Input::readCh() {
// Read in network order.
if (byteSwap) {
nat16 r = readByte();
r |= nat16(readByte()) << 8;
return r;
} else {
nat16 r = nat16(readByte()) << 8;
r |= readByte();
return r;
}
}
byte Utf16Input::readByte() {
if (buf.count() == 0) {
buf = src->read(bufSize);
pos = 0;
}
if (pos >= buf.filled()) {
if (buf.count() < bufSize) {
buf = src->read(bufSize);
} else {
buf.filled(0);
buf = src->read(buf);
}
pos = 0;
}
if (pos < buf.filled())
return buf[pos++];
else
return 0;
}
/**
* Output.
*/
Utf16Output::Utf16Output(OStream *to, Bool byteSwap) : TextOutput(), dest(to), byteSwap(byteSwap) {
init();
}
Utf16Output::Utf16Output(OStream *to, TextInfo info, Bool byteSwap) : TextOutput(info), dest(to), byteSwap(byteSwap) {
init();
}
void Utf16Output::init() {
buf = buffer(engine(), bufSize);
buf.filled(0);
}
void Utf16Output::flush() {
if (buf.filled() > 0)
dest->write(buf);
buf.filled(0);
}
void Utf16Output::close() {
dest->close();
}
void Utf16Output::write(byte *to, wchar ch) {
if (byteSwap) {
to[0] = byte(ch & 0xFF);
to[1] = byte((ch >> 8) & 0xFF);
} else {
to[0] = byte((ch >> 8) & 0xFF);
to[1] = byte(ch & 0xFF);
}
}
void Utf16Output::writeChar(Char ch) {
byte buf[4];
if (ch.leading()) {
write(buf, ch.leading());
write(buf + 2, ch.trailing());
writeBytes(buf, 4);
} else {
write(buf, ch.trailing());
writeBytes(buf, 2);
}
}
void Utf16Output::writeBytes(const byte *data, Nat count) {
Nat filled = buf.filled();
if (filled + count >= buf.count()) {
flush();
filled = buf.filled();
}
memcpy(buf.dataPtr() + filled, data, count);
buf.filled(filled + count);
}
}
|