#!/usr/local/bin/perl
require 'codeconv.pl';
require 'getopts.pl';
sub usage {
print STDERR "rklist2rkc - convert Roma-Kana table to C program\n";
print STDERR " % rklist2rkc -h -s rktable > rk.c\n";
print STDERR " -h: Hiragana \n";
print STDERR " -s: Shift JIS \n";
exit;
}
$options = join(' ',@ARGV);
&usage unless &Getopts('hs');
&usage unless $rktable = shift;
&usage unless -f $rktable;
open(table, "nkf -e $rktable |") || die "Can't invoke NKF";
while(
){
chop;
next if /^#/;
$r = $k = '';
($r,$k) = split(/\s+/);
next if $r eq '' || $k eq '';
$k = &strconv($k);
$rk{$r} = $k;
@kr = split(/,/,$kr{$k});
push(@kr,$r);
$kr{$k} = join(',',@kr);
$nentries++;
}
$str_n = &hexdump(&strconv('¥ó'));
$str_tsu = &hexdump(&strconv('¥Ã'));
print <= 35
#include
#else
#include
#endif
#include "ctype.h"
#endif
#include "romakana.h"
RomaKana *
InitRomaTable()
{
RomaKana *rktable;
#ifdef UNIX
rktable = (RomaKana*)malloc(sizeof(RomaKana)*$nentries);
#endif
#ifdef PALM
rktable = (RomaKana*)MemPtrNew(sizeof(RomaKana)*$nentries);
#endif
EOF
$i = 0;
for $r (sort keys %rk){
print "\trktable[$i].roma = \"$r\";\trktable[$i].kana = \"",&hexdump($rk{$r}),"\";\n";
$i++;
}
print <roma);
if(StrNCompare(r,rk->roma,len)==0){
StrCat(kana,rk->kana);
r += len;
break;
}
}
if(i >= $nentries){
if(*r == 'n' && StrChr("bcdfghjklmnpqrstvwxz",*(r+1))){
StrCat(kana,"$str_n");
r++;
}
else if(StrChr("bcdfghjklmpqrstvwxyz",*r) && *r == *(r+1)){
StrCat(kana,"$str_tsu");
r++;
}
else if(*r == 'n' && ! *(r+1)){
StrCat(kana,"$str_n");
r++;
}
else {
len = StrLen(kana);
kana[len] = *r;
kana[len+1] = '\\0';
r++;
okay = 0;
}
}
}
return okay;
}
EOF
sub strconv {
local($_) = @_;
if($opt_h){
$s = '';
while($_ ne ''){
if(($h,$l) = /^([\x80-\xff])(.)/){
s/^..//;
if(ord($h) == 0xa5){
$h = "\xa4";
}
$s .= $h.$l;
}
else {
s/^.//;
$s .= $&;
}
}
$_ = $s;
}
if($opt_s){
$_ = &jis2sjis(&ujis2jis($_));
}
$_;
}
sub hexdump {
local($_) = @_;
local($s) = '';
while(s/^.//){
$s .= sprintf("\\%03o",ord($&));
}
$s;
}