diff options
51 files changed, 1540 insertions, 124 deletions
diff --git a/bsq/.gitignore b/bsq/.gitignore new file mode 100644 index 0000000..d7756c2 --- /dev/null +++ b/bsq/.gitignore @@ -0,0 +1,2 @@ +a.out +*.o diff --git a/bsq/Makefile b/bsq/Makefile new file mode 100644 index 0000000..11cddfa --- /dev/null +++ b/bsq/Makefile @@ -0,0 +1,46 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/07/19 12:09:54 by cacharle #+# #+# # +# Updated: 2019/07/19 15:18:19 by cacharle ### ########.fr # +# # +# **************************************************************************** # + +OUT = bsq +SRCDIR = srcs +SRC = $(SRCDIR)/main.c +OBJ = $(SRC:.c=.o) +INCLUDES = includes/include.h + +CC = gcc +CCFLAGS = -Wall -Wextra #-Werror +LDFLAGS = -Iincludes + +ARGS = 10 10 2 + +.PHONY: all +all: $(OUT) + +$(OUT): $(OBJ) + $(CC) $(CCFLAGS) $(LDFLAGS) -o $@ $^ + +%.o: %.c $(INCLUDES) + $(CC) $(CCFLAGS) $(LDFLAGS) -c -o $@ $< + +.PHONY: clean + rm -f $(OBJ) + +.PHONY: fclean +fclean: clean + rm -f $(OUT) + +.PHONY: re +re: fclean all + +.PHONY: generate +generate: + ./tests/generate.pl ${ARGS} diff --git a/bsq/auteur b/bsq/auteur new file mode 100644 index 0000000..ab605b4 --- /dev/null +++ b/bsq/auteur @@ -0,0 +1 @@ +samzur:cacharle diff --git a/bsq/includes/include.h b/bsq/includes/include.h new file mode 100644 index 0000000..d505857 --- /dev/null +++ b/bsq/includes/include.h @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* include.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 13:17:32 by cacharle #+# #+# */ +/* Updated: 2019/07/19 15:55:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef INCLUDE_H +# define INCLUDE_H + +# define TRUE 1 +# define FALSE 0 +# define EMPTY 0 +# define OBSTACLE 1 + +struct s_square +{ + int size; + int x; + int y; +}; + +struct s_terrain +{ + char empty; + char fill; + char obstacle; + int size; + char *file; +} + +typedef struct s_terrain t_terrain; +typedef struct s_square t_square; +typedef int t_bool; + +/* +** solve.c - Solve the thing (yes) +*/ + + +/* +** parse.c - Input parsing +** Put file in string, parse it and check if it's valid +*/ + +t_bool check_input(char *input); + +/* +** helper.c - function already made +*/ + +int read_file(int fildes, char **file); +char *ft_memcat(char *file, char buf[BUF_SIZE], int file_size, + int read_size); + + + +#endif diff --git a/bsq/srcs/helper.c b/bsq/srcs/helper.c new file mode 100644 index 0000000..d1fe8bc --- /dev/null +++ b/bsq/srcs/helper.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 15:19:53 by cacharle #+# #+# */ +/* Updated: 2019/07/19 15:19:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> +#include "include.h" + +int read_file(int fildes, char **file) +{ + char buf[BUF_SIZE]; + int file_size; + int read_size; + + file_size = 0; + while ((read_size = read(fildes, buf, BUF_SIZE))) + { + if (read_size < 0) + return (-1); + *file = ft_memcat(*file, buf, file_size, read_size); + file_size += read_size; + } + return (file_size); +} + +char *ft_memcat(char *file, char buf[BUF_SIZE], int file_size, + int read_size) +{ + int i; + char *file_clone; + + if ((file_clone = malloc(sizeof(char) * (file_size + 1))) == NULL) + return (NULL); + i = -1; + while (++i < file_size) + file_clone[i] = file[i]; + free(file); + if ((file = malloc(sizeof(char) * (file_size + read_size + 1))) == NULL) + return (NULL); + i = 0; + while (i < file_size) + { + file[i] = file_clone[i]; + i++; + } + free(file_clone); + while (i < file_size + read_size) + { + file[i] = buf[i - file_size]; + i++; + } + return (file); +} diff --git a/bsq/srcs/main.c b/bsq/srcs/main.c new file mode 100644 index 0000000..637ac13 --- /dev/null +++ b/bsq/srcs/main.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 11:01:32 by cacharle #+# #+# */ +/* Updated: 2019/07/19 13:35:23 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +int main(int argc, char **argv) +{ + + + return (0); +} diff --git a/bsq/srcs/parse.c b/bsq/srcs/parse.c new file mode 100644 index 0000000..8c4af3a --- /dev/null +++ b/bsq/srcs/parse.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 15:23:43 by cacharle #+# #+# */ +/* Updated: 2019/07/19 15:55:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +t_bool check_input(char *input) +{ + char *line; + int i; + + i = 0; + while (input[i] != '\n') + i++; + if (i < 4) + return (FALSE); + parsed.fill = input[i--]; + parsed.obstacle = input[i--]; + parsed.empty = input[i--]; + // parsed.size = // atoi smth + while (*input != '\n') + input++; + i = 0; + while (input[i]) + { + while (input[i] != '\n') + { + + } + if (i != parsed.size + + + } +} diff --git a/bsq/tests/generate.pl b/bsq/tests/generate.pl new file mode 100755 index 0000000..27bd35f --- /dev/null +++ b/bsq/tests/generate.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +use warnings; +use strict; + +die "program x y density" unless (scalar(@ARGV) == 3); + +my ($x, $y, $density) = @ARGV; + +print "$y.ox\n"; +for (my $i = 0; $i < $y; $i++) { + for (my $j = 0; $j < $x; $j++) { + if (int(rand($y) * 2) < $density) { + print "o"; + } + else { + print "."; + } + } + print "\n"; +} @@ -8,9 +8,9 @@ int main() char *const str = "3YaZkAP30iGoBWv L H asdf h LbpX8Hx FWHwB2u1FH0S5"; char *begin = "\n \t hgonjour"; char *end = "jesuis\n\t hhh"; - char *empty = " h bonjour je suis "; + char *empty = ""; char *charset = " H"; - char **strs = ft_split(str, charset); + char **strs = ft_split(empty, charset); printf("tab start\n"); for (int i = 0; strs[i] != 0; i++) { diff --git a/c10/bonjour.c b/c10/bonjour.c new file mode 100644 index 0000000..1b6f283 --- /dev/null +++ b/c10/bonjour.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bonjour.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 17:20:22 by cacharle #+# #+# */ +/* Updated: 2019/07/20 17:31:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/ft.h" + +int main(int argc, char **argv) +{ + int fd; + char buff[BUFF_SIZE + 1]; + int ret; + if (argc < 2) + write(2, "File name missing.\n", 19); + else if (argc > 2) + write(2, "Too many arguments.\n", 20); + else + { + fd = open(argv[1], O_RDONLY); + if (fd == -1) + { + write(2, "Cannot read file.\n", 18); + return (1); + } + while ((ret = read(fd, buff, BUFF_SIZE))) + write(1, buff, ret); + if (close(fd)) + return (1); + } + return (0); +} diff --git a/c10/ex00/main.c b/c10/ex00/main.c index 0cc4470..67fefe6 100644 --- a/c10/ex00/main.c +++ b/c10/ex00/main.c @@ -6,53 +6,38 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/11 18:06:43 by cacharle #+# #+# */ -/* Updated: 2019/07/19 06:33:07 by cacharle ### ########.fr */ +/* Updated: 2019/07/20 17:26:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <unistd.h> #include <fcntl.h> -#include <stdio.h> -#define BUFFER_SIZE 1024 - -int read_write_file(int fildes) -{ - char buf[BUFFER_SIZE]; - int read_size; - - while ((read_size = read(fildes, buf, BUFFER_SIZE)) != 0) - { - if (read_size < 0) - return (-1); - write(STDOUT_FILENO, buf, read_size); - } - return (0); -} +#define BUF_SIZE 64 int main(int argc, char **argv) { int fildes; + int read_size; + char buf[BUF_SIZE + 1]; - if (argc == 1) + if (argc < 2) { - write(STDERR_FILENO, "File name missing.\n", 20); + write(STDERR_FILENO, "File name missing.\n", 19); return (1); } - else if (argc > 2) + if (argc > 2) { - write(STDERR_FILENO, "Too many arguments.\n", 21); + write(STDERR_FILENO, "Too many arguments.\n", 20); return (1); } - if ((fildes = open(argv[1], O_RDONLY)) < 0) + if ((fildes = open(argv[1], O_RDONLY)) == -1) { write(STDERR_FILENO, "Cannot read file.\n", 18); return (1); } - if ((read_write_file(fildes)) < 0) - { - write(STDERR_FILENO, "Cannot read file.\n", 18); + while ((read_size = read(fildes, buf, BUF_SIZE))) + write(STDOUT_FILENO, buf, read_size); + if (close(fildes)) return (1); - } - close(fildes); return (0); } diff --git a/c10/ex02/include.h b/c10/ex02/include.h index d37df25..f806cec 100644 --- a/c10/ex02/include.h +++ b/c10/ex02/include.h @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 15:26:12 by cacharle #+# #+# */ -/* Updated: 2019/07/19 06:59:18 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 07:06:04 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,8 @@ int print_tail(char *filename, int tail_size, int argc, int good_counter); int print_file_tail(int fildes, int tail_size); int read_file(int fildes, char **file); -char *ft_memcat(char *file, char buf[BUF_SIZE], int file_size, int read_size); +char *ft_memcat(char *file, char buf[BUF_SIZE], int file_size, + int read_size); /* ** helper.c diff --git a/c11/ex04/ft_is_sort.c b/c11/ex04/ft_is_sort.c index 84d4fe0..7e95ba2 100644 --- a/c11/ex04/ft_is_sort.c +++ b/c11/ex04/ft_is_sort.c @@ -6,11 +6,11 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:39:43 by cacharle #+# #+# */ -/* Updated: 2019/07/18 21:19:29 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 07:37:39 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -static is_sort_asc(int *tab, int length, int (*f)(int, int)) +static int is_sort_asc(int *tab, int length, int (*f)(int, int)) { int i; @@ -24,7 +24,7 @@ static is_sort_asc(int *tab, int length, int (*f)(int, int)) return (1); } -static is_sort_dsc(int *tab, int length, int (*f)(int, int)) +static int is_sort_dsc(int *tab, int length, int (*f)(int, int)) { int i; @@ -38,7 +38,7 @@ static is_sort_dsc(int *tab, int length, int (*f)(int, int)) return (1); } -int ft_is_sort(int *tab, int length, int (*f)(int, int)) +int ft_is_sort(int *tab, int length, int (*f)(int, int)) { return (is_sort_dsc(tab, length, f) || is_sort_asc(tab, length, f)); } diff --git a/c11/ex06/ft_sort_string_tab.c b/c11/ex06/ft_sort_string_tab.c index c10feaa..4fadff4 100644 --- a/c11/ex06/ft_sort_string_tab.c +++ b/c11/ex06/ft_sort_string_tab.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:48:20 by cacharle #+# #+# */ -/* Updated: 2019/07/18 11:04:01 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 07:27:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,14 +22,14 @@ int ft_strcmp(char *s1, char *s2) return (*s1 - *s2); } -int is_sorted(char **argv) +int is_sorted(char **tab) { int i; - i = 1; - while (argv[i + 1] != NULL) + i = 0; + while (tab[i + 1] != NULL) { - if (ft_strcmp(argv[i], argv[i + 1]) > 0) + if (ft_strcmp(tab[i], tab[i + 1]) > 0) return (0); i++; } @@ -43,7 +43,7 @@ void ft_sort_string_tab(char **tab) while (!is_sorted(tab)) { - i = 1; + i = 0; while (tab[i + 1] != NULL) { if (ft_strcmp(tab[i], tab[i + 1]) > 0) diff --git a/c11/ex07/ft_advanced_sort_string_tab.c b/c11/ex07/ft_advanced_sort_string_tab.c index efcfb37..96c303d 100644 --- a/c11/ex07/ft_advanced_sort_string_tab.c +++ b/c11/ex07/ft_advanced_sort_string_tab.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/16 21:51:06 by cacharle #+# #+# */ -/* Updated: 2019/07/18 11:03:31 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 07:27:53 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -13,6 +13,7 @@ void f_fe(int x); int f_ma(int x); int f_len(char *x); int f_cou(char *x); +int f_sor_dsc(int x, int y); int f_sor(int x, int y); int f_lensort(char *a, char *b); @@ -43,21 +44,30 @@ int main() printf("\n------------------------\n"); int sorted[10] = {1, 2, 3, 4, 5, 5, 6}; - printf("sorted %d", ft_is_sort(sorted, 6, &f_sor)); + printf("sorted asc %d\n", ft_is_sort(sorted, 6, &f_sor)); + int sorted_dsc[10] = {7, 6, 4, 1, 0, -10}; + printf("sorted dsc %d", ft_is_sort(sorted_dsc, 6, &f_sor_dsc)); + printf("\n------------------------\n"); - char **a = malloc(sizeof(char*) * 5); + char **a = malloc(sizeof(char*) * 8); a[0] = malloc(sizeof(char) * 32); a[1] = malloc(sizeof(char) * 32); a[2] = malloc(sizeof(char) * 32); a[3] = malloc(sizeof(char) * 32); - strcpy(a[0], "bonjour"); - strcpy(a[1], "je"); - strcpy(a[2], "suis"); - strcpy(a[3], "charles"); - a[4] = NULL; + a[4] = malloc(sizeof(char) * 32); + a[5] = malloc(sizeof(char) * 32); + a[6] = malloc(sizeof(char) * 32); + strcpy(a[0], "dQ4ilBI6T$"); + strcpy(a[1], "16Tz2R$"); + strcpy(a[2], "4zQplLtBc$"); + strcpy(a[3], "7$"); + strcpy(a[4], "7hE84k$"); + strcpy(a[5], "841SqkO$"); + strcpy(a[6], "ItM$"); + a[7] = NULL; ft_sort_string_tab(a); - for (int i = 0; i < 5; i++) + for (int i = 0; i < 7; i++) printf("%s\n", a[i]); printf("\n------------------------\n"); @@ -102,6 +112,13 @@ int f_cou(char *x) return counter > 2; } +int f_sor_dsc(int x, int y) +{ + if (x == y) + return (0); + return x > y ? -1 : 1; +} + int f_sor(int x, int y) { if (x == y) @@ -1,32 +0,0 @@ -#include <stdio.h> -#include "ex00/ft_list.h" -#include "ex00/ft_create_elem.c" -#include "ex01/ft_list_push_front.c" -/*#include "ex02/ft_list_size.c"*/ -/*#include "ex03/ft_list_last.c"*/ -/*#include "ex04/ft_list_push_back.c"*/ -/*#include "ex05/ft_list_push_strs.c"*/ -/*#include "ex06/ft_list_clear.c"*/ -/*#include "ex07/ft_list_at.c"*/ -/*#include "ex08/ft_list_reverse.c"*/ -/*#include "ex09/ft_list_foreach.c"*/ -/*#include "ex10/ft_list_foreach_if.c"*/ -/*#include "ex11/ft_list_find.c"*/ -/*#include "ex12/ft_list_remove_if.c"*/ -/*#include "ex13/ft_list_merge.c"*/ -/*#include "ex14/ft_list_sort.c"*/ -/*#include "ex15/ft_list_reverse_fun.c"*/ -/*#include "ex16/ft_sorted_list_insert.c"*/ -/*#include "ft_sorted_list_merge.c"*/ - -int main() -{ - t_list *list = NULL; - int a = 4; - void *data = &a; - - list = ft_create_elem(data); - printf("%d\n", list->data); - - -} diff --git a/c12/ex02/ft_list.h b/c12/ex02/ft_list.h index 8b736b4..33ad610 100644 --- a/c12/ex02/ft_list.h +++ b/c12/ex02/ft_list.h @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ -/* Updated: 2019/07/09 14:42:55 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 08:29:04 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,4 @@ typedef struct s_list void *data; } t_list; -t_list *ft_create_elem(void *data); - #endif diff --git a/c12/ex03/ft_list.h b/c12/ex03/ft_list.h index 8b736b4..32a5925 100644 --- a/c12/ex03/ft_list.h +++ b/c12/ex03/ft_list.h @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ -/* Updated: 2019/07/09 14:42:55 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 08:28:53 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,4 @@ typedef struct s_list void *data; } t_list; -t_list *ft_create_elem(void *data); - #endif diff --git a/c12/ex06/ft_list.h b/c12/ex06/ft_list.h index 8b736b4..aeb710e 100644 --- a/c12/ex06/ft_list.h +++ b/c12/ex06/ft_list.h @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ -/* Updated: 2019/07/09 14:42:55 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 08:28:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,4 @@ typedef struct s_list void *data; } t_list; -t_list *ft_create_elem(void *data); - #endif diff --git a/c12/ex07/ft_list.h b/c12/ex07/ft_list.h index 8b736b4..ab7e2d1 100644 --- a/c12/ex07/ft_list.h +++ b/c12/ex07/ft_list.h @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ -/* Updated: 2019/07/09 14:42:55 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 08:28:24 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,4 @@ typedef struct s_list void *data; } t_list; -t_list *ft_create_elem(void *data); - #endif diff --git a/c12/ex08/ft_list.h b/c12/ex08/ft_list.h new file mode 100644 index 0000000..0744591 --- /dev/null +++ b/c12/ex08/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:28:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/c12/ex08/ft_list_reverse.c b/c12/ex08/ft_list_reverse.c index 23d4339..e5c2057 100644 --- a/c12/ex08/ft_list_reverse.c +++ b/c12/ex08/ft_list_reverse.c @@ -6,22 +6,24 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 18:05:23 by cacharle #+# #+# */ -/* Updated: 2019/07/17 19:13:19 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 08:18:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ void ft_list_reverse(t_list **begin_list) { - t_list *tmp; + t_list *cursor; t_list *prev; + t_list *tmp_next; prev = NULL; - tmp = (*begin_list)->next; - while (tmp != NULL) + cursor = *begin_list; + while (cursor != NULL) { - (*begin_list)->next = prev; - prev = *begin_list; - *begin_list = tmp; - tmp = (*begin_list)->next; + tmp_next = cursor->next; + cursor->next = prev; + prev = cursor; + cursor = tmp_next; } + *begin_list = prev; } diff --git a/c12/ex09/ft_list.h b/c12/ex09/ft_list.h index 08a22a5..8f3a487 100644 --- a/c12/ex09/ft_list.h +++ b/c12/ex09/ft_list.h @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ -/* Updated: 2019/07/17 17:11:04 by cacharle ### ########.fr */ +/* Updated: 2019/07/19 08:27:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,4 @@ typedef struct s_list void *data; } t_list; -t_list *ft_create_elem(void *data); - #endif diff --git a/c12/ex09/ft_list_foreach.c b/c12/ex09/ft_list_foreach.c index e69de29..2c9705f 100644 --- a/c12/ex09/ft_list_foreach.c +++ b/c12/ex09/ft_list_foreach.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_foreach.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 08:29:57 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:31:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_list_foreach(t_list *begin_list, void (*f)(void *)) +{ + while (begin_list != NULL) + { + (*f)(begin_list->data); + begin_list = begin_list->next; + } +} diff --git a/c12/ex10/ft_list.h b/c12/ex10/ft_list.h new file mode 100644 index 0000000..5a8dab5 --- /dev/null +++ b/c12/ex10/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:27:25 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/c12/ex10/ft_list_foreach_if.c b/c12/ex10/ft_list_foreach_if.c index e69de29..944e31b 100644 --- a/c12/ex10/ft_list_foreach_if.c +++ b/c12/ex10/ft_list_foreach_if.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_foreach_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 08:31:11 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:34:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_list_foreach_if(t_list *begin_list, void (*f)(void *), void + *data_ref, int (*cmp)()) +{ + while (begin_list != NULL) + if ((*cmp)(begin_list->data, data_ref) == 0) + (*f)(begin_list->data); +} diff --git a/c12/ex11/ft_list.h b/c12/ex11/ft_list.h new file mode 100644 index 0000000..698a86c --- /dev/null +++ b/c12/ex11/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:27:07 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/c12/ex11/ft_list_find.c b/c12/ex11/ft_list_find.c index e69de29..e0d22e8 100644 --- a/c12/ex11/ft_list_find.c +++ b/c12/ex11/ft_list_find.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 08:35:32 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:38:01 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +t_list *ft_list_find(t_list *begin_list, void *data_ref, int (*cmp)()) +{ + while (begin_list != NULL) + { + if ((*cmp)(begin_list->data, data_ref) == 0) + break ; + begin_list = begin_list->next; + } + return (begin_list); +} diff --git a/c12/ex12/ft_list.h b/c12/ex12/ft_list.h new file mode 100644 index 0000000..0f7f09a --- /dev/null +++ b/c12/ex12/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:26:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/c12/ex12/ft_list_remove_if.c b/c12/ex12/ft_list_remove_if.c index e69de29..dc7f310 100644 --- a/c12/ex12/ft_list_remove_if.c +++ b/c12/ex12/ft_list_remove_if.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_remove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 08:38:28 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:49:43 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(), void (*free_fct)(void *)) +{ + t_list *prev; + t_list *cursor; + + prev = NULL; + cursor = *begin_list; + while (cursor) + { + if ((*cmp)(cursor->data, data_ref)) + { + if (prev) + prev->next = cursor->next; + (*free_fct)(cursor->data); + } + cursor = cursor->next; + } +} diff --git a/c12/ex13/ft_list.h b/c12/ex13/ft_list.h new file mode 100644 index 0000000..f0a6674 --- /dev/null +++ b/c12/ex13/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:26:38 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/c12/ex13/ft_list_merge.c b/c12/ex13/ft_list_merge.c index e69de29..4de2ff3 100644 --- a/c12/ex13/ft_list_merge.c +++ b/c12/ex13/ft_list_merge.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 08:50:01 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:51:36 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_list_merge(t_list **begin_list1, t_list *begin_list2) +{ + t_list *cursor; + + cursor = *begin_list1; + while (cursor->next) + cursor = cursor->next; + cursor->next = begin_list2; +} diff --git a/c12/ex14/ft_list.h b/c12/ex14/ft_list.h new file mode 100644 index 0000000..cf65023 --- /dev/null +++ b/c12/ex14/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:26:25 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/c12/ex14/ft_list_sort.c b/c12/ex14/ft_list_sort.c index e69de29..698d345 100644 --- a/c12/ex14/ft_list_sort.c +++ b/c12/ex14/ft_list_sort.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 13:43:11 by cacharle #+# #+# */ +/* Updated: 2019/07/19 13:53:54 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +static int is_sorted(t_list **begin_list, int (*cmp)()) +{ + t_list *cursor; + + while (cursor->next) + { + if ((*cmp)(cursor->data, cursor->next->data) > 0) + return (0); + cursor = cursor->next; + } + return (1); +} + +void ft_list_sort(t_list **begin_list, int (*cmp)()) +{ + t_list *cursor; + void *tmp; + + while (!is_sorted(*begin_list, cmp)) + { + cursor = *begin_list; + while (cursor->next) + { + if ((*cmp)(cursor->data, cursor->next->data) > 0) + { + tmp = cursor->data; + cursor->data = cursor->data->next; + cursor->data->next = tmp; + } + cursor = cursor->next; + } + } +} diff --git a/c12/ex15/ft_list.h b/c12/ex15/ft_list.h new file mode 100644 index 0000000..5eafe75 --- /dev/null +++ b/c12/ex15/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:26:12 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif diff --git a/c12/ex15/ft_list_reverse_fun.c b/c12/ex15/ft_list_reverse_fun.c index e69de29..9ee845e 100644 --- a/c12/ex15/ft_list_reverse_fun.c +++ b/c12/ex15/ft_list_reverse_fun.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_reverse_fun.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/19 13:55:34 by cacharle #+# #+# */ +/* Updated: 2019/07/20 08:18:04 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +static t_list *reverse_rec(t_list *begin_list) +{ + if (!begin_list) + return begin_list; + if (!begin_list->next) + return begin_list; + ft_list_reverse_fun(begin_list->next); + begin_list->next->next = begin_list; + begin_list->next = NULL; +} + +void ft_list_reverse_fun(t_list *begin_list) +{ + begin_list = reverse_rec(begin_list); +} diff --git a/c12/ex16/ft_list.h b/c12/ex16/ft_list.h new file mode 100644 index 0000000..8b736b4 --- /dev/null +++ b/c12/ex16/ft_list.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/09 14:42:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +t_list *ft_create_elem(void *data); + +#endif diff --git a/c12/ex16/ft_sorted_list_insert.c b/c12/ex16/ft_sorted_list_insert.c index e69de29..2ca345a 100644 --- a/c12/ex16/ft_sorted_list_insert.c +++ b/c12/ex16/ft_sorted_list_insert.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sorted_list_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 08:19:32 by cacharle #+# #+# */ +/* Updated: 2019/07/20 08:23:17 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_sorted_list_insert(t_list **begin_list, void *data, int (*cmp)()) +{ + +} diff --git a/c12/ex17/ft_list.h b/c12/ex17/ft_list.h new file mode 100644 index 0000000..ef3a012 --- /dev/null +++ b/c12/ex17/ft_list.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ +/* Updated: 2019/07/19 08:25:54 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +#define FT_LIST_H + +typedef struct s_list +{ + struct s_list *next; + void *data; +} t_list; + +#endif @@ -24,34 +24,40 @@ void ft_free(void *data); int main() { + int a = 1; + int b = 2; + int c = 3; + int d = 4; + int e = 5; + int f = 6; + int g = 7; t_list *list = NULL; - int a = 4; - void *data = &a; - list = ft_create_elem(data); - printf("%d\n", *(int*)list->data); + list = ft_create_elem(&a); + printf("%d", *(int*)list->data); - char b = 'g'; - data = &b; - ft_list_push_front(&list, data); - printf("%c\n", *(char*)list->data); - printf("%d\n", *(char*)list->next->data); - printf("%s\n", (char*)list->next->next); + printf("\n--------------\n"); + ft_list_push_front(&list, &b); + printf("%d\n", *(int*)list->data); + printf("%d\n", *(int*)list->next->data); + printf("%s", (char*)list->next->next); + printf("\n--------------\n"); printf("size %d\n", ft_list_size(list)); - ft_list_push_front(&list, data); - printf("size %d\n", ft_list_size(list)); + ft_list_push_front(&list, &c); + printf("size %d", ft_list_size(list)); - printf("last %d\n", *(int*)ft_list_last(list)->data); + printf("\n--------------\n"); + printf("last %d", *(int*)ft_list_last(list)->data); - double c = 3.14; - data = &c; - ft_list_push_back(&list, data); - printf("list last %f\n", *(double*)ft_list_last(list)->data); + printf("\n--------------\n"); + ft_list_push_back(&list, &c); + printf("list last %d\n", *(int*)ft_list_last(list)->data); t_list *empty = NULL; - ft_list_push_back(&empty, data); - printf("empty last %f\n", *(double*)ft_list_last(list)->data); + ft_list_push_back(&empty, &e); + printf("empty last %d\n", *(int*)ft_list_last(empty)->data); + printf("\n--------------\n"); char **strs = malloc(sizeof(char*) * 4); strs[0] = malloc(sizeof(char) * 32); strs[1] = malloc(sizeof(char) * 32); @@ -72,6 +78,10 @@ int main() printf("%f\n", *(double*)(ft_list_at(list, 1)->data)); printf("%s\n", (char*)ft_list_at(list, 2)); + printf("\n--------------\n"); + // reverse + + printf("\n--------------\n"); } diff --git a/rush02/ex00/Makefile b/rush02/ex00/Makefile new file mode 100644 index 0000000..e299142 --- /dev/null +++ b/rush02/ex00/Makefile @@ -0,0 +1,38 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/07/20 07:26:11 by cacharle #+# #+# # +# Updated: 2019/07/21 11:46:43 by cacharle ### ########.fr # +# # +# **************************************************************************** # + +OUT = rush-02 +SRC = main.c helper.c parse.c error.c dict.c convert.c +OBJ = $(SRC:.c=.o) + +CC = gcc +CCFLAGS = -Wall -Wextra #-Werror + +.PHONY: all +all: $(OUT) + +$(OUT): $(OBJ) + $(CC) $(CCFLAGS) $(OBJ) -o $(OUT) + +%.o: %.c include.h + $(CC) $(CCFLAGS) -c $< -o $@ + +.PHONY: clean +clean: + rm -f $(OBJ) + +.PHONY: fclean +fclean: clean + rm -f $(OUT) + +.PHONY: re +re: fclean all diff --git a/rush02/ex00/convert.c b/rush02/ex00/convert.c new file mode 100644 index 0000000..6119f9c --- /dev/null +++ b/rush02/ex00/convert.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* convert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yiacono <yiacono@student.s19.be> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 11:14:59 by yiacono #+# #+# */ +/* Updated: 2019/07/21 15:03:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" +#include <unistd.h> + +static int check_arg(t_max_nbr nb, t_dict dict) +{ + if (nb < 0) + return (-1); + if (nb == 0) + { + while (dict->key > 0) + dict++; + ft_putstr(dict->value); + return (0); + } + if ((nb / dict->key) >= 1000) + return (-1); + return (0); +} + +static void effective_convert(t_max_nbr nb, t_dict dict) +{ + t_max_nbr div_result; + + while (nb > 0) + { + div_result = nb / dict->key; + if (div_result) + { + if (div_result == 1 && dict->key >= 100) + { + effective_convert(div_result, dict); + ft_putstr(" "); + } + else if (div_result > 1) + { + effective_convert(div_result, dict); + ft_putstr(" "); + } + ft_putstr(dict->value); + nb = nb % dict->key; + if (nb) + ft_putstr(" "); + } + dict++; + } +} + +int convert(t_max_nbr nb, t_dict dict) +{ + if (check_arg(nb, dict) < 0) + return (-1); + effective_convert(nb, dict); + ft_putstr("\n"); + return (0); +} diff --git a/rush02/ex00/dict.c b/rush02/ex00/dict.c new file mode 100644 index 0000000..f408dbc --- /dev/null +++ b/rush02/ex00/dict.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dict.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 18:44:04 by cacharle #+# #+# */ +/* Updated: 2019/07/21 15:01:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "include.h" + +static int is_sorted(t_dict dict) +{ + int i; + + i = 0; + while (dict[i + 1].key != -1) + { + if (dict[i].key < dict[i + 1].key) + return (FALSE); + i++; + } + return (TRUE); +} + +void sort_dict(t_dict dict) +{ + int i; + t_entry tmp; + + while (!is_sorted(dict)) + { + i = 0; + while (dict[i + 1].key != -1) + { + if (dict[i].key < dict[i + 1].key) + { + tmp = dict[i]; + dict[i] = dict[i + 1]; + dict[i + 1] = tmp; + } + i++; + } + } +} + +int as_uniq_keys(t_dict dict) +{ + int i; + + while (dict->value != NULL) + { + i = 1; + while (dict[i].value != NULL) + { + if (dict->key == dict[i].key) + return (FALSE); + i++; + } + dict++; + } + return (TRUE); +} + +int required_values(t_dict dict) +{ + int i; + t_max_nbr key; + int keys_checked; + + i = 0; + keys_checked = 0; + while (dict[i].key != -1) + { + key = dict[i].key; + if (key == 0 || key == 1 || key == 2 || key == 3 || key == 4 || key == 5 + || key == 6 || key == 7 || key == 8 || key == 9 || key == 10 + || key == 11 || key == 12 || key == 13 || key == 14 || key == 15 + || key == 16 || key == 17 || key == 18 || key == 19 || key == 20 + || key == 30 || key == 40 || key == 50 || key == 60 || key == 70 + || key == 80 || key == 90 || key == 100 || key == 1000 + || key == 1000000 || key == 1000000000) + keys_checked++; + i++; + } + return (keys_checked == REQUIRED_SIZE); +} + +/* +** destroy dict, by freeing each value and the dict itself +*/ + +void destroy_dict(t_dict dict, int size) +{ + int i; + + if (dict == NULL) + return ; + i = 0; + if (size == -1) + while (dict[i].key != -1) + { + free(dict[i].value); + i++; + } + else + while (--size > 0) + free(dict[size].value); + free(dict); +} diff --git a/rush02/ex00/error.c b/rush02/ex00/error.c new file mode 100644 index 0000000..27dba41 --- /dev/null +++ b/rush02/ex00/error.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yiacono <yiacono@student.s19.be> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 10:55:27 by agassin #+# #+# */ +/* Updated: 2019/07/21 14:17:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +/* +** similar to atoi but doesn't allow anything before or after ('-' included) +** return the number or -1 in case of error +*/ + +t_max_nbr strict_atoi(char *str) +{ + t_max_nbr nb; + int i; + + nb = 0; + i = 0; + while (str[i]) + { + if (str[i] < '0' || str[i] > '9') + return (-1); + i++; + } + if (i == 0) + return (-1); + i = 0; + while (str[i]) + { + nb *= 10; + nb += str[i++] - '0'; + } + return (nb); +} + +int print_error_if(int status, t_dict dict) +{ + if (status) + { + ft_putstr("Error\n"); + destroy_dict(dict, -1); + } + return (status); +} + +int print_dict_error_if(int status, t_dict dict) +{ + if (status) + { + ft_putstr("Dict Error\n"); + destroy_dict(dict, -1); + } + return (status); +} diff --git a/rush02/ex00/helper.c b/rush02/ex00/helper.c new file mode 100644 index 0000000..e29eb47 --- /dev/null +++ b/rush02/ex00/helper.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 07:25:21 by cacharle #+# #+# */ +/* Updated: 2019/07/21 08:23:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> +#include "include.h" + +/* +** Read an already openned file in `file` +** return the file_size (-1 if a error occurs) +*/ + +int read_file(int fildes, char **file_ptr) +{ + char buf[BUF_SIZE]; + int file_size; + int read_size; + + file_size = 0; + while ((read_size = read(fildes, buf, BUF_SIZE))) + { + if (read_size < 0) + return (-1); + *file_ptr = ft_memcat(*file_ptr, buf, file_size, read_size); + file_size += read_size; + } + return (file_size); +} + +/* +** Concatenate buf[buf_size] into file[file_size] +** Create a copy of the file, free file, +** reallocate it with sufficient memory for the concatenation +** copy each character of file_copy then buf into file +** free file_copy +*/ + +char *ft_memcat(char *file, char buf[BUF_SIZE], int file_size, + int buf_size) +{ + int i; + char *file_copy; + + if ((file_copy = malloc(sizeof(char) * (file_size + 1))) == NULL) + return (NULL); + i = -1; + while (++i < file_size) + file_copy[i] = file[i]; + free(file); + if ((file = malloc(sizeof(char) * (file_size + buf_size + 1))) == NULL) + return (NULL); + i = 0; + while (i < file_size) + { + file[i] = file_copy[i]; + i++; + } + free(file_copy); + while (i < file_size + buf_size) + { + file[i] = buf[i - file_size]; + i++; + } + return (file); +} + +char *ft_strndup(char *src, int n) +{ + int i; + char *dup_ptr; + + dup_ptr = (char*)malloc(sizeof(char) * (n + 1)); + if (dup_ptr == NULL) + return (NULL); + i = 0; + while (src[i] && i < n) + { + dup_ptr[i] = src[i]; + i++; + } + dup_ptr[i] = '\0'; + return (dup_ptr); +} + +/* +** Print a string char by char on the standard output +*/ + +void ft_putstr(char *str) +{ + while (*str) + { + write(1, str, 1); + str++; + } +} diff --git a/rush02/ex00/include.h b/rush02/ex00/include.h new file mode 100644 index 0000000..f50f846 --- /dev/null +++ b/rush02/ex00/include.h @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* include.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yiacono <yiacono@student.s19.be> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 07:31:47 by cacharle #+# #+# */ +/* Updated: 2019/07/21 14:51:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef INCLUDE_H +# define INCLUDE_H + +# define TRUE 1 +# define FALSE 0 +# define BUF_SIZE 1024 +# define REQUIRED_SIZE 32 + +typedef long long int t_max_nbr; +struct s_entry +{ + t_max_nbr key; + char *value; +}; +typedef struct s_entry t_entry; +typedef t_entry* t_dict; +/* +** helper.c - previously made functions +*/ + +int read_file(int fildes, char **file); +char *ft_memcat(char *file, char buf[BUF_SIZE], int file_size, + int buf_size); +char *ft_strndup(char *src, int n); +void ft_putstr(char *str); + +/* +** convert.c - Convert number to string +*/ + +int convert(t_max_nbr nb, t_dict dict); + +/* +** parse.c - Dictionnary reading and converting +*/ + +t_dict parse_dict(char *file, int file_size); +t_dict parse_dict_file(char *filename); +int set_entry(char *line, t_entry *entry); +char *filtered_value(char *str); +int count_lines(char *file); + +/* +** dict.c - operation on dict +*/ + +void sort_dict(t_dict dict); +int required_values(t_dict dict); +int as_uniq_keys(t_dict dict); +void destroy_dict(t_dict dict, int size); + +/* +** error.c - Error checking +*/ + +t_max_nbr strict_atoi(char *str); +int print_error_if(int status, t_dict dict); +int print_dict_error_if(int status, t_dict dict); + +#endif diff --git a/rush02/ex00/main.c b/rush02/ex00/main.c new file mode 100644 index 0000000..14085f6 --- /dev/null +++ b/rush02/ex00/main.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yiacono <yiacono@student.s19.be> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 07:25:21 by cacharle #+# #+# */ +/* Updated: 2019/07/21 15:02:43 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "include.h" + +int main(int argc, char **argv) +{ + t_dict dict; + t_max_nbr nb; + + if (print_error_if(argc > 3 || argc == 1, NULL)) + return (1); + if (argc == 2) + { + dict = parse_dict_file("numbers.dict"); + nb = strict_atoi(argv[1]); + } + if (argc == 3) + { + dict = parse_dict_file(argv[1]); + nb = strict_atoi(argv[2]); + } + if (print_error_if(nb == -1, dict)) + return (1); + if (print_dict_error_if(dict == NULL || !required_values(dict) + || !as_uniq_keys(dict), dict)) + return (1); + sort_dict(dict); + if (print_error_if(convert(nb, dict), dict)) + return (1); + destroy_dict(dict, -1); + return (0); +} diff --git a/rush02/ex00/numbers.dict b/rush02/ex00/numbers.dict new file mode 100644 index 0000000..72e09c9 --- /dev/null +++ b/rush02/ex00/numbers.dict @@ -0,0 +1,32 @@ +0: zero +1: one +2: two +3: three +4: four +5: five +6: six +7: seven +8: eight +9: nine +10: ten +11: eleven +12: twelve +13: thirteen +14: fourteen +15: fifteen +16: sixteen +17: seventeen +18: eighteen +19: nineteen +20: twenty +30: thirty +40: forty +50: fifty +60: sixty +70: seventy +80: eighty +90: ninety +100: hundred +1000: thousand +1000000: million +1000000000: billion diff --git a/rush02/ex00/parse.c b/rush02/ex00/parse.c new file mode 100644 index 0000000..af3d43d --- /dev/null +++ b/rush02/ex00/parse.c @@ -0,0 +1,165 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agassin <agassin@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/20 10:47:14 by cacharle #+# #+# */ +/* Updated: 2019/07/21 14:44:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include "include.h" + +/* +** Open the dictionnary file, pass it to `parse_dict` +** return the dict, NULL if an error occurs +*/ + +t_dict parse_dict_file(char *filename) +{ + char *file; + int fildes; + int file_size; + t_dict dict; + + if ((fildes = open(filename, O_RDONLY)) < 0) + return (NULL); + file = NULL; + if ((file_size = read_file(fildes, &file)) < 0) + return (NULL); + if ((dict = parse_dict(file, file_size)) == NULL) + { + destroy_dict(dict, -1); + free(file); + return (NULL); + } + free(file); + if (close(fildes) < 0) + return (NULL); + return (dict); +} + +/* +** Parse the file and return a dictionary where the keys are +** the numbers and the values the string attached to it +*/ + +t_dict parse_dict(char *file, int file_size) +{ + int i; + int j; + t_dict dict; + + dict = (t_dict)malloc(sizeof(t_entry) * (count_lines(file) + 1)); + if (dict == NULL) + return (NULL); + i = 0; + j = 0; + while (i < file_size) + { + while (file[i] && file[i] == '\n') + i++; + if (file[i] && (set_entry(&file[i], &dict[j++])) == -1) + { + destroy_dict(dict, j - 1); + return (NULL); + } + while (file[i] && file[i] != '\n') + i++; + } + dict[j].key = -1; + dict[j].value = NULL; + return (dict); +} + +/* +** Set the key and value of an entry based on the given line +** return -1 if an error occurs +*/ + +int set_entry(char *line, t_entry *entry) +{ + int i; + char *tmp; + + i = 0; + while (line[i] >= '0' && line[i] <= '9') + i++; + if (i == 0) + return (-1); + tmp = ft_strndup(line, i); + entry->key = strict_atoi(tmp); + free(tmp); + while (line[i] == ' ') + i++; + if (line[i] != ':') + return (-1); + i++; + while (line[i] == ' ') + i++; + entry->value = filtered_value(&line[i]); + if (entry->value == NULL) + return (-1); + return (0); +} + +/* +** Duplicate the line and compress spaces until there is only one left +*/ + +char *filtered_value(char *str) +{ + int i; + int j; + char *value; + + i = 0; + while (str[i] != '\n') + i++; + if (i == 0) + return (NULL); + if ((value = (char*)malloc(sizeof(char) * (i + 1))) == NULL) + return (NULL); + i = 0; + j = 0; + while (str[i] && str[i] != '\n') + { + value[j++] = str[i++]; + while (str[i] && str[i] == ' ') + { + if (str[i + 1] && str[i + 1] != ' ') + break ; + i++; + } + } + value[i] = '\0'; + return (value); +} + +/* +** count the number of line in the file +** without counting empty lines +*/ + +int count_lines(char *file) +{ + int counter; + + counter = 0; + while (*file) + { + if (*file == '\n') + { + counter++; + while (*file == '\n') + file++; + } + file++; + } + return (counter); +} diff --git a/rush02/ex00/test.dict b/rush02/ex00/test.dict new file mode 100644 index 0000000..bbd8b5b --- /dev/null +++ b/rush02/ex00/test.dict @@ -0,0 +1,49 @@ + +11: bonjour +13: thir +14 : fourteen + + + +0: zero +1 : one +2: two +3: three +5: five + + + + +6: six +12: twelve +4: four +7: seven +8: eight +9: nines +10: ten +15: fifteen +16: sixteen +17: seventeen +18: eighteen +19: nineteen +20: twenty +21 : bonjour je suis +30: thirty +40: forty +50: fifty +60: sixty +70: seventy +80: eighty +90: ninety +100 : hundred +1000: thousand +1000000 : million +1000000000 : billion +1000000000000 : trillion +1000000000000000 : quadrillion +1000000000000000000 : quintillion +1000000000000000000000 : hexillion +1000000000000000000000000 : heptillion + + + |
