/* * Write a binary tree node to disk. */ int pdb_write_tree_node_cb(struct pdb* dbptr, FILE* fptr, struct pdb_node_t* nptr, int tabs) { struct binaryTree* tptr; struct linkList* lptr; struct linkNode* lnptr; /* * Create a list from the tree so we can iterate through it. */ tptr = nptr->data; lptr = list_create(); tree_to_list(tptr, lptr); lnptr = lptr->root; /* * Shuffle tree (if set). */ if (pdb_is_set(dbptr, PDB_WRITE_SHUFFLE)) list_shuffle(lptr); /* * Set PDB_WRITE_NODE_FIRST_ID as the first in the list * if PDB_WRITE_NODE_FIRST is set. * Only if root node. */ if (!tabs) { if (pdb_is_set(dbptr, PDB_WRITE_NODE_FIRST)) { while (lnptr) { nptr = lnptr->data; if (!strcmp(nptr->id, PDB_WRITE_NODE_FIRST_ID)) { list_free_node(lptr, lnptr, 0, NULL); list_add_node_front(lptr, nptr); break; } lnptr = lnptr->next; } } } /* * Write all children to disk. */ lnptr = lptr->root; while (lnptr) { nptr = lnptr->data; if (!pdb_standard_write_node(dbptr, fptr, nptr, tabs)) { list_free(lptr, 0, NULL); return 0; } lnptr = lnptr->next; } /* * Free temp list (but not data). */ list_free(lptr, 0, NULL); return 1; }
/* * Create a backtrace of the given nodes location relative to the root. * The resulting string is allocated and must be freed. */ DLLEXP char* pdb_trace(struct pdb_node_t* nptr) { struct linkList* lptr; struct linkNode* lnptr; char* sbuf; char* buf; int buf_size; int used; char* str; lptr = list_create(); while (nptr) { list_add_node_front(lptr, nptr->id); nptr = nptr->parent; } lnptr = lptr->root; sbuf = (char*)malloc(BLOCK_SIZE); buf = sbuf; buf_size = BLOCK_SIZE; used = 1; memset(sbuf, 0, BLOCK_SIZE); str = NULL; while (lnptr) { str = lnptr->data; if (!str) { lnptr = lnptr->next; continue; } if (*str) { *buf = '/'; ++buf; } while (*str) { *buf = *str; ++buf; ++str; ++used; if (used >= buf_size) { buf_size *= 2; sbuf = (char*)realloc(sbuf, BLOCK_SIZE); buf = (sbuf + (sizeof(char) * (used - 1))); memset(buf, 0, (sizeof(char) * (buf_size - used))); } } lnptr = lnptr->next; } list_free(lptr, 0, NULL); return sbuf; }
/* * Write a link list node to disk. */ int pdb_write_list_node_cb(struct pdb* dbptr, FILE* fptr, struct pdb_node_t* nptr, int tabs) { struct linkList* lptr = nptr->data; struct linkNode* lnptr = lptr->root; /* * Set PDB_WRITE_NODE_FIRST_ID as the first in the list * if PDB_WRITE_NODE_FIRST is set. * Only if root node. */ if (!tabs) { if (pdb_is_set(dbptr, PDB_WRITE_NODE_FIRST)) { while (lnptr) { nptr = lnptr->data; if (!strcmp(nptr->id, PDB_WRITE_NODE_FIRST_ID)) { list_delink_node(lptr, lnptr); list_add_node_front(lptr, nptr); break; } lnptr = lnptr->next; } } } /* * Write all children to disk. */ lnptr = lptr->root; while (lnptr) { nptr = lnptr->data; if (!pdb_standard_write_node(dbptr, fptr, nptr, tabs)) return 0; lnptr = lnptr->next; } return 1; }
int main(void) { list_t *list = NULL; printf("----- Print add elements -----\n"); list_add_node_front(&list, "J"); list_add_node_front(&list, "I"); list_add_node_front(&list, "H"); list_add_node_front(&list, "G"); list_add_node_front(&list, "F"); list_add_node_front(&list, "E"); list_add_node_front(&list, "D"); list_add_node_front(&list, "C"); list_add_node_front(&list, "B"); list_add_node_front(&list, "A"); printf("----- Print list -----\n"); list_print(list, print_callback); printf("----- Access elements -----\n"); printf("The element number %u of the list is %s\n", 0, (char *)list_get_node_by_id(list, 0)); printf("The element number %u of the list is %s\n", 1, (char *)list_get_node_by_id(list, 1)); printf("The element number %u of the list is %s\n", 5, (char *)list_get_node_by_id(list, 5)); printf("The element number %u of the list is %s\n", 9, (char *)list_get_node_by_id(list, 9)); // The last id is 9 so 10 is out of range printf("The element number %u of the list is %s\n", 10,(char *)list_get_node_by_id(list, 10)); printf("----- Delete last element -----\n"); list_del_node_by_id(&list, 9); printf("----- Print list -----\n"); list_print(list, print_callback); printf("----- Delete first element -----\n"); list_del_node_by_id(&list, 0); printf("----- Print list -----\n"); list_print(list, print_callback); uint32_t size = list_get_size(list); printf("----- Size of the list: %u -----\n", size); printf("----- Delete all element -----\n"); uint32_t i; for (i = 0; i < size ; i++) { list_del_node_by_id(&list, 0); } printf("----- Print list -----\n"); list_print(list, print_callback); list_add_node_back(&list, "A"); list_add_node_back(&list, "B"); list_add_node_back(&list, "C"); list_add_node_back(&list, "D"); printf("----- Print list -----\n"); list_print(list, print_callback); printf("----- Reverse the list -----\n"); list_reverse_order(&list); printf("----- Print list -----\n"); list_print(list, print_callback); printf("----- Clean list -----\n"); list_cleanup(&list); i = 0; while (1) { list_add_node_back(&list, "A"); printf("add_elem %u\n", i); ++i; } return (0); }