*** empty log message ***
[m17n/m17n-lib.git] / src / character.c
index 4769c61..a9b3569 100644 (file)
@@ -95,22 +95,13 @@ free_string (int from, int to, void *str, void *arg)
 int
 mchar__init ()
 {
-  char_prop_list = mplist ();
-
-  Mname
-    = mchar_define_property ("name", Mstring);
-  Mcategory
-    = mchar_define_property ("category", Msymbol);
-  Mcombining_class
-    = mchar_define_property ("combining-class", Minteger);
-  Mbidi_category
-    = mchar_define_property ("bidirectional-category", Msymbol);
-  Msimple_case_folding
-    = mchar_define_property ("simple-case-folding", Minteger);
-  Mcomplicated_case_folding
-    = mchar_define_property ("complicated-case-folding", Mtext);
-  Mscript
-    = mchar_define_property ("script", Msymbol);
+  Mname = msymbol ("name");
+  Mcategory = msymbol ("category");
+  Mcombining_class = msymbol ("combining-class");
+  Mbidi_category = msymbol ("bidirectional-category");
+  Msimple_case_folding = msymbol ("simple-case-folding");
+  Mcomplicated_case_folding = msymbol ("complicated-case-folding");
+  Mscript = msymbol ("script");
 
   return 0;
 }
@@ -120,28 +111,68 @@ mchar__fini (void)
 {
   MPlist *p;
 
-  for (p = char_prop_list; mplist_key (p) != Mnil; p = mplist_next (p))
+  if (char_prop_list)
     {
-      MCharPropRecord *record = mplist_value (p);
-
-      if (record->table)
+      for (p = char_prop_list; mplist_key (p) != Mnil; p = mplist_next (p))
        {
-         if (record->type == Mstring)
-           mchartable_map (record->table, NULL, free_string, NULL);
-         M17N_OBJECT_UNREF (record->table);
+         MCharPropRecord *record = mplist_value (p);
+
+         if (record->table)
+           {
+             if (record->type == Mstring)
+               mchartable_map (record->table, NULL, free_string, NULL);
+             M17N_OBJECT_UNREF (record->table);
+           }
+         free (record);
        }
-      free (record);
+      M17N_OBJECT_UNREF (char_prop_list);
     }
-  M17N_OBJECT_UNREF (char_prop_list);
 }
 
+void
+mchar__define_prop (MSymbol key, MSymbol type, void *mdb)
+{
+  MCharPropRecord *record;
+
+  if (char_prop_list)
+    record = mplist_get (char_prop_list, key);
+  else
+    char_prop_list = mplist (), record = NULL;
+  if (record)
+    {
+      if (record->table)
+       M17N_OBJECT_UNREF (record->table);
+    }
+  else
+    {
+      MSTRUCT_CALLOC (record, MERROR_CHAR);
+      mplist_put (char_prop_list, key, record);
+    }
+
+  record->type = type;
+  record->mdb = mdb;
+  if (mdb)
+    {
+      record->table = NULL;
+    }
+  else
+    {
+      void *default_value = NULL;
+
+      if (type == Minteger)
+       default_value = (void *) -1;
+      record->table = mchartable (type, default_value);
+    }
+}
+
+
 /*** @} */
 #endif /* !FOR_DOXYGEN || DOXYGEN_INTERNAL_MODULE */
 \f
 /* External API */
 
-/*** @addtogroup m17nCharacter */
-/*** @{ */
+/*** @addtogroup m17nCharacter
+     @{ */
 /*=*/
 
 #ifdef FOR_DOXYGEN
