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

unicodertf.cpp

00001 /******************************************************************************
00002  *
00003  * unicodertf - SWFilter decendant to convert a double byte unicode file
00004  *                               to RTF tags
00005  */
00006 
00007 
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <unicodertf.h>
00011 
00012 UnicodeRTF::UnicodeRTF() {
00013 }
00014 
00015 
00016 char UnicodeRTF::ProcessText(char *text, int maxlen, const SWKey *key, const SWModule *module)
00017 {
00018         unsigned char *to, *from, *maxto;
00019         int len;
00020         char digit[10];
00021         short ch;       // must be signed per unicode spec (negative is ok for big numbers > 32768)
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         maxto =(unsigned char*)text + maxlen;
00030 
00031         // -------------------------------
00032         for (to = (unsigned char*)text; *from && (to <= maxto); from++) {
00033           ch = 0;
00034           if ((*from & 128) != 128) {
00035                *to++ = *from;
00036                continue;
00037           }
00038           if ((*from & 128) && ((*from & 64) != 64)) {
00039             // error
00040                *from = 'x';
00041                continue;
00042           }
00043           *from <<= 1;
00044           int subsequent;
00045           for (subsequent = 1; (*from & 128); subsequent++) {
00046                 *from <<= 1;
00047                from[subsequent] &= 63;
00048                ch <<= 6;
00049                ch |= from[subsequent];
00050           }
00051           subsequent--;
00052           *from <<=1;
00053           char significantFirstBits = 8 - (2+subsequent);
00054           
00055           ch |= (((short)*from) << (((6*subsequent)+significantFirstBits)-8));
00056           from += subsequent;
00057           *to++ = '\\';
00058           *to++ = 'u';
00059           sprintf(digit, "%d", ch);
00060                 for (char *dig = digit; *dig; dig++)
00061                         *to++ = *dig;
00062                 *to++ = '?';
00063         }
00064         
00065         if (to != maxto) {
00066                 *to++ = 0;
00067         }
00068         *to = 0;
00069         return 0;
00070 }

Generated on Wed Apr 3 22:34:15 2002 for The Sword Project by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002