Entry.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Entry.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Text;
  30. using System.Collections.Generic;
  31. namespace Mono.Tools.LocaleBuilder
  32. {
  33. public class Entry
  34. {
  35. // maps strings to indexes
  36. static Dictionary<string, int> hash = new Dictionary<string, int> ();
  37. static List<string> string_order = new List<string> ();
  38. // idx 0 is reserved to indicate null
  39. static int curpos = 1;
  40. // serialize the strings in Hashtable.
  41. public static string GetStrings ()
  42. {
  43. Console.WriteLine ("Total string data size: {0}", curpos);
  44. if (curpos > UInt16.MaxValue)
  45. throw new Exception ("need to increase idx size in culture-info.h");
  46. StringBuilder ret = new StringBuilder ();
  47. // the null entry
  48. ret.Append ("\"\\0\"\n");
  49. foreach (string s in string_order) {
  50. ret.Append ("\t\"");
  51. ret.Append (s);
  52. ret.Append ("\\0\"\n");
  53. }
  54. return ret.ToString ();
  55. }
  56. static int AddString (string s, int size)
  57. {
  58. if (!hash.ContainsKey (s)) {
  59. int ret;
  60. string_order.Add (s);
  61. ret = curpos;
  62. hash.Add (s, curpos);
  63. curpos += size + 1; // null terminator
  64. return ret;
  65. }
  66. return hash[s];
  67. }
  68. protected static StringBuilder AppendNames (StringBuilder builder, IList<string> names)
  69. {
  70. builder.Append ('{');
  71. for (int i = 0; i < names.Count; i++) {
  72. if (i > 0)
  73. builder.Append (", ");
  74. builder.Append (EncodeStringIdx (names[i]));
  75. }
  76. builder.Append ("}");
  77. return builder;
  78. }
  79. internal static String EncodeStringIdx (string str)
  80. {
  81. if (str == null)
  82. return "0";
  83. StringBuilder ret = new StringBuilder ();
  84. byte[] ba = new UTF8Encoding ().GetBytes (str);
  85. bool in_hex = false;
  86. foreach (byte b in ba) {
  87. if (b > 127 || (in_hex && is_hex (b))) {
  88. ret.AppendFormat ("\\x{0:x}", b);
  89. in_hex = true;
  90. } else {
  91. if (b == '\\')
  92. ret.Append ('\\');
  93. ret.Append ((char) b);
  94. in_hex = false;
  95. }
  96. }
  97. int res = AddString (ret.ToString (), ba.Length);
  98. return res.ToString ();
  99. }
  100. private static bool is_hex (int e)
  101. {
  102. return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
  103. }
  104. }
  105. }