common: New functions nvc_delete_named and nvc_get_string.

* common/name-value.c (nvc_delete_named): New.
(nvc_get_string): New.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2019-05-07 11:01:15 +02:00
parent c856ee7312
commit b5985d0ca2
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
3 changed files with 65 additions and 0 deletions

View File

@ -514,6 +514,21 @@ nvc_delete (nvc_t pk, nve_t entry)
nve_release (entry, pk->private_key_mode);
}
/* Delete the entries with NAME from PK. */
void
nvc_delete_named (nvc_t pk, const char *name)
{
nve_t e;
if (!valid_name (name))
return;
while ((e = nvc_lookup (pk, name)))
nvc_delete (pk, e);
}
/* Lookup and iteration. */
@ -563,6 +578,25 @@ nve_next_value (nve_t entry, const char *name)
return NULL;
}
/* Return the string for the first entry in NVC with NAME. If an
* entry with NAME is missing in NVC or its value is the empty string
* NULL is returned. Note that the The returned string is a pointer
* into NVC. */
const char *
nvc_get_string (nvc_t nvc, const char *name)
{
nve_t item;
if (!nvc)
return NULL;
item = nvc_lookup (nvc, name);
if (!item)
return NULL;
return nve_value (item);
}
/* Private key handling. */

View File

@ -72,6 +72,9 @@ nve_t nve_next (nve_t entry);
/* Get the next entry with the given name. */
nve_t nve_next_value (nve_t entry, const char *name);
/* Return the string for the first entry in NVC with NAME or NULL. */
const char *nvc_get_string (nvc_t nvc, const char *name);
/* Adding and modifying values. */
@ -88,6 +91,9 @@ gpg_error_t nvc_set (nvc_t pk, const char *name, const char *value);
/* Delete the given entry from PK. */
void nvc_delete (nvc_t pk, nve_t pke);
/* Delete the entries with NAME from PK. */
void nvc_delete_named (nvc_t pk, const char *name);
/* Private key handling. */

View File

@ -292,6 +292,7 @@ run_modification_tests (void)
{
gpg_error_t err;
nvc_t pk;
nve_t e;
gcry_sexp_t key;
char *buf;
@ -344,6 +345,30 @@ run_modification_tests (void)
assert (strcmp (buf, "") == 0);
xfree (buf);
/* Test whether we can delete an entry by name. */
err = nvc_add (pk, "Key:", "(3:foo)");
assert (!err);
e = nvc_lookup (pk, "Key:");
assert (e);
nvc_delete_named (pk, "Kez:"); /* Delete an inexistant name. */
e = nvc_lookup (pk, "Key:");
assert (e);
nvc_delete_named (pk, "Key:");
e = nvc_lookup (pk, "Key:");
assert (!e);
/* Ditto but now whether it deletes all entries with that name. We
* don't use "Key" because that name is special in private key mode. */
err = nvc_add (pk, "AKey:", "A-value");
assert (!err);
err = nvc_add (pk, "AKey:", "B-value");
assert (!err);
e = nvc_lookup (pk, "AKey:");
assert (e);
nvc_delete_named (pk, "AKey:");
e = nvc_lookup (pk, "AKey:");
assert (!e);
nvc_set (pk, "Foo:", "A really long value spanning across multiple lines"
" that has to be wrapped at a convenient space.");
buf = nvc_to_string (pk);