summaryrefslogtreecommitdiffstats
path: root/src/utils/common/lookup.h
blob: b6dc8ee8ee18f54f39183eddb88e020d75543d2c (plain)
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
/*  Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

#include <histedit.h>

#include "libknot/mm_ctx.h"
#include "contrib/qp-trie/trie.h"

/*! Lookup context. */
typedef struct {
	/*! Memory pool context. */
	knot_mm_t mm;
	/*! Main trie storage. */
	trie_t *trie;

	/*! Current (iteration) data context. */
	struct {
		/*! Stored key. */
		char *key;
		/*! Corresponding key data. */
		void *data;
	} found;

	/*! Iteration context. */
	struct {
		/*! Total number of possibilities. */
		size_t count;
		/*! The first possibility. */
		char *first_key;
		/*! Hat-trie iterator. */
		trie_it_t *it;
	} iter;
} lookup_t;

/*!
 * Initializes the lookup context.
 *
 * \param[in] lookup  Lookup context.
 *
 * \return Error code, KNOT_EOK if successful.
 */
int lookup_init(lookup_t *lookup);

/*!
 * Deinitializes the lookup context.
 *
 * \param[in] lookup  Lookup context.
 */
void lookup_deinit(lookup_t *lookup);

/*!
 * Inserts given key and data into the lookup.
 *
 * \param[in] lookup  Lookup context.
 * \param[in] str     Textual key.
 * \param[in] data    Key textual data.
 *
 * \return Error code, KNOT_EOK if successful.
 */
int lookup_insert(lookup_t *lookup, const char *str, void *data);

/*!
 * Removes given key from the lookup.
 *
 * \param[in] lookup  Lookup context.
 * \param[in] str     Textual key.
 *
 * \return Error code, KNOT_EOK if successful.
 */
int lookup_remove(lookup_t *lookup, const char *str);

/*!
 * Searches the lookup container for the given key.
 *
 * \note If one candidate, lookup.found contains the key/data,
 *       if more candidates, lookup.found contains the common key prefix and
 *       lookup.iter.first_key is the first candidate key.
 *
 * \param[in] lookup   Lookup context.
 * \param[in] str      Textual key.
 * \param[in] str_len  Textual key length.
 *
 * \return Error code, KNOT_EOK if 1 candidate, KNOT_ENOENT if no candidate,
 *         and KNOT_EFEWDATA if more candidates are possible.
 */
int lookup_search(lookup_t *lookup, const char *str, size_t str_len);

/*!
 * Moves the lookup iterator to the next key candidate.
 *
 * \note lookup.found is updated.
 *
 * \param[in] lookup   Lookup context.
 */
void lookup_list(lookup_t *lookup);

/*!
 * Completes the string based on the lookup content or prints all candidates.
 *
 * \param[in] lookup     Lookup context.
 * \param[in] str        Textual key.
 * \param[in] str_len    Textual key length.
 * \param[in] el         Editline context.
 * \param[in] add_space  Add one space after completed string flag.
 *
 * \return Error code, same as lookup_search().
 */
int lookup_complete(lookup_t *lookup, const char *str, size_t str_len,
                    EditLine *el, bool add_space);