Пример #1
0
static command_trie_t *
command_trie_find_node (command_trie_t *trie, gchar *input,
                        gboolean auto_complete)
{
	command_trie_t *node = NULL;
	GList *l;

	/* FIXME: If this happens when a parent command is given, make it nice! */
	if (input == NULL) {
		return NULL;
	}

	if (*input == 0) {
		/* End of token, return current action, or unique completion */
		if (command_trie_valid_match (trie)) {
			node = trie;
		} else if (auto_complete) {
			node = command_trie_find_leaf (trie);
		}
	} else {
		/* Recurse in next trie node */
		l = g_list_find_custom (trie->next, input, command_trie_elem_cmp);
		if (l != NULL) {
			node = command_trie_find_node ((command_trie_t *) l->data, input + 1,
			                               auto_complete);
		}
	}

	return node;
}
Пример #2
0
/* Given a trie node, try to find a unique leaf, else return NULL. */
static command_trie_t*
command_trie_find_leaf (command_trie_t *trie)
{
	GList *succ;
	command_trie_t *leaf = NULL;

	if (trie) {
		succ = g_list_first (trie->next);
		if (succ && succ->next == NULL && !command_trie_valid_match (trie)) {
			/* Not a matching node, a single successor: recurse */
			leaf = command_trie_find_leaf ((command_trie_t *) succ->data);
		} else if (!succ && command_trie_valid_match (trie)) {
			leaf = trie;
		}
	}

	return leaf;
}