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         bool lastUni = false;
00033         for (to = (unsigned char*)text; *from && (to <= maxto); from++) {
00034                 ch = 0;
00035                 if ((*from & 128) != 128) {
00036 //                      if ((*from == ' ') && (lastUni))
00037 //                              *to++ = ' ';
00038                         *to++ = *from;
00039                         lastUni = false;
00040                         continue;
00041                 }
00042                 if ((*from & 128) && ((*from & 64) != 64)) {
00043             // error
00044                         *from = 'x';
00045                         continue;
00046                 }
00047                 *from <<= 1;
00048                 int subsequent;
00049                 for (subsequent = 1; (*from & 128); subsequent++) {
00050                         *from <<= 1;
00051                         from[subsequent] &= 63;
00052                         ch <<= 6;
00053                         ch |= from[subsequent];
00054                 }
00055                 subsequent--;
00056                 *from <<=1;
00057                 char significantFirstBits = 8 - (2+subsequent);
00058                 
00059                 ch |= (((short)*from) << (((6*subsequent)+significantFirstBits)-8));
00060                 from += subsequent;
00061                 *to++ = '\\';
00062                 *to++ = 'u';
00063                 sprintf(digit, "%d", ch);
00064                 for (char *dig = digit; *dig; dig++)
00065                         *to++ = *dig;
00066                 *to++ = '?';
00067                 lastUni = true;
00068         }
00069            
00070         if (to != maxto) {
00071                 *to++ = 0;
00072         }
00073         *to = 0;
00074         return 0;
00075 }

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