+package Chise_utils;
+
+require 5.005;
+use strict;
+use warnings;
+
+require Exporter;
+
+use utf8;
+use BerkeleyDB;
+use vars qw(%db %chardb $atr
+ );
+
+our @ISA = qw(Exporter);
+
+# Items to export into callers namespace by default. Note: do not export
+# names by default without a very good reason. Use EXPORT_OK instead.
+# Do not simply export all your public functions/methods/constants.
+
+# This allows declaration use Chise_utils ':all';
+# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
+# will save memory.
+our %EXPORT_TAGS = ( 'all' => [ qw(
+ %db %chardb
+ &get_char_attribute
+ &get_chars_matching
+ &get_chars_containing
+ &get_chars_matching
+ &get_chars_for
+ &de_er
+) ] );
+
+our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
+
+our @EXPORT = qw(
+);
+our $VERSION = '0.01';
+
+
+# Preloaded methods go here.
+
+my $DB_HOME="";
+if(-e '/usr/local/lib/xemacs-21.4.10/i686-pc-linux/char-db/system-char-id'){
+ $DB_HOME='/usr/local/lib/xemacs-21.4.10/i686-pc-linux/char-db/system-char-id';
+}elsif(-e 'd:/work/chise/char-db/system-char-id'){
+ $DB_HOME='d:/work/chise/char-db/system-char-id';
+}elsif(-e '/usr/local/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db/system-char-id'){
+ $DB_HOME='/usr/local/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db/system-char-id';
+}elsif(-e '/usr/local/xemacs-utf2000/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db/system-char-id'){
+ $DB_HOME='/usr/local/xemacs-utf2000/lib/xemacs-21.4.10/powerpc-apple-darwin6.4/char-db/system-char-id';
+}else{
+ print STDERR "No database found.\n";
+ print STDERR "Pleas set \$DB_HOME to Chise_utils.pm.\n";
+ exit 1;
+}
+
+for (<$DB_HOME/*>){
+ next if(/\.txt$/);
+ $atr=$_;
+ $atr=~s!$DB_HOME/!!;
+ $db{$atr}=$_;
+}
+
+foreach $atr (keys %db){
+ if(defined($db{$atr}) and -f $db{$atr}){
+ tie %{$chardb{$atr}}, "BerkeleyDB::Hash",
+ -Filename => $db{$atr};
+ }else{
+ print STDERR "no target\n";
+ exit 1;
+ }
+}
+
+
+sub get_char_attribute{
+ my($char,$atr)=@_;
+ if($chardb{$atr}->{"?$char"}){
+ return $chardb{$atr}->{"?$char"};
+ }else{
+ return "no attribute for $char";
+ }
+}
+
+sub get_chars_containing{
+ my($atr,$value)=@_;
+ my($char,@res);
+ if(defined(%{$chardb{$atr}})){
+ foreach $char (keys %{$chardb{$atr}}){
+ if($chardb{$atr}->{$char}=~/$value/){
+ push @res,$char;
+ }
+ }
+ }
+ return @res;
+}
+
+sub get_chars_matching{
+ my($atr,$value)=@_;
+ my($char,@res);
+ if(defined(%{$chardb{$atr}})){
+ foreach $char (keys %{$chardb{$atr}}){
+ if($chardb{$atr}->{$char}=~/^$value$/){
+ push @res,$char;
+ }
+ }
+ }
+ return @res;
+}
+
+sub get_chars_for{
+ my($query)=@_;
+ my @q=split(",",$query);
+ my(%res,@res,$atr,$value);
+ my $i=0;
+ foreach $query (@q){
+ if($query=~/==/){
+ ($atr,$value)=split("==",$query,2);
+ $i++;
+ foreach (&get_chars_matching($atr,$value)){
+ $res{$_}++;
+ }
+ }elsif($query=~/=~/){
+ ($atr,$value)=split("=~",$query,2);
+ $i++;
+ foreach (&get_chars_containing($atr,$value)){
+ $res{$_}++;
+ }
+ }
+ }
+ foreach (keys %res){
+ if($res{$_}==$i){
+ push @res,$_;
+ }
+ }
+ return @res;
+}
+
+sub de_er{
+ my($char)=@_;
+ if($char=~/^\d+$/){
+ $char=pack("U",$char);
+ }elsif($char=~/U[\+\-](\d+)/){
+ $char=pack("U",$1);
+ }elsif($char=~m/CDP\-(\d+)/){
+ # chinese-big5-cdp CDP- 4 X),
+ # ideograph-daikanwa M- 5 d),
+ # ideograph-cbeta CB 5 d),
+ # ideograph-gt GT- 5 d),
+ # ideograph-gt-k GT-K 5 d),
+ # japanese-jisx0208-1990 J90- 4 X),
+ # japanese-jisx0208 J83- 4 X),
+ # japanese-jisx0213-1 JX1- 4 X),
+ # japanese-jisx0213-2 JX2- 4 X),
+ # japanese-jisx0212 JSP- 4 X),
+ # japanese-jisx0208-1978 J78- 4 X),
+ # chinese-cns11643-1 C1- 4 X),
+ # chinese-cns11643-2 C2- 4 X),
+ # chinese-cns11643-3 C3- 4 X),
+ # chinese-cns11643-4 C4- 4 X),
+ # chinese-cns11643-5 C5- 4 X),
+ # chinese-cns11643-6 C6- 4 X),
+ # chinese-cns11643-7 C7- 4 X),
+ # korean-ksc5601 K0- 4 X),
+ }
+ return $char;
+}
+
+# Autoload methods go after =cut, and are processed by the autosplit program.
+
+1;
+__END__
+# Below is stub documentation for your module. You better edit it!
+
+=head1 NAME
+
+Chise_utils - Perl extension for blah blah blah
+
+=head1 SYNOPSIS
+
+ use Chise_utils;
+ blah blah blah
+
+=head1 DESCRIPTION
+
+Stub documentation for Chise_utils, created by h2xs. It looks like the
+author of the extension was negligent enough to leave the stub
+unedited.
+
+Blah blah blah.
+
+=head2 EXPORT
+
+None by default.
+
+
+=head1 AUTHOR
+
+A. U. Thor, a.u.thor@a.galaxy.far.far.away
+
+=head1 SEE ALSO
+
+perl(1).
+
+=cut