summaryrefslogtreecommitdiff
path: root/sa-extract/strutil.c
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2012-03-13 09:24:47 +0100
committerPatrick Simianer <p@simianer.de>2012-03-13 09:24:47 +0100
commitef6085e558e26c8819f1735425761103021b6470 (patch)
tree5cf70e4c48c64d838e1326b5a505c8c4061bff4a /sa-extract/strutil.c
parent10a232656a0c882b3b955d2bcfac138ce11e8a2e (diff)
parentdfbc278c1057555fda9312291c8024049e00b7d8 (diff)
merge with upstream
Diffstat (limited to 'sa-extract/strutil.c')
-rw-r--r--sa-extract/strutil.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/sa-extract/strutil.c b/sa-extract/strutil.c
new file mode 100644
index 00000000..456de87a
--- /dev/null
+++ b/sa-extract/strutil.c
@@ -0,0 +1,63 @@
+#include <string.h>
+#include <stdlib.h>
+
+/* Like strsep(3) except that the delimiter is a string, not a set of characters.
+*/
+char *strstrsep(char **stringp, const char *delim) {
+ char *match, *save;
+ save = *stringp;
+ if (*stringp == NULL)
+ return NULL;
+ match = strstr(*stringp, delim);
+ if (match == NULL) {
+ *stringp = NULL;
+ return save;
+ }
+ *match = '\0';
+ *stringp = match + strlen(delim);
+ return save;
+}
+
+static char **words = NULL;
+static int max_words;
+char **split(char *s, const char *delim, int *pn) {
+ int i;
+ char *tok, *rest;
+
+ if (words == NULL) {
+ max_words = 10;
+ words = malloc(max_words*sizeof(char *));
+ }
+ i = 0;
+ rest = s;
+ while ((tok = (delim ? strstrsep(&rest, delim) : strsep(&rest, " \t\n"))) != NULL) {
+ if (!delim && !*tok) // empty token
+ continue;
+ while (i+1 >= max_words) {
+ max_words *= 2;
+ words = realloc(words, max_words*sizeof(char *));
+ }
+ words[i] = tok;
+ i++;
+ }
+ words[i] = NULL;
+ if (pn != NULL)
+ *pn = i;
+ return words;
+}
+
+inline int isspace(char c) {
+ return (c == ' ' || c == '\t' || c == '\n');
+}
+
+char *strip(char *s) {
+ int n;
+ while (isspace(*s) && *s != '\0')
+ s++;
+ n = strlen(s);
+ while (n > 0 && isspace(s[n-1])) {
+ s[n-1] = '\0';
+ n--;
+ }
+ return s;
+}