(struct CHISE_DS): Add new member `property_names'.
authortomo <tomo>
Sat, 17 Jul 2004 02:08:52 +0000 (02:08 +0000)
committertomo <tomo>
Sat, 17 Jul 2004 02:08:52 +0000 (02:08 +0000)
(CHISE_DS_open): Initialize `property_names'.
(CHISE_DS_close): Destroy `property_names'.
(chise_ds_location): New function.
(chise_ds_get_property): New function.
(struct CHISE_Property_Table): New structure.
(chise_ds_open_property_table): New function.
(chise_pt_close): Likewise.
(chise_property_setup_db): Likewise.
(chise_property_sync): Likewise.
(chise_feature_set_property_value): Likewise.
(chise_feature_load_property_value): Likewise.
(chise_feature_gets_property_value): Likewise.

chise.c

diff --git a/chise.c b/chise.c
index a3048bd..53e4293 100644 (file)
--- a/chise.c
+++ b/chise.c
@@ -43,6 +43,12 @@ chise_ds_open_ccs_table (CHISE_DS *ds, const char *ccs);
 int chise_ccst_close (CHISE_CCS_Table *table);
 
 
+CHISE_Property_Table*
+chise_ds_open_property_table (CHISE_DS *ds, const char *property);
+
+int chise_pt_close (CHISE_Property_Table *table);
+
+
 typedef DB CHISE_Attribute_Table;
 
 CHISE_Attribute_Table*
@@ -76,6 +82,7 @@ struct CHISE_DS
   unsigned char *location;
   CHISE_NAME_TABLE* feature_names;
   CHISE_NAME_TABLE* ccs_names;
+  CHISE_NAME_TABLE* property_names;
   DBTYPE subtype;
   int modemask;
 };
@@ -115,6 +122,15 @@ CHISE_DS_open (CHISE_DS_Type type, const unsigned char *location,
       free (ds->location);
       free (ds);
     }
+
+  ds->property_names = chise_make_name_table ();
+  if (ds->property_names == NULL)
+    {
+      free (ds->ccs_names);
+      free (ds->feature_names);
+      free (ds->location);
+      free (ds);
+    }
   return ds;
 }
 
@@ -127,10 +143,18 @@ CHISE_DS_close (CHISE_DS *ds)
     chise_destroy_name_table (ds->feature_names);
   if (ds->ccs_names != NULL)
     chise_destroy_name_table (ds->ccs_names);
+  if (ds->property_names != NULL)
+    chise_destroy_name_table (ds->property_names);
   free (ds);
   return 0;
 }
 
+unsigned char*
+chise_ds_location (CHISE_DS *ds)
+{
+  return ds->location;
+}
+
 CHISE_Feature_Table*
 chise_ds_get_feature (CHISE_DS *ds, const unsigned char *feature)
 {
@@ -173,6 +197,27 @@ chise_ds_get_ccs (CHISE_DS *ds, const unsigned char *ccs)
   return ct;
 }
 
+CHISE_Property_Table*
+chise_ds_get_property (CHISE_DS *ds, const unsigned char *property)
+{
+  CHISE_Property_Table* pt;
+
+  pt = chise_name_table_get (ds->property_names, property);
+  if (pt != NULL)
+    return pt;
+
+  pt = chise_ds_open_property_table (ds, property);
+  if (pt == NULL)
+    return NULL;
+
+  if (chise_name_table_put (ds->property_names, property, pt))
+    {
+      chise_pt_close (pt);
+      return NULL;
+    }
+  return pt;
+}
+
 int
 chise_ds_foreach_char_feature_name (CHISE_DS *ds,
                                    int (*func) (CHISE_DS *ds,
@@ -617,6 +662,161 @@ chise_ccs_set_decoded_char (CHISE_CCS ccs,
 }
 
 
+struct CHISE_Property_Table
+{
+  CHISE_DS *ds;
+  unsigned char *name;
+  CHISE_Attribute_Table *db;
+  u_int32_t access;
+};
+
+CHISE_Property_Table*
+chise_ds_open_property_table (CHISE_DS *ds, const char *property)
+{
+  CHISE_Property_Table* table;
+  size_t len = strlen (property);
+
+  if (ds == NULL)
+    return NULL;
+
+  table = (CHISE_Property_Table*)malloc (sizeof (CHISE_Property_Table));
+  if (table == NULL)
+    return NULL;
+
+  table->ds = ds;
+  table->db = NULL;
+  table->access = 0;
+  table->name = (unsigned char*)malloc (len + 1);
+  if (table->name == NULL)
+    {
+      free (table);
+      return NULL;
+    }
+  strcpy (table->name, property);
+  return table;
+}
+
+int
+chise_pt_close (CHISE_Property_Table *table)
+{
+  int status;
+
+  if (table == NULL)
+    return -1;
+
+  if (table->db == NULL)
+    status = -1;
+  else
+    status = CHISE_Attribute_Table_close (table->db);
+
+  if (table->name == NULL)
+    status = -1;
+  else
+    {
+      free (table->name);
+      status = 0;
+    }
+  free (table);
+  return status;
+}
+
+int
+chise_property_setup_db (CHISE_Property property, int writable)
+{
+  u_int32_t access;
+
+  if (property == NULL)
+    return -1;
+
+  if (writable)
+    {
+      if ((property->access & DB_CREATE) == 0)
+       {
+         if (property->db != NULL)
+           {
+             CHISE_Attribute_Table_close (property->db);
+             property->db = NULL;
+           }
+         property->access = 0;
+       }
+      access = DB_CREATE;
+    }
+  else
+    access = DB_RDONLY;
+
+  if (property->db == NULL)
+    {
+      CHISE_DS *ds = property->ds;
+
+      property->db
+       = CHISE_Attribute_Table_open (ds->location, "feature",
+                                     "property", property->name,
+                                     ds->subtype, access, ds->modemask);
+      if (property->db == NULL)
+       return -1;
+      property->access = access;
+    }
+  return 0;
+}
+
+int
+chise_property_sync (CHISE_Property property)
+{
+  int status;
+
+  if (property->db == NULL)
+    status = 0;
+  else
+    status = CHISE_Attribute_Table_close (property->db);
+  property->db = NULL;
+  property->access = 0;
+  return status;
+}
+
+int
+chise_feature_set_property_value (CHISE_Feature feature,
+                                 CHISE_Property property,
+                                 unsigned char *value)
+{
+  if (property == NULL)
+    return -1;
+  if (chise_property_setup_db (property, 1))
+    return -1;
+  return chise_attribute_table_put (property->db, feature->name, value);
+}
+
+int
+chise_feature_load_property_value (CHISE_Feature feature,
+                                  CHISE_Property_Table *table,
+                                  CHISE_Value *valdatum)
+{
+  if (chise_property_setup_db (table, 0))
+    return -1;
+  return chise_attribute_table_get (table->db, feature->name, valdatum);
+}
+
+unsigned char*
+chise_feature_gets_property_value (CHISE_Feature feature,
+                                  CHISE_Property_Table *table,
+                                  unsigned char *buf, size_t size)
+{
+  CHISE_Value valdatum;
+  int status;
+
+  if (chise_property_setup_db (table, 0))
+    return NULL;
+  status = chise_attribute_table_get (table->db,
+                                     feature->name, &valdatum);
+  if (status)
+    return NULL;
+  if (size < valdatum.size)
+    return NULL;
+  strncpy (buf, valdatum.data, valdatum.size);
+  buf[valdatum.size] = '\0';
+  return buf;
+}
+
+
 CHISE_Attribute_Table*
 CHISE_Attribute_Table_open (const unsigned char *db_dir,
                            const char *category,