From 217bcb0d4e3ba60604921cb40d5a11a64f93cfc7 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 15 Jul 2019 10:20:37 +0200 Subject: c08 strdup malloc + 1 for \0, c09 begin --- c09/ex00/libft_creator.sh | 5 +-- c09/ex01/Makefile | 30 +++++++++++------- c09/ex01/includes/ft.h | 22 ++++++++++++++ c09/ex01/srcs/ft_putchar.c | 18 +++++++++++ c09/ex01/srcs/ft_putstr.c | 19 ++++++++++++ c09/ex01/srcs/ft_strcmp.c | 21 +++++++++++++ c09/ex01/srcs/ft_strlen.c | 21 +++++++++++++ c09/ex01/srcs/ft_swap.c | 20 ++++++++++++ c09/ex02/ft_split.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 219 insertions(+), 13 deletions(-) mode change 100644 => 100755 c09/ex00/libft_creator.sh create mode 100644 c09/ex01/includes/ft.h create mode 100644 c09/ex01/srcs/ft_putchar.c create mode 100644 c09/ex01/srcs/ft_putstr.c create mode 100644 c09/ex01/srcs/ft_strcmp.c create mode 100644 c09/ex01/srcs/ft_strlen.c create mode 100644 c09/ex01/srcs/ft_swap.c create mode 100644 c09/ex02/ft_split.c (limited to 'c09') diff --git a/c09/ex00/libft_creator.sh b/c09/ex00/libft_creator.sh old mode 100644 new mode 100755 index f799ee2..9c0aefa --- a/c09/ex00/libft_creator.sh +++ b/c09/ex00/libft_creator.sh @@ -1,3 +1,4 @@ #!/bin/sh -gcc -c *.c -ar -rcs libft.a *.o +gcc -c ft_putchar.c ft_swap.c ft_putstr.c ft_strlen.c ft_strcmp.c +ar -rs libft.a ft_putchar.o ft_swap.o ft_putstr.o ft_strlen.o ft_strcmp.o +rm -f ft_putchar.o ft_swap.o ft_putstr.o ft_strlen.o ft_strcmp.o diff --git a/c09/ex01/Makefile b/c09/ex01/Makefile index 0fec395..ba69535 100644 --- a/c09/ex01/Makefile +++ b/c09/ex01/Makefile @@ -1,28 +1,36 @@ -SRCDIR = srcs -SRC = ft_putchar.c ft_swap.c ft_putstr.c ft_strlen.c ft_strcmp.c -OBJ = $(SRC:.c = .o) -INCLUDEDIR = includes -INCLUDES = ft.h +OUT = libft.a CC = gcc CCFLAGS = -Wall -Wextra -Werror -OUT = libft.a +OBJ = srcs/ft_putchar.o srcs/ft_swap.o srcs/ft_putstr.o srcs/ft_strlen.o srcs/ft_strcmp.o -all: OUT .PHONY: all +all: $(OUT) $(OUT): $(OBJ) - ar -crs $(OBJ) + ar -crs $(OUT) $(OBJ) + +srcs/ft_putchar.o: srcs/ft_putchar.c includes/ft.h + $(CC) $(CCFLAGS) -c $< -o $@ + +srcs/ft_swap.o: srcs/ft_swap.c includes/ft.h + $(CC) $(CCFLAGS) -c $< -o $@ + +srcs/ft_putstr.o: srcs/ft_putstr.c includes/ft.h + $(CC) $(CCFLAGS) -c $< -o $@ + +srcs/ft_strlen.o: srcs/ft_strlen.c includes/ft.h + $(CC) $(CCFLAGS) -c $< -o $@ -$(OBJ): %.o: %.c +srcs/ft_strcmp.o: srcs/ft_strcmp.c includes/ft.h $(CC) $(CCFLAGS) -c $< -o $@ .PHONY: clean clean: - rm -f srcs/*.o + rm -f $(OBJ) .PHONY: fclean fclean: clean - rm $(OUT) + rm -f $(OUT) .PHONY: re re: fclean all diff --git a/c09/ex01/includes/ft.h b/c09/ex01/includes/ft.h new file mode 100644 index 0000000..e044fdd --- /dev/null +++ b/c09/ex01/includes/ft.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/15 08:52:33 by cacharle #+# #+# */ +/* Updated: 2019/07/15 09:12:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_H +# define FT_H + +void ft_putchar(char c); +void ft_swap(int *a, int *b); +void ft_putstr(char *str); +int ft_strlen(char *str); +int ft_strcmp(char *s1, char *s2); + +#endif diff --git a/c09/ex01/srcs/ft_putchar.c b/c09/ex01/srcs/ft_putchar.c new file mode 100644 index 0000000..8a53dc8 --- /dev/null +++ b/c09/ex01/srcs/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/02 22:03:32 by cacharle #+# #+# */ +/* Updated: 2019/07/03 14:21:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/c09/ex01/srcs/ft_putstr.c b/c09/ex01/srcs/ft_putstr.c new file mode 100644 index 0000000..c4f4564 --- /dev/null +++ b/c09/ex01/srcs/ft_putstr.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:17:16 by cacharle #+# #+# */ +/* Updated: 2019/07/11 16:18:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putstr(char *str) +{ + while (*str) + write(1, str++, 1); +} diff --git a/c09/ex01/srcs/ft_strcmp.c b/c09/ex01/srcs/ft_strcmp.c new file mode 100644 index 0000000..7b96c2e --- /dev/null +++ b/c09/ex01/srcs/ft_strcmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:20:33 by cacharle #+# #+# */ +/* Updated: 2019/07/15 09:09:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcmp(char *s1, char *s2) +{ + while (*s1 == *s2 && *s1 && *s2) + { + s1++; + s2++; + } + return (*s1 - *s2); +} diff --git a/c09/ex01/srcs/ft_strlen.c b/c09/ex01/srcs/ft_strlen.c new file mode 100644 index 0000000..8c6ba48 --- /dev/null +++ b/c09/ex01/srcs/ft_strlen.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:19:43 by cacharle #+# #+# */ +/* Updated: 2019/07/15 08:54:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} diff --git a/c09/ex01/srcs/ft_swap.c b/c09/ex01/srcs/ft_swap.c new file mode 100644 index 0000000..989b2c2 --- /dev/null +++ b/c09/ex01/srcs/ft_swap.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:18:42 by cacharle #+# #+# */ +/* Updated: 2019/07/11 16:19:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_swap(int *a, int *b) +{ + int tmp; + + tmp = *a; + *a = *b; + *b = tmp; +} diff --git a/c09/ex02/ft_split.c b/c09/ex02/ft_split.c new file mode 100644 index 0000000..e1c0186 --- /dev/null +++ b/c09/ex02/ft_split.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 15:58:03 by cacharle #+# #+# */ +/* Updated: 2019/07/15 09:21:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int in_charset(char character, char *charset) +{ + while (*charset) + if (character == *charset++) + return (1); + return (0); +} + +int count_segment(char *str, char *charset) +{ + int counter; + + counter = 0; + while (*str) + { + if (!in_charset(*str, charset)) + { + counter++; + while (!in_charset(*str, charset) && *str) + str++; + if (!*str) + break ; + } + str++; + } + return (counter); +} + +char **heck(char *str, char *charset, char *tmp, int i) +{ + char **strs; + int j; + + if ((strs = (char**)malloc(sizeof(char*) + * (count_segment(str, charset) + 1))) == NULL) + return (NULL); + j = 0; + while (*str) + { + while (in_charset(*str, charset)) + str++; + i = 0; + while (!in_charset(str[i], charset) && str[i]) + i++; + if ((tmp = (char*)malloc(sizeof(char) * i + 1)) == NULL) + return (NULL); + i = 0; + while (!in_charset(*str, charset) && *str) + tmp[i++] = *str++; + tmp[i] = '\0'; + strs[j++] = tmp; + } + strs[j] = 0; + return (strs); +} + +char **ft_split(char *str, char *charset) +{ + if (!*str) + return (NULL); + return (heck(str, charset, NULL, 0)); +} -- cgit