Back to index

lightning-sunbird  0.9+nobinonly
GenCyrillicClass.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #include "nsICharsetConverterManager.h"
00039 #include <iostream.h>
00040 #include "nsISupports.h"
00041 #include "nsIComponentManager.h"
00042 #include "nsIServiceManager.h"
00043 #include "nsIUnicodeDecoder.h"
00044 #include "nsIUnicodeEncoder.h"
00045 #include "nsCRT.h"
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #if defined(XP_WIN) || defined(XP_OS2)
00049 #include <io.h>
00050 #endif
00051 #ifdef XP_UNIX
00052 #include <unistd.h>
00053 #endif
00054 
00055 //---------------------------------------------------------------------------
00056 void header()
00057 {
00058 char *header=
00059 "#ifndef nsCyrillicClass_h__\n"
00060 "#define nsCyrillicClass_h__\n"
00061 "/* PLEASE DO NOT EDIT THIS FILE DIRECTLY. THIS FILE IS GENERATED BY \n"
00062 "   GenCyrllicClass found in mozilla/intl/chardet/tools\n"
00063 " */\n";
00064    printf(header);
00065 }
00066 //---------------------------------------------------------------------------
00067 void footer()
00068 {
00069    printf("#endif\n");
00070 }
00071 //---------------------------------------------------------------------------
00072 void npl()
00073 {
00074 char *npl=
00075 "/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */\n"
00076 "/* ***** BEGIN LICENSE BLOCK *****\n"
00077 " * Version: MPL 1.1/GPL 2.0/LGPL 2.1\n"
00078 " *\n"
00079 " * The contents of this file are subject to the Mozilla Public License Version\n"
00080 " * 1.1 (the \"License\"); you may not use this file except in compliance with\n"
00081 " * the License. You may obtain a copy of the License at\n"
00082 " * http://www.mozilla.org/MPL/\n"
00083 " *\n"
00084 " * Software distributed under the License is distributed on an \"AS IS\" basis,\n"
00085 " * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\n"
00086 " * for the specific language governing rights and limitations under the\n"
00087 " * License.\n"
00088 " *\n"
00089 " * The Original Code is mozilla.org code.\n"
00090 " *\n"
00091 " * The Initial Developer of the Original Code is\n"
00092 " * Netscape Communications Corporation.\n"
00093 " * Portions created by the Initial Developer are Copyright (C) 2001\n"
00094 " * the Initial Developer. All Rights Reserved.\n"
00095 " *\n"
00096 " * Contributor(s):\n"
00097 " *\n"
00098 " * Alternatively, the contents of this file may be used under the terms of\n"
00099 " * either the GNU General Public License Version 2 or later (the \"GPL\"), or\n"
00100 " * the GNU Lesser General Public License Version 2.1 or later (the \"LGPL\"),\n"
00101 " * in which case the provisions of the GPL or the LGPL are applicable instead\n"
00102 " * of those above. If you wish to allow use of your version of this file only\n"
00103 " * under the terms of either the GPL or the LGPL, and not to allow others to\n"
00104 " * use your version of this file under the terms of the MPL, indicate your\n"
00105 " * decision by deleting the provisions above and replace them with the notice\n"
00106 " * and other provisions required by the GPL or the LGPL. If you do not delete\n"
00107 " * the provisions above, a recipient may use your version of this file under\n"
00108 " * the terms of any one of the MPL, the GPL or the LGPL.\n"
00109 " *\n"
00110 " * ***** END LICENSE BLOCK ***** */\n";
00111    printf(npl);
00112 }
00113 //---------------------------------------------------------------------------
00114 static nsIUnicodeEncoder* gKOI8REncoder = nsnull;
00115 static nsICharsetConverterManager* gCCM = nsnull;
00116 
00117 //---------------------------------------------------------------------------
00118 PRUint8 CyrillicClass(nsIUnicodeDecoder* decoder, PRUint8 byte)
00119 {
00120    PRUnichar ubuf[2];
00121    PRUint8 bbuf[2];
00122 
00123    PRInt32 blen = 1;
00124    PRInt32 ulen = 1;
00125    nsresult res = decoder->Convert((char*)&byte, &blen, ubuf, &ulen);
00126    if(NS_SUCCEEDED(res) && (1 == ulen ))
00127    {
00128      ubuf[0] = nsCRT::ToUpper(ubuf[0]);
00129      blen=1;
00130      res = gKOI8REncoder->Convert(ubuf,&ulen,(char*)bbuf,&blen);
00131      if(NS_SUCCEEDED(res) && (1 == blen))
00132      {
00133         if(0xe0 <= bbuf[0])
00134         {
00135               return bbuf[0] - (PRUint8)0xdf;
00136         }
00137      }
00138    }
00139    return 0;
00140 }
00141 //---------------------------------------------------------------------------
00142 void genCyrillicClass(const char* name, const char* charset)
00143 {
00144    nsIUnicodeDecoder *decoder = nsnull;
00145    nsresult res = NS_OK;
00146    nsAutoString str(charset);
00147    res = gCCM->GetUnicodeDecoder(&str, &decoder);
00148    if(NS_FAILED(res))
00149    {
00150       printf("cannot locate %s Decoder\n", charset);
00151       return;
00152    }
00153    printf("static const PRUint8 %sMap [128] = {\n",name);
00154    PRUint8 i,j;
00155    for(i=0x80;i!=0x00;i+=0x10)
00156    {
00157      for(j=0;j<=0x0f;j++)
00158      {
00159         PRUint8 cls = CyrillicClass(decoder, i+j);
00160         printf(" %2d, ",cls);
00161      }
00162      printf("\n");
00163    }
00164    printf("};\n");
00165    NS_IF_RELEASE(decoder);
00166 }
00167 //---------------------------------------------------------------------------
00168 
00169 
00170 int main(int argc, char** argv) {
00171   nsresult res = nsnull;
00172 
00173   nsCOMPtr<nsICharsetConverterManager> gCCM = do_GetService(kCharsetConverterManagerCID, &res);
00174 
00175   if(NS_FAILED(res) && (nsnull != gCCM))
00176    {
00177       printf("cannot locate CharsetConverterManager\n");
00178       return(-1);
00179    }
00180    nsAutoString koi8r("KOI8-R");
00181    res = gCCM->GetUnicodeEncoder(&koi8r,&gKOI8REncoder);
00182    if(NS_FAILED(res) && (nsnull != gKOI8REncoder))
00183    {
00184       printf("cannot locate KOI8-R Encoder\n");
00185       return(-1);
00186    }
00187 
00188 
00189    npl();
00190    header();
00191    
00192      genCyrillicClass("KOI8", "KOI8-R");
00193      genCyrillicClass("CP1251", "windows-1251");
00194      genCyrillicClass("IBM866", "IBM866");
00195      genCyrillicClass("ISO88595", "ISO-8859-5");
00196      genCyrillicClass("MacCyrillic", "x-mac-cyrillic");
00197    footer();
00198    NS_IF_RELEASE(gKOI8REncoder);
00199    return(0);
00200 };