diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-15 10:20:37 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-15 10:20:37 +0200 |
| commit | 217bcb0d4e3ba60604921cb40d5a11a64f93cfc7 (patch) | |
| tree | 32a8f1f84e0b5f3617988932ccc068d471207208 | |
| parent | 3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f (diff) | |
| download | piscine-217bcb0d4e3ba60604921cb40d5a11a64f93cfc7.tar.gz piscine-217bcb0d4e3ba60604921cb40d5a11a64f93cfc7.tar.bz2 piscine-217bcb0d4e3ba60604921cb40d5a11a64f93cfc7.zip | |
c08 strdup malloc + 1 for \0, c09 begin
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | c02/ex01/ft_strncpy.c | 2 | ||||
| -rw-r--r-- | c07/ex00/ft_strdup.c | 4 | ||||
| -rw-r--r-- | c08/ex04/ft_strs_to_tab.c | 8 | ||||
| -rw-r--r-- | c08/main.c | 16 | ||||
| -rwxr-xr-x[-rw-r--r--] | c09/ex00/libft_creator.sh | 5 | ||||
| -rw-r--r-- | c09/ex01/Makefile | 30 | ||||
| -rw-r--r-- | c09/ex01/includes/ft.h | 22 | ||||
| -rw-r--r-- | c09/ex01/srcs/ft_putchar.c | 18 | ||||
| -rw-r--r-- | c09/ex01/srcs/ft_putstr.c | 19 | ||||
| -rw-r--r-- | c09/ex01/srcs/ft_strcmp.c | 21 | ||||
| -rw-r--r-- | c09/ex01/srcs/ft_strlen.c | 21 | ||||
| -rw-r--r-- | c09/ex01/srcs/ft_swap.c | 20 | ||||
| -rw-r--r-- | c09/ex02/ft_split.c | 76 |
14 files changed, 235 insertions, 28 deletions
@@ -1,2 +1,3 @@ a.out *.o +*.a diff --git a/c02/ex01/ft_strncpy.c b/c02/ex01/ft_strncpy.c index 0526dbb..815d941 100644 --- a/c02/ex01/ft_strncpy.c +++ b/c02/ex01/ft_strncpy.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/03 19:02:31 by cacharle #+# #+# */ -/* Updated: 2019/07/04 21:15:46 by cacharle ### ########.fr */ +/* Updated: 2019/07/15 10:11:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/c07/ex00/ft_strdup.c b/c07/ex00/ft_strdup.c index e53a3af..754f317 100644 --- a/c07/ex00/ft_strdup.c +++ b/c07/ex00/ft_strdup.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 15:39:50 by cacharle #+# #+# */ -/* Updated: 2019/07/09 07:36:10 by cacharle ### ########.fr */ +/* Updated: 2019/07/15 10:15:03 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ char *ft_strdup(char *src) int i; char *dup_ptr; - dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src)); + dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src) + 1); if (dup_ptr == NULL) { errno = ENOMEM; diff --git a/c08/ex04/ft_strs_to_tab.c b/c08/ex04/ft_strs_to_tab.c index f6fbfaf..51ca0af 100644 --- a/c08/ex04/ft_strs_to_tab.c +++ b/c08/ex04/ft_strs_to_tab.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:15:32 by cacharle #+# #+# */ -/* Updated: 2019/07/14 11:12:08 by cacharle ### ########.fr */ +/* Updated: 2019/07/15 10:12:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,12 +23,12 @@ int ft_strlen(char *str) return (counter); } -char *ft_strdup(char *src) +char *ft_strndup(char *src) { int i; char *dup_ptr; - dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src)); + dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src) + 1); if (dup_ptr == NULL) return (NULL); i = 0; @@ -54,7 +54,7 @@ struct s_stock_str *ft_strs_to_tab(int ac, char **av) { tmp_stock.size = ft_strlen(av[i]); tmp_stock.str = av[i]; - if ((tmp_stock.copy = ft_strdup(av[i])) == NULL) + if ((tmp_stock.copy = ft_strndup(av[i])) == NULL) return (NULL); strs_stocks[i++] = tmp_stock; } @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/12 15:24:19 by cacharle #+# #+# */ -/* Updated: 2019/07/13 08:55:06 by cacharle ### ########.fr */ +/* Updated: 2019/07/15 10:11:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,15 +27,15 @@ int main() printf("--------------------\n"); char **a = (char**)malloc(sizeof(char*) * 3); - a[0] = (char*)malloc(sizeof(char) * 32); - a[1] = (char*)malloc(sizeof(char) * 32); - a[2] = (char*)malloc(sizeof(char) * 32); - strcpy(a[0], "binjour"); - strcpy(a[1], "je"); - strcpy(a[2], "suis"); + a[0] = (char*)malloc(sizeof(char) * 1024); + a[1] = (char*)malloc(sizeof(char) * 1024); + a[2] = (char*)malloc(sizeof(char) * 1024); + strcpy(a[0], "ieSrBXBI5d4rW9O2"); + strcpy(a[1], "eadLhPjehRMwjqv9JJ7Q0WiLTBoVxsLx"); + strcpy(a[2], "MKynFC5rJwCJ0jdD"); t_stock_str *b = ft_strs_to_tab(3, a); for (int i = 0; b[i].str != 0; i++) - printf("%s | %s | %d\n", b[i].str, b[i].copy, b[i].size); + printf("%s \n%s | %d\n", b[i].str, b[i].copy, b[i].size); b[0].copy = "bafj"; ft_show_tab(b); diff --git a/c09/ex00/libft_creator.sh b/c09/ex00/libft_creator.sh index f799ee2..9c0aefa 100644..100755 --- 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/02 22:03:32 by cacharle #+# #+# */ +/* Updated: 2019/07/03 14:21:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:17:16 by cacharle #+# #+# */ +/* Updated: 2019/07/11 16:18:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 15:58:03 by cacharle #+# #+# */ +/* Updated: 2019/07/15 09:21:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> + +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)); +} |
