2005-05-12 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / tools / locale-builder / Entry.cs
1 //
2 //
3 //
4
5 using System;
6 using System.Text;
7 using System.Collections;
8
9 namespace Mono.Tools.LocaleBuilder {
10
11         public class Entry {
12
13                 // maps strings to indexes
14                 static Hashtable hash;
15                 static ArrayList string_order;
16                 // idx 0 is reserved to indicate null
17                 static int curpos = 1;
18
19                 // serialize the strings in Hashtable.
20                 public static string GetStrings () {
21                         Console.WriteLine ("Total string data size: {0}", curpos);
22                         if (curpos > UInt16.MaxValue)
23                                 throw new Exception ("need to increase idx size in culture-info.h");
24                         StringBuilder ret = new StringBuilder ();
25                         // the null entry
26                         ret.Append ("\"\\0\"\n");
27                         foreach (string s in string_order) {
28                                 ret.Append ("\t\"");
29                                 ret.Append (s);
30                                 ret.Append ("\\0\"\n");
31                         }
32                         return ret.ToString ();
33                 }
34                 static Entry () {
35                         hash = new Hashtable ();
36                         string_order = new ArrayList ();
37                 }
38                 static int AddString (string s, int size) {
39                         object o = hash [s];
40                         if (o == null) {
41                                 int ret;
42                                 string_order.Add (s);
43                                 ret = curpos;
44                                 hash [s] = curpos;
45                                 curpos += size + 1; // null terminator
46                                 return ret;
47                         } else {
48                                 return (int)o;
49                         }
50                 }
51
52                 internal static String EncodeStringIdx (string str)
53                 {
54                         if (str == null)
55                                 return "0";
56
57                         StringBuilder ret = new StringBuilder ();
58                         byte [] ba = new UTF8Encoding ().GetBytes (str);
59                         bool in_hex = false;
60                         foreach (byte b in ba) {
61                                 if (b > 127 || (in_hex && is_hex (b))) {
62                                         ret.AppendFormat ("\\x{0:x}", b);
63                                         in_hex = true;
64                                 } else {
65                                         if (b == '\\')
66                                                 ret.Append ('\\');
67                                         ret.Append ((char) b);
68                                         in_hex = false;
69                                 }
70                         }
71                         int res = AddString (ret.ToString (), ba.Length);
72                         return res.ToString ();
73                 }
74
75                 private static bool is_hex (int e)
76                 {
77                         return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
78                 }
79         }
80 }
81
82
83
84