[Pkg-libvirt-commits] [libguestfs] 34/87: utils: Add utility function to split string into list of strings.
Hilko Bengen
bengen at moszumanska.debian.org
Wed Feb 19 21:10:07 UTC 2014
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to branch debian
in repository libguestfs.
commit 8999eb125d2f3c224a1c77562cb71565061b1924
Author: Richard W.M. Jones <rjones at redhat.com>
Date: Sat Jan 18 15:25:26 2014 +0000
utils: Add utility function to split string into list of strings.
(cherry picked from commit 89a617c07d83ac4517d62810c1eb597fa54a5d60)
---
src/guestfs-internal-frontend.h | 1 +
src/utils.c | 56 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/src/guestfs-internal-frontend.h b/src/guestfs-internal-frontend.h
index e2cb0a5..3b0a480 100644
--- a/src/guestfs-internal-frontend.h
+++ b/src/guestfs-internal-frontend.h
@@ -97,6 +97,7 @@ extern size_t guestfs___count_strings (char *const *);
extern char *guestfs___concat_strings (char *const *);
extern char **guestfs___copy_string_list (char *const *);
extern char *guestfs___join_strings (const char *sep, char *const *);
+extern char **guestfs___split_string (char sep, const char *);
extern char *guestfs___exit_status_to_string (int status, const char *cmd_name, char *buffer, size_t buflen);
extern int guestfs___random_string (char *ret, size_t len);
extern char *guestfs___drive_name (size_t index, char *ret);
diff --git a/src/utils.c b/src/utils.c
index 3c30608..5eebdf6 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -127,6 +127,62 @@ guestfs___join_strings (const char *sep, char *const *argv)
return r;
}
+/* Split string at separator character 'sep', returning the list of
+ * strings. Returns NULL on memory allocation failure.
+ *
+ * Note (assuming sep is ':'):
+ * str == NULL => aborts
+ * str == "" => returns []
+ * str == "abc" => returns ["abc"]
+ * str == ":" => returns ["", ""]
+ */
+char **
+guestfs___split_string (char sep, const char *str)
+{
+ size_t i, n, c, len = strlen (str);
+ char reject[2] = { sep, '\0' };
+ char **ret;
+
+ /* We have to handle the empty string case differently else the code
+ * below will return [""].
+ */
+ if (str[0] == '\0') {
+ ret = malloc (1 * sizeof (char *));
+ if (!ret)
+ return NULL;
+ ret[0] = NULL;
+ return ret;
+ }
+
+ for (n = i = 0; i < len; ++i)
+ if (str[i] == sep)
+ n++;
+
+ /* We always return a list of length 1 + (# separator characters).
+ * We also have to add a trailing NULL.
+ */
+ ret = malloc ((n+2) * sizeof (char *));
+ if (!ret)
+ return NULL;
+ ret[n+1] = NULL;
+
+ for (n = i = 0; i <= len; ++i, ++n) {
+ c = strcspn (&str[i], reject);
+ ret[n] = strndup (&str[i], c);
+ if (ret[n] == NULL) {
+ for (i = 0; i < n; ++i)
+ free (ret[i]);
+ free (ret);
+ return NULL;
+ }
+ i += c;
+ if (str[i] == '\0') /* end of string? */
+ break;
+ }
+
+ return ret;
+}
+
/* Translate a wait/system exit status into a printable string. The
* string must be freed by the caller.
*/
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git
More information about the Pkg-libvirt-commits
mailing list