-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
55 lines (47 loc) · 1.66 KB
/
util.c
File metadata and controls
55 lines (47 loc) · 1.66 KB
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
#include "util.h"
#include <string.h>
#include <stdio.h>
/**
* Retrieves the next token from a string.
*
* Parameters:
* - str_ptr: maintains context in the string, i.e., where the next token in the
* string will be. If the function returns token N, then str_ptr will be
* updated to point to token N+1. To initialize, declare a char * that points
* to the string being tokenized. The pointer will be updated after each
* successive call to next_token.
*
* - delim: the set of characters to use as delimiters
*
* Returns: char pointer to the next token in the string.
*/
char *next_token(char **str_ptr, const char *delim)
{
if (*str_ptr == NULL) {
return NULL;
}
size_t tok_start = strspn(*str_ptr, delim);
size_t tok_end = strcspn(*str_ptr + tok_start, delim);
/* Zero length token. We must be finished. */
if (tok_end == 0) {
*str_ptr = NULL;
return NULL;
}
/* Take note of the start of the current token. We'll return it later. */
char *current_ptr = *str_ptr + tok_start;
/* Shift pointer forward (to the end of the current token) */
*str_ptr += tok_start + tok_end;
if (**str_ptr == '\0') {
/* If the end of the current token is also the end of the string, we
* must be at the last token. */
*str_ptr = NULL;
} else {
/* Replace the matching delimiter with a NUL character to terminate the
* token string. */
**str_ptr = '\0';
/* Shift forward one character over the newly-placed NUL so that
* next_pointer now points at the first character of the next token. */
(*str_ptr)++;
}
return current_ptr;
}