From: <ny...@us...> - 2006-12-27 17:24:51
|
Revision: 208 http://svn.sourceforge.net/pmplib/?rev=208&view=rev Author: nyaochi Date: 2006-12-27 09:24:52 -0800 (Wed, 27 Dec 2006) Log Message: ----------- Source code clean-up: make dic_t manage the buffer for db.dic. Modified Paths: -------------- trunk/pmplib/lib/pmp_iriverplus3/dic.c trunk/pmplib/lib/pmp_iriverplus3/dic.h trunk/pmplib/lib/pmp_iriverplus3/ip3db.c trunk/pmplib/lib/pmp_iriverplus3/ip3db.h Modified: trunk/pmplib/lib/pmp_iriverplus3/dic.c =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/dic.c 2006-12-27 17:07:29 UTC (rev 207) +++ trunk/pmplib/lib/pmp_iriverplus3/dic.c 2006-12-27 17:24:52 UTC (rev 208) @@ -64,6 +64,7 @@ {0, 0x0ACE, {IP3DBF_OBJECTS_FILETYPE, IP3DBF_OBJECTS_PARENTUID, IP3DBF_OBJECTS_PROPERTIES}}, }; +static int dic_serialize(dic_t* dic, uint8_t* buffer, int is_storing); static void dic_field_init(dic_field_t* entry) { @@ -129,6 +130,7 @@ dic_t* dic_new() { + uint8_t* dic_template = NULL; dic_t* dic = (dic_t*)malloc(sizeof(dic_t)); if (dic) { dic_table_init(&dic->music); @@ -141,6 +143,14 @@ dic->objects.fields = (dic_field_t*)calloc(dic->objects.num_fields, sizeof(dic_field_t)); dic->objects.num_indices = sizeof(objects_indices) / sizeof(objects_indices[0]); dic->objects.indices = objects_indices; + + /* Read the database description, which rules everything in a database. */ + dic_template = dic_get_template(&dic->size); + dic->buffer = (uint8_t*)malloc(dic->size); + if (dic->buffer) { + memcpy(dic->buffer, dic_template, dic->size); + dic_serialize(dic, dic->buffer, 0); + } } return dic; } @@ -150,12 +160,38 @@ if (dic) { dic_table_finish(&dic->music); dic_table_finish(&dic->objects); + free(dic->buffer); free(dic); } } -int dic_serialize(dic_t* dic, uint8_t* buffer, int is_storing) +int dic_read(dic_t* dic, FILE *fpi) { + free(dic->buffer); + dic->size = 0; + + fread_all(fpi, &dic->buffer, &dic->size); + + /* Parse db.dic */ + if (dic_serialize(dic, dic->buffer, 0) != 0) { + return 1; + } + return 0; +} + +int dic_write(dic_t* dic, FILE *fpo) +{ + if (dic_serialize(dic, dic->buffer, 1) != 0) { + return 1; + } + + fwrite(dic->buffer, 1, dic->size, fpo); + + return 0; +} + +static int dic_serialize(dic_t* dic, uint8_t* buffer, int is_storing) +{ int i; uint32_t next = 0; Modified: trunk/pmplib/lib/pmp_iriverplus3/dic.h =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/dic.h 2006-12-27 17:07:29 UTC (rev 207) +++ trunk/pmplib/lib/pmp_iriverplus3/dic.h 2006-12-27 17:24:52 UTC (rev 208) @@ -47,12 +47,16 @@ struct tag_dic_t { dic_table_t music; dic_table_t objects; + + uint8_t* buffer; + long size; }; typedef struct tag_dic_t dic_t; dic_t* dic_new(); void dic_finish(dic_t* dic); -int dic_serialize(dic_t* dic, uint8_t* buffer, int is_storing); +int dic_read(dic_t* dic, FILE *fpi); +int dic_write(dic_t* dic, FILE *fpo); void dic_dump(dic_t* dic, FILE *fp); uint32_t dic_get_idxroot(dic_t* dic, int table, int index); void dic_set_idxroot(dic_t* dic, int table, int index, uint32_t root); Modified: trunk/pmplib/lib/pmp_iriverplus3/ip3db.c =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/ip3db.c 2006-12-27 17:07:29 UTC (rev 207) +++ trunk/pmplib/lib/pmp_iriverplus3/ip3db.c 2006-12-27 17:24:52 UTC (rev 208) @@ -102,21 +102,11 @@ void ip3db_init(ip3db_t* db) { - uint8_t *dic_template = NULL; - /* Construct db->dat, db->dic, and db->idx */ memset(db, 0, sizeof(*db)); db->dat = dat_new(); db->dic = dic_new(); db->idx = idx_new(); - - /* Initialize db->dic, which rules everything in a database. */ - dic_template = dic_get_template(&db->dic_size); - db->dic_buffer = (uint8_t*)malloc(db->dic_size); - if (db->dic_buffer) { - memcpy(db->dic_buffer, dic_template, db->dic_size); - dic_serialize(db->dic, db->dic_buffer, 0); - } } void ip3db_finish(ip3db_t* db) @@ -124,7 +114,6 @@ dic_finish(db->dic); dat_finish(db->dat); idx_finish(db->idx); - free(db->dic_buffer); memset(db, 0, sizeof(*db)); } @@ -138,15 +127,9 @@ ip3db_finish(db); return 1; } - fread_all(fp, &db->dic_buffer, &db->dic_size); + dic_read(db->dic, fp); fclose(fp); - /* Parse db.dic */ - if (dic_serialize(db->dic, db->dic_buffer, 0) != 0) { - ip3db_finish(db); - return 1; - } - /* Read db.dat */ fp = ucs2fopen(datfn, "rb"); if (!fp) { @@ -156,7 +139,7 @@ dat_read(db->dat, db->dic, fp); fclose(fp); - /* Read and parse db.idx */ + /* Read db.idx */ fp = ucs2fopen(idxfn, "rb"); if (!fp) { ip3db_finish(db); @@ -190,16 +173,11 @@ idx_write(db->idx, db->dic, fp); fclose(fp); - if (dic_serialize(db->dic, db->dic_buffer, 1) != 0) { - ip3db_finish(db); - return 1; - } - fp = ucs2fopen(dicfn, "wb"); if (!fp) { return 1; } - fwrite(db->dic_buffer, 1, db->dic_size, fp); + dic_write(db->dic, fp); fclose(fp); return 0; Modified: trunk/pmplib/lib/pmp_iriverplus3/ip3db.h =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/ip3db.h 2006-12-27 17:07:29 UTC (rev 207) +++ trunk/pmplib/lib/pmp_iriverplus3/ip3db.h 2006-12-27 17:24:52 UTC (rev 208) @@ -161,13 +161,9 @@ struct tag_idx_t; typedef struct tag_idx_t idx_t; typedef struct { - uint8_t* dic_buffer; - long dic_size; - dat_t* dat; dic_t* dic; idx_t* idx; - } ip3db_t; void ip3db_variant_init(ip3db_variant_t* var, int type); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |