Repository navigation
Expand file tree
/
Copy pathft_split.old
More file actions
105 lines (98 loc) · 2.07 KB
/
Copy pathft_split.old
File metadata and controls
105 lines (98 loc) · 2.07 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
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
#include <stdio.h>
#include "libft.h"
char **parent_alloc(char const *s, char c)
{
char **out;
char const *cpy;
int n_del;
int ind;
cpy = ft_strtrim(s, &c);
ind = 0;
while (cpy[ind])
{
if (cpy[ind] == c && cpy[ind + 1] != c) && cpy[ind - 1] != c)
n_del++;
ind++;
}
printf("%d delimiters counted!\n", n_del);
out = ft_calloc(n_del + 1, sizeof(char *));
if (!out)
{
return (NULL);
printf("Parent array allocation failed!\n");
}
printf("Successfully allocated for %d char pointers!\n", n_del + 1);
out[n_del] = 0;
printf("Parent array ended with Null pointer\n");
return (out);
}
char *substringer(int id, int size, int pos, const char* string, int del)
{
char *substring;
substring = ft_calloc(size, sizeof(char));
if (!substring)
{
printf("Split allocation failed!");
return (NULL);
}
printf(" < = Succesfully allocated %d for substring.\n", size);
ft_strlcpy(substring, &string[pos - size + 1], size);
printf("Copied %d characters from string[%d] to substring[%d]\n", size - 1, pos - size + 1, id);
substring[size] = '\0';
printf("Null terminated at substring[%d][%d]\n", id, size);
printf("Substring [%d]: %s\n", id, substring);
return (substring);
}
char **ft_split(char const *s, char c)
{
char **out;
char *strcp;
int i;
int sizer;
int j;
out = parent_alloc(s, c);
printf("Successfully allocated parent array\n");
i = 0;
j = 0;
sizer = 0;
strcp = ft_strtrim(s, &c);
while (s[i] != '\0')
{
if (s[i] == c)
if (s[i] == c && s[i + 1] != c && s[i - 1] != c)
{
printf("Seperator found at %d", i);
out[j] = substringer(j, sizer, i, s);
j++;
sizer = -1;
}
i++;
sizer++;
}
if (s[i - 1] == c)
substringer(j, sizer, i, s);
else
substringer(j, sizer, i, s);
return (out);
}
int main(int argc, char *argv[])
{
if (argc < 3)
return (0);
int k = 0;
char **prnt_ar = ft_split(argv[1], *argv[2]);
while (prnt_ar[k])
{
printf("Substring [%d]: %s\n", k, prnt_ar[k]);
k++;
}
k = 0;
while (prnt_ar[k])
{
free(prnt_ar[k]);
k++;
}
free(prnt_ar[k]);
free(prnt_ar);
return (1);
}