Stb.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. public class StbTable
  5. {
  6. List<string> tagList;
  7. public string[] TagList
  8. {
  9. get
  10. {
  11. return tagList.ToArray();
  12. }
  13. }
  14. string name;
  15. public string Name
  16. {
  17. get { return name; }
  18. }
  19. string str;
  20. public string String
  21. {
  22. get { return str; }
  23. }
  24. public StbTable(string name, string str)
  25. {
  26. this.name = name;
  27. this.str = str;
  28. tagList = ParseTagList(str);
  29. }
  30. public static string UnescapeStr(string str)
  31. {
  32. int i, len;
  33. string tmp;
  34. len = str.Length;
  35. tmp = "";
  36. for (i = 0; i < len; i++)
  37. {
  38. if (str[i] == '\\')
  39. {
  40. i++;
  41. switch (str[i])
  42. {
  43. case '\\':
  44. tmp += '\\';
  45. break;
  46. case ' ':
  47. tmp += ' ';
  48. break;
  49. case 'n':
  50. case 'N':
  51. tmp += '\n';
  52. break;
  53. case 'r':
  54. case 'R':
  55. tmp += '\r';
  56. break;
  57. case 't':
  58. case 'T':
  59. tmp += '\t';
  60. break;
  61. }
  62. }
  63. else
  64. {
  65. tmp += str[i];
  66. }
  67. }
  68. return tmp;
  69. }
  70. public static StbTable ParseTableLine(string line, ref string prefix)
  71. {
  72. int i, len;
  73. int string_start;
  74. int len_name;
  75. string name, name2;
  76. line = line.TrimStart(' ', '\t');
  77. len = line.Length;
  78. if (len == 0)
  79. {
  80. return null;
  81. }
  82. if (line[0] == '#' || (line[0] == '/' && line[1] == '/'))
  83. {
  84. return null;
  85. }
  86. bool b = false;
  87. len_name = 0;
  88. for (i = 0; i < line.Length; i++)
  89. {
  90. if (line[i] == ' ' || line[i] == '\t')
  91. {
  92. b = true;
  93. break;
  94. }
  95. len_name++;
  96. }
  97. if (b == false)
  98. {
  99. return null;
  100. }
  101. name = line.Substring(0, len_name);
  102. string_start = len_name;
  103. for (i = len_name; i < len; i++)
  104. {
  105. if (line[i] != ' ' && line[i] != '\t')
  106. {
  107. break;
  108. }
  109. string_start++;
  110. }
  111. if (i == len)
  112. {
  113. return null;
  114. }
  115. string str = line.Substring(string_start);
  116. str = UnescapeStr(str);
  117. if (Str.StrCmpi(name, "PREFIX"))
  118. {
  119. prefix = str;
  120. prefix = prefix.TrimStart();
  121. if (Str.StrCmpi(prefix, "$") || Str.StrCmpi(prefix, "NULL"))
  122. {
  123. prefix = "";
  124. }
  125. return null;
  126. }
  127. name2 = "";
  128. if (prefix != "")
  129. {
  130. name2 += prefix + "@";
  131. }
  132. name2 += name;
  133. return new StbTable(name2, str);
  134. }
  135. public static bool CompareTagList(string[] list1, string[] list2)
  136. {
  137. if (list1.Length != list2.Length)
  138. {
  139. return false;
  140. }
  141. int i;
  142. for (i = 0; i < list1.Length; i++)
  143. {
  144. if (list1[i] != list2[i])
  145. {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. public static List<string> ParseTagList(string str)
  152. {
  153. List<string> list = new List<string>();
  154. int i, len;
  155. int mode = 0;
  156. string tmp = "";
  157. str += "_";
  158. len = str.Length;
  159. for (i = 0; i < len; i++)
  160. {
  161. char c = str[i];
  162. if (mode == 0)
  163. {
  164. switch (c)
  165. {
  166. case '%':
  167. if (str[i + 1] == '%')
  168. {
  169. i++;
  170. tmp += c;
  171. }
  172. else
  173. {
  174. mode = 1;
  175. tmp = "" + c;
  176. }
  177. break;
  178. default:
  179. tmp = "" + c;
  180. break;
  181. }
  182. }
  183. else
  184. {
  185. string tag;
  186. switch (c)
  187. {
  188. case 'c':
  189. case 'C':
  190. case 'd':
  191. case 'i':
  192. case 'o':
  193. case 'u':
  194. case 'x':
  195. case 'X':
  196. case 'e':
  197. case 'E':
  198. case 'f':
  199. case 'g':
  200. case 'G':
  201. case 'n':
  202. case 'N':
  203. case 's':
  204. case 'S':
  205. case 'r':
  206. case ' ':
  207. tmp += c;
  208. tag = tmp;
  209. list.Add(tag);
  210. mode = 0;
  211. break;
  212. default:
  213. tmp += c;
  214. break;
  215. }
  216. }
  217. }
  218. return list;
  219. }
  220. }
  221. public class Stb
  222. {
  223. Dictionary<string, StbTable> tableList;
  224. string name;
  225. public string Name
  226. {
  227. get { return name; }
  228. }
  229. public Stb(string fileName)
  230. {
  231. init(File.ReadAllBytes(fileName), fileName);
  232. }
  233. public Stb(string fileName, string name)
  234. {
  235. init(File.ReadAllBytes(fileName), name);
  236. }
  237. public Stb(byte[] data, string name)
  238. {
  239. init(data, name);
  240. }
  241. void init(byte[] data, string name)
  242. {
  243. if (data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf)
  244. {
  245. byte[] tmp = new byte[data.Length - 3];
  246. Array.Copy(data, 3, tmp, 0, data.Length - 3);
  247. data = tmp;
  248. }
  249. StringReader sr = new StringReader(Str.Utf8Encoding.GetString(data));
  250. tableList = new Dictionary<string, StbTable>();
  251. this.name = name;
  252. string prefix = "";
  253. while (true)
  254. {
  255. string tmp = sr.ReadLine();
  256. if (tmp == null)
  257. {
  258. break;
  259. }
  260. StbTable t = StbTable.ParseTableLine(tmp, ref prefix);
  261. if (t != null)
  262. {
  263. if (tableList.ContainsKey(t.Name.ToUpper()) == false)
  264. {
  265. tableList.Add(t.Name.ToUpper(), t);
  266. }
  267. else
  268. {
  269. ShowWarning(name, string.Format("Duplicated '{0}'", t.Name));
  270. }
  271. }
  272. }
  273. }
  274. protected static void ShowWarning(string name, string str)
  275. {
  276. Console.WriteLine("{0}: Warning: {1}", name, str);
  277. }
  278. protected static void ShowError(string name, string str)
  279. {
  280. Console.WriteLine("{0}: Error: {1}", name, str);
  281. }
  282. public static int Compare(string file1, string file2)
  283. {
  284. Stb stb1 = new Stb(file1, "File1");
  285. Stb stb2 = new Stb(file2, "File2");
  286. int num = 0;
  287. string file1_fn = Path.GetFileName(file1);
  288. string file2_fn = Path.GetFileName(file2);
  289. foreach (string name1 in stb1.tableList.Keys)
  290. {
  291. if (name1.Equals("DEFAULT_FONT_WIN7", StringComparison.InvariantCultureIgnoreCase) ||
  292. name1.Equals("DEFAULT_FONT_HIGHDPI", StringComparison.InvariantCultureIgnoreCase))
  293. {
  294. continue;
  295. }
  296. StbTable t1 = stb1.tableList[name1];
  297. if (stb2.tableList.ContainsKey(name1) == false)
  298. {
  299. ShowError(stb2.name, string.Format("Missing '{0}'", t1.Name));
  300. num++;
  301. }
  302. else
  303. {
  304. StbTable t2 = stb2.tableList[name1];
  305. if (StbTable.CompareTagList(t1.TagList, t2.TagList) == false)
  306. {
  307. ShowError(stb2.name, string.Format("Difference printf-style parameters '{0}'", t1.Name));
  308. num++;
  309. }
  310. }
  311. }
  312. Console.WriteLine("\nThere are {0} errors.\n\n{1}\n", num,
  313. (num == 0 ? "Good work! No problem!" : "You must correct them before sending us Pull Requests!"));
  314. return num;
  315. }
  316. }