This repository was archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.c
More file actions
91 lines (82 loc) · 2.19 KB
/
split.c
File metadata and controls
91 lines (82 loc) · 2.19 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
/*
** EPITECH PROJECT, 2021
** split.c
** File description:
** .split in java, more or less
*/
#include "my.h"
#include <unistd.h>
#include <stdlib.h>
static void coding_style(char **split, int *count, int *a)
{
split[(*count)][(*a)] = '\0';
(*a) = 0;
(*count)++;
}
static void coding_style2(char **split, int **a, char *str)
{
split[(*a[3])][(*a[0])] = str[(*a[2])];
if (str[(*a[2])] == '\0')
(*a[2])--;
}
void fill_split(char **split, char *str, char *to, int nb)
{
int count = 0;
int a = 0;
bool quote = false;
for (int i = 0, c = 0, tmp = 0, b = 0, t2 = 0; str[i] != '\0'; i++, a++) {
if (str[i] == '\"')
quote = quote ? false : true;
for (c = 0, tmp = i; str[tmp] == to[c] && !quote; tmp++, c++);
for (t2 = a != 0 ? i + 1 : i, b = 0;
str[t2] == to[b] && !quote; t2++, b++);
if (c == my_strlen(to) && b != my_strlen(to) && count < nb - 1) {
i = tmp;
coding_style(split, &count, &a);
}
coding_style2(split, (int *[4]){&a, &b, &i, &count}, str);
}
split[count][a] = '\0';
split[count + 1] = NULL;
}
int check_split(char *str, char *to)
{
int nb = 1;
bool quote = false;
int a = 0;
int b = 0;
int tmp = 0;
int t2 = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == '\"')
quote = quote ? false : true;
for (a = 0, tmp = i; str[tmp] == to[a] && !quote; tmp++, a++);
for (t2 = a != 0 ? i + 1 : i, b = 0;
str[t2] == to[b] && !quote; t2++, b++);
if (a == my_strlen(to) && b != my_strlen(to))
nb++;
}
return (nb);
}
char **split(char *str, char *to, int nb)
{
char **split;
if (str == NULL)
return NULL;
if (nb <= 0)
nb = check_split(str, to);
else if (check_split(str, to) < nb)
nb = check_split(str, to);
else
nb++;
split = malloc(sizeof(char *) * (nb + 1));
if (split == NULL)
return (NULL);
for (int i = 0; i != nb; i++) {
split[i] = malloc(sizeof(char) * (my_strlen(str) + 2));
if (split[i] == NULL)
return (NULL);
}
fill_split(split, str, to, nb);
return (split);
}