summaryrefslogtreecommitdiff
path: root/lib/frrstr.h
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2018-05-11 15:32:06 -0400
committerQuentin Young <qlyoung@cumulusnetworks.com>2018-06-06 16:15:34 +0000
commitfe011935cdeda14b61297e72bc30eae46ccd4f55 (patch)
treeca89a7f40f70a5e5477298d1555d127f94a8b46f /lib/frrstr.h
parenta86dc99646ba282f8e4fcfac180b3d6b706429bf (diff)
lib: add string utilities
I see lots of the same code being copy-pasted and slightly tweaked for string processing all over the codebase. Time to start aggregating these pieces into something consistent and correct. Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/frrstr.h')
-rw-r--r--lib/frrstr.h86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/frrstr.h b/lib/frrstr.h
new file mode 100644
index 0000000000..f5edbf7b46
--- /dev/null
+++ b/lib/frrstr.h
@@ -0,0 +1,86 @@
+/*
+ * FRR string processing utilities.
+ * Copyright (C) 2018 Cumulus Networks, Inc.
+ * Quentin Young
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef _FRRSTR_H_
+#define _FRRSTR_H_
+
+#include <sys/types.h>
+#include <regex.h>
+
+#include "vector.h"
+
+/*
+ * Tokenizes a string, storing tokens in a vector. Whitespace is ignored.
+ * Delimiter characters are not included.
+ *
+ * string
+ * The string to split
+ *
+ * delimiter
+ * Delimiter string, as used in strsep()
+ *
+ * Returns:
+ * The split string. Each token is allocated with MTYPE_TMP.
+ */
+void frrstr_split(const char *string, const char *delimiter, char ***result,
+ int *argc);
+vector frrstr_split_vec(const char *string, const char *delimiter);
+
+/*
+ * Concatenate string array into a single string.
+ *
+ * argv
+ * array of string pointers to concatenate
+ *
+ * argc
+ * array length
+ *
+ * join
+ * string to insert between each part, or NULL for nothing
+ *
+ * Returns:
+ * the joined string, allocated with MTYPE_TMP
+ */
+char *frrstr_join(const char **parts, int argc, const char *join);
+char *frrstr_join_vec(vector v, const char *join);
+
+/*
+ * Filter string vector.
+ * Removes lines that do not contain a match for the provided regex.
+ *
+ * v
+ * The vector to filter.
+ *
+ * filter
+ * Regex to filter with.
+ */
+void frrstr_filter_vec(vector v, regex_t *filter);
+
+/*
+ * Free allocated string vector.
+ * Assumes each item is allocated with MTYPE_TMP.
+ *
+ * v
+ * the vector to free
+ */
+void frrstr_strvec_free(vector v);
+
+
+#endif /* _FRRSTR_H_ */