@@ -160,16 +191,15 @@ mchar__fini (void)
 #endif /* FOR_DOXYGEN */
 
 /***en
-    @ingroup m17nCharacter
     @name Variables: Keys of character properties
 
     These symbols are used as keys of character properties.  */
 
 /***ja
-     @name ÊÑ¿ô: Ê¸»ú¥×¥í¥Ñ¥Æ¥£¤Î¥­¡¼
+    @ingroup m17nCharacter
+    @name ÊÑ¿ô: Ê¸»ú¥×¥í¥Ñ¥Æ¥£¤Î¥­¡¼
 
-     ¤³¤ì¤é¤Î¥·¥ó¥Ü¥ë¤Ïʸ»ú¥×¥í¥Ñ¥Æ¥£¤Î¥­¡¼¤È¤·¤Æ»È¤ï¤ì¤ë¡£*/
-/*=*/
+    ¤³¤ì¤é¤Î¥·¥ó¥Ü¥ë¤Ïʸ»ú¥×¥í¥Ñ¥Æ¥£¤Î¥­¡¼¤È¤·¤Æ»È¤ï¤ì¤ë¡£*/
 /*** @{ */
 
 /***en
@@ -375,36 +405,13 @@ MSymbol
 mchar_define_property (const char *name, MSymbol type)
 {
   MSymbol key = msymbol (name);
-  MCharPropRecord *record;
-
-  record = mplist_get (char_prop_list, key);
-  if (record)
-    {
-      if (record->table)
-       M17N_OBJECT_UNREF (record->table);
-    }
-  else
-    {
-      MSTRUCT_CALLOC (record, MERROR_CHAR);
-      mplist_put (char_prop_list, key, record);
-    }
+  void *mdb;
 
-  record->type = type;
-  if (mdatabase__finder
-      && (record->mdb = (*mdatabase__finder) (Mchar_table, type, key, Mnil)))
-    {
-      record->table = NULL;
-    }
+  if (mdatabase__finder)
+    mdb = (*mdatabase__finder) (Mchar_table, type, key, Mnil);
   else
-    {
-      void *default_value = NULL;
-
-      record->mdb = NULL;
-      if (type == Minteger)
-       default_value = (void *) -1;
-      record->table = mchartable (type, default_value);
-    }
-
+    mdb = NULL;
+  mchar__define_prop (key, type, mdb);
   return key;
 }
 
@@ -445,6 +452,8 @@ mchar_get_prop (int c, MSymbol key)
 {
   MCharPropRecord *record;
 
+  if (! char_prop_list)
+    return NULL;
   record = mplist_get (char_prop_list, key);
   if (! record)
     return NULL;
@@ -490,6 +499,8 @@ mchar_put_prop (int c, MSymbol key, void *val)
 {
   MCharPropRecord *record;
 
+  if (! char_prop_list)
+    MERROR (MERROR_CHAR, -1);
   record = mplist_get (char_prop_list, key);
   if (! record)
     return -1;
@@ -503,6 +514,55 @@ mchar_put_prop (int c, MSymbol key, void *val)
   return mchartable_set (record->table, c, val);
 }
 
+/*=*/
+
+/***en
+    @brief Get the char-table for a character property.
+
+    The mchar_get_prop_table () function returns a char-table that
+    contains the character property whose key is $KEY.  If $TYPE is
+    not NULL, this function stores the type of the property in the
+    place pointed by $TYPE.  See mchar_define_property () for types of
+    character property.
+
+    @return
+    If $KEY is a valid character property key, this function returns a
+    char-table.  Otherwise NULL is retuned.  */
+
+/***ja
+    @brief Ê¸»ú¥×¥í¥Ñ¥Æ¥£¤Îʸ»ú¥Æ¡¼¥Ö¥ë¤òÆÀ¤ë.
+
+    ´Ø¿ô mchar_get_prop_table () ¤Ï¡¢¥­¡¼¤¬ $KEY ¤Ç¤¢¤ëʸ»ú¥×¥í¥Ñ¥Æ¥£
+    ¤ò´Þ¤àʸ»ú¥Æ¡¼¥Ö¥ë¤òÊÖ¤¹¡£¤â¤· $TYPE ¤¬ NULL ¤Ç¤Ê¤±¤ì¤Ð¡¢ $TYPE ¤Ç
+    »Ø¤µ¤ì¤ë¾ì½ê¤Ë¤½¤Îʸ»ú¤Î¥×¥í¥Ñ¥Æ¥£¤ò³ÊǼ¤¹¤ë¡£Ê¸»ú¥×¥í¥Ñ¥Æ¥£¤Î¼ïÎà
+    ¤Ë´Ø¤·¤Æ¤Ï mchar_define_property () ¤ò¸«¤è¡£
+
+    @return
+    ¤â¤· $KEY ¤¬ÀµÅö¤Êʸ»ú¥×¥í¥Ñ¥Æ¥£¤Î¥­¡¼¤Ç¤¢¤ì¤Ð¡¢Ê¸»ú¥Æ¡¼¥Ö¥ë¤¬ÊÖ¤µ
+    ¤ì¤ë¡£¤½¤¦¤Ç¤Ê¤¤¾ì¹ç¤Ï NULL ¤¬ÊÖ¤µ¤ì¤ë¡£  */
+
+MCharTable *
+mchar_get_prop_table (MSymbol key, MSymbol *type)
+{
+  MCharPropRecord *record;
+
+  if (! char_prop_list)
+    return NULL;
+  record = mplist_get (char_prop_list, key);
+  if (! record)
+    return NULL;
+  if (record->mdb)
+    {
+      record->table = (*mdatabase__loader) (record->mdb);
+      if (! record->table)
+       MERROR (MERROR_DB, NULL);
+      record->mdb = NULL;
+    }
+  if (type)
+    *type = record->type;
+  return record->table;
+}
+
 /*** @} */
 
 /*