(char_table_open_db_maybe): Use `chise_ds_get_feature' instead of
[chise/xemacs-chise.git] / src / chartab.c
index e5663c1..0c6e168 100644 (file)
@@ -63,6 +63,10 @@ Lisp_Object Vword_combining_categories, Vword_separating_categories;
 #endif /* MULE */
 
 \f
+#ifdef CHISE
+CHISE_DS *default_chise_data_source = NULL;
+#endif
+
 #ifdef UTF2000
 
 EXFUN (Fchar_refs_simplify_char_specs, 1);
@@ -1272,7 +1276,9 @@ mark_char_table (Lisp_Object obj)
 
   mark_object (ct->table);
   mark_object (ct->name);
+#ifndef CHISE
   mark_object (ct->db);
+#endif
 #else
   int i;
 
@@ -1602,7 +1608,9 @@ static const struct lrecord_description char_table_description[] = {
   { XD_LISP_OBJECT, offsetof(Lisp_Char_Table, table) },
   { XD_LISP_OBJECT, offsetof(Lisp_Char_Table, default_value) },
   { XD_LISP_OBJECT, offsetof(Lisp_Char_Table, name) },
+#ifndef CHISE
   { XD_LISP_OBJECT, offsetof(Lisp_Char_Table, db) },
+#endif
 #else
   { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Char_Table, ascii), NUM_ASCII_CHARS },
 #ifdef MULE
@@ -1804,8 +1812,12 @@ and 'syntax.  See `valid-char-table-type-p'.
     ct->mirror_table = Qnil;
 #else
   ct->name = Qnil;
+#ifdef CHISE
+  ct->feature_table = NULL;
+#else
   ct->db = Qnil;
 #endif
+#endif
   ct->next_table = Qnil;
   XSETCHAR_TABLE (obj, ct);
   if (ty == CHAR_TABLE_TYPE_SYNTAX)
@@ -1879,7 +1891,11 @@ as CHAR-TABLE.  The values will not themselves be copied.
   ctnew->default_value = ct->default_value;
   /* [tomo:2002-01-21] Perhaps this code seems wrong */
   ctnew->name = ct->name;
+#ifdef CHISE
+  ctnew->feature_table = ct->feature_table;
+#else
   ctnew->db = ct->db;
+#endif
 
   if (UINT8_BYTE_TABLE_P (ct->table))
     {
@@ -2358,7 +2374,8 @@ For internal use.  Don't use it.
 */
        (c, value))
 {
-  put_char_id_table_0 (char_attribute_table_to_put, c, value_to_put);
+  put_char_id_table_0 (char_attribute_table_to_put,
+                      XCHAR (c), value_to_put);
   return Qnil;
 }
 #endif
@@ -2394,11 +2411,21 @@ put_char_table (Lisp_Char_Table *ct, struct chartab_range *range,
        */
        if ( CHAR_TABLEP (encoding_table) )
          {
+           Lisp_Object mother = XCHARSET_MOTHER (range->charset);
+
            char_attribute_table_to_put = ct;
            value_to_put = val;
            Fmap_char_attribute (Qput_char_table_map_function,
                                 XCHAR_TABLE_NAME (encoding_table),
                                 Qnil);
+           if ( CHARSETP (mother) )
+             {
+               struct chartab_range r;
+
+               r.type = CHARTAB_RANGE_CHARSET;
+               r.charset = mother;
+               put_char_table (ct, &r, val);
+             }
          }
 #if 0
        else
@@ -3315,6 +3342,131 @@ Remove CHARACTER's ATTRIBUTE.
 }
 
 #ifdef HAVE_CHISE_CLIENT
