diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2afc796 --- /dev/null +++ b/Makefile @@ -0,0 +1,97 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fanno +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2016/02/23 16:40:31 by fanno #+# #+# # +# Updated: 2016/02/25 10:03:34 by fanno ### ########.fr # +# # +# **************************************************************************** # + +NAME = fillit + +CC = gcc +FLAGS = #-Wall -Werror -Wextra + +LIB_PATH = libft +LIB = $(LIB_PATH)/libft.a +LIB_LINK = -L $(LIB_PATH) -lft + +INC_DIR = includes +INCS = -I $(LIB_PATH)/$(INC_DIR) -I $(INC_DIR) + +SRC_DIR = src +SRC_BASE = ft_check_ir0.c \ + ft_check_ir90.c \ + ft_check_jr0.c \ + ft_check_jr180.c \ + ft_check_jr270.c \ + ft_check_jr90.c \ + ft_check_lr0.c \ + ft_check_lr180.c \ + ft_check_lr270.c \ + ft_check_lr90.c \ + ft_check_or0.c \ + ft_check_sr0.c \ + ft_check_sr90.c \ + ft_check_tiles.c \ + ft_check_tr0.c \ + ft_check_tr180.c \ + ft_check_tr270.c \ + ft_check_tr90.c \ + ft_check_zr0.c \ + ft_check_zr90.c \ + ft_get_file.c \ + ft_get_struct.c \ + ft_print_grid.c \ + ft_solve.c \ + ft_solve_2.c \ + ft_split_file_content.c \ + ft_strstr_binary.c \ + main.c + +OBJ_DIR = obj + +SRCS = $(addprefix $(SRC_DIR)/, $(SRC_BASE)) +OBJS = $(addprefix $(OBJ_DIR)/, $(SRC_BASE:.c=.o)) + +C_NO = "\033[00m" +C_OK = "\033[35m" +C_GOOD = "\033[32m" +C_ERROR = "\033[31m" +C_WARN = "\033[33m" + +SUCCESS = $(C_GOOD)SUCCESS$(C_NO) +OK = $(C_OK)OK$(C_NO) + +all: obj $(NAME) + +$(NAME): $(LIB) $(OBJS) + @$(CC) $(FLAGS) -o $@ $^ $(LIB_LINK) + @echo "Compiling" [ $(NAME) ] $(SUCCES) + +$(LIB): + @make -C $(LIB_PATH) + +obj: + @mkdir -p obj + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/*.h + @$(CC) $(FLAGS) $(INCS) -c -o $@ $< + @echo "Linking" [ $< ] $(OK) + +clean: + @rm -f $(OBJS) + @rm -rf $(OBJ_DIR) + @echo "Cleaning" [ $(NAME) ] "..." $(OK) + +fclean: clean + @rm -f $(NAME) + @make -C $(LIB_PATH) fclean + @echo "Delete" [ $(NAME) ] $(OK) + +re: fclean all + +.PHONY: clean all re fclean diff --git a/includes/fillit.h b/includes/fillit.h new file mode 100644 index 0000000..23a9e6a --- /dev/null +++ b/includes/fillit.h @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fillit.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/12 12:00:51 by gbrochar #+# #+# */ +/* Updated: 2016/02/25 10:04:01 by fanno ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FILLIT_H +# define FILLIT_H + +# include +# include +# include +# include "../libft/libft.h" + +typedef struct s_tetromino +{ + char type; + char x; + char y; +} t_tetromino; + +int ft_strstr_binary(const char *s1, char *s2); +void ft_replace_linefeed(char **tiles); +char **ft_create_types(void); +char **ft_allocate_types(void); + +t_tetromino *ft_get_file(char *file_name); +t_tetromino *ft_split_file_content(char *file_content, int tiles_nb); +t_tetromino *ft_check_tiles(char **tiles); +t_tetromino *ft_get_struct(char **tiles); + +void ft_solve(t_tetromino *tiles); +int ft_solve_2(char grid[22][23], t_tetromino *tiles, int nb); +void ft_print_grid(char grid[22][23]); + +void init(int (*f_check[19])(char[22][23], t_tetromino *, int)); +void ft_init_grid(char grid[22][23], t_tetromino *tiles); + +int ft_check_ir0(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_ir90(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_or0(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_tr0(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_tr90(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_tr180(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_tr270(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_sr0(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_sr90(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_zr0(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_zr90(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_lr0(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_lr90(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_lr180(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_lr270(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_jr0(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_jr90(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_jr180(char grid[22][23], t_tetromino *tiles, int nb); +int ft_check_jr270(char grid[22][23], t_tetromino *tiles, int nb); + +#endif diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..d758806 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,46 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: gbrochar +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2015/11/27 19:26:17 by gbrochar #+# #+# # +# Updated: 2015/12/09 16:47:26 by gbrochar ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a + +SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \ +ft_isprint.c ft_isspace.c ft_itoa.c ft_lstadd.c ft_lstdel.c ft_lstdelone.c \ +ft_lstiter.c ft_lstmap.c ft_lstnew.c ft_memalloc.c ft_memccpy.c ft_memchr.c \ +ft_memcmp.c ft_memcpy.c ft_memdel.c ft_memmove.c ft_memset.c ft_power.c \ +ft_putchar.c ft_putchar_fd.c ft_putendl.c ft_putendl_fd.c ft_putnbr.c \ +ft_putnbr_fd.c ft_putstr.c ft_putstr_fd.c ft_sort_int.c ft_sqrt.c ft_strcat.c \ +ft_strchr.c ft_strclr.c ft_strcmp.c ft_strcpy.c ft_strdel.c ft_strdup.c \ +ft_strequ.c ft_striter.c ft_striteri.c ft_strjoin.c ft_strlcat.c ft_strlen.c \ +ft_strmap.c ft_strmapi.c ft_strncat.c ft_strncmp.c ft_strncpy.c ft_strnequ.c \ +ft_strnew.c ft_strnstr.c ft_strrchr.c ft_strsplit.c ft_strstr.c ft_strsub.c \ +ft_strtrim.c ft_swap.c ft_tolower.c ft_toupper.c + +OBJ = $(SRC:.c=.o) + +CC = gcc + +FLAGS = #-Wall -Werror -Wextra + +all: $(NAME) + +$(NAME): + $(CC) $(FLAGS) -c $(SRC) + ar rc $(NAME) $(OBJ) + ranlib $(NAME) + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +re: fclean all diff --git a/libft/auteur b/libft/auteur new file mode 100644 index 0000000..b363db4 --- /dev/null +++ b/libft/auteur @@ -0,0 +1 @@ +gbrochar diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100644 index 0000000..a1b6129 --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 19:18:43 by gbrochar #+# #+# */ +/* Updated: 2016/02/10 17:16:28 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi(const char *str) +{ + long result; + long sign; + char *str2; + + str2 = (char *)str; + result = 0; + while (ft_isspace(*str2)) + str2++; + sign = (*str2 == '-') ? -1 : 1; + if (*str2 == '-' || *str2 == '+') + str2++; + while (*str2 >= '0' && *str2 <= '9') + { + result = (result * 10) + (*str2 - '0'); + str2++; + } + return ((int)(result * sign)); +} diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c new file mode 100644 index 0000000..e2131b2 --- /dev/null +++ b/libft/ft_bzero.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/24 11:39:18 by gbrochar #+# #+# */ +/* Updated: 2015/11/24 20:46:21 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + char *byte; + + byte = s; + while (n-- > 0) + { + *byte = 0; + byte++; + } +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c new file mode 100644 index 0000000..f21a2cc --- /dev/null +++ b/libft/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:51:03 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:22:14 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c new file mode 100644 index 0000000..e4f61d6 --- /dev/null +++ b/libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:59:08 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:42:21 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return (((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ? 1 : 0)); +} diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c new file mode 100644 index 0000000..c7daff9 --- /dev/null +++ b/libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:48:25 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:32:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return ((c >= 0 && c <= 127) ? 1 : 0); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100644 index 0000000..e850324 --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:53:19 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 14:41:12 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return ((c >= '0' && c <= '9') ? 1 : 0); +} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c new file mode 100644 index 0000000..72badb5 --- /dev/null +++ b/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:44:48 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:33:24 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return ((c >= 32 && c <= 126) ? 1 : 0); +} diff --git a/libft/ft_isspace.c b/libft/ft_isspace.c new file mode 100644 index 0000000..89eb16a --- /dev/null +++ b/libft/ft_isspace.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 14:37:03 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 14:39:18 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(int c) +{ + if (c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r' + || c == ' ') + return (1); + return (0); +} diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c new file mode 100644 index 0000000..8439491 --- /dev/null +++ b/libft/ft_itoa.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:56:59 by gbrochar #+# #+# */ +/* Updated: 2015/12/05 16:22:09 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_itoa(int n) +{ + char *str; + size_t str_size; + int n_mem; + + n_mem = n; + str_size = (n < 0) ? 3 : 2; + while ((n > 9 || n < -9) && str_size++) + n /= 10; + str = (char *)malloc((str_size--) * sizeof(char)); + if (!str) + return (NULL); + str[str_size--] = '\0'; + while (n_mem > 9 || n_mem < -9) + { + str[str_size--] = (n_mem < 0) ? -(n_mem % 10) + '0' : n_mem % 10 + '0'; + n_mem = n_mem / 10; + } + str[0] = (n_mem < 0) ? '-' : (n_mem + '0'); + str[1] = (n_mem < 0) ? (-n_mem + '0') : str[1]; + return (str); +} diff --git a/libft/ft_lstadd.c b/libft/ft_lstadd.c new file mode 100644 index 0000000..411b6eb --- /dev/null +++ b/libft/ft_lstadd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/01 14:24:17 by gbrochar #+# #+# */ +/* Updated: 2015/12/02 12:57:13 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd(t_list **alst, t_list *new) +{ + new->next = *alst; + *alst = new; +} diff --git a/libft/ft_lstdel.c b/libft/ft_lstdel.c new file mode 100644 index 0000000..9c02c08 --- /dev/null +++ b/libft/ft_lstdel.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/30 21:34:50 by gbrochar #+# #+# */ +/* Updated: 2015/12/05 15:10:37 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) +{ + t_list *tmp; + + while (*alst) + { + tmp = *alst; + ft_lstdelone(alst, del); + *alst = tmp->next; + } +} diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c new file mode 100644 index 0000000..3448e9a --- /dev/null +++ b/libft/ft_lstdelone.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/30 21:27:05 by gbrochar #+# #+# */ +/* Updated: 2015/12/02 13:28:32 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) +{ + del((*alst)->content, (*alst)->content_size); + ft_memdel((void **)alst); +} diff --git a/libft/ft_lstiter.c b/libft/ft_lstiter.c new file mode 100644 index 0000000..5cddff7 --- /dev/null +++ b/libft/ft_lstiter.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/02 13:38:16 by gbrochar #+# #+# */ +/* Updated: 2015/12/04 16:41:19 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + t_list *start; + t_list *tmp; + + start = lst; + while (lst->next) + { + tmp = lst; + f(lst); + lst = tmp->next; + } + f(lst); + lst = start; +} diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c new file mode 100644 index 0000000..8341e24 --- /dev/null +++ b/libft/ft_lstmap.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/02 14:33:07 by gbrochar #+# #+# */ +/* Updated: 2015/12/02 16:17:18 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *lst_begin; + t_list *lst_new; + + lst_begin = ft_lstnew((f(lst))->content, (f(lst))->content_size); + lst_new = lst_begin; + lst = lst->next; + while (lst->next) + { + lst_new->next = ft_lstnew((f(lst))->content, (f(lst))->content_size); + lst_new = lst_new->next; + lst = lst->next; + } + lst_new->next = ft_lstnew((f(lst))->content, (f(lst))->content_size); + return (lst_begin); +} diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c new file mode 100644 index 0000000..c0b09bf --- /dev/null +++ b/libft/ft_lstnew.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/30 21:21:09 by gbrochar #+# #+# */ +/* Updated: 2015/12/05 15:10:20 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *lst; + + lst = (t_list *)malloc(sizeof(t_list)); + if (!lst) + return (NULL); + if (!content) + { + lst->content = NULL; + lst->content_size = 0; + } + else + { + lst->content = ft_memalloc(content_size); + if (!lst->content) + { + free(lst); + return (NULL); + } + ft_memmove(lst->content, content, content_size); + lst->content_size = content_size; + } + lst->next = NULL; + return (lst); +} diff --git a/libft/ft_memalloc.c b/libft/ft_memalloc.c new file mode 100644 index 0000000..92dcd59 --- /dev/null +++ b/libft/ft_memalloc.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 18:11:36 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:28:31 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memalloc(size_t size) +{ + unsigned char *ptr; + size_t i; + + i = 0; + ptr = (unsigned char *)malloc(size); + if (!ptr) + return (NULL); + while (i < size) + ptr[i++] = 0; + return (ptr); +} diff --git a/libft/ft_memccpy.c b/libft/ft_memccpy.c new file mode 100644 index 0000000..0c624ee --- /dev/null +++ b/libft/ft_memccpy.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/24 14:40:00 by gbrochar #+# #+# */ +/* Updated: 2015/12/08 14:37:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memccpy(void *dst, const void *src, int c, size_t n) +{ + size_t i; + unsigned char to_find; + unsigned char *dst_byte; + unsigned char *src_byte; + + to_find = (unsigned char)c; + i = 0; + dst_byte = (unsigned char *)dst; + src_byte = (unsigned char *)src; + while (i != n && src_byte[i] != to_find) + { + dst_byte[i] = src_byte[i]; + i++; + } + if (src_byte[i] == to_find) + { + dst_byte[i] = to_find; + return (&dst[i + 1]); + } + return (NULL); +} diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c new file mode 100644 index 0000000..fc6a9d9 --- /dev/null +++ b/libft/ft_memchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 13:03:54 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 14:07:56 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + unsigned char to_find; + unsigned char *str; + size_t i; + + i = 0; + to_find = c; + str = (unsigned char *)s; + while (i < n) + { + if (str[i] == to_find) + return ((void *)&s[i]); + i++; + } + return (NULL); +} diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c new file mode 100644 index 0000000..18aa48e --- /dev/null +++ b/libft/ft_memcmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:13:46 by gbrochar #+# #+# */ +/* Updated: 2015/11/30 14:46:05 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *s1_byte; + unsigned char *s2_byte; + size_t i; + + s1_byte = (unsigned char *)s1; + s2_byte = (unsigned char *)s2; + i = 0; + if (n <= 0) + return (0); + while (s1_byte[i] == s2_byte[i]) + { + if (i + 1 == n) + return (0); + i++; + } + return (s1_byte[i] - s2_byte[i]); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100644 index 0000000..8a42505 --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:42:55 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 14:43:02 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + size_t i; + unsigned char *dst_byte; + const unsigned char *src_byte; + + i = 0; + dst_byte = dst; + src_byte = src; + while (i < n) + { + dst_byte[i] = src_byte[i]; + i++; + } + return (dst); +} diff --git a/libft/ft_memdel.c b/libft/ft_memdel.c new file mode 100644 index 0000000..cd84e74 --- /dev/null +++ b/libft/ft_memdel.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 18:23:10 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 18:25:10 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_memdel(void **ap) +{ + free(*ap); + *ap = NULL; +} diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c new file mode 100644 index 0000000..ce42e2a --- /dev/null +++ b/libft/ft_memmove.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 12:36:04 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 13:03:31 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + unsigned char *dst_byte; + const unsigned char *src_byte; + size_t index; + + dst_byte = dst; + src_byte = src; + if (dst_byte > src_byte) + { + index = len; + while (index-- > 0) + dst_byte[index] = src_byte[index]; + } + else + { + index = 0; + while (index < len) + { + dst_byte[index] = src_byte[index]; + index++; + } + } + return (dst); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c new file mode 100644 index 0000000..8606775 --- /dev/null +++ b/libft/ft_memset.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/23 17:53:16 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 12:18:32 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *b, int c, size_t len) +{ + unsigned char *byte; + size_t i; + + byte = b; + i = 0; + while (i++ < len) + { + *byte = c; + byte++; + } + return (b); +} diff --git a/libft/ft_power.c b/libft/ft_power.c new file mode 100644 index 0000000..130d4a4 --- /dev/null +++ b/libft/ft_power.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_power.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/05 15:23:07 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:03:41 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_power(int nb, int pow) +{ + int ret; + + ret = 1; + if (pow < 0) + return (-1); + while (pow--) + ret *= nb; + return (ret); +} diff --git a/libft/ft_putchar.c b/libft/ft_putchar.c new file mode 100644 index 0000000..82125ba --- /dev/null +++ b/libft/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/24 12:01:00 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 16:47:24 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c new file mode 100644 index 0000000..4fc2ee7 --- /dev/null +++ b/libft/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:01:19 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:02:06 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libft/ft_putendl.c b/libft/ft_putendl.c new file mode 100644 index 0000000..52a3be5 --- /dev/null +++ b/libft/ft_putendl.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:04:13 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:04:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl(char const *s) +{ + ft_putstr(s); + ft_putchar('\n'); +} diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c new file mode 100644 index 0000000..de5409b --- /dev/null +++ b/libft/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:04:58 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:05:51 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char const *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/libft/ft_putnbr.c b/libft/ft_putnbr.c new file mode 100644 index 0000000..7e6ab30 --- /dev/null +++ b/libft/ft_putnbr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 08:58:30 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:49:28 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr(int n) +{ + if (n == -2147483648) + ft_putstr("-2147483648"); + else if (n < 0) + { + ft_putchar('-'); + ft_putnbr(-n); + } + else if (n > 9) + { + ft_putnbr(n / 10); + ft_putchar(n % 10 + '0'); + } + else + ft_putchar(n + '0'); +} diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..070cfd2 --- /dev/null +++ b/libft/ft_putnbr_fd.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:10:52 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:49:38 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n == -2147483648) + ft_putstr_fd("-2147483648", fd); + else if (n < 0) + { + ft_putchar_fd('-', fd); + ft_putnbr_fd(-n, fd); + } + else if (n > 9) + { + ft_putnbr_fd(n / 10, fd); + ft_putchar_fd(n % 10 + '0', fd); + } + else + ft_putchar_fd(n + '0', fd); +} diff --git a/libft/ft_putstr.c b/libft/ft_putstr.c new file mode 100644 index 0000000..4c13770 --- /dev/null +++ b/libft/ft_putstr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:23:12 by gbrochar #+# #+# */ +/* Updated: 2016/02/10 15:14:09 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + write(1, s, ft_strlen(s)); +} diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c new file mode 100644 index 0000000..a7d909a --- /dev/null +++ b/libft/ft_putstr_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:02:48 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 09:04:05 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char const *s, int fd) +{ + while (*s) + write(fd, s++, 1); +} diff --git a/libft/ft_sort_int.c b/libft/ft_sort_int.c new file mode 100644 index 0000000..9ed36cd --- /dev/null +++ b/libft/ft_sort_int.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sort_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/05 18:36:08 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:04:19 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sort_int(int *tab, int size) +{ + int i; + int tmp; + + i = 0; + while ((i + 1) < size) + { + if (tab[i] > tab[i + 1]) + { + tmp = tab[i]; + tab[i] = tab[i + 1]; + tab[i + 1] = tmp; + i = 0; + } + else + i++; + } +} diff --git a/libft/ft_sqrt.c b/libft/ft_sqrt.c new file mode 100644 index 0000000..7d71faf --- /dev/null +++ b/libft/ft_sqrt.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sqrt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/07 08:28:11 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 08:50:43 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +long ft_sqrt(long n) +{ + long sqrt; + + sqrt = 0; + while ((sqrt * sqrt) != n && (sqrt * sqrt) < n) + sqrt++; + return (((sqrt * sqrt) == n) ? (sqrt) : (-1)); +} diff --git a/libft/ft_strcat.c b/libft/ft_strcat.c new file mode 100644 index 0000000..d3c8666 --- /dev/null +++ b/libft/ft_strcat.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 16:26:53 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 19:39:14 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *s1, const char *s2) +{ + char *s3; + + s3 = s1; + while (*s3) + s3++; + while (*s2) + *s3++ = *s2++; + *s3 = '\0'; + return (s1); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100644 index 0000000..340018e --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 09:50:37 by gbrochar #+# #+# */ +/* Updated: 2015/11/27 10:23:06 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s != '\0') + if (*s++ == (char)c) + return ((char *)--s); + if ((char)c == '\0') + return ((char *)s); + return (NULL); +} diff --git a/libft/ft_strclr.c b/libft/ft_strclr.c new file mode 100644 index 0000000..6f9dc0a --- /dev/null +++ b/libft/ft_strclr.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 20:22:52 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 21:09:55 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strclr(char *s) +{ + while (*s) + *s++ = '\0'; +} diff --git a/libft/ft_strcmp.c b/libft/ft_strcmp.c new file mode 100644 index 0000000..1d70b10 --- /dev/null +++ b/libft/ft_strcmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 14:52:37 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:34:12 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + while (*s1 || *s2) + if (*s1++ != *s2++) + return ((unsigned char)*--s1 - (unsigned char)*--s2); + return (0); +} diff --git a/libft/ft_strcpy.c b/libft/ft_strcpy.c new file mode 100644 index 0000000..867bb36 --- /dev/null +++ b/libft/ft_strcpy.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 15:10:54 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 15:56:39 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dst, const char *src) +{ + int i; + + i = 0; + while (src[i] != '\0') + { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; + return (dst); +} diff --git a/libft/ft_strdel.c b/libft/ft_strdel.c new file mode 100644 index 0000000..75872b1 --- /dev/null +++ b/libft/ft_strdel.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 20:21:47 by gbrochar #+# #+# */ +/* Updated: 2015/12/08 15:21:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strdel(char **as) +{ + ft_memdel((void **)as); +} diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c new file mode 100644 index 0000000..de3ebb0 --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:47:23 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 19:28:47 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *str; + int len; + int i; + + i = 0; + len = ft_strlen(s1); + str = malloc((len + 1) * sizeof(char)); + if (!str) + return (NULL); + while (i < len) + { + str[i] = s1[i]; + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft/ft_strequ.c b/libft/ft_strequ.c new file mode 100644 index 0000000..0e229a6 --- /dev/null +++ b/libft/ft_strequ.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 08:35:37 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 15:47:45 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strequ(char const *s1, char const *s2) +{ + int i; + + i = 0; + while (s1[i] == s2[i]) + { + if (s1[i] == '\0' && s2[i] == '\0') + return (1); + i++; + } + return (0); +} diff --git a/libft/ft_striter.c b/libft/ft_striter.c new file mode 100644 index 0000000..4748141 --- /dev/null +++ b/libft/ft_striter.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 20:37:55 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:51:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striter(char *s, void (*f)(char *)) +{ + int i; + + i = 0; + while (s[i]) + { + f(&s[i]); + i++; + } +} diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c new file mode 100644 index 0000000..bb633d0 --- /dev/null +++ b/libft/ft_striteri.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 21:02:16 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 10:30:40 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *s)) +{ + unsigned int i; + + i = 0; + while (s[i]) + { + f(i, &s[i]); + i++; + } +} diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100644 index 0000000..ac47fc1 --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:48:11 by gbrochar #+# #+# */ +/* Updated: 2015/12/05 16:25:38 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *str; + size_t len; + size_t i; + + i = 0; + len = ft_strlen(s1) + ft_strlen(s2); + str = (char *)malloc((len + 1) * sizeof(char)); + if (!str) + return (NULL); + while (i < len) + { + if (i < ft_strlen(s1)) + str[i] = s1[i]; + else + str[i] = s2[i - ft_strlen(s1)]; + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c new file mode 100644 index 0000000..9823fd5 --- /dev/null +++ b/libft/ft_strlcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/26 08:00:59 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 15:48:23 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t dst_len; + size_t src_len; + size_t i_src; + size_t i_dst; + + dst_len = ft_strlen(dst); + src_len = ft_strlen(src); + i_src = 0; + i_dst = dst_len; + if (dst_len >= size) + return (size + src_len); + while (src[i_src] && (size-- - (dst_len + 1)) > 0) + dst[i_dst++] = src[i_src++]; + dst[i_dst] = '\0'; + return (dst_len + src_len); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100644 index 0000000..c9e60fa --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:43:32 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 14:46:58 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + int i; + + i = 0; + while (s[i]) + i++; + return (i); +} diff --git a/libft/ft_strmap.c b/libft/ft_strmap.c new file mode 100644 index 0000000..fac7843 --- /dev/null +++ b/libft/ft_strmap.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 21:30:34 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 16:53:26 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + char *str; + int i; + int len; + + len = ft_strlen(s); + i = 0; + str = (char *)malloc((len + 1) * sizeof(char)); + if (!str) + return (NULL); + while (i < len) + { + str[i] = f(s[i]); + i++; + } + str[len] = '\0'; + return (str); +} diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c new file mode 100644 index 0000000..a83e346 --- /dev/null +++ b/libft/ft_strmapi.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 21:39:30 by gbrochar #+# #+# */ +/* Updated: 2015/12/02 16:22:05 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *str; + unsigned int i; + + i = 0; + str = (char *)malloc((ft_strlen(s) + 1) * sizeof(char)); + if (!str) + return (NULL); + while (s[i]) + { + str[i] = f(i, s[i]); + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft/ft_strncat.c b/libft/ft_strncat.c new file mode 100644 index 0000000..9a4b9eb --- /dev/null +++ b/libft/ft_strncat.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 19:29:04 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 12:33:35 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncat(char *s1, const char *s2, size_t n) +{ + size_t i_dst; + size_t i_src; + + i_dst = ft_strlen(s1); + i_src = 0; + while (i_src < n && s2[i_src] != '\0') + { + s1[i_dst] = s2[i_src]; + i_dst++; + i_src++; + } + s1[i_dst] = '\0'; + return (s1); +} diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c new file mode 100644 index 0000000..db3be20 --- /dev/null +++ b/libft/ft_strncmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 14:57:36 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 15:34:45 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + while ((*s1 || *s2) && n--) + if (*s1++ != *s2++) + return ((unsigned char)*--s1 - (unsigned char)*--s2); + return (0); +} diff --git a/libft/ft_strncpy.c b/libft/ft_strncpy.c new file mode 100644 index 0000000..173165b --- /dev/null +++ b/libft/ft_strncpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 16:18:18 by gbrochar #+# #+# */ +/* Updated: 2015/11/25 16:21:09 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncpy(char *dst, const char *src, size_t n) +{ + size_t i; + + i = 0; + while (i != n && src[i] != '\0') + { + dst[i] = src[i]; + i++; + } + while (i != n) + { + dst[i] = '\0'; + i++; + } + return (dst); +} diff --git a/libft/ft_strnequ.c b/libft/ft_strnequ.c new file mode 100644 index 0000000..ece7ced --- /dev/null +++ b/libft/ft_strnequ.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 08:37:07 by gbrochar #+# #+# */ +/* Updated: 2015/12/05 16:24:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + size_t i; + + i = 0; + while (s1[i] == s2[i] || i == n) + { + if ((s1[i] == '\0' && s2[i] == '\0') || i == n) + return (1); + i++; + } + return (0); +} diff --git a/libft/ft_strnew.c b/libft/ft_strnew.c new file mode 100644 index 0000000..6c82802 --- /dev/null +++ b/libft/ft_strnew.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/28 18:26:56 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 16:25:54 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnew(size_t size) +{ + char *str; + size_t i; + + i = 0; + str = (char *)malloc(size * sizeof(char) + 1); + if (!str) + return (NULL); + while (i < size + 1) + str[i++] = '\0'; + return (str); +} diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100644 index 0000000..1982911 --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 12:49:32 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 16:39:13 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *s1, char *s2, size_t n) +{ + size_t i; + size_t i_mem; + size_t i_tofind; + + i_mem = 0; + if (s2[0] == '\0') + return ((char *)s1); + while (s1[i_mem] != '\0' && i_mem < n) + { + i_tofind = 0; + while (s1[i_mem] != s2[i_tofind] && s1[i_mem] != '\0') + i_mem++; + if (s1[i_mem] == '\0') + return (NULL); + i = i_mem; + while ((s1[i] == s2[i_tofind] || s2[i_tofind] == '\0') && i++ <= n) + if (s2[i_tofind++] == '\0') + return ((char *)&s1[i_mem]); + if (i > n) + return (NULL); + i_mem++; + } + return (NULL); +} diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c new file mode 100644 index 0000000..e295607 --- /dev/null +++ b/libft/ft_strrchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 10:05:56 by gbrochar #+# #+# */ +/* Updated: 2015/11/27 10:24:37 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + + i = ft_strlen(s); + if (c) + { + while (i--) + if (s[i] == (char)c) + return ((char *)&s[i]); + return (NULL); + } + return ((char *)&s[i]); +} diff --git a/libft/ft_strsplit.c b/libft/ft_strsplit.c new file mode 100644 index 0000000..0784026 --- /dev/null +++ b/libft/ft_strsplit.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:55:10 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:00:21 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_wordcount(char const *s, char c) +{ + int word_count; + int i; + + i = 0; + word_count = 0; + while (s[i] != '\0') + { + while (s[i] == c) + i++; + if (s[i] != '\0') + word_count++; + while (s[i] != c && s[i] != '\0') + i++; + } + return (word_count); +} + +static void *ft_taballoc(char const *s, char **str_tab, char c) +{ + int i; + int i2; + int wl; + + i = 0; + i2 = 0; + while (s[i2]) + { + wl = 0; + while (s[i2] == c && s[i2] != '\0') + i2++; + while (s[i2] != c && s[i2] != '\0') + { + wl++; + i2++; + } + if (wl != 0) + *str_tab++ = (char *)malloc((wl + 1) * sizeof(char)); + if (!(str_tab - 1)) + return (NULL); + } + return (*str_tab); +} + +char **ft_strsplit(char const *s, char c) +{ + char **str_tab; + int i; + int i2; + int i3; + int wc; + + i = 0; + i3 = 0; + wc = ft_wordcount(s, c); + str_tab = (char **)malloc((wc + 1) * sizeof(char *)); + if (!str_tab) + return (NULL); + ft_taballoc(s, str_tab, c); + while (i != wc) + { + i2 = 0; + while (s[i3] == c && s[i3] != '\0') + i3++; + while (s[i3] != c && s[i3] != '\0') + str_tab[i][i2++] = s[i3++]; + str_tab[i][i2] = '\0'; + i++; + } + str_tab[i] = NULL; + return (str_tab); +} diff --git a/libft/ft_strstr.c b/libft/ft_strstr.c new file mode 100644 index 0000000..5f985f0 --- /dev/null +++ b/libft/ft_strstr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 10:24:53 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 16:38:40 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strstr(const char *s1, char *s2) +{ + int i; + int i_mem; + int i_tofind; + + i_mem = 0; + if (s2[0] == '\0') + return ((char *)s1); + while (s1[i_mem] != '\0') + { + i_tofind = 0; + while (s1[i_mem] != s2[i_tofind] && s1[i_mem] != '\0') + i_mem++; + if (s1[i_mem] == '\0') + return (NULL); + i = i_mem; + while (s1[i++] == s2[i_tofind] || s2[i_tofind] == '\0') + if (s2[i_tofind++] == '\0') + return ((char *)&s1[i_mem]); + i_mem++; + } + return (NULL); +} diff --git a/libft/ft_strsub.c b/libft/ft_strsub.c new file mode 100644 index 0000000..4434689 --- /dev/null +++ b/libft/ft_strsub.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:14:36 by gbrochar #+# #+# */ +/* Updated: 2015/12/08 15:34:26 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *str; + size_t i; + + i = 0; + str = (char *)malloc((len + 1) * sizeof(char)); + if (!str) + return (NULL); + while (i < len) + str[i++] = s[start++]; + str[i] = '\0'; + return (str); +} diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100644 index 0000000..c3480c6 --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:54:12 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:00:33 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s) +{ + char *str; + size_t i; + size_t i2; + size_t ws; + + i = ft_strlen(s) - 1; + i2 = 0; + ws = 0; + while ((s[i] == ' ' || s[i] == '\n' || s[i] == '\t') && i--) + ws++; + i = 0; + while ((s[i] == ' ' || s[i] == '\n' || s[i] == '\t') && ws < ft_strlen(s)) + { + i++; + ws++; + } + str = (char *)malloc((ft_strlen(s) + 1 - ws) * sizeof(char)); + if (!str) + return (NULL); + while (i2 < (ft_strlen(s) - ws)) + str[i2++] = s[i++]; + str[i2] = '\0'; + return (str); +} diff --git a/libft/ft_swap.c b/libft/ft_swap.c new file mode 100644 index 0000000..5a99a20 --- /dev/null +++ b/libft/ft_swap.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/12/07 09:44:07 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 11:01:13 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_swap(int *a, int *b) +{ + int c; + + c = *a; + *a = *b; + *b = c; +} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c new file mode 100644 index 0000000..22b7a20 --- /dev/null +++ b/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:39:55 by gbrochar #+# #+# */ +/* Updated: 2015/11/29 08:40:07 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if ((int)c >= 'A' && (int)c <= 'Z') + c += 32; + return (c); +} diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c new file mode 100644 index 0000000..f0f24ec --- /dev/null +++ b/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 18:43:24 by gbrochar #+# #+# */ +/* Updated: 2015/11/28 16:01:01 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if ((int)c >= 'a' && (int)c <= 'z') + c -= 32; + return (c); +} diff --git a/libft/libft.h b/libft/libft.h new file mode 100644 index 0000000..2d0d5a9 --- /dev/null +++ b/libft/libft.h @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/24 12:52:28 by gbrochar #+# #+# */ +/* Updated: 2015/12/07 09:31:40 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include + +typedef struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +} t_list; + +void ft_lstadd(t_list **alst, t_list *new); +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); +int ft_atoi(const char *str); +void ft_bzero(void *s, size_t n); +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_isprint(int c); +int ft_isspace(int c); +char *ft_itoa(int n); +void *ft_memalloc(size_t size); +void *ft_memccpy(void *dst, const void *src, int c, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void ft_memdel(void **ap); +void *ft_memmove(void *dst, const void *src, size_t len); +void *ft_memset(void *b, int c, size_t len); +int ft_power(int nb, int pow); +void ft_putchar(char c); +void ft_putchar_fd(char c, int fd); +void ft_putendl(char const *s); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr(int n); +void ft_putnbr_fd(int n, int fd); +void ft_putstr(char const *s); +void ft_putstr_fd(char const *s, int fd); +void ft_sort_int(int *tab, int size); +long ft_sqrt(long n); +char *ft_strcat(char *s1, const char *s2); +char *ft_strchr(const char *s, int c); +void ft_strclr(char *s); +int ft_strcmp(const char *s1, const char *s2); +char *ft_strcpy(char *dst, const char *src); +void ft_strdel(char **as); +char *ft_strdup(const char *s1); +int ft_strequ(char const *s1, char const *s2); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *s)); +char *ft_strjoin(char const *s1, char const *s2); +size_t ft_strlcat(char *dst, const char *src, size_t size); +size_t ft_strlen(const char *s); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +char *ft_strncat(char *s1, const char *s2, size_t n); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strncpy(char *dst, const char *src, size_t n); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strnew(size_t size); +char *ft_strnstr(const char *s1, char *s2, size_t n); +char *ft_strrchr(const char *s, int c); +char **ft_strsplit(char const *s, char c); +char *ft_strstr(const char *s1, char *s2); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strtrim(char const *s); +void ft_swap(int *a, int *b); +int ft_tolower(int c); +int ft_toupper(int c); + +#endif diff --git a/src/ft_check_ir0.c b/src/ft_check_ir0.c new file mode 100644 index 0000000..a4aa123 --- /dev/null +++ b/src/ft_check_ir0.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_ir0.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 05:01:23 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:23:55 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_ir0(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 3 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x + 1][y] == -1 + && grid[x + 2][y] == -1 && grid[x + 3][y] == -1) + { + grid[x][y] = nb; + grid[x + 1][y] = nb; + grid[x + 2][y] = nb; + grid[x + 3][y] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_ir90.c b/src/ft_check_ir90.c new file mode 100644 index 0000000..d6f1f31 --- /dev/null +++ b/src/ft_check_ir90.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_ir90.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 05:01:23 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:01 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_ir90(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (y + 3 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x][y + 1] == -1 + && grid[x][y + 2] == -1 && grid[x][y + 3] == -1) + { + grid[x][y] = nb; + grid[x][y + 1] = nb; + grid[x][y + 2] = nb; + grid[x][y + 3] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_jr0.c b/src/ft_check_jr0.c new file mode 100644 index 0000000..86f7cd2 --- /dev/null +++ b/src/ft_check_jr0.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_jr0.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:39:47 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_jr0(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 2 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y + 1] == -1 && grid[x + 1][y + 1] == -1 + && grid[x + 2][y] == -1 && grid[x + 2][y + 1] == -1) + { + grid[x][y + 1] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 2][y] = nb; + grid[x + 2][y + 1] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_jr180.c b/src/ft_check_jr180.c new file mode 100644 index 0000000..ca8f9a9 --- /dev/null +++ b/src/ft_check_jr180.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_jr180.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/24 15:21:21 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_jr180(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 2 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x][y + 1] == -1 + && grid[x + 1][y] == -1 && grid[x + 2][y] == -1) + { + grid[x][y] = nb; + grid[x][y + 1] = nb; + grid[x + 1][y] = nb; + grid[x + 2][y] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_jr270.c b/src/ft_check_jr270.c new file mode 100644 index 0000000..2b5d2f5 --- /dev/null +++ b/src/ft_check_jr270.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_jr270.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:25:22 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_jr270(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 2 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x][y + 1] == -1 + && grid[x][y + 2] == -1 && grid[x + 1][y + 2] == -1) + { + grid[x][y] = nb; + grid[x][y + 1] = nb; + grid[x][y + 2] = nb; + grid[x + 1][y + 2] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_jr90.c b/src/ft_check_jr90.c new file mode 100644 index 0000000..e7f5b44 --- /dev/null +++ b/src/ft_check_jr90.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_jr90.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:43:53 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_jr90(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 2 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x + 1][y] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 1][y + 2] == -1) + { + grid[x][y] = nb; + grid[x + 1][y] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 1][y + 2] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_lr0.c b/src/ft_check_lr0.c new file mode 100644 index 0000000..4927881 --- /dev/null +++ b/src/ft_check_lr0.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_lr0.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:23:47 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_lr0(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 2 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x + 1][y] == -1 + && grid[x + 2][y] == -1 && grid[x + 2][y + 1] == -1) + { + grid[x][y] = nb; + grid[x + 1][y] = nb; + grid[x + 2][y] = nb; + grid[x + 2][y + 1] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_lr180.c b/src/ft_check_lr180.c new file mode 100644 index 0000000..9008f3b --- /dev/null +++ b/src/ft_check_lr180.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_lr180.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:25:12 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_lr180(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 2 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x][y + 1] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 2][y + 1] == -1) + { + grid[x][y] = nb; + grid[x][y + 1] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 2][y + 1] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_lr270.c b/src/ft_check_lr270.c new file mode 100644 index 0000000..d22c5a5 --- /dev/null +++ b/src/ft_check_lr270.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_lr270.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:31:26 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_lr270(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 2 >= grid[21][22]) + return (1); + if (grid[x][y + 2] == -1 && grid[x + 1][y] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 1][y + 2] == -1) + { + grid[x][y + 2] = nb; + grid[x + 1][y] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 1][y + 2] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_lr90.c b/src/ft_check_lr90.c new file mode 100644 index 0000000..be11452 --- /dev/null +++ b/src/ft_check_lr90.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_lr90.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:29:19 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_lr90(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 2 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x][y + 1] == -1 + && grid[x][y + 2] == -1 && grid[x + 1][y] == -1) + { + grid[x][y] = nb; + grid[x][y + 1] = nb; + grid[x][y + 2] = nb; + grid[x + 1][y] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_or0.c b/src/ft_check_or0.c new file mode 100644 index 0000000..fc3b2d8 --- /dev/null +++ b/src/ft_check_or0.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_or0.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 05:01:23 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:08 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_or0(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x + 1][y] == -1 + && grid[x][y + 1] == -1 && grid[x + 1][y + 1] == -1) + { + grid[x][y] = nb; + grid[x + 1][y] = nb; + grid[x][y + 1] = nb; + grid[x + 1][y + 1] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_sr0.c b/src/ft_check_sr0.c new file mode 100644 index 0000000..5db763d --- /dev/null +++ b/src/ft_check_sr0.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_sr0.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 05:01:23 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:45 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_sr0(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 2 >= grid[21][22]) + return (1); + if (grid[x][y + 1] == -1 && grid[x][y + 2] == -1 + && grid[x + 1][y] == -1 && grid[x + 1][y + 1] == -1) + { + grid[x][y + 1] = nb; + grid[x][y + 2] = nb; + grid[x + 1][y] = nb; + grid[x + 1][y + 1] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_sr90.c b/src/ft_check_sr90.c new file mode 100644 index 0000000..f8996ed --- /dev/null +++ b/src/ft_check_sr90.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_sr90.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 05:01:23 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:52 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_sr90(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 2 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x + 1][y] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 2][y + 1] == -1) + { + grid[x][y] = nb; + grid[x + 1][y] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 2][y + 1] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_tiles.c b/src/ft_check_tiles.c new file mode 100644 index 0000000..9720186 --- /dev/null +++ b/src/ft_check_tiles.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_tiles.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/01/07 12:51:35 by gbrochar #+# #+# */ +/* Updated: 2016/01/08 18:35:24 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +t_tetromino *ft_check_tiles(char **tiles) +{ + int i; + int i2; + int hashtag_count; + int dot_count; + + i = -1; + while (tiles[++i]) + { + if (tiles[i][4] != '\n' || tiles[i][9] != '\n' || tiles[i][14] != '\n' + || tiles[i][19] != '\n' || tiles[i][20] != '\n') + return (NULL); + dot_count = 0; + hashtag_count = 0; + i2 = 0; + while (i2 != 21) + { + if (tiles[i][i2] == '#') + hashtag_count++; + if (tiles[i][i2++] == '.') + dot_count++; + } + if (hashtag_count != 4 || dot_count != 12) + return (NULL); + } + return (ft_get_struct(tiles)); +} diff --git a/src/ft_check_tr0.c b/src/ft_check_tr0.c new file mode 100644 index 0000000..02b6cc1 --- /dev/null +++ b/src/ft_check_tr0.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_tr0.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:16 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_tr0(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 2 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x][y + 1] == -1 + && grid[x + 1][y + 1] == -1 && grid[x][y + 2] == -1) + { + grid[x][y] = nb; + grid[x][y + 1] = nb; + grid[x + 1][y + 1] = nb; + grid[x][y + 2] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_tr180.c b/src/ft_check_tr180.c new file mode 100644 index 0000000..349c7cf --- /dev/null +++ b/src/ft_check_tr180.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_tr180.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:28 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_tr180(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 2 >= grid[21][22]) + return (1); + if (grid[x + 1][y] == -1 && grid[x][y + 1] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 1][y + 2] == -1) + { + grid[x + 1][y] = nb; + grid[x][y + 1] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 1][y + 2] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_tr270.c b/src/ft_check_tr270.c new file mode 100644 index 0000000..5fecbf5 --- /dev/null +++ b/src/ft_check_tr270.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_tr270.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:37 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_tr270(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 2 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x + 1][y] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 2][y] == -1) + { + grid[x][y] = nb; + grid[x + 1][y] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 2][y] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_tr90.c b/src/ft_check_tr90.c new file mode 100644 index 0000000..924137a --- /dev/null +++ b/src/ft_check_tr90.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_tr90.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 15:36:00 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:21 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_tr90(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 2 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y + 1] == -1 && grid[x + 1][y] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 2][y + 1] == -1) + { + grid[x][y + 1] = nb; + grid[x + 1][y] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 2][y + 1] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_zr0.c b/src/ft_check_zr0.c new file mode 100644 index 0000000..6a89555 --- /dev/null +++ b/src/ft_check_zr0.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_zr0.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 05:01:23 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:24:58 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_zr0(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 1 >= grid[21][22] || y + 2 >= grid[21][22]) + return (1); + if (grid[x][y] == -1 && grid[x][y + 1] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 1][y + 2] == -1) + { + grid[x][y] = nb; + grid[x][y + 1] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 1][y + 2] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_check_zr90.c b/src/ft_check_zr90.c new file mode 100644 index 0000000..9088493 --- /dev/null +++ b/src/ft_check_zr90.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_zr90.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 05:01:23 by gbrochar #+# #+# */ +/* Updated: 2016/02/17 15:25:04 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_check_zr90(char grid[22][23], t_tetromino *tiles, int nb) +{ + int x; + int y; + + x = tiles[nb].x; + y = tiles[nb].y; + if (x + 2 >= grid[21][22] || y + 1 >= grid[21][22]) + return (1); + if (grid[x][y + 1] == -1 && grid[x + 1][y] == -1 + && grid[x + 1][y + 1] == -1 && grid[x + 2][y] == -1) + { + grid[x][y + 1] = nb; + grid[x + 1][y] = nb; + grid[x + 1][y + 1] = nb; + grid[x + 2][y] = nb; + return (0); + } + return (1); +} diff --git a/src/ft_get_file.c b/src/ft_get_file.c new file mode 100644 index 0000000..6439ce3 --- /dev/null +++ b/src/ft_get_file.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/01/07 07:44:27 by gbrochar #+# #+# */ +/* Updated: 2016/01/09 16:17:47 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +void ft_exit(int fd, char *str) +{ + close(fd); + free(str); + str = NULL; +} + +t_tetromino *ft_get_file(char *file_name) +{ + int file_des; + int file_len; + char *file_content; + + file_content = ft_strnew(546); + file_des = open(file_name, O_RDONLY); + if (file_des == -1) + { + ft_exit(file_des, file_content); + return (NULL); + } + file_len = read(file_des, file_content, 546); + if (file_len % 21 != 20) + { + ft_exit(file_des, file_content); + return (NULL); + } + close(file_des); + return (ft_split_file_content(file_content, ((file_len / 21) + 1))); +} diff --git a/src/ft_get_struct.c b/src/ft_get_struct.c new file mode 100644 index 0000000..4c7fa0a --- /dev/null +++ b/src/ft_get_struct.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_struct.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/01/08 18:32:49 by gbrochar #+# #+# */ +/* Updated: 2016/02/25 11:35:48 by fanno ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +char **ft_allocate_types(void) +{ + char **types; + int i; + + i = 0; + types = (char **)malloc(20 * sizeof(char *)); + while (i != 20) + types[i++] = ft_strnew(20); + return (types); +} + +char **ft_create_types(void) +{ + char **types; + + types = ft_allocate_types(); + types[0] = "#....#....#....#"; + types[1] = "####"; + types[2] = "##...##"; + types[3] = "###...#"; + types[4] = "#...##....#"; + types[5] = "#...###"; + types[6] = "#....##...#"; + types[7] = "##..##"; + types[8] = "#....##....#"; + types[9] = "##....##"; + types[10] = "#...##...#"; + types[11] = "#....#....##"; + types[12] = "###..#"; + types[13] = "##....#....#"; + types[14] = "#..###"; + types[15] = "#....#...##"; + types[16] = "#....###"; + types[17] = "##...#....#"; + types[18] = "###....#"; + types[19] = NULL; + return (types); +} + +t_tetromino *ft_create_tiles(void) +{ + t_tetromino *tiles; + int i; + + i = 0; + tiles = (t_tetromino *)malloc(26 * sizeof(t_tetromino)); + while (i != 26) + { + tiles[i].type = -1; + tiles[i].x = 0; + tiles[i++].y = 0; + } + return (tiles); +} + +void ft_replace_linefeed(char **tiles) +{ + int i; + int i2; + + i = 0; + while (tiles[i]) + { + i2 = 0; + while (tiles[i][i2]) + { + if (tiles[i][i2] == '\n' && i2 != 20) + tiles[i][i2] = '.'; + i2++; + } + i++; + } +} + +t_tetromino *ft_get_struct(char **tiles) +{ + char **types; + t_tetromino *t_tiles; + int i; + int i_types; + + ft_replace_linefeed(tiles); + types = ft_create_types(); + t_tiles = ft_create_tiles(); + i = 0; + while (tiles[i]) + { + i_types = 0; + while (types[i_types]) + if (ft_strstr_binary(tiles[i], types[i_types++]) == 1) + { + t_tiles[i].type = i_types - 1; + break ; + } + if (t_tiles[i].type == -1) + return (NULL); + i++; + } + return (t_tiles); +} diff --git a/src/ft_print_grid.c b/src/ft_print_grid.c new file mode 100644 index 0000000..0c83a1c --- /dev/null +++ b/src/ft_print_grid.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_grid.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 05:18:57 by gbrochar #+# #+# */ +/* Updated: 2016/02/15 05:29:30 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +void ft_print_grid(char grid[22][23]) +{ + int i; + int j; + + i = 0; + while (i != grid[21][22]) + { + j = 0; + while (j != grid[21][22]) + { + if (grid[i][j] == -1) + ft_putchar('.'); + else + ft_putchar(grid[i][j] + 'A'); + j++; + } + ft_putchar('\n'); + i++; + } +} diff --git a/src/ft_solve.c b/src/ft_solve.c new file mode 100644 index 0000000..88055a5 --- /dev/null +++ b/src/ft_solve.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_solve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/09 18:37:23 by gbrochar #+# #+# */ +/* Updated: 2016/02/24 15:42:56 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +void init(int (*f_check[19])(char[22][23], t_tetromino *, int)) +{ + f_check[0] = &ft_check_ir0; + f_check[1] = &ft_check_ir90; + f_check[2] = &ft_check_or0; + f_check[3] = &ft_check_tr0; + f_check[4] = &ft_check_tr90; + f_check[5] = &ft_check_tr180; + f_check[6] = &ft_check_tr270; + f_check[7] = &ft_check_sr0; + f_check[8] = &ft_check_sr90; + f_check[9] = &ft_check_zr0; + f_check[10] = &ft_check_zr90; + f_check[11] = &ft_check_lr0; + f_check[12] = &ft_check_lr90; + f_check[13] = &ft_check_lr180; + f_check[14] = &ft_check_lr270; + f_check[15] = &ft_check_jr0; + f_check[16] = &ft_check_jr90; + f_check[17] = &ft_check_jr180; + f_check[18] = &ft_check_jr270; +} + +void ft_init_grid(char grid[22][23], t_tetromino *tiles) +{ + int i; + int j; + + i = 0; + while (i != 22) + { + j = 0; + while (j != 22) + grid[i][j++] = -1; + i++; + } + j = 2; + i = 0; + while (tiles[i].type != -1 && i != 26) + ++i; + i *= 4; + while (j * j < i) + ++j; + grid[21][22] = j; +} + +void ft_solve(t_tetromino *tiles) +{ + char grid[22][23]; + int nb; + int ret; + + nb = 0; + ft_init_grid(grid, tiles); + while ((ret = ft_solve_2(grid, tiles, nb)) != 0) + { + if (ret == 1) + ++nb; + else + --nb; + } + ft_print_grid(grid); +} diff --git a/src/ft_solve_2.c b/src/ft_solve_2.c new file mode 100644 index 0000000..4bb4a13 --- /dev/null +++ b/src/ft_solve_2.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_solve_2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/02/15 03:36:57 by gbrochar #+# #+# */ +/* Updated: 2016/02/25 08:55:13 by fanno ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +void ft_del_tile(char grid[22][23], int nb) +{ + int i; + int j; + + i = 0; + while (i != grid[21][22]) + { + j = 0; + while (j != grid[21][22]) + { + if (grid[i][j] == nb) + grid[i][j] = -1; + ++j; + } + ++i; + } +} + +int ft_update_position(t_tetromino *tiles, int nb, char grid[22][23]) +{ + if (tiles[nb].y == (grid[21][22] - 1) && tiles[nb].x == (grid[21][22] - 1)) + { + tiles[nb].y = 0; + tiles[nb].x = 0; + if (nb == 0) + return (1); + ft_del_tile(grid, nb - 1); + return (ft_update_position(tiles, nb - 1, grid) + 2); + } + else if (tiles[nb].y == (grid[21][22] - 1)) + { + ++tiles[nb].x; + tiles[nb].y = 0; + } + else + ++tiles[nb].y; + return (0); +} + +int ft_solve_2(char grid[22][23], t_tetromino *tiles, int nb) +{ + int ret; + int (*f_check[19])(char[22][23], t_tetromino *tiles, int nb); + + init(f_check); + if (tiles[nb].type == -1) + return (0); + while ((f_check[(int)(tiles[nb].type)](grid, tiles, nb)) == 1) + { + if ((ret = ft_update_position(tiles, nb, grid)) == 1) + ++grid[21][22]; + else + while (ret != 0) + { + return (2); + } + } + return (1); +} diff --git a/src/ft_split_file_content.c b/src/ft_split_file_content.c new file mode 100644 index 0000000..fbdb41d --- /dev/null +++ b/src/ft_split_file_content.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_file_content.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/01/07 09:46:46 by gbrochar #+# #+# */ +/* Updated: 2016/01/07 18:35:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +t_tetromino *ft_split_file_content(char *file_content, int tiles_nb) +{ + char **tiles; + int i; + int i_tiles; + int i_file; + + i_file = 0; + i = 0; + tiles = (char **)malloc((tiles_nb + 1) * sizeof(char *)); + while (i != tiles_nb) + tiles[i++] = ft_strnew(21); + tiles[i] = NULL; + i = -1; + while (++i != tiles_nb) + { + i_tiles = 0; + while (i_tiles != 21) + { + tiles[i][i_tiles++] = file_content[i_file]; + i_file++; + } + } + tiles[tiles_nb - 1][20] = '\n'; + return (ft_check_tiles(tiles)); +} diff --git a/src/ft_strstr_binary.c b/src/ft_strstr_binary.c new file mode 100644 index 0000000..fe428f6 --- /dev/null +++ b/src/ft_strstr_binary.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr_binary.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/27 10:24:53 by gbrochar #+# #+# */ +/* Updated: 2016/01/09 11:40:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_strstr_binary(const char *s1, char *s2) +{ + int i; + int i_mem; + int i_tofind; + + i_mem = 0; + while (s1[i_mem] != '\0') + { + i_tofind = 0; + while (s1[i_mem] != s2[i_tofind] && s1[i_mem] != '\0') + i_mem++; + if (s1[i_mem] == '\0') + return (0); + i = i_mem; + while (s1[i++] == s2[i_tofind] || s2[i_tofind] == '\0') + if (s2[i_tofind++] == '\0') + return (1); + i_mem++; + } + return (0); +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..4c62283 --- /dev/null +++ b/src/main.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/01/06 17:54:32 by gbrochar #+# #+# */ +/* Updated: 2016/02/28 10:37:44 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int main(int argc, char **argv) +{ + t_tetromino *tiles; + int i; + + i = 0; + if (argc == 2) + { + tiles = ft_get_file(argv[1]); + if (!tiles) + ft_putstr_fd("error\n", 1); + else + ft_solve(tiles); + } + else + ft_putstr_fd("usage: ./fillit source_file\n", 1); + return (0); +}