summaryrefslogtreecommitdiff
path: root/firmware/common/strptokspn.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/common/strptokspn.c')
-rw-r--r--firmware/common/strptokspn.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/firmware/common/strptokspn.c b/firmware/common/strptokspn.c
new file mode 100644
index 0000000000..f4b92c0712
--- /dev/null
+++ b/firmware/common/strptokspn.c
@@ -0,0 +1,88 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2022 by William WIlgus
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. *
19 ****************************************************************************/
20
21#include "config.h"
22
23#include <stddef.h>
24#include <string.h>
25#include "strtok_r.h"
26/* strptokspn_r is a custom implementation of strtok_r that does NOT modify
27 * the source string.
28 *
29 * strptokspn_r reads ptr as a series of zero or more tokens,
30 * and sep as delimiters of the tokens
31 * The tokens can be separated by one or more of the delimiters
32 * first call searches for the first token skipping over any leading delimiters.
33 * Returns pointer to first token
34 * Pointer *len contains the span to the first delimeter
35 * (this would be the resulting strlen had token actually been NULL terminated)
36 * Pointer **end contains pointer to first character after the last delimeter
37 *
38 * When strptokspn_r is called with a ptr == NULL, the next token is read from
39 * Pointer **end
40 *
41 * Note the returned token is NOT NULL terminated by the function as in strtok_r
42 * However the caller can use ret[len+1] = '\0'; to emulate a call to strtok_r
43*/
44
45const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end)
46{
47 *len = 0;
48 if (!ptr)
49 /* we got NULL input so then we get our last position instead */
50 ptr = *end;
51
52 /* pass all letters that are including in the separator string */
53 while (*ptr && strchr(sep, *ptr))
54 ++ptr;
55
56 if (*ptr) {
57 /* so this is where the next piece of string starts */
58 const char *start = ptr;
59
60 /* set the end pointer to the first byte after the start */
61 *end = start + 1;
62
63 /* scan through the string to find where it ends, it ends on a
64 null byte or a character that exists in the separator string */
65 while (**end && !strchr(sep, **end))
66 ++*end;
67 *len = (*end - start) - 1; /* this would be the string len if there actually was a NULL */
68 if (**end) { /* the end is not a null byte */
69 ++*end; /* advance last pointer to beyond the match */
70 }
71
72 return start; /* return the position where the string starts */
73 }
74
75 /* we ended up on a null byte, there are no more strings to find! */
76 return NULL;
77}
78
79#if !defined(HAVE_STRTOK_R)
80char * strtok_r(char *ptr, const char *sep, char **end)
81{
82 size_t len;
83 char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end);
84 if (ret)
85 ret[len + 1] = '\0';
86 return ret;
87}
88#endif