From: <ny...@us...> - 2006-12-27 17:07:29
|
Revision: 207 http://svn.sourceforge.net/pmplib/?rev=207&view=rev Author: nyaochi Date: 2006-12-27 09:07:29 -0800 (Wed, 27 Dec 2006) Log Message: ----------- Source code clean-up. The ip3db_t structure does not hold the entire memory block for db.dat any more. Modified Paths: -------------- trunk/pmplib/lib/pmp_iriverplus3/dat.c trunk/pmplib/lib/pmp_iriverplus3/dat.h trunk/pmplib/lib/pmp_iriverplus3/ip3db.c trunk/pmplib/lib/pmp_iriverplus3/ip3db.h Modified: trunk/pmplib/lib/pmp_iriverplus3/dat.c =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/dat.c 2006-12-27 16:38:53 UTC (rev 206) +++ trunk/pmplib/lib/pmp_iriverplus3/dat.c 2006-12-27 17:07:29 UTC (rev 207) @@ -247,7 +247,7 @@ free(dat); } -size_t dat_serialize(dat_t* dat, const dic_t* dic, uint8_t* buffer, int is_storing) +static size_t dat_serialize(dat_t* dat, const dic_t* dic, uint8_t* buffer, int is_storing) { if (is_storing) { int i; @@ -263,6 +263,48 @@ return 0; } +int dat_read(dat_t* dat, const dic_t* dic, FILE *fpi) +{ + long buffer_size = 0; + uint8_t* buffer = NULL; + + fread_all(fpi, &buffer, &buffer_size); + if (!buffer) { + return 1; + } + + if (dat_serialize(dat, dic, buffer, 0) != 0) { + free(buffer); + return 1; + } + + free(buffer); + return 0; +} + +int dat_write(dat_t* dat, const dic_t* dic, FILE *fpo) +{ + long buffer_size = 0x00040000; + uint8_t* buffer = (uint8_t*)calloc(buffer_size, sizeof(uint8_t)); + + if (!buffer) { + return 1; + } + + if (dat_serialize(dat, dic, buffer, 1) != 0) { + free(buffer); + return 1; + } + + if (fwrite(buffer, 1, buffer_size, fpo) != buffer_size) { + free(buffer); + return 1; + } + + free(buffer); + return 0; +} + void dat_dump(dat_t* dat, const dic_t* dic, FILE *fp) { fprintf(fp, "===== db.dat =====\n"); Modified: trunk/pmplib/lib/pmp_iriverplus3/dat.h =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/dat.h 2006-12-27 16:38:53 UTC (rev 206) +++ trunk/pmplib/lib/pmp_iriverplus3/dat.h 2006-12-27 17:07:29 UTC (rev 207) @@ -46,9 +46,9 @@ dat_t* dat_new(); void dat_finish(dat_t* dat); -size_t dat_serialize(dat_t* dat, const dic_t* dic, uint8_t* buffer, int is_storing); +int dat_read(dat_t* dat, const dic_t* dic, FILE *fpi); +int dat_write(dat_t* dat, const dic_t* dic, FILE *fpo); void dat_dump(dat_t* dat, const dic_t* dic, FILE *fp); - void dat_construct(dat_t* dat, dic_t* dic, const ip3db_music_record_t* records, int num_records); #endif/*__IP3DB_DAT_H__*/ Modified: trunk/pmplib/lib/pmp_iriverplus3/ip3db.c =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/ip3db.c 2006-12-27 16:38:53 UTC (rev 206) +++ trunk/pmplib/lib/pmp_iriverplus3/ip3db.c 2006-12-27 17:07:29 UTC (rev 207) @@ -95,11 +95,7 @@ break; case IP3DBVT_STRING: ucs2free(dst->value.str); - if (src->value.str) { - dst->value.str = ucs2dup(src->value.str); - } else { - dst->value.str = 0; - } + dst->value.str = src->value.str ? ucs2dup(src->value.str) : 0; break; } } @@ -108,15 +104,19 @@ { 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); - memcpy(db->dic_buffer, dic_template, db->dic_size); - dic_serialize(db->dic, db->dic_buffer, 0); + 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,9 +124,8 @@ dic_finish(db->dic); dat_finish(db->dat); idx_finish(db->idx); - free(db->dat_buffer); free(db->dic_buffer); - ip3db_init(db); + memset(db, 0, sizeof(*db)); } result_t ip3db_read(ip3db_t* db, const ucs2char_t* datfn, const ucs2char_t* dicfn, const ucs2char_t* idxfn) @@ -154,15 +153,9 @@ ip3db_finish(db); return 1; } - fread_all(fp, &db->dat_buffer, &db->dat_size); + dat_read(db->dat, db->dic, fp); fclose(fp); - /* Parse db.dat */ - if (dat_serialize(db->dat, db->dic, db->dat_buffer, 0) != 0) { - ip3db_finish(db); - return 1; - } - /* Read and parse db.idx */ fp = ucs2fopen(idxfn, "rb"); if (!fp) { @@ -179,21 +172,12 @@ { FILE *fp = 0; - free(db->dat_buffer); - - db->dat_size = 0x00040000; - db->dat_buffer = (uint8_t*)calloc(db->dat_size, sizeof(uint8_t)); - - if (dat_serialize(db->dat, db->dic, db->dat_buffer, 1) != 0) { - ip3db_finish(db); - return 1; - } - + /* Write db.dat. This will allocate offset values of the records. */ fp = ucs2fopen(datfn, "wb"); if (!fp) { return 1; } - fwrite(db->dat_buffer, 1, db->dat_size, fp); + dat_write(db->dat, db->dic, fp); fclose(fp); /* Construct db.idx and update db.dic. */ Modified: trunk/pmplib/lib/pmp_iriverplus3/ip3db.h =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/ip3db.h 2006-12-27 16:38:53 UTC (rev 206) +++ trunk/pmplib/lib/pmp_iriverplus3/ip3db.h 2006-12-27 17:07:29 UTC (rev 207) @@ -92,6 +92,9 @@ */ typedef ip3db_variant_t ip3db_music_record_t[IP3DBF_MUSIC_LAST]; +/** + * Fields in a object record. + */ enum { IP3DBF_OBJECTS_NONE = -1, IP3DBF_OBJECTS_BEGIN = 0, @@ -109,11 +112,17 @@ IP3DBF_OBJECTS_LAST, }; +/** + * Fields chunks in db.dat. + */ enum { IP3DBIDX_MUSIC = 0, IP3DBIDX_OBJECTS, }; +/** + * Indices for the music chunk. + */ enum { IP3DBIDX_MUSIC_NONE = -1, IP3DBIDX_MUSIC_BEGIN = 0, @@ -132,6 +141,9 @@ IP3DBIDX_MUSIC_LAST, }; +/** + * Indices for the object chunk. + */ enum { IP3DBIDX_OBJECT_NONE = -1, IP3DBIDX_OBJECT_BEGIN = 0, @@ -149,8 +161,6 @@ struct tag_idx_t; typedef struct tag_idx_t idx_t; typedef struct { - uint8_t* dat_buffer; - long dat_size; uint8_t* dic_buffer; long dic_size; @@ -172,9 +182,9 @@ void ip3db_finish(ip3db_t* db); result_t ip3db_read(ip3db_t* db, const ucs2char_t* datfn, const ucs2char_t* dicfn, const ucs2char_t* idxfn); result_t ip3db_write(ip3db_t* db, const ucs2char_t* datfn, const ucs2char_t* dicfn, const ucs2char_t* idxfn); +result_t ip3db_set(ip3db_t* db, const ip3db_music_record_t* records, int num_records); result_t ip3db_dump(ip3db_t* db, FILE *fpo); void ip3db_record_init(ip3db_t* db, ip3db_music_record_t* record); -result_t ip3db_set(ip3db_t* db, const ip3db_music_record_t* records, int num_records); #endif /*_IP3DB_IP3DB_H__*/ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |