|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <fnmatch.h> |
| 5 | +#include "node.h" |
| 6 | + |
| 7 | +char** match_entries(char **keys, char *query) { |
| 8 | + char **matched_entries; |
| 9 | + size_t i = 0, j = 0; |
| 10 | + char *key = keys[i]; |
| 11 | + while (key) { |
| 12 | + if (fnmatch(query, key, 0) == 0) { |
| 13 | + matched_entries[j] = key; |
| 14 | + j++; |
| 15 | + } |
| 16 | + i++; |
| 17 | + key = keys[i]; |
| 18 | + } |
| 19 | + matched_entries[j+1] = NULL; |
| 20 | + return matched_entries; |
| 21 | +} |
| 22 | + |
| 23 | +void print_index(struct Node *node) { |
| 24 | + if (node->children_i > 0) { |
| 25 | + for (size_t i = 0; i < node->children_i; i++) { |
| 26 | + print_index(&node->children[i]); |
| 27 | + } |
| 28 | + } |
| 29 | + printf("Node %s\n", node->name); |
| 30 | +} |
| 31 | + |
| 32 | +struct Node* clear(struct Node *node) { |
| 33 | + if(!node) return node; |
| 34 | + if (node->children_i > 0) { |
| 35 | + for (size_t i = 0; i < node->children_i; i++) { |
| 36 | + clear(&node->children[i]); |
| 37 | + } |
| 38 | + } |
| 39 | + printf("Freeing node %s children\n", node->name); |
| 40 | + free(node->children); |
| 41 | + node->children = NULL; |
| 42 | + node->children_i = 0; |
| 43 | + node->children_size = 0; |
| 44 | + // Clear root node if all sub-tree children have been cleared |
| 45 | + if(node && strcmp(node->name, "root") == 0) { |
| 46 | + free(node); |
| 47 | + node = NULL; |
| 48 | + return node; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +struct Node* insert(struct Node *parent, char *path) { |
| 53 | + if (!parent->children) { |
| 54 | + parent->children = calloc(parent->children_size, sizeof(struct Node)); |
| 55 | + } |
| 56 | + if (parent->children_i == parent->children_size) { |
| 57 | + parent->children_size *= 2; |
| 58 | + printf("Resizing children of %s to %zu\n", parent->name, parent->children_size); |
| 59 | + struct Node *new_children = realloc(parent->children, parent->children_size * sizeof(struct Node)); |
| 60 | + if (new_children) { |
| 61 | + parent->children = new_children; |
| 62 | + memset(&parent->children[parent->children_i], 0, |
| 63 | + sizeof(struct Node) * (parent->children_size - parent->children_i)); |
| 64 | + } |
| 65 | + else return NULL; |
| 66 | + } |
| 67 | + parent->children[parent->children_i].name = path; |
| 68 | + parent->children[parent->children_i].children_i = 0; |
| 69 | + parent->children[parent->children_i].children_size = 1; |
| 70 | + printf("Inserted %s to %s\n", parent->children[parent->children_i].name, parent->name); |
| 71 | + parent->children_i++; |
| 72 | + return &parent->children[parent->children_i-1]; |
| 73 | +} |
| 74 | + |
| 75 | +struct Node* name_is_child(struct Node *node, char *name) { |
| 76 | + struct Node *child; |
| 77 | + for (int i=0; i<node->children_i; i++) { |
| 78 | + child = &node->children[i]; |
| 79 | + if (strcmp(child->name, name) == 0) return child; |
| 80 | + } |
| 81 | + return NULL; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +int insert_str(struct Node *parent, char *path) { |
| 86 | + if (!path) return 0; |
| 87 | + char *token, *string, *tofree; |
| 88 | + tofree = string = strdup(path); |
| 89 | + token = strsep(&string, "."); |
| 90 | + if (token) { |
| 91 | + printf("Inserting node %s to parent %s\n", token, parent->name); |
| 92 | + insert(parent, token); |
| 93 | + struct Node *node = &parent->children[parent->children_i]; |
| 94 | + return insert_str(node, string); |
| 95 | + } |
| 96 | + free(tofree); |
| 97 | +} |
| 98 | + |
| 99 | +struct Node* build_index(char *paths[]) { |
| 100 | + struct Node *root = malloc(sizeof(struct Node)); |
| 101 | + root->name = "root"; |
| 102 | + root->children_i = 0; |
| 103 | + root->children_size = 1; |
| 104 | + root->children = NULL; |
| 105 | + struct Node *node, *temp = NULL; |
| 106 | + temp = root; |
| 107 | + size_t i = 0; |
| 108 | + char *path = paths[i]; |
| 109 | + while (path) { |
| 110 | + node = insert(temp, path); |
| 111 | + i++; |
| 112 | + path = paths[i]; |
| 113 | + temp = node; |
| 114 | + } |
| 115 | + return root; |
| 116 | +} |
| 117 | + |
| 118 | +void insert_paths(struct Node *node, char *paths[]) { |
| 119 | + size_t i = 0; |
| 120 | + char *path = paths[i]; |
| 121 | + struct Node *child; |
| 122 | + while (path) { |
| 123 | + child = name_is_child(node, path); |
| 124 | + if (!child) { |
| 125 | + child = insert(node, path); |
| 126 | + } |
| 127 | + else { |
| 128 | + printf("Name %s already child of %s\n", path, node->name); |
| 129 | + } |
| 130 | + node = child; |
| 131 | + i++; |
| 132 | + path = paths[i]; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +struct Node* init_node(char *name) { |
| 137 | + struct Node *node = malloc(sizeof(struct Node)); |
| 138 | + node->name = "root"; |
| 139 | + node->children_i = 0; |
| 140 | + node->children_size = 1; |
| 141 | + node->children = NULL; |
| 142 | + return node; |
| 143 | +} |
| 144 | + |
| 145 | +int is_leaf(struct Node *parent) { |
| 146 | + if (!parent) return 0; |
| 147 | + return parent->children_i == 0; |
| 148 | +} |
| 149 | + |
| 150 | +/* |
| 151 | +
|
| 152 | +void insert_all_paths(struct Node *root, char **paths) { |
| 153 | + if(!paths) return; |
| 154 | + size_t i = 0; |
| 155 | + size_t j = 0; |
| 156 | + char *sub_paths = paths[i]; |
| 157 | + char *path = sub_paths[j]; |
| 158 | + while(path) { |
| 159 | + printf("%s\n", path); |
| 160 | + } |
| 161 | + }*/ |
0 commit comments