Introduce virBitmapParseSeparator

This will be used for the caller that needs to specify a separator.
Currently identical to virBitmapParse.

Also change one test case to use the new function.
This commit is contained in:
Ján Tomko 2016-06-17 14:38:11 +02:00
parent ba7c026dab
commit d728689d9b
5 changed files with 44 additions and 7 deletions

View File

@ -1207,6 +1207,7 @@ virBitmapNextClearBit;
virBitmapNextSetBit;
virBitmapOverlaps;
virBitmapParse;
virBitmapParseSeparator;
virBitmapSetAll;
virBitmapSetBit;
virBitmapSetBitExpand;

View File

@ -400,7 +400,7 @@ char *virBitmapFormat(virBitmapPtr bitmap)
}
/**
* virBitmapParse:
* virBitmapParseSeparator:
* @str: points to a string representing a human-readable bitmap
* @terminator: character separating the bitmap to parse
* @bitmap: a bitmap created from @str
@ -422,10 +422,10 @@ char *virBitmapFormat(virBitmapPtr bitmap)
* Returns 0 on success, or -1 in case of error.
*/
int
virBitmapParse(const char *str,
char terminator,
virBitmapPtr *bitmap,
size_t bitmapSize)
virBitmapParseSeparator(const char *str,
char terminator,
virBitmapPtr *bitmap,
size_t bitmapSize)
{
bool neg = false;
const char *cur = str;
@ -519,6 +519,37 @@ virBitmapParse(const char *str,
return -1;
}
/**
* virBitmapParse:
* @str: points to a string representing a human-readable bitmap
* @terminator: character separating the bitmap to parse
* @bitmap: a bitmap created from @str
* @bitmapSize: the upper limit of num of bits in created bitmap
*
* This function is the counterpart of virBitmapFormat. This function creates
* a bitmap, in which bits are set according to the content of @str.
*
* @str is a comma separated string of fields N, which means a number of bit
* to set, and ^N, which means to unset the bit, and N-M for ranges of bits
* to set.
*
* To allow parsing of bitmaps within larger strings it is possible to set
* a termination character in the argument @terminator. When the character
* in @terminator is encountered in @str, the parsing of the bitmap stops.
* Pass 0 as @terminator if it is not needed. Whitespace characters may not
* be used as terminators.
*
* Returns 0 on success, or -1 in case of error.
*/
int
virBitmapParse(const char *str,
char terminator,
virBitmapPtr *bitmap,
size_t bitmapSize)
{
return virBitmapParseSeparator(str, terminator, bitmap, bitmapSize);
}
/**
* virBitmapNewCopy:
* @src: the source bitmap.

View File

@ -90,6 +90,11 @@ int virBitmapParse(const char *str,
virBitmapPtr *bitmap,
size_t bitmapSize)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
int
virBitmapParseSeparator(const char *str,
char terminator,
virBitmapPtr *bitmap,
size_t bitmapSize);
virBitmapPtr virBitmapNewCopy(virBitmapPtr src) ATTRIBUTE_NONNULL(1);

View File

@ -1049,7 +1049,7 @@ sexpr_to_xend_topology(const struct sexpr *root, virCapsPtr caps)
if (!(cpuset = virBitmapNew(numCpus)))
goto error;
} else {
if (virBitmapParse(cur, 'n', &cpuset, numCpus) < 0)
if (virBitmapParseSeparator(cur, 'n', &cpuset, numCpus) < 0)
goto error;
nb_cpus = virBitmapCountBits(cpuset);

View File

@ -526,7 +526,7 @@ test10(const void *opaque ATTRIBUTE_UNUSED)
int ret = -1;
virBitmapPtr b1 = NULL, b2 = NULL, b3 = NULL, b4 = NULL;
if (virBitmapParse("0-3,5-8,11-15", 0, &b1, 20) < 0 ||
if (virBitmapParseSeparator("0-3,5-8,11-15f16", 'f', &b1, 20) < 0 ||
virBitmapParse("4,9,10,16-19", 0, &b2, 20) < 0 ||
virBitmapParse("15", 0, &b3, 20) < 0 ||
virBitmapParse("0,^0", 0, &b4, 20) < 0)