diff --git a/src/common/libidset/Makefile.am b/src/common/libidset/Makefile.am index 5825b8e14a35..984fbce398f5 100644 --- a/src/common/libidset/Makefile.am +++ b/src/common/libidset/Makefile.am @@ -16,7 +16,8 @@ fluxinclude_HEADERS = idset.h libidset_la_SOURCES = idset.c \ idset_private.h \ idset_decode.c \ - idset_encode.c + idset_encode.c \ + idset_format.c libidset_la_CPPFLAGS = \ $(AM_CPPFLAGS) diff --git a/src/common/libidset/idset.h b/src/common/libidset/idset.h index 83993f02836b..2eecc9bc5147 100644 --- a/src/common/libidset/idset.h +++ b/src/common/libidset/idset.h @@ -100,6 +100,17 @@ size_t idset_count (const struct idset *idset); */ bool idset_equal (const struct idset *set1, const struct idset *set2); +/* Substitute a numerical 'id' for bracketed text (e.g. an embedded idset) + * in 'fmt', placing result in 'buf'. Returns 0 on success, -1 on failure + * with errno set. + */ +int idset_format (char *buf, size_t bufsz, const char *fmt, unsigned int id); + +/* Decode the first bracketed idset embedded in a string. + * Returns idset on success, or NULL on failure with errno set. + */ +struct idset *idset_decode_embedded (const char *s); + #endif /* !FLUX_IDSET_H */ /* diff --git a/src/common/libidset/idset_format.c b/src/common/libidset/idset_format.c new file mode 100644 index 000000000000..a3f1e7d2ae39 --- /dev/null +++ b/src/common/libidset/idset_format.c @@ -0,0 +1,63 @@ +/************************************************************\ + * Copyright 2014 Lawrence Livermore National Security, LLC + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) + * + * This file is part of the Flux resource manager framework. + * For details, see https://github.com/flux-framework. + * + * SPDX-License-Identifier: LGPL-3.0 +\************************************************************/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include +#include +#include +#include +#include +#include +#include + +#include "idset.h" +#include "idset_private.h" + +int idset_format (char *buf, size_t bufsz, const char *fmt, unsigned int id) +{ + char *p, *q; + + if (!buf || !fmt) { + errno = EINVAL; + return -1; + } + if (!(p = strchr (fmt, '[')) || !(q = strchr (p + 1, ']'))) { + errno = EINVAL; + return -1; + } + if (snprintf (buf, + bufsz, + "%.*s%u%s", + (int)(p - fmt), + fmt, + id, + q + 1) >= bufsz) { + errno = EOVERFLOW; + return -1; + } + return 0; +} + +struct idset *idset_decode_embedded (const char *s) +{ + char *p, *q; + + if (!s || !(p = strchr (s, '[')) || !(q = strchr (p + 1, ']'))) { + errno = EINVAL; + return NULL; + } + return idset_ndecode (p, q - p + 1); +} + +/* + * vi:tabstop=4 shiftwidth=4 expandtab + */