#!/usr/bin/env perl
#
# Output a convertion table from UCS character to multibyte character
# Copyright (C) 2006  MIRACLE LINUX CORPORATION.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

use strict;
use Encode;
use Encode::EUCJPMS;

my %search_plane = (
    'cp932'       => [0],
    'cp51932'     => [0],
    'eucjp-ms'    => [0],
    'shiftjis'    => [0],
    'euc-jp'      => [0],
);

sub print_ucs2mb_one_char {
    my ($ucs, $mb) = @_;
    my $mblen = length($mb);

    printf "<U%04X> ", $ucs;
    for (my $i = 0; $i < $mblen; $i++) {
        printf "\\x%02X", ord(substr($mb, $i, 1));
    }
    print "\n";
}

sub dump_ucs2mb_plane {
    my ($codeset, $plane) = @_;

    for (my $u = 0; $u < 0x10000; $u++) {
        next if ($plane == 0 && $u >= 0xD800 && $u <= 0xDFFF);
        my $utf8 = chr($plane << 16 | $u);
        my $mb = encode($codeset, $utf8);
        if ($u == 0x003F || $mb ne "\x3F") {
            print_ucs2mb_one_char(ord($utf8), $mb);
        }
    }
}

sub dump_ucs2mb {
    my $codeset = shift;
    my $p = $search_plane{lc($codeset)};
    foreach my $plane (@$p) {
        dump_ucs2mb_plane($codeset, $plane);
    }
}

my $codeset;

if ($#ARGV != 0) {
    printf STDERR "Usage: ucs2mb_perl.pl codeset\n";
    exit 1;
}
$codeset = $ARGV[0];
if (!defined(find_encoding($codeset))) {
    print STDERR "Unknown encoding $codeset\n";
    exit 1;
}

dump_ucs2mb($codeset);

0;
