]> git.proxmox.com Git - libgit2.git/blame - src/strmap.h
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / strmap.h
CommitLineData
c2b67043 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
c2b67043
RB
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7#ifndef INCLUDE_strmap_h__
8#define INCLUDE_strmap_h__
9
10#include "common.h"
11
22a2d3d5 12/** A map with C strings as key. */
ac3d33df 13typedef struct kh_str_s git_strmap;
c2b67043 14
22a2d3d5
UG
15/**
16 * Allocate a new string map.
17 *
18 * @param out Pointer to the map that shall be allocated.
19 * @return 0 on success, an error code if allocation has failed.
20 */
21int git_strmap_new(git_strmap **out);
22
23/**
24 * Free memory associated with the map.
25 *
26 * Note that this function will _not_ free keys or values added
27 * to this map.
28 *
29 * @param map Pointer to the map that is to be free'd. May be
30 * `NULL`.
31 */
94af9155 32void git_strmap_free(git_strmap *map);
22a2d3d5
UG
33
34/**
35 * Clear all entries from the map.
36 *
37 * This function will remove all entries from the associated map.
38 * Memory associated with it will not be released, though.
39 *
40 * @param map Pointer to the map that shall be cleared. May be
41 * `NULL`.
42 */
a13cfd2a 43void git_strmap_clear(git_strmap *map);
c2b67043 44
22a2d3d5
UG
45/**
46 * Return the number of elements in the map.
47 *
48 * @parameter map map containing the elements
49 * @return number of elements in the map
50 */
51size_t git_strmap_size(git_strmap *map);
c2b67043 52
22a2d3d5
UG
53/**
54 * Return value associated with the given key.
55 *
56 * @param map map to search key in
57 * @param key key to search for
58 * @return value associated with the given key or NULL if the key was not found
59 */
60void *git_strmap_get(git_strmap *map, const char *key);
c2b67043 61
22a2d3d5
UG
62/**
63 * Set the entry for key to value.
64 *
65 * If the map has no corresponding entry for the given key, a new
66 * entry will be created with the given value. If an entry exists
67 * already, its value will be updated to match the given value.
68 *
69 * @param map map to create new entry in
70 * @param key key to set
71 * @param value value to associate the key with; may be NULL
72 * @return zero if the key was successfully set, a negative error
73 * code otherwise
74 */
75int git_strmap_set(git_strmap *map, const char *key, void *value);
76
77/**
78 * Delete an entry from the map.
79 *
80 * Delete the given key and its value from the map. If no such
81 * key exists, this will do nothing.
82 *
83 * @param map map to delete key in
84 * @param key key to delete
85 * @return `0` if the key has been deleted, GIT_ENOTFOUND if no
86 * such key was found, a negative code in case of an
87 * error
88 */
89int git_strmap_delete(git_strmap *map, const char *key);
90
91/**
92 * Check whether a key exists in the given map.
93 *
94 * @param map map to query for the key
95 * @param key key to search for
96 * @return 0 if the key has not been found, 1 otherwise
97 */
a13cfd2a 98int git_strmap_exists(git_strmap *map, const char *key);
22a2d3d5
UG
99
100/**
101 * Iterate over entries of the map.
102 *
103 * This functions allows to iterate over all key-value entries of
104 * the map. The current position is stored in the `iter` variable
105 * and should be initialized to `0` before the first call to this
106 * function.
107 *
108 * @param map map to iterate over
109 * @param value pointer to the variable where to store the current
110 * value. May be NULL.
111 * @param iter iterator storing the current position. Initialize
112 * with zero previous to the first call.
113 * @param key pointer to the variable where to store the current
114 * key. May be NULL.
115 * @return `0` if the next entry was correctly retrieved.
116 * GIT_ITEROVER if no entries are left. A negative error
117 * code otherwise.
118 */
119int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key);
120
121#define git_strmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
122 while (git_strmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
ac3d33df
JK
123 code; \
124 } }
125
22a2d3d5
UG
126#define git_strmap_foreach_value(h, vvar, code) { size_t __i = 0; \
127 while (git_strmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
ac3d33df
JK
128 code; \
129 } }
c2b67043
RB
130
131#endif