Ejemplo n.º 1
0
/**
 * @short Searchs for a given key, and returns that node and its parent (if parent!=NULL)
 * @memberof onion_dict_t
 *
 * If not found, returns the parent where it should be. Nice for adding too.
 */
static const onion_dict_node *onion_dict_find_node(const onion_dict *d, const onion_dict_node *current, const char *key, const onion_dict_node **parent){
	if (!current){
		return NULL;
	}
	int cmp=d->cmp(key, current->data.key);
	//ONION_DEBUG0("%s cmp %s = %d",key, current->data.key, cmp);
	if (cmp==0)
		return current;
	if (parent) *parent=current;
	if (cmp<0)
		return onion_dict_find_node(d, current->left, key, parent);
	else // if (cmp>0)
		return onion_dict_find_node(d, current->right, key, parent);
}
Ejemplo n.º 2
0
/**
 * @short Gets a value. For dicts returns NULL; use onion_dict_get_dict.
 * @memberof onion_dict_t
 */
const char *onion_dict_get(const onion_dict *dict, const char *key){
	const onion_dict_node *r;
	r=onion_dict_find_node(dict, dict->root, key, NULL);
	if (r && !(r->data.flags&OD_DICT))
		return r->data.value;
	return NULL;
}
Ejemplo n.º 3
0
/**
 * @short Gets a value, only if its a dict
 * @memberof onion_dict_t
 */
onion_dict *onion_dict_get_dict(const onion_dict *dict, const char *key){
	const onion_dict_node *r;
	r=onion_dict_find_node(dict, dict->root, key, NULL);
	if (r){
		if (r->data.flags&OD_DICT)
			return (onion_dict*)r->data.value;
	}
	return NULL;
}
Ejemplo n.º 4
0
/**
 * @short Gets a value. For dicts returns NULL; use onion_dict_get_dict.
 * @memberof onion_dict_t
 */
const char *onion_dict_get(const onion_dict *dict, const char *key){
	if (!dict) // Get from null dicts, returns null data.
		return NULL;
	const onion_dict_node *r;
	r=onion_dict_find_node(dict, dict->root, key, NULL);
	if (r && !(r->data.flags&OD_DICT))
		return r->data.value;
	return NULL;
}