+
+int char_table_open_db_maybe (Lisp_Char_Table* cit);
+void char_table_close_db_maybe (Lisp_Char_Table* cit);
+Lisp_Object char_table_get_db (Lisp_Char_Table* cit, Emchar ch);
+
+int
+open_chise_data_source_maybe ()
+{
+  if (default_chise_data_source == NULL)
+    {
+      Lisp_Object db_dir = Vexec_directory;
+      int modemask = 0755;             /* rwxr-xr-x */
+
+      if (NILP (db_dir))
+       db_dir = build_string ("../lib-src");
+      db_dir = Fexpand_file_name (build_string ("char-db"), db_dir);
+
+      default_chise_data_source
+       = chise_open_data_source (CHISE_DS_Berkeley_DB,
+                                 XSTRING_DATA (db_dir),
+                                 DB_HASH, modemask);
+      if (default_chise_data_source == NULL)
+       return -1;
+    }
+  return 0;
+}
+
+DEFUN ("close-char-data-source", Fclose_char_data_source, 0, 0, 0, /*
+Close data-source of CHISE.
+*/
+       ())
+{
+#ifdef CHISE
+  int status = chise_ds_close (default_chise_data_source);
+
+  default_chise_data_source = NULL;
+  if (status)
+    return Qt;
+#endif
+  return Qnil;
+}
+
+int
+char_table_open_db_maybe (Lisp_Char_Table* cit)
+{
+  Lisp_Object attribute = CHAR_TABLE_NAME (cit);
+
+  if (!NILP (attribute))
+    {
+#ifdef CHISE
+      if (cit->feature_table == NULL)
+       {
+         if ( open_chise_data_source_maybe () )
+           return -1;
+
+         cit->feature_table
+           = chise_ds_get_feature (default_chise_data_source,
+                                   XSTRING_DATA (Fsymbol_name (attribute)));
+         if (cit->feature_table == NULL)
+           return -1;
+       }
+#else
+      if (NILP (Fdatabase_live_p (cit->db)))
+       {
+         Lisp_Object db_file
+           = char_attribute_system_db_file (Qsystem_char_id, attribute, 0);
+
+         cit->db = Fopen_database (db_file, Qnil, Qnil,
+                                   build_string ("r"), Qnil);
+         if (NILP (cit->db))
+           return -1;
+       }
+#endif
+      return 0;
+    }
+  else
+    return -1;
+}
+
+void
+char_table_close_db_maybe (Lisp_Char_Table* cit)
+{
+#ifdef CHISE
+  if (cit->feature_table != NULL)
+    {
+      /* chise_ft_close (cit->feature_table); */
+      cit->feature_table = NULL;
+    }
+#else
+  if (!NILP (cit->db))
+    {
+      if (!NILP (Fdatabase_live_p (cit->db)))
+       Fclose_database (cit->db);
+      cit->db = Qnil;
+    }
+#endif
+}
+
+Lisp_Object
+char_table_get_db (Lisp_Char_Table* cit, Emchar ch)
+{
+  Lisp_Object val;
+#ifdef CHISE
+  CHISE_Value value;
+  int status
+    = chise_char_load_feature_value (ch, cit->feature_table, &value);
+
+  if (!status)
+    {
+      val = Fread (make_string (chise_value_data (&value),
+                               chise_value_size (&value) ));
+    }
+  else
+    val = Qunbound;
+#else
+  val = Fget_database (Fprin1_to_string (make_char (ch), Qnil),
+                      cit->db, Qunbound);
+  if (!UNBOUNDP (val))
+    val = Fread (val);
+  else
+    val = Qunbound;
+#endif
+  return val;
+}
+
 Lisp_Object
 char_attribute_system_db_file (Lisp_Object key_type, Lisp_Object attribute,
                               int writing_mode)
@@ -3432,7 +3584,11 @@ Mount database file on char-attribute-table ATTRIBUTE.
       ct = XCHAR_TABLE (table);
       ct->table = Qunloaded;
       XCHAR_TABLE_UNLOADED(table) = 1;
+#ifdef CHISE
+      ct->feature_table = NULL;
+#else
       ct->db = Qnil;
+#endif
       return Qt;
     }
 #endif
@@ -3453,13 +3609,7 @@ Close database of ATTRIBUTE.
     ct = XCHAR_TABLE (table);
   else
     return Qnil;
-
-  if (!NILP (ct->db))
-    {
-      if (!NILP (Fdatabase_live_p (ct->db)))
-       Fclose_database (ct->db);
-      ct->db = Qnil;
-    }
+  char_table_close_db_maybe (ct);
 #endif
   return Qnil;
 }
@@ -3486,9 +3636,7 @@ Reset values of ATTRIBUTE with database file.
        }
       ct = XCHAR_TABLE (table);
       ct->table = Qunloaded;
-      if (!NILP (Fdatabase_live_p (ct->db)))
-       Fclose_database (ct->db);
-      ct->db = Qnil;
+      char_table_close_db_maybe (ct);
       XCHAR_TABLE_UNLOADED(table) = 1;
       return Qt;
     }
@@ -3503,36 +3651,43 @@ load_char_attribute_maybe (Lisp_Char_Table* cit, Emchar ch)
 
   if (!NILP (attribute))
     {
-      if (NILP (Fdatabase_live_p (cit->db)))
-       {
-         Lisp_Object db_file
-           = char_attribute_system_db_file (Qsystem_char_id, attribute, 0);
+      Lisp_Object val;
 
-         cit->db = Fopen_database (db_file, Qnil, Qnil,
-                                   build_string ("r"), Qnil);
-       }
-      if (!NILP (cit->db))
-       {
-         Lisp_Object val
-           = Fget_database (Fprin1_to_string (make_char (ch), Qnil),
-                            cit->db, Qunbound);
-         if (!UNBOUNDP (val))
-           val = Fread (val);
-         else
-           val = Qunbound;
-         if (!NILP (Vchar_db_stingy_mode))
-           {
-             Fclose_database (cit->db);
-             cit->db = Qnil;
-           }
-         return val;
-       }
+      if (char_table_open_db_maybe (cit))
+       return Qunbound;
+
+      val = char_table_get_db (cit, ch);
+
+      if (!NILP (Vchar_db_stingy_mode))
+       char_table_close_db_maybe (cit);
+
+      return val;
     }
   return Qunbound;
 }
 
 Lisp_Char_Table* char_attribute_table_to_load;
 
