1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsICharsetConverterManager.h"
6 #include <iostream.h>
7 #include "nsISupports.h"
8 #include "nsIComponentManager.h"
9 #include "nsIServiceManager.h"
10 #include "nsIUnicodeDecoder.h"
11 #include "nsIUnicodeEncoder.h"
12 #include "nsCRT.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #if defined(XP_WIN)
16 #include <io.h>
17 #endif
18 #ifdef XP_UNIX
19 #include <unistd.h>
20 #endif
21 
22 //---------------------------------------------------------------------------
header()23 void header() {
24   char* header =
25       "#ifndef nsCyrillicClass_h__\n"
26       "#define nsCyrillicClass_h__\n"
27       "/* PLEASE DO NOT EDIT THIS FILE DIRECTLY. THIS FILE IS GENERATED BY \n"
28       "   GenCyrllicClass found in mozilla/intl/chardet/tools\n"
29       " */\n";
30   printf(header);
31 }
32 //---------------------------------------------------------------------------
footer()33 void footer() { printf("#endif\n"); }
34 //---------------------------------------------------------------------------
npl()35 void npl() {
36   char* npl =
37       "/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 "
38       "-*- */\n"
39       "/* This Source Code Form is subject to the terms of the Mozilla Public\n"
40       " * License, v. 2.0. If a copy of the MPL was not distributed with this\n"
41       " * file, You can obtain one at http://mozilla.org/MPL/2.0/. */\n";
42   printf(npl);
43 }
44 //---------------------------------------------------------------------------
45 static nsIUnicodeEncoder* gKOI8REncoder = nullptr;
46 static nsICharsetConverterManager* gCCM = nullptr;
47 
48 //---------------------------------------------------------------------------
CyrillicClass(nsIUnicodeDecoder * decoder,uint8_t byte)49 uint8_t CyrillicClass(nsIUnicodeDecoder* decoder, uint8_t byte) {
50   char16_t ubuf[2];
51   uint8_t bbuf[2];
52 
53   int32_t blen = 1;
54   int32_t ulen = 1;
55   nsresult res = decoder->Convert((char*)&byte, &blen, ubuf, &ulen);
56   if (NS_SUCCEEDED(res) && (1 == ulen)) {
57     ubuf[0] = nsCRT::ToUpper(ubuf[0]);
58     blen = 1;
59     res = gKOI8REncoder->Convert(ubuf, &ulen, (char*)bbuf, &blen);
60     if (NS_SUCCEEDED(res) && (1 == blen)) {
61       if (0xe0 <= bbuf[0]) {
62         return bbuf[0] - (uint8_t)0xdf;
63       }
64     }
65   }
66   return 0;
67 }
68 //---------------------------------------------------------------------------
genCyrillicClass(const char * name,const char * charset)69 void genCyrillicClass(const char* name, const char* charset) {
70   nsIUnicodeDecoder* decoder = nullptr;
71   nsresult res = NS_OK;
72   nsAutoString str(charset);
73   res = gCCM->GetUnicodeDecoder(&str, &decoder);
74   if (NS_FAILED(res)) {
75     printf("cannot locate %s Decoder\n", charset);
76     return;
77   }
78   printf("static const uint8_t %sMap [128] = {\n", name);
79   uint8_t i, j;
80   for (i = 0x80; i != 0x00; i += 0x10) {
81     for (j = 0; j <= 0x0f; j++) {
82       uint8_t cls = CyrillicClass(decoder, i + j);
83       printf(" %2d, ", cls);
84     }
85     printf("\n");
86   }
87   printf("};\n");
88   NS_IF_RELEASE(decoder);
89 }
90 //---------------------------------------------------------------------------
91 
main(int argc,char ** argv)92 int main(int argc, char** argv) {
93   nsresult res = nullptr;
94 
95   nsCOMPtr<nsICharsetConverterManager> gCCM =
96       do_GetService(kCharsetConverterManagerCID, &res);
97 
98   if (NS_FAILED(res) && (nullptr != gCCM)) {
99     printf("cannot locate CharsetConverterManager\n");
100     return (-1);
101   }
102   nsAutoString koi8r("KOI8-R");
103   res = gCCM->GetUnicodeEncoder(&koi8r, &gKOI8REncoder);
104   if (NS_FAILED(res) && (nullptr != gKOI8REncoder)) {
105     printf("cannot locate KOI8-R Encoder\n");
106     return (-1);
107   }
108 
109   npl();
110   header();
111 
112   genCyrillicClass("KOI8", "KOI8-R");
113   genCyrillicClass("CP1251", "windows-1251");
114   genCyrillicClass("IBM866", "IBM866");
115   genCyrillicClass("ISO88595", "ISO-8859-5");
116   genCyrillicClass("MacCyrillic", "x-mac-cyrillic");
117   footer();
118   NS_IF_RELEASE(gKOI8REncoder);
119   return (0);
120 };
121