2005-07-15 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / Mono.Globalization.Unicode / create-combining-class-source.cs
1 //
2 //
3 // create-combining-class-source.cs
4 //
5 // Author:
6 //      Atsushi Enomoto  <atsushi@ximian.com>
7 //
8 // Copyright 2005 Novell, Inc
9 //
10 // It creates combining class information table.
11 //
12
13 using System;
14 using System.Globalization;
15 using System.IO;
16
17 namespace Mono.Globalization.Unicode
18 {
19         internal class CombiningClassCodeGenerator
20         {
21                 private int lineCount = 0;
22
23                 public static void Main ()
24                 {
25                         new CombiningClassCodeGenerator ().Run ();
26                 }
27
28                 private void Run ()
29                 {
30                         try {
31                                 Process ();
32                         } catch (Exception ex) {
33                                 throw new InvalidOperationException ("Internal error at line " + lineCount + " : " + ex);
34                         }
35                 }
36
37                 private void Process ()
38                 {
39                         Console.WriteLine ("public static byte GetCombiningClass (int c)");
40                         Console.WriteLine ("{");
41                         Console.WriteLine ("    switch (c) {");
42
43                         TextReader reader = Console.In;
44                         while (reader.Peek () != -1) {
45                                 string line = reader.ReadLine ();
46                                 lineCount++;
47                                 int idx = line.IndexOf ('#');
48                                 if (idx >= 0)
49                                         line = line.Substring (0, idx).Trim ();
50                                 if (line.Length == 0)
51                                         continue;
52                                 int n = 0;
53                                 while (Char.IsDigit (line [n]) || Char.IsLetter (line [n]))
54                                         n++;
55                                 int cp = int.Parse (line.Substring (0, n), NumberStyles.HexNumber);
56
57                                 int cpEnd = -1;
58                                 if (line [n] == '.' && line [n + 1] == '.')
59                                         cpEnd = int.Parse (line.Substring (n + 2, n), NumberStyles.HexNumber);
60                                 int nameStart = line.IndexOf (';') + 1;
61                                 int valueStart = line.IndexOf (';', nameStart) + 1;
62                                 string val = valueStart == 0 ? line.Substring (nameStart) :
63                                         line.Substring (nameStart, valueStart - nameStart - 1);
64                                 SetProp (cp, cpEnd, short.Parse (val));
65                         }
66
67                         Console.WriteLine ("            return {0};", prevVal);
68                         Console.WriteLine ("    default:");
69                         Console.WriteLine ("            return 0;");
70                         Console.WriteLine ("    }");
71                         Console.WriteLine ("}");
72
73                         reader.Close ();
74                 }
75
76                 private short prevVal;
77
78                 private void SetProp (int cp, int cpEnd, short val)
79                 {
80                         if (val == 0)
81                                 return;
82
83                         if (prevVal != val && prevVal != 0)
84                                 Console.WriteLine ("\t\treturn {0};", prevVal);
85                         prevVal = val;
86
87                         if (cpEnd < 0)
88                                 Console.WriteLine ("\tcase 0x{0:X}:", cp);
89                         else
90                                 for (int i = cp; i <= cpEnd; i++)
91                                         Console.WriteLine ("\tcase 0x{0:X}:", i);
92                 }
93         }
94 }
95