2006-01-24 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / I18N / tools / table_from.cs
1 /**
2  * Create a table from CHARSET to Unicode.
3  *
4  * @author Bruno Haible
5  */
6
7 using System; /* String, Console */
8 using System.Text; /* Encoding */
9
10 public class table_from {
11   static String toHexString1 (int i) {
12     return new String(new char[] { "0123456789ABCDEF".get_Chars(i) });
13   }
14   static String toHexString2 (int i) {
15     return  toHexString1((i>>4)&0x0f)
16            +toHexString1(i&0x0f);
17   }
18   static String toHexString4 (int i) {
19     return  toHexString1((i>>12)&0x0f)
20            +toHexString1((i>>8)&0x0f)
21            +toHexString1((i>>4)&0x0f)
22            +toHexString1(i&0x0f);
23   }
24   static void printOutput(char[] outp) {
25     Console.Out.Write("0x");
26     for (int j = 0; j < outp.Length; j++) {
27       if (j > 0)
28         Console.Out.Write(" 0x");
29       if (j+1 < outp.Length
30           && outp[j] >= 0xd800 && outp[j] < 0xdc00
31           && outp[j+1] >= 0xdc00 && outp[j+1] < 0xe000) {
32         int c = 0x10000 + ((outp[j] - 0xd800) << 10) + (outp[j+1] - 0xdc00);
33         Console.Out.Write(((Int32)c).ToString("X"));
34         j++;
35       } else
36         Console.Out.Write(toHexString4(outp[j]));
37     }
38   }
39   public static int Main (String[] args) {
40     try {
41       if (args.Length != 1) {
42         Console.Error.WriteLine("Usage: mono table_from charset");
43         return 1;
44       }
45       String charset = args[0];
46       Encoding encoding;
47       try {
48         encoding = Encoding.GetEncoding(charset);
49       } catch (NotSupportedException e) {
50         Console.Error.WriteLine("no converter for "+charset);
51         return 1;
52       }
53       byte[] qmark = encoding.GetBytes(new char[] { (char)0x003f });
54       for (int i0 = 0; i0 < 0x100; i0++) {
55         char[] outp = encoding.GetChars(new byte[] { (byte)i0 });
56         if (outp.Length > 0
57             && !(outp.Length >= 1 && outp[0] == 0x003f
58                  && !(qmark.Length == 1 && i0 == qmark[0]))) {
59           Console.Out.Write("0x"+toHexString2(i0)+"\t");
60           printOutput(outp);
61           Console.Out.WriteLine();
62         } else if (outp.Length <= 1) {
63           for (int i1 = 0; i1 < 0x100; i1++) {
64             outp = encoding.GetChars(new byte[] { (byte)i0, (byte)i1 });
65             if (outp.Length > 0
66                 && !(outp.Length >= 1 && outp[0] == 0x003f
67                      && !(qmark.Length == 2 && i0 == qmark[0] && i1 == qmark[1]))) {
68               Console.Out.Write("0x"+toHexString2(i0)+toHexString2(i1)+"\t");
69               printOutput(outp);
70               Console.Out.WriteLine();
71             } else if (outp.Length <= 1) {
72               for (int i2 = 0; i2 < 0x100; i2++) {
73                 outp = encoding.GetChars(new byte[] { (byte)i0, (byte)i1, (byte)i2 });
74                 if (outp.Length > 0
75                     && !(outp.Length >= 1 && outp[0] == 0x003f
76                          && !(qmark.Length == 3
77                               && i0 == qmark[0] && i1 == qmark[1] && i2 == qmark[2]))) {
78                   Console.Out.Write("0x"+toHexString2(i0)+toHexString2(i1)+toHexString2(i2)+"\t");
79                   printOutput(outp);
80                   Console.Out.WriteLine();
81                 } else if (outp.Length <= 1) {
82                   for (int i3 = 0; i3 < 0x100; i3++) {
83                     outp = encoding.GetChars(new byte[] { (byte)i0, (byte)i1, (byte)i2, (byte)i3 });
84                     if (outp.Length > 0
85                         && !(outp.Length >= 1 && outp[0] == 0x003f
86                              && !(qmark.Length == 4
87                                   && i0 == qmark[0] && i1 == qmark[1] && i2 == qmark[2] && i3 == qmark[3]))) {
88                       Console.Out.Write("0x"+toHexString2(i0)+toHexString2(i1)+toHexString2(i2)+toHexString2(i3)+"\t");
89                       printOutput(outp);
90                       Console.Out.WriteLine();
91                     }
92                   }
93                 }
94               }
95             }
96           }
97         }
98       }
99     } catch (Exception e) {
100       Console.Error.WriteLine(e);
101       Console.Error.WriteLine(e.StackTrace);
102       return 1;
103     }
104     return 0;
105   }
106 }