Initial revision
authorKoichi KAMICHI <kamichi@fonts.jp>
Thu, 11 Aug 2005 14:51:10 +0000 (14:51 +0000)
committerKoichi KAMICHI <kamichi@fonts.jp>
Thu, 11 Aug 2005 14:51:10 +0000 (14:51 +0000)
tools/chise_tools.i [new file with mode: 0755]
tools/chiseperl.pl [new file with mode: 0755]

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