support 5.8.1 only.
[chise/perl.git] / Chise_utils / scripts / sample.pl
1 #!/usr/bin/perl -w -CSD
2
3 use Getopt::Long;  
4 use strict;
5 use vars qw($atr $value $char
6             $opt_map $opt_rev_map $opt_mapfrom $opt_mapto
7             $opt_atr $opt_allatr $opt_showatr
8             $opt_query
9             $opt_help
10             );
11 use Chise_utils ':all';
12 require 5.008001;
13
14 my $usage=<<EOF;
15 Usage: perl $0 [-m <map> | --map <map> |
16                 -r <reverse map> |
17                 --mapfrom <map> |
18                 --mapto <map> |
19                 -q <expression> |
20                 --show-attribute |
21                 -h | --help |
22                 -a=<atribuite> <char> |
23                 -aa <char>]
24     -q  <expression>
25       ex. of <expression>
26          total-strokes==8 means
27            the attribute of total-strokes exactly matches to 8.
28          ids-parts=‾ø½‹§‚ï½¢ means
29            the attribute of ids-parts contains the string ø½‹§‚ï½¢.
30          total-strokes==8,ids-parts=‾ø½‹§‚ï½¢ means both at the same time.
31     -a  get <attribute> of <char>
32     -aa get all attributes of <char>
33 EOF
34
35 &GetOptions("mapto=s"=>Â¥$opt_mapto,
36             "mapfrom=s"=>Â¥$opt_mapfrom,
37             "m=s"=>Â¥$opt_map,
38             "r=s"=>Â¥$opt_rev_map,
39             "a=s"=>Â¥$opt_atr,
40             "aa"=>Â¥$opt_allatr,
41             "show-attribute"=>Â¥$opt_showatr,
42             "q"=>Â¥$opt_query,
43             "help",Â¥$opt_help,
44             "h",Â¥$opt_help);
45
46 if($opt_help
47    or !((($opt_map or $opt_rev_map or $opt_mapfrom or $opt_mapto) and @ARGV==0)
48         or($opt_showatr and @ARGV==0)
49         or @ARGV==1
50         )
51    ){
52     print $usage;
53     exit 1;
54 }else{
55     $char=shift or $char="";
56     $char=&de_er($char);
57 }
58
59 if($opt_mapfrom){
60     $opt_map="<=".$opt_mapfrom;
61 }elsif($opt_mapto){
62     $opt_map="=>".$opt_mapto;
63 }
64
65 if($opt_map){
66     if(&get_db($opt_map)){
67         foreach $char (sort keys %{$chardb{$opt_map}}){
68             if($chardb{$opt_map}->{$char}){
69                 print $char,"Â¥t",$chardb{$opt_map}->{$char},"Â¥n";
70             }
71         }
72     }else{
73         print STDERR "No map for $opt_map.Â¥n";
74     }
75 }elsif($opt_rev_map){
76     if(&get_reverse_db($opt_rev_map)){
77         foreach $char (sort keys %{$reverse_chardb{$opt_rev_map}}){
78             if($reverse_chardb{$opt_rev_map}->{$char}){
79                 print $char,"Â¥t",$reverse_chardb{$opt_rev_map}->{$char},"Â¥n";
80             }
81         }
82     }else{
83         print STDERR "No map for $opt_rev_map.Â¥n";
84     }
85 }elsif($opt_allatr){
86     print $char,"Â¥n";
87     foreach $atr (sort keys %db){
88         if($value=&get_char_attribute($char,$atr)){
89             print "  ",$atr,":",$value,"Â¥n";
90         }
91     }
92 }elsif($opt_atr){
93     print $char,"Â¥n";
94     print "  ",$opt_atr,":",&get_char_attribute($char,$opt_atr),"Â¥n";
95 }elsif($opt_showatr){
96 #    print "In DBÂ¥n";
97     foreach $atr (sort keys %db){
98         print "  ",$atr,"Â¥n";
99     }
100 #    print "In reverse DBÂ¥n";
101 #    foreach $atr (sort keys %reverse_db){
102 #       print "  ",$atr,"Â¥n";
103 #    }
104 }elsif($opt_query){
105     my $query=$char;
106     print join "Â¥n",&get_chars_for($query),"Â¥n";
107 }
108
109 exit 0;