cp936tocdx.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/user/local/bin/perl
  2. # -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. $rowwidth = ((0xff - 0x80)+(0x7f - 0x40));
  8. sub cp936tonum()
  9. {
  10. my($cp936) = (@_);
  11. my($first,$second,$jnum);
  12. $first = hex(substr($cp936,2,2));
  13. $second = hex(substr($cp936,4,2));
  14. $jnum = ($first - 0x81 ) * $rowwidth;
  15. if($second >= 0x80)
  16. {
  17. $jnum += $second - 0x80 + (0x7f-0x40);
  18. }
  19. else
  20. {
  21. $jnum += $second - 0x40;
  22. }
  23. return $jnum;
  24. }
  25. @map = {};
  26. sub readtable()
  27. {
  28. open(CP936, "<gbkcommon.txt") || die "cannot open gbkcommon.txt";
  29. while(<CP936>)
  30. {
  31. if(! /^#/) {
  32. chop();
  33. ($j, $u, $r) = split(/\t/,$_);
  34. if(length($j) > 4)
  35. {
  36. $n = &cp936tonum($j);
  37. $map{$n} = $u;
  38. }
  39. }
  40. }
  41. }
  42. sub printtable()
  43. {
  44. for($i=0;$i<126;$i++)
  45. {
  46. printf ( "/* 0x%2XXX */\n", ( $i + 0x81));
  47. for($j=0;$j<(0x7f-0x40);$j++)
  48. {
  49. if("" eq ($map{($i * $rowwidth + $j)}))
  50. {
  51. printf "0xFFFD,"
  52. }
  53. else
  54. {
  55. printf $map{($i * $rowwidth + $j)} . ",";
  56. }
  57. if( 0 == (($j + 1) % 8))
  58. {
  59. printf "/* 0x%2X%1X%1X*/\n", $i+0x81, 4+($j/16), (7==($j%16))?0:8;
  60. }
  61. }
  62. print "0xFFFD,";
  63. printf "/* 0x%2X%1X%1X*/\n", $i+0x81, 4+($j/16),(7==($j%16))?0:8;
  64. for($j=0;$j < (0xff-0x80);$j++)
  65. {
  66. if("" eq ($map{($i * $rowwidth + $j + 0x3f)})) # user defined chars map to 0xFFFD
  67. {
  68. if ( ( $i == 125 ) and ( $j == (0xff - 0x80 - 1 )))
  69. {
  70. printf "0xFFFD"; #has no ',' followed last item
  71. }
  72. else
  73. {
  74. printf "0xFFFD,";
  75. }
  76. }
  77. else
  78. {
  79. if ( ( $i == 125 ) and ( $j == (0xff - 0x80 - 1 )))
  80. {
  81. printf $map{($i * $rowwidth + $j + 0x3f)}; #has no ',' followed last item
  82. }
  83. else
  84. {
  85. printf $map{($i * $rowwidth + $j + 0x3f)} . ",";
  86. }
  87. }
  88. if( 0 == (($j + 1) % 8))
  89. {
  90. printf "/* 0x%2X%1X%1X*/\n", $i+0x81, 8+($j/16), (7==($j%16))?0:8;
  91. }
  92. }
  93. printf " /* 0x%2X%1X%1X*/\n", $i+0x81, 8+($j/16),(7==($j%16))?0:8;
  94. }
  95. }
  96. sub printnpl()
  97. {
  98. $npl = <<END_OF_NPL;
  99. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  100. /* This Source Code Form is subject to the terms of the Mozilla Public
  101. * License, v. 2.0. If a copy of the MPL was not distributed with this
  102. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  103. END_OF_NPL
  104. print $npl;
  105. }
  106. sub printdontmodify()
  107. {
  108. $dont_modify = <<END_OF_DONT_MODIFY;
  109. /*
  110. This file is generated by mozilla/intl/uconv/tools/cp936tocdx.pl
  111. Please do not modify this file by hand
  112. Instead, you should download CP936.TXT from
  113. http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/
  114. and put under mozilla/intl/uconv/toools
  115. and run perl cp936tocdx.pl > ../ucvcn/cp936map.h
  116. If you have question, mailto:ftan\@netscape.com
  117. */
  118. END_OF_DONT_MODIFY
  119. print $dont_modify;
  120. }
  121. &readtable();
  122. &printnpl();
  123. &printdontmodify();
  124. &printtable();