From 20353947cb356a71733a96137f781eab818d6cdd Mon Sep 17 00:00:00 2001 From: imiyazaki Date: Fri, 14 Mar 2003 15:46:05 +0000 Subject: [PATCH] simplify code. add fall back. --- Chise_utils/Chise_utils.pm | 22 ++++++++++------------ Chise_utils/sample.PL | 6 ++---- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/Chise_utils/Chise_utils.pm b/Chise_utils/Chise_utils.pm index 7d3d7fe..b0d7608 100644 --- a/Chise_utils/Chise_utils.pm +++ b/Chise_utils/Chise_utils.pm @@ -119,7 +119,7 @@ for (glob "$DB_HOME/*"){ sub get_db{ my($atr)=@_; - return if(defined(%{$chardb{$atr}})); + return 1 if(defined(%{$chardb{$atr}})); if(defined($db{$atr}) and -f $db{$atr}){ tie %{$chardb{$atr}}, "BerkeleyDB::Hash", -Filename => $db{$atr}; @@ -130,7 +130,7 @@ sub get_db{ sub get_reverse_db{ my($atr)=@_; - return if(defined(%{$reverse_chardb{$atr}})); + return 1 if(defined(%{$reverse_chardb{$atr}})); if(defined($reverse_db{$atr}) and -f $reverse_db{$atr}){ tie %{$reverse_chardb{$atr}}, "BerkeleyDB::Hash", -Filename => $reverse_db{$atr}; @@ -142,9 +142,7 @@ sub get_reverse_db{ sub get_char_attribute{ my($char,$atr)=@_; - unless(defined($chardb{$atr})){ - &get_db($atr) or return ""; - } + &get_db($atr) or return ""; if($chardb{$atr}->{"?$char"}){ return $chardb{$atr}->{"?$char"}; }else{ @@ -155,8 +153,7 @@ sub get_char_attribute{ sub get_chars_containing{ my($atr,$value)=@_; my($char,@res); - &get_db($atr); - if(defined(%{$chardb{$atr}})){ + if(&get_db($atr)){ foreach $char (keys %{$chardb{$atr}}){ if($chardb{$atr}->{$char}=~/$value/){ $char=~s/^\?//; @@ -171,16 +168,17 @@ sub get_chars_matching{ my($atr,$value)=@_; my($char,@res); if(defined($reverse_db{$atr})){ - &get_reverse_db($atr); - if(defined(%{$reverse_chardb{$atr}})){ + if(&get_reverse_db($atr)){ if($char=$reverse_chardb{$atr}->{$value}){ $char=~s/^\?//; push @res,$char; } } - }else{ - &get_db($atr); - if(defined(%{$chardb{$atr}})){ + } + # else{ + # fall back if DB inconsistency exists. + unless(@res){ + if(&get_db($atr)){ foreach $char (keys %{$chardb{$atr}}){ if($chardb{$atr}->{$char} eq $value){ $char=~s/^\?//; diff --git a/Chise_utils/sample.PL b/Chise_utils/sample.PL index 4ac5c89..5548e96 100644 --- a/Chise_utils/sample.PL +++ b/Chise_utils/sample.PL @@ -61,8 +61,7 @@ if($opt_mapfrom){ } if($opt_map){ - &get_db($opt_map); - if(defined(%{$chardb{$opt_map}})){ + if(&get_db($opt_map)){ foreach $char (sort keys %{$chardb{$opt_map}}){ if($chardb{$opt_map}->{$char}){ print $char,"\t",$chardb{$opt_map}->{$char},"\n"; @@ -72,8 +71,7 @@ if($opt_map){ print STDERR "No map for $opt_map.\n"; } }elsif($opt_rev_map){ - &get_reverse_db($opt_rev_map); - if(defined(%{$reverse_chardb{$opt_rev_map}})){ + if(&get_reverse_db($opt_rev_map)){ foreach $char (sort keys %{$reverse_chardb{$opt_rev_map}}){ if($reverse_chardb{$opt_rev_map}->{$char}){ print $char,"\t",$reverse_chardb{$opt_rev_map}->{$char},"\n"; -- 1.7.10.4