Skip to content

Commit

Permalink
libidset: add embedded idset expansion support
Browse files Browse the repository at this point in the history
Add two functions that simplify expansion of strings that have
embedded idsets.  For example we can represent the set of
hostnames "fluke1", "fluke2", and "fluke3" more succinctly
with an embedded idset, e.g. "fluke[1-3]".

When presented with a string containing an embedded idset,
reproducing the original set of stirngs is simplified with
the following functions:

/* Decode the first bracketed idset embedded within 's'.
 */
struct idset *idset_decode_embedded (const char *s);

/* Replace the first bracketed idset embedded in 'fmt' with 'id'.
 */
int idset_format (char *buf,
                  size_t bufsz,
                  const char *fmt,
                  unsigned int id)
  • Loading branch information
garlick committed Dec 19, 2019
1 parent 193859f commit 1ffadf2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/common/libidset/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/common/libidset/idset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/*
Expand Down
63 changes: 63 additions & 0 deletions src/common/libidset/idset_format.c
Original file line number Diff line number Diff line change
@@ -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 <sys/param.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>

#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
*/

0 comments on commit 1ffadf2

Please sign in to comment.