]>
git.proxmox.com Git - libgit2.git/blob - src/strmap.h
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_strmap_h__
8 #define INCLUDE_strmap_h__
12 /** A map with C strings as key. */
13 typedef struct kh_str_s git_strmap
;
16 * Allocate a new string map.
18 * @param out Pointer to the map that shall be allocated.
19 * @return 0 on success, an error code if allocation has failed.
21 int git_strmap_new(git_strmap
**out
);
24 * Free memory associated with the map.
26 * Note that this function will _not_ free keys or values added
29 * @param map Pointer to the map that is to be free'd. May be
32 void git_strmap_free(git_strmap
*map
);
35 * Clear all entries from the map.
37 * This function will remove all entries from the associated map.
38 * Memory associated with it will not be released, though.
40 * @param map Pointer to the map that shall be cleared. May be
43 void git_strmap_clear(git_strmap
*map
);
46 * Return the number of elements in the map.
48 * @parameter map map containing the elements
49 * @return number of elements in the map
51 size_t git_strmap_size(git_strmap
*map
);
54 * Return value associated with the given key.
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
60 void *git_strmap_get(git_strmap
*map
, const char *key
);
63 * Set the entry for key to value.
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.
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
75 int git_strmap_set(git_strmap
*map
, const char *key
, void *value
);
78 * Delete an entry from the map.
80 * Delete the given key and its value from the map. If no such
81 * key exists, this will do nothing.
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
89 int git_strmap_delete(git_strmap
*map
, const char *key
);
92 * Check whether a key exists in the given map.
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
98 int git_strmap_exists(git_strmap
*map
, const char *key
);
101 * Iterate over entries of the map.
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
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
115 * @return `0` if the next entry was correctly retrieved.
116 * GIT_ITEROVER if no entries are left. A negative error
119 int git_strmap_iterate(void **value
, git_strmap
*map
, size_t *iter
, const char **key
);
121 #define git_strmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
122 while (git_strmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
126 #define git_strmap_foreach_value(h, vvar, code) { size_t __i = 0; \
127 while (git_strmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \