d
This commit is contained in:
parent
26b7aed203
commit
80df47ddb4
22
Makefile
22
Makefile
@ -6,7 +6,7 @@
|
||||
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
|
||||
# Updated: 2022/10/04 19:41:01 by cchauvet ### ########.fr #
|
||||
# Updated: 2022/10/05 21:02:36 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
SRCS = ft_isalpha.c \
|
||||
@ -54,14 +54,11 @@ BSRCS = ft_lstnew.c \
|
||||
ft_lstiter.c \
|
||||
ft_lstmap.c
|
||||
|
||||
ESRCS = ft_strndup.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
BOBJS = ${BSRCS:.c=.o}
|
||||
|
||||
EOBJS = ${ESRCS:.c=.o}
|
||||
|
||||
NAME = libft
|
||||
|
||||
CFLAG = -Wall -Werror -Wextra
|
||||
@ -74,14 +71,11 @@ all: ${NAME}
|
||||
${NAME}: ${OBJS}
|
||||
ar -rc ${NAME}.a ${OBJS}
|
||||
|
||||
bonus: ${OBJS} ${BOBJS} ${EOBJS}
|
||||
ar -rc ${NAME}.a ${OBJS} ${BOBJS}
|
||||
|
||||
extra:
|
||||
ar -rc ${NAME}.a ${OBJS} ${BOBJS} ${EOBJS}
|
||||
bonus: ${OBJS} ${BOBJS}
|
||||
rc ${NAME}.a ${OBJS}
|
||||
|
||||
clean:
|
||||
rm -f ${OBJS} ${BOBJS} ${EOBJS}
|
||||
rm -f ${OBJS} ${BOBJS}
|
||||
|
||||
fclean: clean
|
||||
rm -f ${NAME}.a
|
||||
@ -89,11 +83,3 @@ fclean: clean
|
||||
re: fclean all
|
||||
|
||||
.PHONY: bonus extra clean fclean re
|
||||
|
||||
test: fclean
|
||||
rm -f a.out
|
||||
gcc ${CFLAGS} -g ${SRCS} ${BSRCS} ${ESRCS} main.c
|
||||
|
||||
so:
|
||||
gcc -nostartfiles -fPIC $(CFLAG) $(SRCS)
|
||||
gcc -nostartfiles -shared -o libft.so $(OBJS)
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 00:01:05 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/30 01:34:44 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/10/05 00:04:30 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,7 +18,7 @@ void ft_lstclear(t_list **lst, void (*del)(void *))
|
||||
|
||||
if (lst == NULL || del == NULL)
|
||||
return ;
|
||||
while (*lst == NULL)
|
||||
while (*lst != NULL)
|
||||
{
|
||||
next = (*lst)->next;
|
||||
ft_lstdelone(*lst, del);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 00:02:55 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/30 01:26:24 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/10/05 21:01:41 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
|
||||
void ft_lstdelone(t_list *lst, void (*del)(void *))
|
||||
{
|
||||
if (lst == NULL || lst == NULL)
|
||||
if (del == NULL || lst == NULL)
|
||||
return ;
|
||||
if (lst != NULL)
|
||||
{
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 00:05:43 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/30 00:33:06 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/10/05 20:58:18 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,9 +14,11 @@
|
||||
|
||||
void ft_lstiter(t_list *lst, void (*f)(void *))
|
||||
{
|
||||
while (lst->next == NULL)
|
||||
if (f == NULL)
|
||||
return ;
|
||||
while (lst != NULL)
|
||||
{
|
||||
f(lst);
|
||||
f(lst->content);
|
||||
lst = lst->next;
|
||||
}
|
||||
}
|
||||
|
28
ft_lstmap.c
28
ft_lstmap.c
@ -6,8 +6,34 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 00:05:05 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 14:10:47 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/10/05 00:08:45 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
||||
{
|
||||
t_list *root;
|
||||
t_list *last;
|
||||
|
||||
if (f == NULL || del == NULL)
|
||||
return (NULL);
|
||||
root = ft_lstnew(f(lst->content));
|
||||
if (root == NULL)
|
||||
return (NULL);
|
||||
last = root;
|
||||
lst = lst->next;
|
||||
while (lst != NULL)
|
||||
{
|
||||
last->next = ft_lstnew(f(lst->content));
|
||||
if (last->next == NULL)
|
||||
{
|
||||
ft_lstclear(&root, del);
|
||||
return (NULL);
|
||||
}
|
||||
lst = lst->next;
|
||||
last = last->next;
|
||||
}
|
||||
return (root);
|
||||
}
|
||||
|
100
ft_split.c
100
ft_split.c
@ -5,40 +5,19 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 17:37:50 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 21:24:29 by cchauvet ### ########.fr */
|
||||
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/05 20:47:09 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static size_t ft_seglen(const char *s, char c)
|
||||
static void *ft_cancel(char **tab, size_t len)
|
||||
{
|
||||
size_t len;
|
||||
size_t i;
|
||||
|
||||
if (*s == 0)
|
||||
return (1);
|
||||
len = 1;
|
||||
i = 0;
|
||||
while (s[i])
|
||||
if (tab != NULL)
|
||||
{
|
||||
while (s[i] == c && s[i])
|
||||
i++;
|
||||
if (s[i])
|
||||
len++;
|
||||
while (s[i] != c && s[i])
|
||||
i++;
|
||||
}
|
||||
if (len == 1)
|
||||
return (2);
|
||||
return (len);
|
||||
}
|
||||
|
||||
static void *ft_cancel(size_t len, char **tab)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
@ -46,53 +25,68 @@ static void *ft_cancel(size_t len, char **tab)
|
||||
i++;
|
||||
}
|
||||
free(tab);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static char **ft_segmentator(const char *s, char c, size_t len, char **tab)
|
||||
static size_t ft_seglen(const char *s, char c)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
ssize_t start;
|
||||
size_t len;
|
||||
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (i < len)
|
||||
if (s == NULL)
|
||||
return (0);
|
||||
len = 0;
|
||||
while (*s != 0)
|
||||
{
|
||||
while (s[j] == c)
|
||||
j++;
|
||||
start = j;
|
||||
while (s[j] != c)
|
||||
j++;
|
||||
tab[i] = ft_substr(s, start, j - start);
|
||||
if (tab[i] == NULL)
|
||||
return (ft_cancel(len, tab));
|
||||
i++;
|
||||
while (*s == c && *s != 0)
|
||||
s++;
|
||||
if (*s != 0)
|
||||
len++;
|
||||
while (*s != c && *s != 0)
|
||||
s++;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
|
||||
{
|
||||
size_t tab_index;
|
||||
size_t let_index;
|
||||
size_t start;
|
||||
|
||||
tab_index = 0;
|
||||
let_index = 0;
|
||||
start = 0;
|
||||
if (tab == NULL || s == NULL)
|
||||
return (NULL);
|
||||
while (tab_index < len)
|
||||
{
|
||||
while (s[let_index] == c && s[let_index] != 0)
|
||||
let_index++;
|
||||
start = let_index;
|
||||
while (s[let_index] != c && s[let_index] != 0)
|
||||
let_index++;
|
||||
tab[tab_index] = ft_substr(s, start, let_index - start);
|
||||
if (tab[tab_index] == NULL)
|
||||
return (ft_cancel(tab, tab_index));
|
||||
tab_index++;
|
||||
}
|
||||
return (tab);
|
||||
}
|
||||
|
||||
char **ft_split(const char *s, char c)
|
||||
{
|
||||
char **tab;
|
||||
size_t len;
|
||||
char **tab;
|
||||
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
len = ft_seglen(s, c);
|
||||
tab = malloc(sizeof(char *) * (len));
|
||||
tab = malloc((len + 1) * sizeof(char *));
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
if (c == 0 || !*s)
|
||||
{
|
||||
tab[0] = ft_strdup(s);
|
||||
if (tab[0] == NULL)
|
||||
tab[len] = NULL;
|
||||
if (ft_segsplitter(tab, len, s, c) == NULL)
|
||||
return (NULL);
|
||||
}
|
||||
else
|
||||
tab = ft_segmentator(s, c, len - 1, tab);
|
||||
tab[len - 1] = NULL;
|
||||
return (tab);
|
||||
}
|
||||
|
87
ft_split.old
Normal file
87
ft_split.old
Normal file
@ -0,0 +1,87 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nkimel <nkimel@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/28 16:55:02 by nkimel #+# #+# */
|
||||
/* Updated: 2022/09/29 19:13:03 by nkimel ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
static void *free_array(char **a)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (a != NULL)
|
||||
{
|
||||
i = 0;
|
||||
while (a[i] != NULL)
|
||||
free(a[i++]);
|
||||
free(a);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static size_t skip_char(const char **s, char c, bool invert)
|
||||
{
|
||||
size_t i;
|
||||
const char *p;
|
||||
|
||||
i = 0;
|
||||
p = *s;
|
||||
while (((!invert && *p == c) || (invert && *p != c)) && *p != '\0')
|
||||
{
|
||||
p++;
|
||||
i++;
|
||||
}
|
||||
*s = p;
|
||||
return (i);
|
||||
}
|
||||
|
||||
static size_t count_elems(const char *s, char c)
|
||||
{
|
||||
size_t n_elem;
|
||||
|
||||
n_elem = 0;
|
||||
skip_char(&s, c, false);
|
||||
while (*s != '\0')
|
||||
{
|
||||
skip_char(&s, c, true);
|
||||
skip_char(&s, c, false);
|
||||
n_elem++;
|
||||
}
|
||||
return (n_elem);
|
||||
}
|
||||
|
||||
char **ft_split(char const *s, char c)
|
||||
{
|
||||
char **array;
|
||||
size_t n_elem;
|
||||
size_t arr_i;
|
||||
const char *rem_s;
|
||||
size_t len;
|
||||
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
n_elem = count_elems(s, c);
|
||||
array = ft_calloc(n_elem + 1, sizeof(char *));
|
||||
if (array == NULL)
|
||||
return (array);
|
||||
skip_char(&s, c, false);
|
||||
arr_i = 0;
|
||||
while (arr_i < n_elem)
|
||||
{
|
||||
rem_s = s;
|
||||
len = skip_char(&s, c, true);
|
||||
array[arr_i] = ft_substr(rem_s, 0, len);
|
||||
if (array[arr_i++] == NULL)
|
||||
return (free_array(array));
|
||||
skip_char(&s, c, false);
|
||||
}
|
||||
return (array);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 15:08:26 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/10/05 20:56:37 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
while (*s != c)
|
||||
while (*s != (char) c)
|
||||
{
|
||||
if (*s == 0)
|
||||
return (NULL);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 16:00:49 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 11:03:42 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/10/04 23:24:11 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,7 +17,7 @@ char *ft_strdup(const char *s)
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
out = malloc(sizeof(char) * (ft_strlen(s) + 1));
|
||||
out = ft_calloc((ft_strlen(s) + 1), sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
|
31
ft_strndup.c
31
ft_strndup.c
@ -1,31 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strndup.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 10:33:40 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 19:02:02 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strndup(const char *s, size_t n)
|
||||
{
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
out = malloc((n + 0) * sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
while (i < n && *s)
|
||||
{
|
||||
out[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
out[i] = -1;
|
||||
return (out);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 08:25:33 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 15:03:56 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/10/05 20:50:49 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,7 +18,7 @@ char *ft_strrchr(const char *s, int c)
|
||||
|
||||
i = ft_strlen(s) + 1;
|
||||
while (i-- > 0)
|
||||
if (s[i] == c)
|
||||
if (s[i] == (char) c)
|
||||
return ((char *) s + i);
|
||||
return (NULL);
|
||||
}
|
||||
|
33
ft_substr.c
33
ft_substr.c
@ -6,29 +6,30 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/28 18:53:44 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 19:16:16 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/10/05 20:53:19 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_substr(const char *s, unsigned int start, size_t len)
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
size_t max;
|
||||
char *sub;
|
||||
ssize_t size;
|
||||
char *ptr;
|
||||
|
||||
if (s == NULL || !*s)
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
max = ft_strlen(s);
|
||||
if (max > len)
|
||||
max = len;
|
||||
if (start >= ft_strlen(s))
|
||||
return (ft_strdup(""));
|
||||
sub = malloc((max + 1) * sizeof(char));
|
||||
if (sub == NULL)
|
||||
size = ft_strlen(s);
|
||||
size -= start;
|
||||
if (size < 0)
|
||||
size = 0;
|
||||
if ((size_t)size > len)
|
||||
size = len;
|
||||
ptr = malloc((size + 1) * sizeof(char));
|
||||
if (ptr == NULL)
|
||||
return (NULL);
|
||||
sub[max] = 0;
|
||||
while (max-- > 0)
|
||||
sub[max] = s[max + start];
|
||||
return (sub);
|
||||
ptr[size] = '\0';
|
||||
while (size-- > 0)
|
||||
ptr[size] = s[start + size];
|
||||
return (ptr);
|
||||
}
|
||||
|
BIN
libft.h.gch
BIN
libft.h.gch
Binary file not shown.
35
main.c
35
main.c
@ -1,35 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 13:56:50 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 22:58:20 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "libft.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char **splitted;
|
||||
char *a;
|
||||
char c;
|
||||
|
||||
if (argc < 2)
|
||||
return (1);
|
||||
a = argv[1];
|
||||
c = argv[2][0];
|
||||
splitted = ft_split(a, c);
|
||||
//splitted = ft_split("lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse", ' ');
|
||||
while (*splitted != NULL)
|
||||
{
|
||||
puts(*splitted);
|
||||
free(*splitted);
|
||||
splitted++;
|
||||
}
|
||||
return (0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user