From: <ny...@us...> - 2006-12-27 08:08:29
|
Revision: 202 http://svn.sourceforge.net/pmplib/?rev=202&view=rev Author: nyaochi Date: 2006-12-27 00:08:26 -0800 (Wed, 27 Dec 2006) Log Message: ----------- Implemented AVL tree for db.idx. Sophisticated dump routine for db.idx by using the AVL tree library. We are now close to generate db.idx Modified Paths: -------------- trunk/pmplib/include/os.h trunk/pmplib/lib/pmp_iriverplus3/idx.c trunk/pmplib/lib/pmp_iriverplus3/idx.h trunk/pmplib/lib/pmp_iriverplus3/ip3db.c trunk/pmplib/lib/pmp_iriverplus3/ip3db.h Modified: trunk/pmplib/include/os.h =================================================================== --- trunk/pmplib/include/os.h 2006-12-25 15:15:36 UTC (rev 201) +++ trunk/pmplib/include/os.h 2006-12-27 08:08:26 UTC (rev 202) @@ -29,6 +29,7 @@ #if defined(WIN32) || defined(OS2) typedef unsigned char uint8_t; typedef unsigned int uint32_t; +typedef int int32_t; typedef unsigned short uint16_t; #elif defined(bsdi) || defined(FREEBSD) || defined(OPENBSD) Modified: trunk/pmplib/lib/pmp_iriverplus3/idx.c =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/idx.c 2006-12-25 15:15:36 UTC (rev 201) +++ trunk/pmplib/lib/pmp_iriverplus3/idx.c 2006-12-27 08:08:26 UTC (rev 202) @@ -1,5 +1,5 @@ /* - * Low-level library for db.idx. + * AVL tree implementation for db.idx. * * Copyright (c) 2006 Nyaochi * @@ -42,138 +42,366 @@ #include "util.h" #include "ip3db.h" #include "dic.h" +#include "dat.h" +#include "idx.h" +#define COMP(a, b) ((a)>(b))-((a)<(b)) + typedef struct { uint32_t size; uint32_t unknown1; uint32_t unknown2; -} header_t; +} idx_header_t; -typedef struct { - uint32_t left; - uint32_t right; - uint32_t height; - uint32_t leaf; -} node_t; +struct tag_avlnode_t { + uint32_t left; + uint32_t right; + int32_t balance; + uint32_t tail; +}; +typedef struct tag_avlnode_t avlnode_t; -typedef struct { - uint32_t dat_offset; - uint32_t next; -} tail_t; +struct tag_avltail_t { + uint32_t data; + uint32_t next; +}; +typedef struct tag_avltail_t avltail_t; -static size_t idx_serialize_header(uint8_t *block, header_t *header, int is_storing) +struct tag_avl_t { + uint8_t* buffer; + uint32_t size; + uint32_t offset; +}; +typedef struct tag_avl_t avl_t; + +typedef int (*avl_comp_t)(avl_t* avl, uint32_t x, uint32_t y); + +static uint32_t avl_current(avl_t* avl) { - uint8_t *p = block; - p += serialize_uint32be(p, &header->size, is_storing); - p += serialize_uint32be(p, &header->unknown1, is_storing); - p += serialize_uint32be(p, &header->unknown2, is_storing); - return sizeof(header_t); + return avl->offset; } -static size_t idx_serialize_node(uint8_t *block, node_t *node, int is_storing) +static uint32_t avl_allocate(avl_t* avl, uint32_t size) { - uint8_t *p = block; - p += serialize_uint32be(p, &node->left, 0); - p += serialize_uint32be(p, &node->right, 0); - p += serialize_uint32be(p, &node->height, 0); - p += serialize_uint32be(p, &node->leaf, 0); - return sizeof(node_t); + uint32_t offset = avl->offset; + avl->offset += size; + return offset; } -static size_t idx_serialize_tail(uint8_t *block, tail_t* tail, int is_storing) +static avlnode_t *avlnode(avl_t* avl, uint32_t offset) { - uint8_t *p = block; - p += serialize_uint32be(p, &tail->dat_offset, is_storing); - p += serialize_uint32be(p, &tail->next, is_storing); - return sizeof(tail_t); + return (avlnode_t*)(avl->buffer + offset); } -/** - * Prototype definition of a callback function for idx_walk. - * @param buffer The pointer to the index buffer. - * @param offset The offset address of the current node. - * @param node The information of the node. - * @param key The key value of the node. - * @param types The key types. - * @param level The current key level. - * @param flag - * @param instance The instance value. - */ -typedef int (*idx_walk_callback_t)( - uint8_t* buffer, - uint32_t offset, - node_t* node, - ip3db_variant_t* key, - dic_table_t* dic_table, - int index, - int level, - int flag, - void *instance - ); +static void avl_rewind(avl_t* avl, uint32_t offset) +{ + memset(avlnode(avl, offset), 0, avl->offset - offset); + avl->offset = offset; +} -void idx_walk( - uint8_t *buffer, - uint32_t offset, - dic_table_t* dic_table, - int index, - int level, - idx_walk_callback_t callback, - void *instance - ) +static uint32_t avlnode_new(avl_t* avl) { - node_t node; - ip3db_variant_t key; + uint32_t offset = avl_allocate(avl, sizeof(avlnode_t)); + avlnode_t* a = avlnode(avl, offset); + a->left = 0; + a->right = 0; + a->balance = 0; + a->tail = 0; + return offset; +} + +static uint32_t avltail_new(avl_t* avl) +{ + uint32_t offset = avl_allocate(avl, sizeof(avltail_t)); + avltail_t* a = (avltail_t*)avlnode(avl, offset); + a->data = 0; + a->next = 0; + return offset; +} + +static void avl_swl(avl_t* avl, uint32_t *offr) +{ + uint32_t offa = *offr; + avlnode_t* a = avlnode(avl, offa); + uint32_t offb = a->right; + avlnode_t* b = avlnode(avl, offb); + *offr = offb; + a->right = b->left; + b->left = offa; +} + +static void avl_swr(avl_t* avl, uint32_t *offr) +{ + uint32_t offa = *offr; + avlnode_t* a = avlnode(avl, offa); + uint32_t offb = a->left; + avlnode_t* b = avlnode(avl, offb); + *offr = offb; + a->left = b->right; + b->right = offa; +} + +static void avl_nasty(avl_t* avl, uint32_t offr) +{ + avlnode_t* root = avlnode(avl, offr); + avlnode_t* left = avlnode(avl, root->left); + avlnode_t* right = avlnode(avl, root->right); + switch (root->balance) { + case -1: + left->balance = 0; + right->balance = 1; + break; + case 0: + left->balance = 0; + right->balance = 0; + break; + case 1: + left->balance = -1; + right->balance = 0; + break; + } + root->balance = 0; +} + +static int avl_insert(avl_t* avl, uint32_t* offr, uint32_t* offa, avl_comp_t comp) +{ + avlnode_t* root = NULL; + avlnode_t* left = NULL; + avlnode_t* right = NULL; + + if (!*offr) { + /* An empty tree: set the current node as the root node. */ + *offr = *offa; + return 1; + } else { + uint32_t x = *offr + sizeof(avlnode_t); + uint32_t y = *offa + sizeof(avlnode_t); + int cmp = comp(avl, x, y); + if (cmp > 0) { + /* Insert the node to the left sub-tree. */ + root = avlnode(avl, *offr); + if (root->left) { + uint32_t off_left = root->left; + int ret = avl_insert(avl, &off_left, offa, comp); + if (ret > 0) { + switch (root->balance--) { + case 1: return 0; + case 0: return 1; + } + left = avlnode(avl, root->left); + if (left->balance < 0) { + avl_swr(avl, offr); + root = avlnode(avl, *offr); + right = avlnode(avl, root->right); + root->balance = 0; + right->balance = 0; + } else { + avl_swl(avl, &root->left); + avl_swr(avl, offr); + avl_nasty(avl, *offr); + } + } else if (ret == 0) { + root->left = off_left; + } else { + return -1; + } + return 0; + } else { + root->left = *offa; + if (root->balance--) return 0; + return 1; + } + } else if (cmp < 0) { + /* Insert the node to the right sub-tree. */ + root = avlnode(avl, *offr); + if (root->right) { + uint32_t off_right = root->right; + int ret = avl_insert(avl, &off_right, offa, comp); + if (ret > 0) { + switch (root->balance++) { + case -1: return 0; + case 0: return 1; + } + right = avlnode(avl, root->right); + if (right->balance > 0) { + avl_swl(avl, offr); + root = avlnode(avl, *offr); + left = avlnode(avl, root->left); + root->balance = 0; + left->balance = 0; + } else { + avl_swr(avl, &root->right); + avl_swl(avl, offr); + avl_nasty(avl, *offr); + } + } else if (ret == 0) { + root->right = off_right; + } else { + return -1; + } + return 0; + } else { + root->right = *offa; + if (root->balance++) return 0; + return 1; + } + } else { + *offa = *offr; + return -1; + } + } +} + +static int avl_comp_byte(avl_t* avl, uint32_t _x, uint32_t _y) +{ + uint8_t* x = avl->buffer + _x; + uint8_t* y = avl->buffer + _y; + return COMP(*x, *y); +} + +static int avl_comp_word(avl_t* avl, uint32_t _x, uint32_t _y) +{ + uint16_t* x = (uint16_t*)(avl->buffer + _x); + uint16_t* y = (uint16_t*)(avl->buffer + _y); + return COMP(*x, *y); +} + +static int avl_comp_dword(avl_t* avl, uint32_t _x, uint32_t _y) +{ + uint32_t* x = (uint32_t*)(avl->buffer + _x); + uint32_t* y = (uint32_t*)(avl->buffer + _y); + return COMP(*x, *y); +} + +static int avl_comp_ucs2string(avl_t* avl, uint32_t _x, uint32_t _y) +{ + ucs2char_t* x = (ucs2char_t*)(avl->buffer + _x); + ucs2char_t* y = (ucs2char_t*)(avl->buffer + _y); + while (*x && *y && *x == *y) { + x++; + y++; + } + return COMP(*x, *y); +} + +static int avl_insert_key(avl_t* avl, const void* key, size_t size, int type, uint32_t* offset, uint32_t* root) +{ + int ret = 0; + uint32_t offset_prev = avl_current(avl); + uint32_t offset_this = avlnode_new(avl); + uint32_t offset_key = avl_allocate(avl, (uint32_t)size); + void *pkey = avlnode(avl, offset_key); + static avl_comp_t comp[] = {NULL, avl_comp_byte, avl_comp_word, avl_comp_dword, avl_comp_ucs2string}; + + memcpy(pkey, key, size); + ret = avl_insert(avl, root, &offset_this, comp[type]); + if (ret != -1) { + /* A new key. */ + *offset = offset_this; + return 0; + } else { + /* The key exists in the AVL tree. */ + avl_rewind(avl, offset_prev); + *offset = offset_this; + return 1; + } +} + +static int avl_insert_keydata(avl_t* avl, const void* key, size_t size, int type, uint32_t data, uint32_t* root) +{ + uint32_t offset = 0; + int ret = avl_insert_key(avl, key, size, type, &offset, root); + avlnode_t* node = avlnode(avl, offset); + uint32_t offset_tail = avltail_new(avl); + avltail_t* tail = (avltail_t*)avlnode(avl, offset_tail); + tail->data = data; + tail->next = node->tail; + node->tail = offset_tail; + return ret; +} + +static void from_uint16be(uint16_t* value) +{ + uint8_t* src = (uint8_t*)value; + uint16_t tmp = (uint16_t)src[0] << 8 | (uint16_t)src[1]; + *value = tmp; +} + +static void from_uint32be(uint32_t* value) +{ + uint8_t* src = (uint8_t*)value; + uint32_t tmp = (uint32_t)src[0] << 24 | (uint32_t)src[1] << 16 | (uint32_t)src[2] << 8 | (uint32_t)src[3]; + *value = tmp; +} + +static void from_ucs2be_string(ucs2char_t* value) +{ + while (*value) { + from_uint16be((uint16_t*)value); + value++; + } +} + +static void from_be_avlnode(avlnode_t* node) +{ + from_uint32be(&node->left); + from_uint32be(&node->right); + from_uint32be(&node->balance); + from_uint32be(&node->tail); +} + +static void from_be_avltree(avl_t* avl, uint32_t offset, dic_table_t* dic_table, int index, int level) +{ + avlnode_t* node = avlnode(avl, offset); int field = dic_table->indices[index].fields[level]; int type = dic_table->fields[field].type; - uint8_t* p = buffer + offset; - /* Read the node information. */ - p += idx_serialize_node(p, &node, 0); + /* Convert the current node. */ + from_be_avlnode(node); - /* Descend to left children. */ - if (node.left) { - idx_walk(buffer, node.left, dic_table, index, level, callback, instance); + /* Descend to the left node. */ + if (node->left) { + from_be_avltree(avl, node->left, dic_table, index, level); } - /* Read the key value. */ - ip3db_variant_init(&key, type); + /* Convert the key value. */ switch (type) { case IP3DBVT_BYTE: - p += serialize_uint8(p, &key.value.byte, 0); break; case IP3DBVT_WORD: - p += serialize_uint16be(p, &key.value.word, 0); + from_uint16be((uint16_t*)(node+1)); break; case IP3DBVT_DWORD: - p += serialize_uint32be(p, &key.value.dword, 0); + from_uint32be((uint32_t*)(node+1)); break; case IP3DBVT_STRING: - p += serialize_ucs2be_string_var_alloc(p, &key.value.str) * sizeof(ucs2char_t); + from_ucs2be_string((ucs2char_t*)(node+1)); break; } - /* Invoke the callback function. */ - if (callback && type) { - callback(buffer, offset, &node, &key, dic_table, index, level, 1, instance); - } - - /* Descend to the next key level if necessary. */ - if (level+1 < IP3DBIDX_MAX_KEYLEVEL) { - int next_field = dic_table->indices[index].fields[level+1]; - if (next_field != -1) { - idx_walk(buffer, node.leaf, dic_table, index, level+1, callback, instance); + /* Convert the sub AVL trees or tail. */ + if (node->tail) { + if (level+1 < IP3DBIDX_MAX_KEYLEVEL && dic_table->indices[index].fields[level+1] != -1) { + /* Convert the AVL tree in the next level. */ + from_be_avltree(avl, node->tail, dic_table, index, level+1); + } else { + /* Convert the tail. */ + avltail_t* tail = (avltail_t*)avlnode(avl, node->tail); + for (;;) { + from_uint32be(&tail->next); + from_uint32be(&tail->data); + if (!tail->next) { + break; + } + tail = (avltail_t*)avlnode(avl, tail->next); + } } } - /* Invoke the callback function. */ - if (callback && type) { - callback(buffer, offset, &node, &key, dic_table, index, level, 0, instance); + /* Descend to the right node. */ + if (node->right) { + from_be_avltree(avl, node->right, dic_table, index, level); } - - /* Descend to right children. */ - if (node.right) { - idx_walk(buffer, node.right, dic_table, index, level, callback, instance); - } } static void fprinti(FILE *fp, int n) @@ -181,89 +409,164 @@ while (n--) fputc(' ', fp); } -int idx_walkcb_dump( - uint8_t* buffer, - uint32_t offset, - node_t* node, - ip3db_variant_t* key, - dic_table_t* dic_table, - int index, - int level, - int flag, - void *instance - ) +static void avl_dump(avl_t* avl, uint32_t offset, dic_table_t* dic_table, int index, int level, FILE *fpo) { - FILE *fp = (FILE*)instance; + avlnode_t* node = avlnode(avl, offset); int field = dic_table->indices[index].fields[level]; int type = dic_table->fields[field].type; - int is_tail = (IP3DBIDX_MAX_KEYLEVEL <= level+1 || dic_table->indices[index].fields[level+1] == -1); int indent = 2 * (level + 1); - if (flag) { - fprinti(fp, indent); - fprintf(fp, "NODE (0x%08X) = {\n", offset); - fprinti(fp, indent); - fprintf(fp, " left: 0x%08X\n", node->left); - fprinti(fp, indent); - fprintf(fp, " right: 0x%08X\n", node->right); - fprinti(fp, indent); - fprintf(fp, " data: 0x%08X\n", node->leaf); + /* Descend to the left node. */ + if (node->left) { + avl_dump(avl, node->left, dic_table, index, level, fpo); + } - fprinti(fp, indent); - fprintf(fp, " key: "); - switch (key->type) { - case IP3DBVT_BYTE: - fprintf(fp, "0x%02X", key->value.byte); + /* Dump the current node. */ + fprinti(fpo, indent); + fprintf(fpo, "NODE (0x%08X)\n", offset); + fprinti(fpo, indent); + fprintf(fpo, " left: 0x%08X\n", node->left); + fprinti(fpo, indent); + fprintf(fpo, " right: 0x%08X\n", node->right); + fprinti(fpo, indent); + fprintf(fpo, " tail: 0x%08X\n", node->tail); + fprinti(fpo, indent); + fprintf(fpo, " key: "); + switch (type) { + case IP3DBVT_BYTE: + fprintf(fpo, "0x%02X", *(uint8_t*)(node+1)); + break; + case IP3DBVT_WORD: + fprintf(fpo, "0x%04X", *(uint16_t*)(node+1)); + break; + case IP3DBVT_DWORD: + fprintf(fpo, "0x%08X", *(uint32_t*)(node+1)); + break; + case IP3DBVT_STRING: + fprints(fpo, "%s", (ucs2char_t*)(node+1)); + break; + } + fprintf(fpo, "\n"); + + /* Convert the sub AVL trees or tail. */ + if (node->tail) { + if (level+1 < IP3DBIDX_MAX_KEYLEVEL && dic_table->indices[index].fields[level+1] != -1) { + /* Convert the AVL tree in the next level. */ + avl_dump(avl, node->tail, dic_table, index, level+1, fpo); + } else { + /* Convert the tail. */ + avltail_t* tail = (avltail_t*)avlnode(avl, node->tail); + + fprinti(fpo, indent); + fprintf(fpo, " TAIL: [\n"); + for (;;) { + fprinti(fpo, indent); + fprintf(fpo, " 0x%08X\n", tail->data); + if (!tail->next) { + break; + } + tail = (avltail_t*)avlnode(avl, tail->next); + } + fprinti(fpo, indent); + fprintf(fpo, " ]\n", node->left); + } + } + + /* Descend to the right node. */ + if (node->right) { + avl_dump(avl, node->right, dic_table, index, level, fpo); + } +} + +void avl_walk2(avl_t* avl, uint32_t offr, avl_comp_t comp) +{ + wchar_t* key = 0; + avlnode_t* root = avlnode(avl, offr); + avltail_t* tail = (avltail_t*)avlnode(avl, root->tail); + if (root->left) avl_walk2(avl, root->left, comp); + key = (wchar_t*)avlnode(avl, offr + sizeof(avlnode_t)); + printf("%S: ", key); + for (;;) { + printf("%d ", tail->data); + if (!tail->next) { break; - case IP3DBVT_WORD: - fprintf(fp, "0x%04X", key->value.word); - break; - case IP3DBVT_DWORD: - fprintf(fp, "0x%08X", key->value.dword); - break; - case IP3DBVT_STRING: - fprints(fp, "%s", key->value.str); - break; } - fprintf(fp, "\n"); + tail = (avltail_t*)avlnode(avl, tail->next); + } + printf("\n"); - if (is_tail) { - tail_t tail; + if (root->right) avl_walk2(avl, root->right, comp); +} - memset(&tail, 0, sizeof(tail)); - tail.next = node->leaf; +void avl_walk(avl_t* avl, uint32_t offr, avl_comp_t comp) +{ + wchar_t* key = 0; + avlnode_t* root = avlnode(avl, offr); + if (root->left) avl_walk(avl, root->left, comp); + key = (wchar_t*)avlnode(avl, offr + sizeof(avlnode_t)); + printf("%S: ", key); + avl_walk2(avl, root->tail, comp); + if (root->right) avl_walk(avl, root->right, comp); +} - fprinti(fp, indent); - fprintf(fp, " TAIL: [\n", node->left); - while (tail.next) { - uint8_t *p = buffer + tail.next; - idx_serialize_tail(p, &tail, 0); - fprinti(fp, indent); - fprintf(fp, " 0x%08X\n", tail.dat_offset); - } - fprinti(fp, indent); - fprintf(fp, " ]\n", node->left); - } - } else { - fprinti(fp, indent); - fprintf(fp, "}\n"); +idx_t *idx_new() +{ + idx_t* idx = (idx_t*)malloc(sizeof(idx_t)); + idx->max_size = 0x00020000; + idx->buffer = (uint8_t*)malloc(idx->max_size); + idx->size = 0; + idx->avl = (avl_t*)malloc(sizeof(avl_t)); + idx->avl->buffer = idx->buffer; + idx->avl->offset = sizeof(idx_header_t); + idx->avl->size = idx->max_size; + return idx; +} + +void idx_finish(idx_t* idx) +{ + free(idx->avl); + free(idx->buffer); + free(idx); +} + +int idx_read(idx_t* idx, dic_t* dic, FILE *fpi) +{ + int i; + idx_header_t* header = (idx_header_t*)idx->buffer; + + /* Read the whole data at a time. */ + if (fread(idx->buffer, 1, idx->max_size, fpi) != idx->max_size) { + return 1; } + /* Convert the byte order of the header from big endian to the native one. */ + from_uint32be(&header->size); + from_uint32be(&header->unknown1); + from_uint32be(&header->unknown2); + + /* Convert the byte order of AVL trees. */ + for (i = 0;i < dic->music.num_indices;++i) { + uint32_t idx_root = dic_get_idxroot(dic, IP3DBIDX_MUSIC, i); + from_be_avltree(idx->avl, idx_root, &dic->music, i, 0); + } + for (i = 0;i < dic->objects.num_indices;++i) { + uint32_t idx_root = dic_get_idxroot(dic, IP3DBIDX_OBJECTS, i); + from_be_avltree(idx->avl, idx_root, &dic->objects, i, 0); + } return 0; } -int idx_dump(uint8_t* buffer, dic_t* dic, FILE *fpo) +result_t idx_dump(idx_t* idx, dic_t* dic, FILE *fpo) { int i; - header_t header; + idx_header_t* header = (idx_header_t*)idx->buffer; fprintf(fpo, "===== db.idx =====\n"); /* Dump the header. */ - idx_serialize_header(buffer, &header, 0); - fprintf(fpo, "size: 0x%08X\n", header.size); - fprintf(fpo, "unknown1: 0x%08X\n", header.unknown1); - fprintf(fpo, "unknown2: 0x%08X\n", header.unknown2); + fprintf(fpo, "size: 0x%08X\n", header->size); + fprintf(fpo, "unknown1: 0x%08X\n", header->unknown1); + fprintf(fpo, "unknown2: 0x%08X\n", header->unknown2); /* Dump the binary search trees. */ for (i = 0;i < dic->music.num_indices;++i) { @@ -271,17 +574,16 @@ fprintf(fpo, "["); dic_repr_index(dic, IP3DBIDX_MUSIC, i, fpo); fprintf(fpo, "]\n"); - idx_walk(buffer, idx_root, &dic->music, i, 0, idx_walkcb_dump, (void*)stdout); + avl_dump(idx->avl, idx_root, &dic->music, i, 0, fpo); } for (i = 0;i < dic->objects.num_indices;++i) { uint32_t idx_root = dic_get_idxroot(dic, IP3DBIDX_OBJECTS, i); fprintf(fpo, "["); dic_repr_index(dic, IP3DBIDX_OBJECTS, i, fpo); fprintf(fpo, "]\n"); - idx_walk(buffer, idx_root, &dic->objects, i, 0, idx_walkcb_dump, (void*)stdout); + avl_dump(idx->avl, idx_root, &dic->objects, i, 0, fpo); } fprintf(fpo, "\n"); return 0; } - Modified: trunk/pmplib/lib/pmp_iriverplus3/idx.h =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/idx.h 2006-12-25 15:15:36 UTC (rev 201) +++ trunk/pmplib/lib/pmp_iriverplus3/idx.h 2006-12-27 08:08:26 UTC (rev 202) @@ -24,6 +24,19 @@ #ifndef __IP3DB_IDX_H__ #define __IP3DB_IDX_H__ -int idx_dump(uint8_t* buffer, dic_t* dic, FILE *fpo); +struct tag_avl_t; typedef struct tag_avl_t avl_t; +struct tag_idx_t { + uint8_t* buffer; + uint32_t size; + uint32_t max_size; + avl_t* avl; +}; +typedef struct tag_idx_t idx_t; + +idx_t *idx_new(); +void idx_finish(idx_t* idx); +result_t idx_read(idx_t* idx, dic_t* dic, FILE *fpi); +result_t idx_dump(idx_t* idx, dic_t* dic, FILE *fpo); + #endif/*__IP3DB_IDX_H__*/ Modified: trunk/pmplib/lib/pmp_iriverplus3/ip3db.c =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/ip3db.c 2006-12-25 15:15:36 UTC (rev 201) +++ trunk/pmplib/lib/pmp_iriverplus3/ip3db.c 2006-12-27 08:08:26 UTC (rev 202) @@ -105,6 +105,7 @@ memset(db, 0, sizeof(*db)); db->dat = dat_new(); db->dic = dic_new(); + db->idx = idx_new(); dic_template = dic_get_template(&db->dic_size); db->dic_buffer = (uint8_t*)malloc(db->dic_size); @@ -116,9 +117,9 @@ { dic_finish(db->dic); dat_finish(db->dat); + idx_finish(db->idx); free(db->dat_buffer); free(db->dic_buffer); - free(db->idx_buffer); ip3db_init(db); } @@ -126,39 +127,39 @@ { FILE *fp = 0; - fp = ucs2fopen(datfn, "rb"); + fp = ucs2fopen(dicfn, "rb"); if (!fp) { ip3db_finish(db); return 1; } - fread_all(fp, &db->dat_buffer, &db->dat_size); + fread_all(fp, &db->dic_buffer, &db->dic_size); fclose(fp); - fp = ucs2fopen(dicfn, "rb"); - if (!fp) { + if (dic_serialize(db->dic, db->dic_buffer, 0) != 0) { ip3db_finish(db); return 1; } - fread_all(fp, &db->dic_buffer, &db->dic_size); - fclose(fp); - fp = ucs2fopen(idxfn, "rb"); + fp = ucs2fopen(datfn, "rb"); if (!fp) { ip3db_finish(db); return 1; } - fread_all(fp, &db->idx_buffer, &db->idx_size); + fread_all(fp, &db->dat_buffer, &db->dat_size); fclose(fp); - if (dic_serialize(db->dic, db->dic_buffer, 0) != 0) { + if (dat_serialize(db->dat, db->dic, db->dat_buffer, 0) != 0) { ip3db_finish(db); return 1; } - if (dat_serialize(db->dat, db->dic, db->dat_buffer, 0) != 0) { + fp = ucs2fopen(idxfn, "rb"); + if (!fp) { ip3db_finish(db); return 1; } + idx_read(db->idx, db->dic, fp); + fclose(fp); return 0; } @@ -168,7 +169,6 @@ FILE *fp = 0; free(db->dat_buffer); - free(db->idx_buffer); db->dat_size = 0x00040000; //db->dat_buffer = (uint8_t*)calloc(db->dat_size, sizeof(uint8_t)); @@ -221,7 +221,7 @@ dat_dump(db->dat, db->dic, fpo); /* Dump db.idx */ - idx_dump(db->idx_buffer, db->dic, fpo); + idx_dump(db->idx, db->dic, fpo); return 0; } @@ -230,8 +230,6 @@ /* Construct Objects table in db.dat */ dat_construct(db->dat, db->dic, records, num_records); - /* Construct Music table in db.dat */ - /* Construct db.idx */ /* Update db.dic */ Modified: trunk/pmplib/lib/pmp_iriverplus3/ip3db.h =================================================================== --- trunk/pmplib/lib/pmp_iriverplus3/ip3db.h 2006-12-25 15:15:36 UTC (rev 201) +++ trunk/pmplib/lib/pmp_iriverplus3/ip3db.h 2006-12-27 08:08:26 UTC (rev 202) @@ -146,17 +146,17 @@ struct tag_dat_t; typedef struct tag_dat_t dat_t; struct tag_dic_t; typedef struct tag_dic_t dic_t; +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; - uint8_t* idx_buffer; - long idx_size; dat_t* dat; dic_t* dic; + idx_t* idx; } ip3db_t; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |