int yadb_insert(record_t* record) { int ret = 0; if (!find_node(yadb(), record->key)) { ret = ins_node(yadb(), record->key, record->val); } return ret; }
int main(void) { /* Declare YOUR variables here ! */ BiTree mytree; int menu_choice; srand((unsigned int)time(NULL)); if ((mytree = BITREEinit(my_destroy)) == NULL) { printf("\nFatal error - bailing out...\n!"); BITREEdestroy(mytree); exit(-1); } /* Don't forget to set the compare callback..! */ BITREEsetcompare(mytree, my_cmp); /* Initialize - and add nodes to the table... */ create_nodes(mytree, NR_OF_ITEMS); do { menu_choice = menu(MAIN_MENU_ROW, 0, 4); switch (menu_choice) { case 1: ins_node(mytree); break; case 2: rem_node(mytree); break; case 3: search_node(mytree); break; case 4: my_clearscrn(); printf("--- PRINT TREE ---\n"); print_tree(mytree); prompt_and_pause("\n\n"); break; default: final_status(mytree); break; } } while (menu_choice); prompt_and_pause("\n\nLet's tidy up and destroy the tree..- Bye!"); BITREEdestroy(mytree); return 0; }
int load(yadb_t* db) { int ret = 0; FILE* fp; size_t count; int rows; if ((fp = fopen(db->filename, "rb")) != NULL) { { count = fread((void*)(&rows), sizeof(int), 1, fp); if (count != 1) { fclose(fp); return ret; } } { int i; record_t record; int status; for (i = 0; i < rows; i++) { count = fread((void*)(&record), sizeof(record_t), 1, fp); if (count == 1) { printf("Key: %s\n", record.key); printf("Val: %s\n", record.val); status = ins_node(db, record.key, record.val); if (status != 1) { fclose(fp); return ret; } } else { fclose(fp); return ret; } } ret = 1; } fclose(fp); } return ret; }