+#ifdef CHISE
+int
+load_char_attribute_table_map_func (CHISE_Char_ID cid,
+                                   CHISE_Feature feature,
+                                   CHISE_Value *value);
+int
+load_char_attribute_table_map_func (CHISE_Char_ID cid,
+                                   CHISE_Feature feature,
+                                   CHISE_Value *value)
+{
+  Emchar code = cid;
+  Lisp_Object ret = get_char_id_table_0 (char_attribute_table_to_load, code);
+
+  if (EQ (ret, Qunloaded))
+    put_char_id_table_0 (char_attribute_table_to_load, code,
+                        Fread (make_string ((Bufbyte *) value->data,
+                                            value->size)));
+  return 0;
+}
+#else
 Lisp_Object Qload_char_attribute_table_map_function;
 
 DEFUN ("load-char-attribute-table-map-function",
@@ -3549,6 +3704,7 @@ For internal use.  Don't use it.
     put_char_id_table_0 (char_attribute_table_to_load, code, Fread (value));
   return Qnil;
 }
+#endif
 
 DEFUN ("load-char-attribute-table", Fload_char_attribute_table, 1, 1, 0, /*
 Load values of ATTRIBUTE into database file.
@@ -3560,29 +3716,28 @@ Load values of ATTRIBUTE into database file.
                                Qunbound);
   if (CHAR_TABLEP (table))
     {
-      Lisp_Char_Table *ct = XCHAR_TABLE (table);
+      Lisp_Char_Table *cit = XCHAR_TABLE (table);
 
-      if (NILP (Fdatabase_live_p (ct->db)))
-       {
-         Lisp_Object db_file
-             = char_attribute_system_db_file (Qsystem_char_id, attribute, 0);
+      if (char_table_open_db_maybe (cit))
+       return Qnil;
 
-         ct->db = Fopen_database (db_file, Qnil, Qnil,
-                                  build_string ("r"), Qnil);
-       }
-      if (!NILP (ct->db))
-       {
-         struct gcpro gcpro1;
-
-         char_attribute_table_to_load = XCHAR_TABLE (table);
-         GCPRO1 (table);
-         Fmap_database (Qload_char_attribute_table_map_function, ct->db);
-         UNGCPRO;
-         Fclose_database (ct->db);
-         ct->db = Qnil;
-         XCHAR_TABLE_UNLOADED(table) = 0;
-         return Qt;
-       }
+      char_attribute_table_to_load = XCHAR_TABLE (table);
+      {
+       struct gcpro gcpro1;
+
+       GCPRO1 (table);
+#ifdef CHISE
+       chise_char_feature_value_iterate
+         (cit->feature_table,
+          &load_char_attribute_table_map_func);
+#else
+       Fmap_database (Qload_char_attribute_table_map_function, cit->db);
+#endif
+       UNGCPRO;
+      }
+      char_table_close_db_maybe (cit);
+      XCHAR_TABLE_UNLOADED(table) = 0;
+      return Qt;
     }
   return Qnil;
 }
@@ -4039,16 +4194,20 @@ word_boundary_p (Emchar c1, Emchar c2)
     c2 = cmpchar_component (c2, 0, 1);
 #endif
 
+#ifndef UTF2000
   if (EQ (CHAR_CHARSET (c1), CHAR_CHARSET (c2)))
+#endif
     {
       tail = Vword_separating_categories;
       default_result = 0;
     }
+#ifndef UTF2000
   else
     {
       tail = Vword_combining_categories;
       default_result = 1;
     }
+#endif
 
   category_set1 = CATEGORY_SET (c1);
   if (NILP (category_set1))
@@ -4113,9 +4272,12 @@ syms_of_chartab (void)
   DEFSUBR (Fmount_char_attribute_table);
   DEFSUBR (Freset_char_attribute_table);
   DEFSUBR (Fclose_char_attribute_table);
+  DEFSUBR (Fclose_char_data_source);
+#ifndef CHISE
   defsymbol (&Qload_char_attribute_table_map_function,
             "load-char-attribute-table-map-function");
   DEFSUBR (Fload_char_attribute_table_map_function);
+#endif
   DEFSUBR (Fload_char_attribute_table);
 #endif
   DEFSUBR (Fchar_attribute_alist);