From: Koichi KAMICHI Date: Thu, 11 Aug 2005 14:51:10 +0000 (+0000) Subject: Initial revision X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35f84ece29adecdebf8457a064bd8021adeba4e7;p=chise%2Fkage.git Initial revision --- diff --git a/tools/chise_tools.i b/tools/chise_tools.i new file mode 100755 index 0000000..5dd6401 --- /dev/null +++ b/tools/chise_tools.i @@ -0,0 +1,96 @@ +// 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 new file mode 100755 index 0000000..273faa7 --- /dev/null +++ b/tools/chiseperl.pl @@ -0,0 +1,83 @@ +# 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); + +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($_); + } +} + +1;