diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-12-15 21:40:26 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-12-15 21:40:26 +0200 |
commit | 42015f0462e8f1b2292a48455ce740a627c14025 (patch) | |
tree | 78352fb16802115d5f16470e6aea1419c563f090 /lib/forlan.c | |
parent | e60288d595ec405c5d416e5da54b516e65f8a8c5 (diff) | |
download | eclat-42015f0462e8f1b2292a48455ce740a627c14025.tar.gz eclat-42015f0462e8f1b2292a48455ce740a627c14025.tar.bz2 |
Implement map lookup functions in forlan.
* lib/forlan.c: New functions "has_map" and "lookup".
* lib/map.c (eclat_map_open): Don't issue error message
if failed to open the map. Leave that to the caller.
* src/util.c (translate_ids)
(translate_resource_ids): Issue an error message if
eclat_map_open fails.
Diffstat (limited to 'lib/forlan.c')
-rw-r--r-- | lib/forlan.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/forlan.c b/lib/forlan.c index 828fb09..cd5d08a 100644 --- a/lib/forlan.c +++ b/lib/forlan.c @@ -1068,7 +1068,50 @@ func_timestamp(forlan_eval_env_t env, struct grecs_list *list) env->retval.v.string = buf; } +static void +func_lookup(forlan_eval_env_t env, struct grecs_list *list) +{ + struct forlan_value *p; + const char *key; + char *mapname; + struct eclat_map *map; + int dir; + char *q; + + p = list->head->data; + + dir = eclat_map_name_split(p->v.string, &mapname, &q); + if (dir == -1) + die(EX_USAGE, "map %s: bad qualifier %s", p->v.string, q); + + map = eclat_map_lookup(mapname); + free(mapname); + + p = list->head->next->data; + key = p->v.string; + + if (map) { + if (eclat_map_open(map) == eclat_map_ok && + eclat_map_get(map, dir, key, &env->retval.v.string) + == eclat_map_ok) + return; + } else + err("no such map: %s", mapname); + env->retval.v.string = grecs_strdup(key); +} + +static void +func_has_map(forlan_eval_env_t env, struct grecs_list *list) +{ + struct forlan_value *p; + struct eclat_map *map; + + p = list->head->data; + map = eclat_map_lookup(p->v.string); + env->retval.v.num = map && eclat_map_open(map) == eclat_map_ok; +} + static struct forlan_function functab[] = { { "dump", forlan_value_void, "n", 1, 1, func_dump }, { "print", forlan_value_void, "", 1, -1, func_print }, @@ -1079,6 +1122,8 @@ static struct forlan_function functab[] = { { "exit", forlan_value_void, "s", 1, 1, func_exit }, { "empty", forlan_value_boolean, "n", 1, 1, func_empty }, { "timestamp", forlan_value_literal, "s", 1, 1, func_timestamp }, + { "lookup", forlan_value_literal, "ss", 2, 2, func_lookup }, + { "has_map",forlan_value_boolean, "s", 1, 1, func_has_map }, { NULL } }; |