X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=tools%2Flocale-builder%2FEntry.cs;h=69bb43fe5760dbee56b72409dce74358bb9faa23;hb=b4ef279dd767faf5bc881a17eb2dae07f07c0026;hp=a26f64c33387a99996cd667701b8cc122a1f5cc8;hpb=5d9434fcb3acc1ed7d3d30603faae797d672fe65;p=mono.git diff --git a/tools/locale-builder/Entry.cs b/tools/locale-builder/Entry.cs index a26f64c3338..69bb43fe576 100644 --- a/tools/locale-builder/Entry.cs +++ b/tools/locale-builder/Entry.cs @@ -1,84 +1,134 @@ // +// Entry.cs // +// Authors: +// Marek Safar +// +// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Text; -using System.Collections; +using System.Collections.Generic; -namespace Mono.Tools.LocaleBuilder { +namespace Mono.Tools.LocaleBuilder +{ + public class Entry + { + public static readonly Mapping General = new Mapping (); + public static readonly Mapping Patterns = new Mapping (); + public static readonly Mapping DateTimeStrings = new Mapping (); - public class Entry { + public class Mapping + { + // maps strings to indexes + Dictionary hash = new Dictionary (); + List string_order = new List (); + // idx 0 is reserved to indicate null + int curpos = 1; - // maps strings to indexes - static Hashtable hash; - static ArrayList string_order; - // idx 0 is reserved to indicate null - static int curpos = 1; + // serialize the strings in Hashtable. + public string GetStrings () + { + Console.WriteLine ("Total string data size: {0}", curpos); + if (curpos > UInt16.MaxValue) + throw new Exception ("need to increase idx size in culture-info.h"); + StringBuilder ret = new StringBuilder (); + // the null entry + ret.Append ("\t\"\\0\"\n"); + foreach (string s in string_order) { + ret.Append ("\t\""); + ret.Append (s); + ret.Append ("\\0\"\n"); + } + return ret.ToString (); + } - // serialize the strings in Hashtable. - public static string GetStrings () { - Console.WriteLine ("Total string data size: {0}", curpos); - if (curpos > UInt16.MaxValue) - throw new Exception ("need to increase idx size in culture-info.h"); - StringBuilder ret = new StringBuilder (); - // the null entry - ret.Append ("\"\\0\"\n"); - foreach (string s in string_order) { - ret.Append ("\t\""); - ret.Append (s); - ret.Append ("\\0\"\n"); + public int AddString (string s, int size) + { + if (!hash.ContainsKey (s)) { + int ret; + string_order.Add (s); + ret = curpos; + hash.Add (s, curpos); + curpos += size + 1; // null terminator + return ret; + } + + return hash[s]; } - return ret.ToString (); - } - static Entry () { - hash = new Hashtable (); - string_order = new ArrayList (); } - static int AddString (string s, int size) { - object o = hash [s]; - if (o == null) { - int ret; - string_order.Add (s); - ret = curpos; - hash [s] = curpos; - curpos += size + 1; // null terminator - return ret; - } else { - return (int)o; + + protected static StringBuilder AppendNames (StringBuilder builder, IList names) + { + builder.Append ('{'); + for (int i = 0; i < names.Count; i++) { + if (i > 0) + builder.Append (", "); + + builder.Append (Encode (DateTimeStrings, names[i])); } - } + builder.Append ("}"); - internal static String EncodeStringIdx (string str) - { - if (str == null) - return "0"; + return builder; + } - StringBuilder ret = new StringBuilder (); - byte [] ba = new UTF8Encoding ().GetBytes (str); - bool in_hex = false; - foreach (byte b in ba) { - if (b > 127 || (in_hex && is_hex (b))) { - ret.AppendFormat ("\\x{0:x}", b); - in_hex = true; - } else { - if (b == '\\') - ret.Append ('\\'); - ret.Append ((char) b); - in_hex = false; - } - } - int res = AddString (ret.ToString (), ba.Length); - return res.ToString (); - } - private static bool is_hex (int e) + public static string EncodeStringIdx (string str) { - return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f'); + return Encode (General, str); } - } -} + protected static string EncodePatternStringIdx (string str) + { + return Encode (Patterns, str); + } + static string Encode (Mapping mapping, string str) + { + if (str == null) + return "0"; + StringBuilder ret = new StringBuilder (); + byte[] ba = new UTF8Encoding ().GetBytes (str); + bool in_hex = false; + foreach (byte b in ba) { + if (b > 127 || (in_hex && is_hex (b))) { + ret.AppendFormat ("\\x{0:x}", b); + in_hex = true; + } else { + if (b == '\\') + ret.Append ('\\'); + ret.Append ((char) b); + in_hex = false; + } + } + int res = mapping.AddString (ret.ToString (), ba.Length); + return res.ToString (); + } + static bool is_hex (int e) + { + return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f'); + } + } +}