forked from BennettDixon/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize_string.c
166 lines (148 loc) · 2.82 KB
/
tokenize_string.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdlib.h>
#include "shell.h"
int get_word_length(char *str, char *delims);
int is_delim(char ch, char *delims);
int get_word_count(char *str, char *delims);
char *get_next_word(char *str, char *delims);
/**
* strtow - takes a string and seperates its words
*
* @str: string to seperate into words
* @delims: delimitors to use to delimit words
*
* Return: 2D array of pointers to each word
*/
char **strtow(char *str, char *delims)
{
char **words;
int wc, wordLen, n, i = 0;
if (str == NULL || !*str)
return (NULL);
wc = get_word_count(str, delims);
if (wc == 0)
return (NULL);
words = malloc((wc + 1) * sizeof(char *));
if (words == NULL)
return (NULL);
while (i < wc)
{
wordLen = get_word_length(str, delims);
if (is_delim(*str, delims))
{
str = get_next_word(str, delims);
}
words[i] = malloc((wordLen + 1) * sizeof(char));
if (words[i] == NULL)
{
while (i >= 0)
{
i--;
free(words[i]);
}
free(words);
return (NULL);
}
n = 0;
while (n < wordLen)
{
words[i][n] = *(str + n);
n++;
}
words[i][n] = '\0'; /* set end of str */
str = get_next_word(str, delims);
i++;
}
words[i] = NULL; /* last element is null for iteration */
return (words);
}
/**
* is_delim - checks if stream has delimitor char
*
* @ch: character in stream
*
* @delims: Pointer to null terminated array of delimitors
*
* Return: 1 (success) 0 (failure)
*/
int is_delim(char ch, char *delims)
{
int i = 0;
while (delims[i])
{
if (delims[i] == ch)
return (1);
i++;
}
return (0);
}
/**
* get_word_length - gets the word length of cur word in str
*
* @str: string to get word length from current word
* @delims: delimitors to use to delimit words
*
* Return: word length of current word
*/
int get_word_length(char *str, char *delims)
{
int wLen = 0, pending = 1, i = 0;
while (*(str + i))
{
if (is_delim(str[i], delims))
pending = 1;
else if (pending)
{
wLen++;
}
if (wLen > 0 && is_delim(str[i], delims))
break;
i++;
}
return (wLen);
}
/**
* get_word_count - gets the word count of a string
*
* @str: string to get word count from
* @delims: delimitors to use to delimit words
*
* Return: the word count of the string
*/
int get_word_count(char *str, char *delims)
{
int wc = 0, pending = 1, i = 0;
while (*(str + i))
{
if (is_delim(str[i], delims))
pending = 1;
else if (pending)
{
pending = 0;
wc++;
}
i++;
}
return (wc);
}
/**
* get_next_word - gets the next word in a string
*
* @str: string to get next word from
* @delims: delimitors to use to delimit words
*
* Return: pointer to first char of next word
*/
char *get_next_word(char *str, char *delims)
{
int pending = 0;
int i = 0;
while (*(str + i))
{
if (is_delim(str[i], delims))
pending = 1;
else if (pending)
break;
i++;
}
return (str + i);
}