-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_tok.c
131 lines (122 loc) · 2.53 KB
/
str_tok.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "shell.h"
/**
*_strtok - tokenizes a string based on a set of delimiter characters
*@str: the string to tokenize
*@delim: A string containing a set of delimiter characters
*@save_point: where the next token is saved
*Return: address of the generated token
*/
char *_strtok(char *str, char *delim, char **save_point)
{
char *ch;
size_t i = 0;
if (str == NULL)
str = *save_point;
if (str == NULL)
return (NULL); /* return NULL if we have reached the end */
/* get the delimitor character position*/
ch = get_delim(str, delim);
while (*(str + i) != '\0')
{
if (str + i == ch)
{
if (*(str + i + 1) != '\0')
(*save_point) = (str + i) + 1;
else
(*save_point) = NULL;
*(str + i) = '\0';
return (str);
}
i++;
}
return (NULL);
}
/**
*get_delim - get the address where the delimitor is found
*@str: the string to check
*@delim: delimitor string
*Return: Address where delimitor is found or NULL
*/
char *get_delim(char *str, char *delim)
{
if (str != NULL && delim != NULL)
{
size_t i = 0, j;
while (*(str + i) != '\0')
{
j = 0;
while (*(delim + j) != '\0')
{
if (*(str + i) == *(delim + j))
return (str + i);
j++;
}
i++;
}
}
return (NULL);
}
/**
*_strtok2 - tokenizes a string based on a set of delimiter characters
*@str: the string to tokenize
*@save_point: where the next token is saved
*Return: address of the generated token
*/
char *_strtok2(char *str, char **save_point)
{
char *ch;
size_t i = 0;
if (str == NULL)
str = *save_point;
if (str == NULL)
return (NULL); /* return NULL if we have reached the end */
/* get the delimitor character position*/
ch = get_delim2(str);
while (*(str + i) != '\0')
{
if (str + i == ch)
{
if (*(str + i + 1) != '\0')
{
if (*ch == ';')
(*save_point) = (str + i) + 2;
else
(*save_point) = (str + i) + 3;
}
else
(*save_point) = NULL;
if (*(str + i - 1) == ' ')
*(str + i - 1) = '\0';
else
*(str + i) = '\0';
return (str);
}
i++;
}
return (NULL);
}
/**
*get_delim2 - get the address where the delimitor is found
*@str: the string to check
*Return: Address where delimitor is found or NULL
*/
char *get_delim2(char *str)
{
if (str != NULL)
{
size_t i = 0;
while (*(str + i) != '\0')
{
if (*(str + i) == '&' && *(str + i + 1) == '&')
return (str + i);
if (*(str + i) == ';')
return (str + i);
if (*(str + i) == '|' && *(str + i + 1) == '|')
return (str + i);
if (*(str + i) == '\n')
return (str + i);
i++;
}
}
return (NULL);
}