Back to index

tetex-bin  3.0
UTF8.h
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // UTF8.h
00004 //
00005 // Copyright 2001-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 static int mapUTF8(Unicode u, char *buf, int bufSize) {
00010   if        (u <= 0x0000007f) {
00011     if (bufSize < 1) {
00012       return 0;
00013     }
00014     buf[0] = (char)u;
00015     return 1;
00016   } else if (u <= 0x000007ff) {
00017     if (bufSize < 2) {
00018       return 0;
00019     }
00020     buf[0] = (char)(0xc0 + (u >> 6));
00021     buf[1] = (char)(0x80 + (u & 0x3f));
00022     return 2;
00023   } else if (u <= 0x0000ffff) {
00024     if (bufSize < 3) {
00025       return 0;
00026     }
00027     buf[0] = (char)(0xe0 + (u >> 12));
00028     buf[1] = (char)(0x80 + ((u >> 6) & 0x3f));
00029     buf[2] = (char)(0x80 + (u & 0x3f));
00030     return 3;
00031   } else if (u <= 0x0010ffff) {
00032     if (bufSize < 4) {
00033       return 0;
00034     }
00035     buf[0] = (char)(0xf0 + (u >> 18));
00036     buf[1] = (char)(0x80 + ((u >> 12) & 0x3f));
00037     buf[2] = (char)(0x80 + ((u >> 6) & 0x3f));
00038     buf[3] = (char)(0x80 + (u & 0x3f));
00039     return 4;
00040   } else {
00041     return 0;
00042   }
00043 }
00044 
00045 static int mapUCS2(Unicode u, char *buf, int bufSize) {
00046   if (u <= 0xffff) {
00047     if (bufSize < 2) {
00048       return 0;
00049     }
00050     buf[0] = (char)((u >> 8) & 0xff);
00051     buf[1] = (char)(u & 0xff);
00052     return 2;
00053   } else {
00054     return 0;
00055   }
00056 }