summaryrefslogtreecommitdiffstats
path: root/lib/strutils.c
diff options
context:
space:
mode:
authorKarel Zak2012-05-03 17:28:44 +0200
committerKarel Zak2012-05-03 17:28:44 +0200
commit5ef167714e58132b969cfa4c185ae0159705ca0f (patch)
tree1ba601a1f1157f3a554a4b74c0b9a16126a91926 /lib/strutils.c
parentlsblk: fix /sys/.../ro usage (diff)
downloadkernel-qcow2-util-linux-5ef167714e58132b969cfa4c185ae0159705ca0f.tar.gz
kernel-qcow2-util-linux-5ef167714e58132b969cfa4c185ae0159705ca0f.tar.xz
kernel-qcow2-util-linux-5ef167714e58132b969cfa4c185ae0159705ca0f.zip
lib/strutils: add string_to_bitmask()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/strutils.c')
-rw-r--r--lib/strutils.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/strutils.c b/lib/strutils.c
index 372558acd..bdcdef342 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -470,6 +470,49 @@ int string_to_bitarray(const char *list,
}
/*
+ * LIST ::= <item> [, <item>]
+ *
+ * The <item> is translated to 'id' by name2flag() function and the flags is
+ * set to the 'mask'
+*
+ * Returns: 0 on success, <0 on error.
+ */
+int string_to_bitmask(const char *list,
+ unsigned long *mask,
+ long (*name2flag)(const char *, size_t))
+{
+ const char *begin = NULL, *p;
+
+ if (!list || !name2flag || !mask)
+ return -EINVAL;
+
+ for (p = list; p && *p; p++) {
+ const char *end = NULL;
+ long flag;
+
+ if (!begin)
+ begin = p; /* begin of the level name */
+ if (*p == ',')
+ end = p; /* terminate the name */
+ if (*(p + 1) == '\0')
+ end = p + 1; /* end of string */
+ if (!begin || !end)
+ continue;
+ if (end <= begin)
+ return -1;
+
+ flag = name2flag(begin, end - begin);
+ if (flag < 0)
+ return flag; /* error */
+ *mask |= flag;
+ begin = NULL;
+ if (end && !*end)
+ break;
+ }
+ return 0;
+}
+
+/*
* Parse the lower and higher values in a string containing
* "lower:higher" or "lower-higher" format. Note that either
* the lower or the higher values may be missing, and the def