-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libidset: add embedded idset expansion support
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
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |