---
src/libvirt_private.syms | 1 +
src/util/viruuid.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++
src/util/viruuid.h | 1 +
3 files changed, 81 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a958d94..4127f14 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2077,6 +2077,7 @@ virValidateWWN;
# util/viruuid.h
virGetHostUUID;
+virSearchUuid;
virSetHostUUIDStr;
virUUIDFormat;
virUUIDGenerate;
diff --git a/src/util/viruuid.c b/src/util/viruuid.c
index c5fa9a8..3786393 100644
--- a/src/util/viruuid.c
+++ b/src/util/viruuid.c
@@ -34,6 +34,7 @@
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
+#include <regex.h>
#include "c-ctype.h"
#include "internal.h"
@@ -43,11 +44,14 @@
#include "viralloc.h"
#include "virfile.h"
#include "virrandom.h"
+#include "virstring.h"
#ifndef ENODATA
# define ENODATA EIO
#endif
+#define VIR_FROM_THIS VIR_FROM_NONE
+
static unsigned char host_uuid[VIR_UUID_BUFLEN];
static int
@@ -333,3 +337,78 @@ int virGetHostUUID(unsigned char *uuid)
return ret;
}
+
+
+/**
+ * virSearchUuid:
+ * Return the nth occurrence of a substring in sourceString which matches an uuid
pattern
+ * If there is no substring, ret is not modified
+ *
+ * @sourceString: String to parse
+ * @occurrence: We will return the nth occurrence of uuid in substring, if equals to 0
(or negative), will return the first occurence
+ * @ret: nth occurrence substring matching an uuid pattern
+ * @code
+ char *source = "6853a496-1c10-472e-867a-8244937bd6f0
773ab075-4cd7-4fc2-8b6e-21c84e9cb391 bbb3c75c-d60f-43b0-b802-fd56b84a4222
60c04aa1-0375-4654-8d9f-e149d9885273 4548d465-9891-4c34-a184-3b1c34a26aa8";
+ char *ret1=NULL;
+ char *ret2=NULL;
+ char *ret3=NULL;
+ char *ret4=NULL;
+ virSearchUuid(source, 4,&ret1); //ret1 =
"60c04aa1-0375-4654-8d9f-e149d9885273"
+ virSearchUuid(source, 0,&ret2); //ret2 =
"6853a496-1c10-472e-867a-8244937bd6f0"
+ virSearchUuid(source, 1,&ret3); //ret3 =
"6853a496-1c10-472e-867a-8244937bd6f0"
+ virSearchUuid(source, -4,&ret4); //ret4 =
"6853a496-1c10-472e-867a-8244937bd6f0"
+ * @endcode
+ */
+
+char **
+virSearchUuid(const char *sourceString, int occurrence,char **ret)
+{
+ int position = ((occurrence -1) > 0) ? (occurrence -1) : 0;
+
+ const char *uuidRegex =
"([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})";
+ regex_t pregUuidBracket;
+ size_t i = 0;
+ size_t nmatch = 0;
+ regmatch_t *pmatch = NULL;
+ if (regcomp(&pregUuidBracket, uuidRegex, REG_EXTENDED) != 0) {
+ VIR_DEBUG("Error while compiling regular expression");
+ goto cleanup;
+ }
+ nmatch = pregUuidBracket.re_nsub;
+ if (VIR_ALLOC_N(pmatch, nmatch) != 0) {
+ virReportOOMError();
+ goto cleanup;
+ }
+ while (i < (position+1)) {
+ if (regexec(&pregUuidBracket, sourceString, nmatch, pmatch, 0) == 0) {
+ char *substring = NULL;
+ int start = pmatch[0].rm_so;
+ int end = pmatch[0].rm_eo;
+ size_t size = end - start;
+ if (VIR_ALLOC_N(substring, (size + 1)) != 0) {
+ virReportOOMError();
+ goto cleanup;
+ }
+ if (substring) {
+ if (virStrncpy(substring, &sourceString[start], size, size + 1)) {
+ substring[size] = '\0';
+ if (VIR_STRDUP(*ret, substring) < 0) {
+ VIR_DEBUG("cannot duplicate %s", substring);
+ goto cleanup;
+ }
+ }
+ VIR_FREE(substring);
+ }
+ sourceString = &sourceString[end];
+ }
+ ++i;
+ }
+ regfree(&pregUuidBracket);
+ VIR_FREE(pmatch);
+ return ret;
+
+cleanup:
+ regfree(&pregUuidBracket);
+ VIR_FREE(pmatch);
+ return ret;
+}
diff --git a/src/util/viruuid.h b/src/util/viruuid.h
index bebd338..276e85f 100644
--- a/src/util/viruuid.h
+++ b/src/util/viruuid.h
@@ -40,4 +40,5 @@ int virUUIDParse(const char *uuidstr,
const char *virUUIDFormat(const unsigned char *uuid,
char *uuidstr) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+char ** virSearchUuid(const char *sourceString, int occurrence, char **ret);
#endif /* __VIR_UUID_H__ */
--
1.7.10.4