package jcode;

#jcodejis.pl ver.2

#原作者Copyright (c) 1995-2000 Kazumasa Utashiro <utashiro@iij.ad.jp>
#改造者えふ jcode.plをJIS変換専門、Perl5に改造したものです。

#ver.2 半角カナに対応(全角に変換します)

sub getcode {
    my($s) = $_[0];
    my($code,$sjis,$euc);
    
    if ($s !~ /[\e\200-\377]/) {# not Japanese
	$code = undef;
    }# 'jis'
    elsif ($s =~ /\e\$\@|\e\$B|\e&\@\e\$B|\e\$\(D|\e\([BJ]|\e\(I/o) {
	$code = 'jis';
    }
    elsif ($s =~ /[\000-\006\177\377]/o) {# 'binary'
	$code = 'binary';
    }
    else {# should be 'euc' or 'sjis'
	$sjis=0;
	$euc=0;
	while ($s =~ /(([\201-\237\340-\374][\100-\176\200-\374])+)/go) {
	    $sjis += length($1);
	}
	while ($s =~ /(([\241-\376][\241-\376]|\216[\241-\337]|\217[\241-\376][\241-\376])+)/go) {
	    $euc  += length($1);
	}
	&max($sjis, $euc);
	$code = ('euc', undef, 'sjis')[($sjis<=>$euc) + $[ + 1];
    }
    return $code;
}
sub max { $_[ $[ + ($_[ $[ ] < $_[ $[ + 1 ]) ]; }


sub convert {
    my($s,$icode) = @_;
    my($out,$char);
	if($icode eq ""){
		$icode="sjis";
	}
	if($icode eq "sjis"){
    	$out=&sjis2jis($s);
    }
    elsif($icode eq "euc"){
        $out=&euc2jis($s);
    }
    else{
		$char=&getcode($s);
    	if($char eq "sjis"){
    		$out=&sjis2jis($s);
    	}
    	elsif($char eq "euc"){
    		$out=&euc2jis($s);
    	}
    	else{
    		return undef;
    	}
    	
    }
   	return $out;
}

# SJIS to JIS**************************************************************
sub sjis2jis {
    my($s) = $_[0];
    my $esc_asc="\e(B";
    $s =~ s/(([\201-\237\340-\374][\100-\176\200-\374]|[\241-\337])+)/&_sjis2jis($1) . $esc_asc/geo;
    return $s;
}


sub _sjis2jis {
    my($s) = shift;
    $s =~ s/(([\201-\237\340-\374][\100-\176\200-\374])+|([\241-\337])+)/&__sjis2jis($1)/geo;
    return $s;
}

sub __sjis2jis {
    my($s) = shift;
    my($c1,$c2,$ss);
    if ($s=~/^[\241-\337]/o) {
    	while($s=~/([\241-\337])/sgo){
    		$t=$1;
        	$t=unpack('C',$t);
    		if($t==0xA0){
    			$c1=0x21;
    			$c2=0x21;
    		}
    		elsif($t==0xA1){
    			$c1=0x21;
    			$c2=0x23;
    		}
    		elsif($t==0xA2){
    			$c1=0x21;
    			$c2=0x56;
    		}
    		elsif($t==0xA3){
    			$c1=0x21;
    			$c2=0x57;
    		}
    		elsif($t==0xA4){
    			$c1=0x21;
    			$c2=0x22;
    		}
    		elsif($t==0xA5){
    			$c1=0x21;
    			$c2=0x26;
    		}
    		elsif($t==0xA6){
    			$c1=0x25;
    			$c2=0x72;
    		}
    		elsif($t==0xA7){
    			$c1=0x25;
    			$c2=0x21;
    		}
			elsif($t==0xA8){
    			$c1=0x25;
    			$c2=0x23;
			}
			elsif($t==0xA9){
    			$c1=0x25;
    			$c2=0x25;
			}
			elsif($t==0xAA){
    			$c1=0x25;
    			$c2=0x27;
			}
			elsif($t==0xAB){
    			$c1=0x25;
    			$c2=0x29;
			}
			elsif($t==0xAC){
    			$c1=0x25;
    			$c2=0x63;
			}
			elsif($t==0xAD){
    			$c1=0x25;
    			$c2=0x65;
			}
			elsif($t==0xAE){
    			$c1=0x25;
    			$c2=0x67;
			}
			elsif($t==0xAF){
    			$c1=0x25;
    			$c2=0x43;
			}
			elsif($t==0xB0){
    			$c1=0x21;
    			$c2=0x3C;
			}
			elsif($t==0xB1){
    			$c1=0x25;
    			$c2=0x22;
			}
			elsif($t==0xB2){
    			$c1=0x25;
    			$c2=0x24;
			}
			elsif($t==0xB3){
    			$c1=0x25;
    			$c2=0x26;
			}
			elsif($t==0xB4){
    			$c1=0x25;
    			$c2=0x28;
			}
			elsif($t==0xB5){
    			$c1=0x25;
    			$c2=0x2A;
			}
			elsif($t==0xB6){
    			$c1=0x25;
    			$c2=0x2B;
			}
			elsif($t==0xB7){
    			$c1=0x25;
    			$c2=0x2D;
			}
			elsif($t==0xB8){
    			$c1=0x25;
    			$c2=0x2F;
			}
			elsif($t==0xB9){
    			$c1=0x25;
    			$c2=0x31;
			}
			elsif($t==0xBA){
    			$c1=0x25;
    			$c2=0x33;
			}
			elsif($t==0xBB){
    			$c1=0x25;
    			$c2=0x35;
			}
			elsif($t==0xBC){
    			$c1=0x25;
    			$c2=0x37;
			}
			elsif($t==0xBD){
    			$c1=0x25;
    			$c2=0x39;
			}
			elsif($t==0xBE){
    			$c1=0x25;
    			$c2=0x3B;
			}
			elsif($t==0xBF){
    			$c1=0x25;
    			$c2=0x3D;
			}
			elsif($t==0xC0){
    			$c1=0x25;
    			$c2=0x3F;
			}
			elsif($t==0xC1){
    			$c1=0x25;
    			$c2=0x41;
			}
			elsif($t==0xC2){
    			$c1=0x25;
    			$c2=0x44;
			}
			elsif($t==0xC3){
    			$c1=0x25;
    			$c2=0x46;
			}
			elsif($t==0xC4){
    			$c1=0x25;
    			$c2=0x48;
			}
			elsif($t==0xC5){
    			$c1=0x25;
    			$c2=0x4A;
			}
			elsif($t==0xC6){
    			$c1=0x25;
    			$c2=0x4B;
			}
			elsif($t==0xC7){
    			$c1=0x25;
    			$c2=0x4C;
			}
			elsif($t==0xC8){
    			$c1=0x25;
    			$c2=0x4D;
			}
			elsif($t==0xC9){
    			$c1=0x25;
    			$c2=0x4E;
			}
			elsif($t==0xCA){
    			$c1=0x25;
    			$c2=0x4F;
			}
			elsif($t==0xCB){
    			$c1=0x25;
    			$c2=0x52;
			}
			elsif($t==0xCC){
    			$c1=0x25;
    			$c2=0x55;
			}
			elsif($t==0xCD){
    			$c1=0x25;
    			$c2=0x58;
			}
			elsif($t==0xCE){
    			$c1=0x25;
    			$c2=0x5B;
			}
			elsif($t==0xCF){
    			$c1=0x25;
    			$c2=0x5E;
			}
			elsif($t==0xD0){
    			$c1=0x25;
    			$c2=0x5F;
			}
			elsif($t==0xD1){
    			$c1=0x25;
    			$c2=0x60;
			}
			elsif($t==0xD2){
    			$c1=0x25;
    			$c2=0x61;
			}
			elsif($t==0xD3){
    			$c1=0x25;
    			$c2=0x62;
			}
			elsif($t==0xD4){
    			$c1=0x25;
    			$c2=0x64;
			}
			elsif($t==0xD5){
    			$c1=0x25;
    			$c2=0x66;
			}
			elsif($t==0xD6){
    			$c1=0x25;
    			$c2=0x68;
			}
			elsif($t==0xD7){
    			$c1=0x25;
    			$c2=0x69;
			}
			elsif($t==0xD8){
    			$c1=0x25;
    			$c2=0x6A;
			}
			elsif($t==0xD9){
    			$c1=0x25;
    			$c2=0x6B;
			}
			elsif($t==0xDA){
    			$c1=0x25;
    			$c2=0x6C;
			}
			elsif($t==0xDB){
    			$c1=0x25;
    			$c2=0x6D;
			}
			elsif($t==0xDC){
    			$c1=0x25;
    			$c2=0x6F;
			}
			elsif($t==0xDD){
    			$c1=0x25;
    			$c2=0x73;
			}
			elsif($t==0xDE){
    			$c1=0x21;
    			$c2=0x2B;
			}
			else{
    			$c1=0x21;
    			$c2=0x2C;
			}
			$t=pack('CC',$c1,$c2);
			$ss.=$t;
		}
		$ss =~ tr/\241-\376/\041-\176/;
		return ("\e\$B".$ss);
    }
    else {
		$s =~ s/([\201-\237\340-\374][\100-\176\200-\374])/&s2e($1)/geo;
		$s =~ tr/\241-\376/\041-\176/;
		return ("\e\$B".$s);
    }
}

sub s2e {
    my($c1, $c2, $code);
    ($c1, $c2) = unpack('CC', $code = shift);

    if (0xa1 <= $c1 && $c1 <= 0xdf) {
	$c2 = $c1;
	$c1 = 0x8e;
    } elsif (0x9f <= $c2) {
	$c1 = $c1 * 2 - ($c1 >= 0xe0 ? 0xe0 : 0x60);
	$c2 += 2;
    } else {
	$c1 = $c1 * 2 - ($c1 >= 0xe0 ? 0xe1 : 0x61);
	$c2 += 0x60 + ($c2 < 0x7f);
    }
	pack('CC', $c1, $c2);

}

# EUC to JIS**************************************************************
sub euc2jis {
    my($s) = $_[0];
    my $esc_asc="\e(B";
    $s =~ s/(([\241-\376][\241-\376]|\216[\241-\337]|\217[\241-\376][\241-\376])+)/&_euc2jis($1) . $esc_asc/geo;
    return $s;
}

sub _euc2jis {
    my($s) = shift;
    $s =~ s/(([\241-\376][\241-\376])+|(\216[\241-\337])+|(\217[\241-\376][\241-\376])+)/&__euc2jis($1)/geo;
    return $s;
}
sub __euc2jis {
    my($s) = shift;
    my($esc,$c1,$c2,$t,$ss);
    if ($s =~/^\216/) {
		 while($s=~/(\216[\241-\337])/sgo){
		 	$t=$1;
    		($c1,$c2)=unpack('CC',$t);
    		if($c2==0xA0){
    			$c1=0x21;
    			$c2=0x21;
    		}
    		elsif($c2==0xA1){
    			$c1=0x21;
    			$c2=0x23;
    		}
    		elsif($c2==0xA2){
    			$c1=0x21;
    			$c2=0x56;
    		}
    		elsif($c2==0xA3){
    			$c1=0x21;
    			$c2=0x57;
    		}
    		elsif($c2==0xA4){
    			$c1=0x21;
    			$c2=0x22;
    		}
    		elsif($c2==0xA5){
    			$c1=0x21;
    			$c2=0x26;
    		}
    		elsif($c2==0xA6){
    			$c1=0x25;
    			$c2=0x72;
    		}
    		elsif($c2==0xA7){
    			$c1=0x25;
    			$c2=0x21;
    		}
			elsif($c2==0xA8){
    			$c1=0x25;
    			$c2=0x23;
			}
			elsif($c2==0xA9){
    			$c1=0x25;
    			$c2=0x25;
			}
			elsif($c2==0xAA){
    			$c1=0x25;
    			$c2=0x27;
			}
			elsif($c2==0xAB){
    			$c1=0x25;
    			$c2=0x29;
			}
			elsif($c2==0xAC){
    			$c1=0x25;
    			$c2=0x63;
			}
			elsif($c2==0xAD){
    			$c1=0x25;
    			$c2=0x65;
			}
			elsif($c2==0xAE){
    			$c1=0x25;
    			$c2=0x67;
			}
			elsif($c2==0xAF){
    			$c1=0x25;
    			$c2=0x43;
			}
			elsif($c2==0xB0){
    			$c1=0x21;
    			$c2=0x3C;
			}
			elsif($c2==0xB1){
    			$c1=0x25;
    			$c2=0x22;
			}
			elsif($c2==0xB2){
    			$c1=0x25;
    			$c2=0x24;
			}
			elsif($c2==0xB3){
    			$c1=0x25;
    			$c2=0x26;
			}
			elsif($c2==0xB4){
    			$c1=0x25;
    			$c2=0x28;
			}
			elsif($c2==0xB5){
    			$c1=0x25;
    			$c2=0x2A;
			}
			elsif($c2==0xB6){
    			$c1=0x25;
    			$c2=0x2B;
			}
			elsif($c2==0xB7){
    			$c1=0x25;
    			$c2=0x2D;
			}
			elsif($c2==0xB8){
    			$c1=0x25;
    			$c2=0x2F;
			}
			elsif($c2==0xB9){
    			$c1=0x25;
    			$c2=0x31;
			}
			elsif($c2==0xBA){
    			$c1=0x25;
    			$c2=0x33;
			}
			elsif($c2==0xBB){
    			$c1=0x25;
    			$c2=0x35;
			}
			elsif($c2==0xBC){
    			$c1=0x25;
    			$c2=0x37;
			}
			elsif($c2==0xBD){
    			$c1=0x25;
    			$c2=0x39;
			}
			elsif($c2==0xBE){
    			$c1=0x25;
    			$c2=0x3B;
			}
			elsif($c2==0xBF){
    			$c1=0x25;
    			$c2=0x3D;
			}
			elsif($c2==0xC0){
    			$c1=0x25;
    			$c2=0x3F;
			}
			elsif($c2==0xC1){
    			$c1=0x25;
    			$c2=0x41;
			}
			elsif($c2==0xC2){
    			$c1=0x25;
    			$c2=0x44;
			}
			elsif($c2==0xC3){
    			$c1=0x25;
    			$c2=0x46;
			}
			elsif($c2==0xC4){
    			$c1=0x25;
    			$c2=0x48;
			}
			elsif($c2==0xC5){
    			$c1=0x25;
    			$c2=0x4A;
			}
			elsif($c2==0xC6){
    			$c1=0x25;
    			$c2=0x4B;
			}
			elsif($c2==0xC7){
    			$c1=0x25;
    			$c2=0x4C;
			}
			elsif($c2==0xC8){
    			$c1=0x25;
    			$c2=0x4D;
			}
			elsif($c2==0xC9){
    			$c1=0x25;
    			$c2=0x4E;
			}
			elsif($c2==0xCA){
    			$c1=0x25;
    			$c2=0x4F;
			}
			elsif($c2==0xCB){
    			$c1=0x25;
    			$c2=0x52;
			}
			elsif($c2==0xCC){
    			$c1=0x25;
    			$c2=0x55;
			}
			elsif($c2==0xCD){
    			$c1=0x25;
    			$c2=0x58;
			}
			elsif($c2==0xCE){
    			$c1=0x25;
    			$c2=0x5B;
			}
			elsif($c2==0xCF){
    			$c1=0x25;
    			$c2=0x5E;
			}
			elsif($c2==0xD0){
    			$c1=0x25;
    			$c2=0x5F;
			}
			elsif($c2==0xD1){
    			$c1=0x25;
    			$c2=0x60;
			}
			elsif($c2==0xD2){
    			$c1=0x25;
    			$c2=0x61;
			}
			elsif($c2==0xD3){
    			$c1=0x25;
    			$c2=0x62;
			}
			elsif($c2==0xD4){
    			$c1=0x25;
    			$c2=0x64;
			}
			elsif($c2==0xD5){
    			$c1=0x25;
    			$c2=0x66;
			}
			elsif($c2==0xD6){
    			$c1=0x25;
    			$c2=0x68;
			}
			elsif($c2==0xD7){
    			$c1=0x25;
    			$c2=0x69;
			}
			elsif($c2==0xD8){
    			$c1=0x25;
    			$c2=0x6A;
			}
			elsif($c2==0xD9){
    			$c1=0x25;
    			$c2=0x6B;
			}
			elsif($c2==0xDA){
    			$c1=0x25;
    			$c2=0x6C;
			}
			elsif($c2==0xDB){
    			$c1=0x25;
    			$c2=0x6D;
			}
			elsif($c2==0xDC){
    			$c1=0x25;
    			$c2=0x6F;
			}
			elsif($c2==0xDD){
    			$c1=0x25;
    			$c2=0x73;
			}
			elsif($c2==0xDE){
    			$c1=0x21;
    			$c2=0x2B;
			}
			else{
    			$c1=0x21;
    			$c2=0x2C;
			}
			$t=pack('CC',$c1,$c2);
			$ss.=$t;
		}
		$ss =~ tr/\241-\376/\041-\176/;
		return ("\e\$B".$ss);
    }
    elsif($s =~ tr/\217//d) {
		$esc = "\e\$(D";
		$s =~ tr/\241-\376/\041-\176/;
		return($esc.$s);
    }
    else{
		$esc = "\e\$B";
		$s =~ tr/\241-\376/\041-\176/;
		return($esc.$s);
    }

 
}

#**********************************************************

1;
