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
|
//
// utf8.cc - UTF-8 Text Agents
// Created: 19 Jan 1999
// Author: Theppitak Karoonboonyanan <thep@links.nectec.or.th>
//
#include "utf8.h"
#include "unichar.h"
int writeUTF8(std::ostream& output, unsigned unicode)
{
if (unicode <= 0x007F) {
output.put((unsigned char)(unicode & 0x7F));
} else if (unicode <= 0x07FF) {
output.put((unsigned char)(0xC0 | (unicode >> 6)));
output.put((unsigned char)(0x80 | (unicode & 0x3F)));
} else if (unicode <= 0xFFFF) {
output.put((unsigned char)(0xE0 | (unicode >> 12)));
output.put((unsigned char)(0x80 | ((unicode >> 6) & 0x3F)));
output.put((unsigned char)(0x80 | (unicode & 0x3F)));
} else if (unicode <= 0x1FFFFF) {
output.put((unsigned char)(0xF0 | (unicode >> 18)));
output.put((unsigned char)(0x80 | ((unicode >> 12) & 0x3F)));
output.put((unsigned char)(0x80 | ((unicode >> 6) & 0x3F)));
output.put((unsigned char)(0x80 | (unicode & 0x3F)));
} else if (unicode <= 0x3FFFFFF) {
output.put((unsigned char)(0xF8 | (unicode >> 24)));
output.put((unsigned char)(0x80 | ((unicode >> 18) & 0x3F)));
output.put((unsigned char)(0x80 | ((unicode >> 12) & 0x3F)));
output.put((unsigned char)(0x80 | ((unicode >> 6) & 0x3F)));
output.put((unsigned char)(0x80 | (unicode & 0x3F)));
} else if (unicode <= 0x7FFFFFFF) {
output.put((unsigned char)(0xFC | (unicode >> 30)));
output.put((unsigned char)(0x80 | ((unicode >> 24) & 0x3F)));
output.put((unsigned char)(0x80 | ((unicode >> 18) & 0x3F)));
output.put((unsigned char)(0x80 | ((unicode >> 12) & 0x3F)));
output.put((unsigned char)(0x80 | ((unicode >> 6) & 0x3F)));
output.put((unsigned char)(0x80 | (unicode & 0x3F)));
} else {
// error
return -1;
}
return 0;
}
int readUTF8(std::istream& input, unsigned* pUnicode)
{
char tmp;
if (!input.get(tmp)) { return -1; }
unsigned char c(tmp);
if ((c & 0x80) == 0x00) {
*pUnicode = c;
} else {
// count rest bytes
unsigned char sig = (c << 1);
int nBytes = 0;
while (sig & 0x80) { nBytes++; sig <<= 1; }
if (nBytes == 0) { return -1; } // undefined signature
// get most significant bits of unicode data
// signature bits = nBytes+2 (MSB)
// -> data bits = 8 - (nBytes+2) = 6 - nBytes (LSB)
*pUnicode = (c & (0x3F >> nBytes));
// get rest bits
while (nBytes-- > 0) {
if (!input.get(tmp)) { return -1; }
c = tmp;
c ^= 0x80; // 10xx xxxx -> 00xx xxxx
if (c & 0xC0) { return -1; } // not 10xx xxxx form
*pUnicode = (*pUnicode << 6) | c;
}
}
return 0;
}
bool UTF8Reader::Read(unichar& c)
{
return readUTF8(input, &c) == 0;
}
bool UTF8Writer::Write(unichar c)
{
return writeUTF8(output, c) == 0;
}
|