| 1 |
#include <search.h> |
| 2 |
#include <string.h> |
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include <ffindex_posix_search.h> |
| 6 |
#include <ffutil.h> |
| 7 |
|
| 8 |
/* tree version */ |
| 9 |
|
| 10 |
ffindex_entry_t *ffindex_tree_get_entry(ffindex_index_t* index, char* name) |
| 11 |
{ |
| 12 |
ffindex_entry_t search; |
| 13 |
strncpy(search.name, name, FFINDEX_MAX_ENTRY_NAME_LENTH); |
| 14 |
return (ffindex_entry_t *)tfind((const void *)&search, &index->tree_root, ffindex_compare_entries_by_name); |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
ffindex_index_t* ffindex_tree_unlink(ffindex_index_t* index, char* name_to_unlink) |
| 19 |
{ |
| 20 |
if(index->tree_root == NULL) |
| 21 |
{ |
| 22 |
fferror_print(__FILE__, __LINE__, __func__, "tree is NULL"); |
| 23 |
return NULL; |
| 24 |
} |
| 25 |
ffindex_entry_t search; |
| 26 |
strncpy(search.name, name_to_unlink, FFINDEX_MAX_ENTRY_NAME_LENTH); |
| 27 |
tdelete((const void *)&search, &index->tree_root, ffindex_compare_entries_by_name); |
| 28 |
return index; |
| 29 |
} |
| 30 |
|
| 31 |
ffindex_index_t* ffindex_index_as_tree(ffindex_index_t* index) |
| 32 |
{ |
| 33 |
index->tree_root = NULL; |
| 34 |
for(size_t i = 0; i < index->n_entries; i++) |
| 35 |
{ |
| 36 |
ffindex_entry_t *entry = &index->entries[i]; |
| 37 |
tsearch((const void *)entry, &index->tree_root, ffindex_compare_entries_by_name); |
| 38 |
//entry = *(ffindex_entry_t **)tsearch((const void *)entry, &index->tree_root, ffindex_compare_entries_by_name); |
| 39 |
//printf("entry find: %s\n", entry->name); |
| 40 |
} |
| 41 |
index->type = TREE; |
| 42 |
return index; |
| 43 |
} |
| 44 |
|
| 45 |
static __thread FILE * ffindex_tree_write_action_index_file = NULL; |
| 46 |
static __thread int ffindex_tree_write_action_ret = EXIT_SUCCESS; |
| 47 |
|
| 48 |
static void ffindex_tree_write_action(const void *node, const VISIT which, const int depth) |
| 49 |
{ |
| 50 |
switch (which) |
| 51 |
{ |
| 52 |
case preorder: |
| 53 |
break; |
| 54 |
case endorder: |
| 55 |
break; |
| 56 |
case postorder: |
| 57 |
case leaf: |
| 58 |
if(ffindex_print_entry(ffindex_tree_write_action_index_file, *(ffindex_entry_t **) node) < 6) |
| 59 |
ffindex_tree_write_action_ret = EXIT_FAILURE; |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
int ffindex_tree_write(ffindex_index_t* index, FILE* index_file) |
| 65 |
{ |
| 66 |
ffindex_tree_write_action_ret = EXIT_SUCCESS; |
| 67 |
ffindex_tree_write_action_index_file = index_file; |
| 68 |
twalk(index->tree_root, ffindex_tree_write_action); |
| 69 |
return ffindex_tree_write_action_ret; |
| 70 |
} |