Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

utf8html.cpp

00001 /******************************************************************************
00002  *
00003  * utf8html -   SWFilter decendant to convert a UTF-8 stream to HTML escapes
00004  *
00005  */
00006 
00007 
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <utf8html.h>
00011 
00012 UTF8HTML::UTF8HTML() {
00013 }
00014 
00015 
00016 char UTF8HTML::ProcessText(char *text, int maxlen, const SWKey *key, const SWModule *module)
00017 {
00018         unsigned char *to, *from;
00019         int len;
00020         char digit[10];
00021         unsigned long ch;
00022 
00023         len = strlenw(text) + 2;                                                // shift string to right of buffer
00024         if (len < maxlen) {
00025                 memmove(&text[maxlen - len], text, len);
00026                 from = (unsigned char*)&text[maxlen - len];
00027         }
00028         else    from = (unsigned char*)text;
00029         // -------------------------------
00030         for (to = (unsigned char*)text; *from; from++) {
00031           ch = 0;
00032           if ((*from & 128) != 128) {
00033 //              if (*from != ' ')
00034                *to++ = *from;
00035                continue;
00036           }
00037           if ((*from & 128) && ((*from & 64) != 64)) {
00038             // error
00039                *from = 'x';
00040                continue;
00041           }
00042           *from <<= 1;
00043           int subsequent;
00044           for (subsequent = 1; (*from & 128); subsequent++) {
00045                 *from <<= 1;
00046                from[subsequent] &= 63;
00047                ch <<= 6;
00048                ch |= from[subsequent];
00049           }
00050           subsequent--;
00051           *from <<=1;
00052           char significantFirstBits = 8 - (2+subsequent);
00053           
00054           ch |= (((short)*from) << (((6*subsequent)+significantFirstBits)-8));
00055           from += subsequent;
00056           *to++ = '&';
00057           *to++ = '#';
00058           sprintf(digit, "%d", ch);
00059                 for (char *dig = digit; *dig; dig++)
00060                         *to++ = *dig;
00061                 *to++ = ';';
00062         }
00063         *to++ = 0;
00064         *to = 0;
00065         return 0;
00066 }

Generated on Thu Jun 20 22:13:01 2002 for The Sword Project by doxygen1.2.15