summaryrefslogtreecommitdiff
path: root/libdm/libdm-string.c
diff options
context:
space:
mode:
authorAlasdair Kergon <agk@redhat.com>2006-08-21 12:07:03 +0000
committerAlasdair Kergon <agk@redhat.com>2006-08-21 12:07:03 +0000
commit7d7736b7b41640bee3fd6803475386d2108c6b6e (patch)
treea7f4dd30698dadf08b121706c95a87b3ec3a7bda /libdm/libdm-string.c
parent4af059bd85d4cd87438dd50329c456e260915459 (diff)
downloadlvm2-7d7736b7b41640bee3fd6803475386d2108c6b6e.tar.gz
Add dm_split_words() and dm_split_lvm_name() to libdevmapper.
Diffstat (limited to 'libdm/libdm-string.c')
-rw-r--r--libdm/libdm-string.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/libdm/libdm-string.c b/libdm/libdm-string.c
new file mode 100644
index 000000000..4ce660807
--- /dev/null
+++ b/libdm/libdm-string.c
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of the device-mapper userspace tools.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "lib.h"
+#include "libdevmapper.h"
+
+#include <ctype.h>
+
+/*
+ * consume characters while they match the predicate function.
+ */
+static char *_consume(char *buffer, int (*fn) (int))
+{
+ while (*buffer && fn(*buffer))
+ buffer++;
+
+ return buffer;
+}
+
+static int _isword(int c)
+{
+ return !isspace(c);
+}
+
+/*
+ * Split buffer into NULL-separated words in argv.
+ * Returns number of words.
+ */
+int dm_split_words(char *buffer, unsigned max, unsigned ignore_comments,
+ char **argv)
+{
+ unsigned arg;
+
+ for (arg = 0; arg < max; arg++) {
+ buffer = _consume(buffer, isspace);
+ if (!*buffer)
+ break;
+
+ argv[arg] = buffer;
+ buffer = _consume(buffer, _isword);
+
+ if (*buffer) {
+ *buffer = '\0';
+ buffer++;
+ }
+ }
+
+ return arg;
+}
+
+/*
+ * Remove hyphen quoting from a component of a name.
+ * NULL-terminates the component and returns start of next component.
+ */
+static char *_unquote(char *component)
+{
+ char *c = component;
+ char *o = c;
+ char *r;
+
+ while (*c) {
+ if (*(c + 1)) {
+ if (*c == '-') {
+ if (*(c + 1) == '-')
+ c++;
+ else
+ break;
+ }
+ }
+ *o = *c;
+ o++;
+ c++;
+ }
+
+ r = (*c) ? c + 1 : c;
+ *o = '\0';
+
+ return r;
+}
+
+int dm_split_lvm_name(struct dm_pool *mem, const char *dmname,
+ char **vgname, char **lvname, char **layer)
+{
+ if (!(*vgname = dm_pool_strdup(mem, dmname)))
+ return 0;
+
+ _unquote(*layer = _unquote(*lvname = _unquote(*vgname)));
+
+ return 1;
+}