Entry.cs 2.3 KB

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