From: Koichi KAMICHI Date: Wed, 5 Apr 2006 13:37:45 +0000 (+0000) Subject: Unify chise_tools.i into chise.i. X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=ee244e2163a6c301882711f4c9ff40e54ccdbee3;p=chise%2Fkage.git Unify chise_tools.i into chise.i. Expand buffer size for feature names. --- diff --git a/tools/chise_tools.i b/tools/chise_tools.i deleted file mode 100755 index 5dd6401..0000000 --- a/tools/chise_tools.i +++ /dev/null @@ -1,96 +0,0 @@ -// chise_tools.i -// -// Copyright (C) 2005 Koichi Kamichi. -// -// reference : http://www.swig.org/Doc1.3/Perl5.html -// sample.c included to libchise - -%module chise_tools - -// This tells SWIG to treat char ** as a special case -%typemap(in) char ** { - AV *tempav; - I32 len; - int i; - SV **tv; - if (!SvROK($input)) - croak("Argument $argnum is not a reference."); - if (SvTYPE(SvRV($input)) != SVt_PVAV) - croak("Argument $argnum is not an array."); - tempav = (AV*)SvRV($input); - len = av_len(tempav); - $1 = (char **) malloc((len+2)*sizeof(char *)); - for (i = 0; i <= len; i++) { - tv = av_fetch(tempav, i, 0); - $1[i] = (char *) SvPV(*tv,PL_na); - } - $1[i] = NULL; -}; - -// This cleans up the char ** array after the function call -%typemap(freearg) char ** { - free($1); -} - -// Creates a new Perl array and places a NULL-terminated char ** into it -%typemap(out) char ** { - AV *myav; - SV **svs; - int i = 0,len = 0; - /* Figure out how many elements we have */ - while ($1[len]) - len++; - svs = (SV **) malloc(len*sizeof(SV *)); - for (i = 0; i < len ; i++) { - svs[i] = sv_newmortal(); - sv_setpv((SV*)svs[i],$1[i]); - }; - myav = av_make(len,svs); - free(svs); - $result = newRV((SV*)myav); - sv_2mortal($result); - argvi++; -} - -%inline %{ - -#include - -unsigned char *get_uchar(char *argv){ - return (unsigned char *)argv; -} - -char *get_char(unsigned char *argv){ - return (char *)argv; -} - -unsigned char buffer[1024]; -int buffer_size = 1024; - -void clear_buffer(){ - int i; - for(i = 0; i < buffer_size; i++){ - buffer[i] = 0; - } -} - -unsigned char feature[10240]; -int feature_size = 10240; - -static int -name_map_func (CHISE_DS *ds, unsigned char *name) -{ - strcat((char *)feature, (char *)name); - strcat((char *)feature, (char *)"\n"); - return 0; -} - -void listup_feature(CHISE_DS *ds){ - int i; - for(i = 0; i < feature_size; i++){ - feature[i] = 0; - } - chise_ds_foreach_char_feature_name (ds, &name_map_func); -} - -%} diff --git a/tools/chiseperl.pl b/tools/chiseperl.pl index 4ba8911..48b1f73 100755 --- a/tools/chiseperl.pl +++ b/tools/chiseperl.pl @@ -1,91 +1,91 @@ -# chiseperl.pl --- some useful funcions for using libchise with SWIG -# -# Copyright (C) 2005 Koichi Kamichi. -# -# global : $chise_ds, @chise_feature, @chise_ft, $chise_newid -# function : void init_chise(); -# void close_chise(); -# void update_chise(); -# void search_newid(); -# $var get_feature_value($feature, $char_id); -# $var set_feature_value($feature, $char_id, $value); -# $var create_new_char($feature, $value); -# $char_id decode_char($feature_char, $int); - -use chise_tools; -use chise; - -sub init_chise{ - $chise_ds = chise::CHISE_DS_open(0, $chise::chise_system_db_dir, 0, 0755); - chise_tools::listup_feature($chise_ds); - @chise_feature = split(/\n/, chise_tools::get_char($chise_tools::feature)); - @chise_ft = (); - foreach(@chise_feature){ - push(@chise_ft, chise::chise_ds_get_feature($chise_ds, chise_tools::get_uchar($_))); - } - $chise_newid = 0x0F0000; -} - -sub get_feature_value{ # feature, char_id -> 0 or value - if(scalar(@_) != 2){ - return 0; - } - my $ft = chise::chise_ds_get_feature($chise_ds, chise_tools::get_uchar($_[0])); - chise_tools::clear_buffer; - chise::chise_char_gets_feature_value($_[1], $ft, - $chise_tools::buffer, $chise_tools::buffer_size); - return chise_tools::get_char($chise_tools::buffer); -} - -sub set_feature_value{ # feature, char_id, value -> result - if(scalar(@_) != 3){ - return 0; - } - my $ft = chise::chise_ds_get_feature($chise_ds, chise_tools::get_uchar($_[0])); - chise::chise_char_set_feature_value($_[1], $ft, chise_tools::get_uchar($_[2])); - return 1; -} - -sub close_chise{ - chise::CHISE_DS_close($chise_ds); -} - -sub create_new_char{ # feature, value -> assinged char_id - &search_newid; - &set_feature_value($_[0], $chise_newid, $_[1]); - $chise_newid++; - return $chise_newid - 1; -} - -sub search_newid{ # search new char_id and update $chise_newid - while(1){ - my $found = 0; - foreach(@chise_ft){ - chise_tools::clear_buffer; - chise::chise_char_gets_feature_value($chise_newid, $_, $chise_tools::buffer, $chise_tools::buffer_size); - if(length(chise_tools::get_char($chise_tools::buffer)) != 0){ - $found = 1; - last; - } - } - if($found == 0){ - last; - } - $chise_newid++; - } -} - -sub update_chise{ # save added data to database - foreach(@chise_ft){ - chise::chise_feature_sync($_); - } -} - -sub decode_char{ # feature, int -> charID, 0 - if(scalar(@_) != 2){ - return 0; - } - return chise::chise_ds_decode_char($chise_ds, chise_tools::get_uchar($_[0]), $_[1]); -} - -1; +# chiseperl.pl --- some useful funcions for using libchise with SWIG +# +# Copyright (C) 2005,2006 KAMICHI Koichi. +# +# global : $chise_ds, @chise_feature, @chise_ft, $chise_newid +# function : void init_chise(); +# void close_chise(); +# void update_chise(); +# void search_newid(); +# $var get_feature_value($feature, $char_id); +# $var set_feature_value($feature, $char_id, $value); +# $var create_new_char($feature, $value); +# $char_id decode_char($feature_char, $int); + +use chise; + +sub init_chise{ +#$chise_ds = chise::CHISE_DS_open($chise::CHISE_DS_BERKELEY_DB, $chise::chise_system_db_dir, 0, 0755); + $chise_ds = chise::CHISE_DS_open(0, $chise::chise_system_db_dir, 0, 0755); + chise::listup_feature($chise_ds); + @chise_feature = split(/\n/, chise::get_char($chise::feature)); + @chise_ft = (); + foreach(@chise_feature){ + push(@chise_ft, chise::chise_ds_get_feature($chise_ds, chise::get_uchar($_))); + } + $chise_newid = 0x0F0000; +} + +sub get_feature_value{ # feature, char_id -> 0 or value + if(scalar(@_) != 2){ + return 0; + } + my $ft = chise::chise_ds_get_feature($chise_ds, chise::get_uchar($_[0])); + chise::clear_buffer; + chise::chise_char_gets_feature_value($_[1], $ft, + $chise::buffer, $chise::buffer_size); + return chise::get_char($chise::buffer); +} + +sub set_feature_value{ # feature, char_id, value -> result + if(scalar(@_) != 3){ + return 0; + } + my $ft = chise::chise_ds_get_feature($chise_ds, chise::get_uchar($_[0])); + chise::chise_char_set_feature_value($_[1], $ft, chise::get_uchar($_[2])); + return 1; +} + +sub close_chise{ + chise::CHISE_DS_close($chise_ds); +} + +sub create_new_char{ # feature, value -> assinged char_id + &search_newid; + &set_feature_value($_[0], $chise_newid, $_[1]); + $chise_newid++; + return $chise_newid - 1; +} + +sub search_newid{ # search new char_id and update $chise_newid + while(1){ + my $found = 0; + foreach(@chise_ft){ + chise::clear_buffer; + chise::chise_char_gets_feature_value($chise_newid, $_, $chise::buffer, $chise::buffer_size); + if(length(chise::get_char($chise::buffer)) != 0){ + $found = 1; + last; + } + } + if($found == 0){ + last; + } + $chise_newid++; + } +} + +sub update_chise{ # save added data to database + foreach(@chise_ft){ + chise::chise_feature_sync($_); + } +} + +sub decode_char{ # feature, int -> charID, 0 + if(scalar(@_) != 2){ + return 0; + } + return chise::chise_ds_decode_char($chise_ds, chise::get_uchar($_[0]), $_[1]); +} + +1;