-
Notifications
You must be signed in to change notification settings - Fork 0
/
links.c
139 lines (120 loc) · 3.25 KB
/
links.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* links.c -- binary search tree of links
*
* author: Pat Gaffney <pat@hypepat.com>
* created: 2016-09-30
* modified: 2016-01-06
* project: patdown
*
************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "errors.h"
#include "patdown.h"
/************************************************************************
* Link Reference Binary Search Tree
*
* A private binary search of `LinkRef` nodes is created as the input
* file is parsed. This tree is then queried as the actual references
* are encountered in the file.
*
************************************************************************/
/** Binary Search Tree Pointer. */
static LinkRef *head = NULL;
/**
* Allocate space for new `LinkRef` node.
*
* - throws fatal_memory_error: Memory could not be allocated.
*
* - returns: A pointer to the new `LinkRef` node.
*/
LinkRef *init_link_ref(void)
{
LinkRef *ref = (LinkRef *) malloc(sizeof(LinkRef));
if (!ref) throw_fatal_memory_error();
/* A link title is optional. */
ref->title[0] = '\0';
ref->left = NULL;
ref->right = NULL;
return ref;
}
/**
* Insert a `LinkRef` node into the binary tree.
*
* - TODO: Implement duplicate detection (should overwrite).
*
* - parameter node: The node to insert into the tree.
*/
// static void insert_link_ref(LinkRef *top, LinkRef *node)
// {
// if (!node) return;
// if (!top) top = node;
// else {
// int rc = strcmp(node->label, top->label);
// if (rc < 0) {
// insert_link_ref(top->left, node);
// }
// else if (rc > 0) {
// insert_link_ref(top->right, node);
// }
// else printf("DUPLICATE NODE: \'%s\'\n", node->label);
// }
// }
/**
* Add a `LinkRef` node the the internal binary search tree.
*
* This function is the public API for inserting nodes.
*
* - parameter node: The node to insert into the tree.
*/
// void add_link_ref(LinkRef *node)
// {
// insert_link_ref(head, node);
// }
/**
* Search the binary tree for a particular link label.
*
* - parameter label: The link to search for.
*
* - returns: A pointer to the link, if found, otherwise `NULL`.
*/
// LinkRef *search_link_refs(LinkRef *top, char *label)
// {
// if (!head) return NULL;
//
// int rc = strcmp(label, head->label);
// if (rc < 0) {
// return search_link_refs(head->left, label);
// }
// else if (rc > 0) {
// return search_link_refs(head->right, label);
// }
// return head;
// }
/**
* Free the memory allocated for a `LinkRef` node.
*
* This is the internal interface for freeing a particular `LinkRef` node.
* All nodes below the parameter in the tree will also be free'd.
*
* - parameter node: The node at which to begin freeing.
*/
static void free_link_ref_node(LinkRef *node)
{
if (node) {
free_link_ref_node(node->left);
free_link_ref_node(node->right);
free(node);
}
}
/**
* Free all nodes in the private binary search tree.
*
* This is the external interface for freeing the internal
* binary serach tree created by parsing the input file.
*/
void free_link_refs(void)
{
free_link_ref_node(head);
}