forked from u-blox/ubxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu_port_clib_strtok_r.c
88 lines (81 loc) · 2.89 KB
/
u_port_clib_strtok_r.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright 2019-2022 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file
* @brief An implementation of the C library function strok_r().
*/
#include "string.h"
#include "u_port_clib_platform_specific.h"
/** strtok_r(): split a string into sub-strings at the
* given delimiter by modifying the string in-place. This function
* is thread-safe.
*
* @param pStr on first call this should be a pointer to
* the string to tokenise. On subsequent calls
* it must be NULL to return further tokens.
* The contents of pStr are modified by this
* function (NULLs being written to delineate
* sub-strings).
* @param pDelimiters the delimiters to tokenise based on.
* @param ppSave a pointer to a pointer that can be used to
* save context between calls.
* @return the token, NULL terminated.
*/
char *strtok_r(char *pStr, const char *pDelimiters, char **ppSave)
{
char *pEnd;
if (pStr == NULL) {
// On subsequent calls, with pStr NULL,
// set pStr to start from the saved position
pStr = *ppSave;
}
if (*pStr != '\0') {
// Find the position of any delimiters
// at the beginning of the string/saved pointer
pStr += strspn(pStr, pDelimiters);
if (*pStr != '\0') {
// Having found a token, find the start
// of the next one
pEnd = pStr + strcspn(pStr, pDelimiters);
if (*pEnd != '\0') {
// Found one: write a NULL to the position
// of the start of the next token
// in order to make sure the returned
// token is terminated and make the
// saved pointer point beyond it for
// next time.
*pEnd = '\0';
*ppSave = ++pEnd;
} else {
// No next one, save the end
// for next time
*ppSave = pEnd;
}
} else {
// None at all, save it and
// set the return value to NULL
*ppSave = pStr;
pStr = NULL;
}
} else {
// If we're at the terminator,
// save where we are and set the
// return value to NULL
*ppSave = pStr;
pStr = NULL;
}
return pStr;
}
// End of file