1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-03 22:56:33 +02:00

common: New function map_static_strings

* common/mapstrings.c (struct intmapping_s): New.
(map_static_strings): New.
* common/stringhelp.c (do_strconcat): Rename to ...
(vstrconcat): this and make global.

* common/t-mapstrings.c (test_map_static_strings): New test.
This commit is contained in:
Werner Koch 2022-03-18 13:47:10 +01:00
parent 8631d4cfe2
commit 449d2fbcde
No known key found for this signature in database
GPG key ID: E3FDFF218E45B72B
4 changed files with 86 additions and 5 deletions

View file

@ -69,6 +69,18 @@ struct mapping_s
static struct mapping_s *mappings;
/* Similar to above but using two integers and a domain as key. */
struct intmapping_s
{
struct intmapping_s *next;
int key1;
int key2;
const char *string;
char domain[1];
};
static struct intmapping_s *intmappings;
/* If STRING has already been mapped, return the mapped string. If
not return NULL. */
static const char *
@ -166,3 +178,40 @@ map_static_macro_string (const char *string)
return store_mapping (string, p);
}
/* If a list of strings has already been mapped to a the tuple
* (DOMAIN,KEY1,KEY2) return that string. If not, create a mapping
* made up of the concatenation of the given strings. */
const char *
map_static_strings (const char *domain, int key1, int key2,
const char *string1, ...)
{
va_list arg_ptr;
struct intmapping_s *m;
if (!string1 || !domain)
return "";
for (m = intmappings; m; m = m->next)
if (m->key1 == key1 && m->key2 == key2 && !strcmp (domain, m->domain))
return m->string;
m = xmalloc (sizeof *m + strlen (domain));
strcpy (m->domain, domain);
m->key1 = key1;
m->key2 = key2;
va_start (arg_ptr, string1);
m->string = vstrconcat (string1, arg_ptr);
va_end (arg_ptr);
if (!m->string)
log_fatal ("map_static_strings failed: %s\n", strerror (errno));
gpgrt_annotate_leaked_object (m->string);
gpgrt_annotate_leaked_object (m);
m->next = intmappings;
intmappings = m;
return m->string;
}