From: Martin Mares Date: Sun, 7 Jun 2015 15:34:51 +0000 (+0200) Subject: Dictionaries support encoding without creation of new entries X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=2f073368e7afea1a921006f67b0bb741b7332755;p=leo.git Dictionaries support encoding without creation of new entries --- diff --git a/dict.c b/dict.c index a3679e7..4542322 100644 --- a/dict.c +++ b/dict.c @@ -1,7 +1,7 @@ /* * Hic Est Leo -- Universal Dictionaries * - * (c) 2014 Martin Mares + * (c) 2014--2015 Martin Mares */ #include @@ -18,6 +18,7 @@ struct kv_map { #define HASH_NODE struct kv_map #define HASH_PREFIX(x) kv_map_##x #define HASH_KEY_STRING name +#define HASH_WANT_FIND #define HASH_WANT_LOOKUP #define HASH_AUTO_POOL 4096 #define HASH_ZERO_FILL @@ -61,3 +62,9 @@ u32 dict_encode(struct dict *d, const char *key) *GARY_PUSH(d->names) = k->name; return k->id; } + +u32 dict_encode_if_exists(struct dict *d, const char *key) +{ + struct kv_map *k = kv_map_find(d->hash, (char *) key); + return k ? k->id : 0; +} diff --git a/dict.h b/dict.h index f214bd7..49d681e 100644 --- a/dict.h +++ b/dict.h @@ -1,7 +1,7 @@ /* * Hic Est Leo -- Universal Dictionaries * - * (c) 2014 Martin Mares + * (c) 2014--2015 Martin Mares */ #ifndef _LEO_DICT_H @@ -24,6 +24,7 @@ static inline const char *dict_decode(struct dict *d, u32 id) } u32 dict_encode(struct dict *d, const char *key); +u32 dict_encode_if_exists(struct dict *d, const char *key); static inline u32 dict_size(struct dict *d) { diff --git a/style.h b/style.h index 83508cf..fb1ff8d 100644 --- a/style.h +++ b/style.h @@ -95,6 +95,11 @@ static inline layer_t style_layer_encode(const char *key) return dict_encode(&style_layer_dict, key); } +static inline layer_t style_layer_encode_if_exists(const char *key) +{ + return dict_encode_if_exists(&style_layer_dict, key); +} + static inline const char *style_layer_decode(layer_t id) { return dict_decode(&style_layer_dict, id);