From 07ef0f77af61c25d09f14733d731dc99cf44996e Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 20 Feb 2023 20:00:23 -0800 Subject: [PATCH] case-map: New function case_map_clone(). This will acquire its first user in an upcoming commit. --- src/data/case-map.c | 16 ++++++++++++++++ src/data/case-map.h | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/data/case-map.c b/src/data/case-map.c index 968b945af2..b47d38e1dd 100644 --- a/src/data/case-map.c +++ b/src/data/case-map.c @@ -72,6 +72,22 @@ insert_mapping (struct case_map *map, size_t from, size_t to) map->map[to] = from; } +/* Returns a copy of OLD, if OLD is nonnull, and otherwise returns NULL. */ +struct case_map * +case_map_clone (const struct case_map *old) +{ + if (!old) + return NULL; + + size_t n_values = caseproto_get_n_widths (old->proto); + struct case_map *new = xmalloc (sizeof *new); + *new = (struct case_map) { + .proto = caseproto_ref (old->proto), + .map = xmemdup (old->proto, n_values * sizeof *new->map), + }; + return new; +} + /* Destroys case map MAP. */ void case_map_destroy (struct case_map *map) diff --git a/src/data/case-map.h b/src/data/case-map.h index 0e9970a873..13e2ef190f 100644 --- a/src/data/case-map.h +++ b/src/data/case-map.h @@ -32,7 +32,7 @@ struct casewriter; struct ccase; struct dictionary; -struct case_map *case_map_create (void); +struct case_map *case_map_clone (const struct case_map *); void case_map_destroy (struct case_map *); struct ccase *case_map_execute (const struct case_map *, struct ccase *); -- 2.30.2