| 1 |
#!/usr/bin/ruby |
| 2 |
|
| 3 |
# txt 形式のかな変換テーブルを C/C++ のヘッダファイルに変換する |
| 4 |
# Satofumi KAMIMURA |
| 5 |
# $Id$ |
| 6 |
|
| 7 |
require 'uconv' |
| 8 |
require 'jcode' |
| 9 |
$KCODE = 'SJIS' |
| 10 |
|
| 11 |
$maxPattern_size = 0 |
| 12 |
|
| 13 |
# 変換処理 |
| 14 |
def convertLine(line) |
| 15 |
out = '' |
| 16 |
|
| 17 |
# if /(.+), (.+)/ =~ line |
| 18 |
if /([^ ]+) (.+)/ =~ line |
| 19 |
before = Regexp.last_match[1] |
| 20 |
after = Regexp.last_match[2] |
| 21 |
|
| 22 |
if before.jlength > $maxPattern_size |
| 23 |
$maxPattern_size = before.jlength |
| 24 |
end |
| 25 |
if after.jlength > $maxPattern_size |
| 26 |
$maxPattern_size = after.jlength |
| 27 |
end |
| 28 |
|
| 29 |
# 変換元の表示 |
| 30 |
out = ' { { ' |
| 31 |
pre_ch = '' |
| 32 |
before.each_char { |ch| |
| 33 |
# 文字がマルチバイト文字の場合 |
| 34 |
if ch.mbchar? |
| 35 |
out += '0x' |
| 36 |
out += ('0' + Uconv.sjistou16(ch)[1].to_s(16))[-2, 2] |
| 37 |
out += ('0' + Uconv.sjistou16(ch)[0].to_s(16))[-2, 2] |
| 38 |
out += ', ' |
| 39 |
else |
| 40 |
if pre_ch == '\\' |
| 41 |
out += "'\\" + ch + "'," |
| 42 |
elsif ch != '\\' |
| 43 |
out += "'" + ch + "'," |
| 44 |
end |
| 45 |
pre_ch = ch |
| 46 |
end |
| 47 |
} |
| 48 |
out += ' 0x00 }, { ' |
| 49 |
|
| 50 |
# 変換後の表示 |
| 51 |
after.each_char { |wch| |
| 52 |
out += '0x' |
| 53 |
out += ('0' + Uconv.sjistou16(wch)[1].to_s(16))[-2, 2] |
| 54 |
out += ('0' + Uconv.sjistou16(wch)[0].to_s(16))[-2, 2] |
| 55 |
out += ', ' |
| 56 |
} |
| 57 |
out += '0x00 },' |
| 58 |
|
| 59 |
# コメントとして変換後の文字を表示 |
| 60 |
out += " }, // " + after + "\n" |
| 61 |
end |
| 62 |
return out |
| 63 |
end |
| 64 |
|
| 65 |
|
| 66 |
# 変換元ファイル |
| 67 |
fileName = ARGV[0] |
| 68 |
|
| 69 |
# 行毎に変換 |
| 70 |
converted_lines = '' |
| 71 |
open (fileName) { |io| |
| 72 |
while line = io.gets |
| 73 |
converted_lines += convertLine(line) |
| 74 |
end |
| 75 |
} |
| 76 |
|
| 77 |
/(.+)_table.txt/ =~ fileName |
| 78 |
typeName = Regexp.last_match[1].capitalize |
| 79 |
capitalizedTypeName = typeName.upcase |
| 80 |
$maxPattern_size += 1 |
| 81 |
|
| 82 |
# 変換結果の出力 |
| 83 |
print <<-EOB |
| 84 |
#ifndef #{capitalizedTypeName}_CONVERT_TBL_H |
| 85 |
#define #{capitalizedTypeName}_CONVERT_TBL_H |
| 86 |
|
| 87 |
/*! |
| 88 |
\\file |
| 89 |
\\brief ローマ字・かな変換テーブル(自動生成) |
| 90 |
|
| 91 |
\\author Satofumi KAMIMURA |
| 92 |
|
| 93 |
$Id$ |
| 94 |
*/ |
| 95 |
|
| 96 |
#include <SDL.h> |
| 97 |
|
| 98 |
|
| 99 |
namespace beego { |
| 100 |
enum { #{capitalizedTypeName}_CONVERT_SIZE_MAX = #{$maxPattern_size} }; |
| 101 |
|
| 102 |
/*! |
| 103 |
\\brief #{typeName} 変換用の Unicode 配列 |
| 104 |
*/ |
| 105 |
const static Uint16 #{typeName}Table[][2][#{$maxPattern_size}] = { |
| 106 |
#{converted_lines} { { 0x00 }, { 0x00 }, }, // テーブルの終端 |
| 107 |
}; |
| 108 |
}; |
| 109 |
|
| 110 |
#endif /* !#{capitalizedTypeName}_CONVERT_TBL_H */ |
| 111 |
EOB |