Blame src/strmap.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "strmap.h"
Packit Service 20376f
Packit Service 20376f
__KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
Packit Service 20376f
Packit Service 20376f
int git_strmap_alloc(git_strmap **map)
Packit Service 20376f
{
Packit Service 20376f
	if ((*map = kh_init(str)) == NULL) {
Packit Service 20376f
		giterr_set_oom();
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_strmap_free(git_strmap *map)
Packit Service 20376f
{
Packit Service 20376f
	kh_destroy(str, map);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_strmap_clear(git_strmap *map)
Packit Service 20376f
{
Packit Service 20376f
	kh_clear(str, map);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_strmap_num_entries(git_strmap *map)
Packit Service 20376f
{
Packit Service 20376f
	return kh_size(map);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_strmap_lookup_index(git_strmap *map, const char *key)
Packit Service 20376f
{
Packit Service 20376f
	return kh_get(str, map, key);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_strmap_valid_index(git_strmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	return idx != kh_end(map);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_strmap_exists(git_strmap *map, const char *key)
Packit Service 20376f
{
Packit Service 20376f
	return kh_get(str, map, key) != kh_end(map);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_strmap_has_data(git_strmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	return kh_exist(map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char *git_strmap_key(git_strmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	return kh_key(map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key)
Packit Service 20376f
{
Packit Service 20376f
	kh_val(map, idx) = key;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void *git_strmap_value_at(git_strmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	return kh_val(map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value)
Packit Service 20376f
{
Packit Service 20376f
	kh_val(map, idx) = value;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_strmap_delete_at(git_strmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	kh_del(str, map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_strmap_put(git_strmap *map, const char *key, int *err)
Packit Service 20376f
{
Packit Service 20376f
	return kh_put(str, map, key, err);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
Packit Service 20376f
{
Packit Service 20376f
	khiter_t idx = kh_put(str, map, key, rval);
Packit Service 20376f
Packit Service 20376f
	if ((*rval) >= 0) {
Packit Service 20376f
		if ((*rval) == 0)
Packit Service 20376f
			kh_key(map, idx) = key;
Packit Service 20376f
		kh_val(map, idx) = value;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_strmap_delete(git_strmap *map, const char *key)
Packit Service 20376f
{
Packit Service 20376f
	khiter_t idx = git_strmap_lookup_index(map, key);
Packit Service 20376f
	if (git_strmap_valid_index(map, idx))
Packit Service 20376f
		git_strmap_delete_at(map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_strmap_next(
Packit Service 20376f
	void **data,
Packit Service 20376f
	git_strmap_iter* iter,
Packit Service 20376f
	git_strmap *map)
Packit Service 20376f
{
Packit Service 20376f
	if (!map)
Packit Service 20376f
		return GIT_ERROR;
Packit Service 20376f
Packit Service 20376f
	while (*iter != git_strmap_end(map)) {
Packit Service 20376f
		if (!(git_strmap_has_data(map, *iter))) {
Packit Service 20376f
			++(*iter);
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		*data = git_strmap_value_at(map, *iter);
Packit Service 20376f
Packit Service 20376f
		++(*iter);
Packit Service 20376f
Packit Service 20376f
		return GIT_OK;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return GIT_ITEROVER;
Packit Service 20376f
}