vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 8f01a399e14f27b4a92d880dd8f3b841c896587d parent afeaadaf05f625339eff865237237d966fe7c5cc Author: Marc André Tanner <mat@brain-dump.org> Date: Thu, 29 Dec 2016 01:34:58 +0100 map: implement map_first Diffstat:
| M | map.c | | | 22 | ++++++++++++++++++++++ |
| M | map.h | | | 2 | ++ |
2 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/map.c b/map.c @@ -232,6 +232,28 @@ void map_iterate(const Map *map, bool (*handle)(const char *, void *, void *), c iterate(*map, handle, data); } +typedef struct { + const char *key; + void *value; +} KeyValue; + +static bool first(const char *key, void *value, void *data) +{ + KeyValue *kv = data; + kv->key = key; + kv->value = value; + return false; +} + +void *map_first(const Map *map, const char **key) +{ + KeyValue kv = { 0 }; + map_iterate(map, first, &kv); + if (key && kv.key) + *key = kv.key; + return kv.value; +} + const Map *map_prefix(const Map *map, const char *prefix) { const Map *n, *top; diff --git a/map.h b/map.h @@ -9,6 +9,8 @@ typedef struct Map Map; Map *map_new(void); /* Retrieves a value, or NULL if it isn't in the map */ void *map_get(const Map*, const char *key); +/* Get first element of the map, or NULL if empty */ +void *map_first(const Map*, const char **key); /* Returns the corresponding value if the given prefix is unique. * Otherwise NULL, if no such prefix exists then errno is set to ENOENT. */ void *map_closest(const Map*, const char *prefix);