提交 c60a2713 编写于 作者: D Daniel P. Berrange

Introduce standard methods for sorting strings with qsort

Add virStringSortCompare and virStringSortRevCompare as
standard functions to use with qsort.
Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
上级 e821de2c
......@@ -1749,6 +1749,8 @@ virStringArrayHasString;
virStringFreeList;
virStringJoin;
virStringListLength;
virStringSortCompare;
virStringSortRevCompare;
virStringSplit;
virStrncpy;
virStrndup;
......
......@@ -486,16 +486,6 @@ error_out:
/*_syscall2(int, pivot_root, char *, newroot, const char *, oldroot)*/
extern int pivot_root(const char * new_root, const char * put_old);
static int lxcContainerChildMountSort(const void *a, const void *b)
{
const char **sa = (const char**)a;
const char **sb = (const char**)b;
/* Deliberately reversed args - we need to unmount deepest
children first */
return strcmp(*sb, *sa);
}
#ifndef MS_REC
# define MS_REC 16384
#endif
......@@ -547,7 +537,7 @@ static int lxcContainerGetSubtree(const char *prefix,
if (mounts)
qsort(mounts, nmounts, sizeof(mounts[0]),
lxcContainerChildMountSort);
virStringSortRevCompare);
ret = 0;
cleanup:
......@@ -816,7 +806,7 @@ static int lxcContainerSetReadOnly(void)
if (mounts)
qsort(mounts, nmounts, sizeof(mounts[0]),
lxcContainerChildMountSort);
virStringSortRevCompare);
for (i = 0; i < nmounts; i++) {
VIR_DEBUG("Bind readonly %s", mounts[i]);
......
......@@ -616,3 +616,32 @@ size_t virStringListLength(char **strings)
return i;
}
/**
* virStringSortCompare:
*
* A comparator function for sorting strings in
* normal order with qsort().
*/
int virStringSortCompare(const void *a, const void *b)
{
const char **sa = (const char**)a;
const char **sb = (const char**)b;
return strcmp(*sa, *sb);
}
/**
* virStringSortRevCompare:
*
* A comparator function for sorting strings in
* reverse order with qsort().
*/
int virStringSortRevCompare(const void *a, const void *b)
{
const char **sa = (const char**)a;
const char **sb = (const char**)b;
return strcmp(*sb, *sa);
}
......@@ -223,4 +223,7 @@ size_t virStringListLength(char **strings);
virAsprintfInternal(false, 0, NULL, NULL, 0, \
strp, __VA_ARGS__)
int virStringSortCompare(const void *a, const void *b);
int virStringSortRevCompare(const void *a, const void *b);
#endif /* __VIR_STRING_H__ */
......@@ -231,6 +231,49 @@ cleanup:
return ret;
}
static int
testStringSortCompare(const void *opaque ATTRIBUTE_UNUSED)
{
const char *randlist[] = {
"tasty", "astro", "goat", "chicken", "turducken",
};
const char *randrlist[] = {
"tasty", "astro", "goat", "chicken", "turducken",
};
const char *sortlist[] = {
"astro", "chicken", "goat", "tasty", "turducken",
};
const char *sortrlist[] = {
"turducken", "tasty", "goat", "chicken", "astro",
};
int ret = -1;
size_t i;
qsort(randlist, ARRAY_CARDINALITY(randlist), sizeof(randlist[0]),
virStringSortCompare);
qsort(randrlist, ARRAY_CARDINALITY(randrlist), sizeof(randrlist[0]),
virStringSortRevCompare);
for (i = 0; i < ARRAY_CARDINALITY(randlist); i++) {
if (STRNEQ(randlist[i], sortlist[i])) {
fprintf(stderr, "sortlist[%zu] '%s' != randlist[%zu] '%s'\n",
i, sortlist[i], i, randlist[i]);
goto cleanup;
}
if (STRNEQ(randrlist[i], sortrlist[i])) {
fprintf(stderr, "sortrlist[%zu] '%s' != randrlist[%zu] '%s'\n",
i, sortrlist[i], i, randrlist[i]);
goto cleanup;
}
}
ret = 0;
cleanup:
return ret;
}
static int
mymain(void)
{
......@@ -282,6 +325,9 @@ mymain(void)
if (virtTestRun("strdup", testStrndupNegative, NULL) < 0)
ret = -1;
if (virtTestRun("virStringSortCompare", testStringSortCompare, NULL) < 0)
ret = -1;
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册