From d1cabbc8d52f790cfff6a44692ea84cff97da9d4 Mon Sep 17 00:00:00 2001 From: Gregory Tertyshny Date: Mon, 26 Dec 2016 18:48:33 +0200 Subject: [PATCH] initial commit --- Makefile | 75 ++++++++++++ inc/fillit.h | 177 +++++++++++++++++++++++++++ libft/Makefile | 102 +++++++++++++++ libft/author | 1 + libft/ft_atoi.c | 35 ++++++ libft/ft_bzero.c | 18 +++ libft/ft_isalnum.c | 20 +++ libft/ft_isalpha.c | 21 ++++ libft/ft_isascii.c | 20 +++ libft/ft_isdigit.c | 20 +++ libft/ft_isprint.c | 20 +++ libft/ft_itoa.c | 73 +++++++++++ libft/ft_lst_at.c | 20 +++ libft/ft_lstadd.c | 22 ++++ libft/ft_lstadd_back.c | 27 ++++ libft/ft_lstdel.c | 31 +++++ libft/ft_lstdelone.c | 23 ++++ libft/ft_lstfind.c | 24 ++++ libft/ft_lstiter.c | 22 ++++ libft/ft_lstmap.c | 26 ++++ libft/ft_lstnew.c | 40 ++++++ libft/ft_lstpop.c | 26 ++++ libft/ft_lststrsplit.c | 38 ++++++ libft/ft_memalloc.c | 23 ++++ libft/ft_memccpy.c | 28 +++++ libft/ft_memchr.c | 24 ++++ libft/ft_memcmp.c | 25 ++++ libft/ft_memcpy.c | 26 ++++ libft/ft_memdel.c | 22 ++++ libft/ft_memmove.c | 33 +++++ libft/ft_memset.c | 23 ++++ libft/ft_putchar.c | 18 +++ libft/ft_putchar_fd.c | 18 +++ libft/ft_putendl.c | 21 ++++ libft/ft_putendl_fd.c | 21 ++++ libft/ft_putnbr.c | 58 +++++++++ libft/ft_putnbr_fd.c | 58 +++++++++ libft/ft_putstr.c | 20 +++ libft/ft_putstr_fd.c | 20 +++ libft/ft_realloc.c | 32 +++++ libft/ft_strcat.c | 26 ++++ libft/ft_strchr.c | 22 ++++ libft/ft_strclr.c | 19 +++ libft/ft_strcmp.c | 25 ++++ libft/ft_strcpy.c | 24 ++++ libft/ft_strdel.c | 18 +++ libft/ft_strdup.c | 26 ++++ libft/ft_strequ.c | 28 +++++ libft/ft_striter.c | 20 +++ libft/ft_striteri.c | 23 ++++ libft/ft_strjoin.c | 32 +++++ libft/ft_strlcat.c | 32 +++++ libft/ft_strlen.c | 23 ++++ libft/ft_strmap.c | 30 +++++ libft/ft_strmapi.c | 30 +++++ libft/ft_strncat.c | 28 +++++ libft/ft_strncmp.c | 29 +++++ libft/ft_strncpy.c | 30 +++++ libft/ft_strnequ.c | 22 ++++ libft/ft_strnew.c | 25 ++++ libft/ft_strnstr.c | 33 +++++ libft/ft_strrchr.c | 27 ++++ libft/ft_strsplit.c | 85 +++++++++++++ libft/ft_strstr.c | 29 +++++ libft/ft_strsub.c | 27 ++++ libft/ft_strtrim.c | 40 ++++++ libft/ft_tolower.c | 20 +++ libft/ft_toupper.c | 20 +++ libft/libft.h | 98 +++++++++++++++ obj/.gitkeep | 0 src/algorithm.c | 44 +++++++ src/check.c | 85 +++++++++++++ src/dancing_links.c | 125 +++++++++++++++++++ src/dancing_links_initialization.c | 82 +++++++++++++ src/dancing_links_initialization_2.c | 83 +++++++++++++ src/error.c | 19 +++ src/get_amount.c | 23 ++++ src/get_types.c | 42 +++++++ src/glue_figure.c | 47 +++++++ src/init.c | 32 +++++ src/main.c | 51 ++++++++ src/move_up_left.c | 47 +++++++ src/read_file.c | 38 ++++++ src/string_to_table.c | 28 +++++ 84 files changed, 2988 insertions(+) create mode 100644 Makefile create mode 100644 inc/fillit.h create mode 100644 libft/Makefile create mode 100644 libft/author create mode 100644 libft/ft_atoi.c create mode 100644 libft/ft_bzero.c create mode 100644 libft/ft_isalnum.c create mode 100644 libft/ft_isalpha.c create mode 100644 libft/ft_isascii.c create mode 100644 libft/ft_isdigit.c create mode 100644 libft/ft_isprint.c create mode 100644 libft/ft_itoa.c create mode 100644 libft/ft_lst_at.c create mode 100644 libft/ft_lstadd.c create mode 100644 libft/ft_lstadd_back.c create mode 100644 libft/ft_lstdel.c create mode 100644 libft/ft_lstdelone.c create mode 100644 libft/ft_lstfind.c create mode 100644 libft/ft_lstiter.c create mode 100644 libft/ft_lstmap.c create mode 100644 libft/ft_lstnew.c create mode 100644 libft/ft_lstpop.c create mode 100644 libft/ft_lststrsplit.c create mode 100644 libft/ft_memalloc.c create mode 100644 libft/ft_memccpy.c create mode 100644 libft/ft_memchr.c create mode 100644 libft/ft_memcmp.c create mode 100644 libft/ft_memcpy.c create mode 100644 libft/ft_memdel.c create mode 100644 libft/ft_memmove.c create mode 100644 libft/ft_memset.c create mode 100644 libft/ft_putchar.c create mode 100644 libft/ft_putchar_fd.c create mode 100644 libft/ft_putendl.c create mode 100644 libft/ft_putendl_fd.c create mode 100644 libft/ft_putnbr.c create mode 100644 libft/ft_putnbr_fd.c create mode 100644 libft/ft_putstr.c create mode 100644 libft/ft_putstr_fd.c create mode 100644 libft/ft_realloc.c create mode 100644 libft/ft_strcat.c create mode 100644 libft/ft_strchr.c create mode 100644 libft/ft_strclr.c create mode 100644 libft/ft_strcmp.c create mode 100644 libft/ft_strcpy.c create mode 100644 libft/ft_strdel.c create mode 100644 libft/ft_strdup.c create mode 100644 libft/ft_strequ.c create mode 100644 libft/ft_striter.c create mode 100644 libft/ft_striteri.c create mode 100644 libft/ft_strjoin.c create mode 100644 libft/ft_strlcat.c create mode 100644 libft/ft_strlen.c create mode 100644 libft/ft_strmap.c create mode 100644 libft/ft_strmapi.c create mode 100644 libft/ft_strncat.c create mode 100644 libft/ft_strncmp.c create mode 100644 libft/ft_strncpy.c create mode 100644 libft/ft_strnequ.c create mode 100644 libft/ft_strnew.c create mode 100644 libft/ft_strnstr.c create mode 100644 libft/ft_strrchr.c create mode 100644 libft/ft_strsplit.c create mode 100644 libft/ft_strstr.c create mode 100644 libft/ft_strsub.c create mode 100644 libft/ft_strtrim.c create mode 100644 libft/ft_tolower.c create mode 100644 libft/ft_toupper.c create mode 100644 libft/libft.h create mode 100644 obj/.gitkeep create mode 100644 src/algorithm.c create mode 100644 src/check.c create mode 100644 src/dancing_links.c create mode 100644 src/dancing_links_initialization.c create mode 100644 src/dancing_links_initialization_2.c create mode 100644 src/error.c create mode 100644 src/get_amount.c create mode 100644 src/get_types.c create mode 100644 src/glue_figure.c create mode 100644 src/init.c create mode 100644 src/main.c create mode 100644 src/move_up_left.c create mode 100644 src/read_file.c create mode 100644 src/string_to_table.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cea4267 --- /dev/null +++ b/Makefile @@ -0,0 +1,75 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: gtertysh +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2016/12/14 16:54:48 by gtertysh #+# #+# # +# Updated: 2016/12/23 19:59:53 by gtertysh ### ########.fr # +# # +# **************************************************************************** # + +NAME = fillit + +SRCDIR = ./src/ + +OBJDIR = ./obj/ + +SRC_FILES = main.c \ + error.c \ + init.c \ + check.c \ + algorithm.c \ + glue_figure.c \ + move_up_left.c \ + read_file.c \ + string_to_table.c \ + dancing_links.c \ + dancing_links_initialization.c \ + dancing_links_initialization_2.c \ + get_types.c \ + get_amount.c + +OBJ_FILES = $(SRC_FILES:.c=.o) + +SRC = $(addprefix $(SRCDIR), $(SRC_FILES)) + +OBJ = $(addprefix $(OBJDIR), $(OBJ_FILES)) + +INC = -I ./inc -I $(LIBFOLDER) + +LIB = libft.a + +LIBFLAGS = -lft -L $(LIBFOLDER) + +LIBFOLDER = ./libft/ + +FLAGS = -Werror -Wextra -Wall + +CC = clang + +all: $(NAME) + +$(NAME): $(OBJ) $(LIBFOLDER)$(LIB) + $(CC) $(FLAGS) $(OBJ) $(LIBFLAGS) -o $(NAME) + +$(OBJDIR)%.o : $(SRCDIR)%.c + $(CC) $(FLAGS) $(INC) -c $< -o $@ + +$(LIBFOLDER)$(LIB): + make -C $(LIBFOLDER) + +clean: + rm -rf $(OBJ) + +fclean: clean + rm -rf $(NAME) + +libclean: + make clean -C $(LIBFOLDER) + +libfclean: + make fclean -C $(LIBFOLDER) + +re: fclean libfclean all diff --git a/inc/fillit.h b/inc/fillit.h new file mode 100644 index 0000000..b7908b8 --- /dev/null +++ b/inc/fillit.h @@ -0,0 +1,177 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fillit.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/14 16:42:06 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 17:46:30 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FILLIT_H +# define FILLIT_H + +# include "libft.h" +# include +# include +# include + +# define BUF_S 8192 + +/* +** s_coord contain +** coordinates of all # +** within tetromino +*/ + +typedef struct s_coord +{ + int y; + int x; +} t_coord; + +/* +** s_ttrmn contain: +** - t[17] represent tample +** tetromino in 4x4 square +** as string +** - actual width and height +** of tetromino +** - array of coordinates +** of each # +*/ + +typedef struct s_ttrmn +{ + char t[17]; + int width; + int height; + t_coord c[4]; +} t_ttrmn; + +/* +** s_node - node of double-linked list +** for Knuth's "dancing links". +** For more detail see: +** https://arxiv.org/pdf/cs/0011047v1.pdf +*/ + +typedef struct s_node +{ + struct s_node *left; + struct s_node *right; + struct s_node *up; + struct s_node *down; + struct s_node *column; + t_coord coord; + int type; + int size; +} t_node; + +/* +** declaration of global variable +** with all possible (19) +** tetrominoes where each one +** sits in upper-left corner +*/ + +extern t_ttrmn g_templates[19]; + +/* +** reads data from file to one string. +*/ + +char *read_file(char *path); + +/* +** check right amount of '\n', their position +** and allowed chars +*/ + +void check_string(char *string); + +/* +** split string to table by '\n' +*/ + +char **to_table(char **string); + +/* +** creates new table where each string represent whole figrure. +** "Glues" each set of four strings into one +*/ + +char **glue_figure(char **table); + +/* +** move tetromino in upper-left corner +*/ + +void move_up_left(char **table); + +/* +** creates first node of double-chained list +** (in future "torus") +*/ + +t_node *init_root(int size); + +/* +** links new node to the left of the root +*/ + +t_node *add_column(t_node *root); + +/* +** calls function above to create first size * size + amount +** nodes, where: +** size - size of square side +** amount - quantity of tetrominoes in file +*/ + +t_node **add_cols(t_node *root, int number); + +/* +** actually, the same as add_column. But why not to add yet another +** "useful" function ? :) +*/ + +t_node *add_node(t_node *col); + +/* +** again, the same as add_node() and add_column(). For clarity :) +*/ + +t_node *add_node_with_coord(t_node *col, t_coord row, int type); + +/* +** output of the program +*/ + +void print_square(t_node **sol, int amount, int size); + +/* +** add all possible position variants of a given figures. +*/ + +void add_rows(int *types, int amount, int size, + t_node **cols_arr); +void link_row(int *col_nums, t_node **cols_arr); +void add_row(int *col_numbers, t_node **cols_arr, + t_coord coord, int type); +void search(t_node *root, t_node **solution, int k, int amount); +int cover(t_node *to_cover); +int uncover(t_node *to_uncover); +int *get_types(char **ttr); +int get_amount(char **ttr); +int ft_sqrt_ceil(int num); +void fill_map(char *map, int size, t_node *ttr, int letter); +void init(char *path); +void error(void); +void check_ch(char ch); +void check_table(char **ttr); +void algorithm(int *types, int amount); + +#endif diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..fbe50a5 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,102 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: gtertysh +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2016/11/28 11:42:56 by gtertysh #+# #+# # +# Updated: 2016/12/06 20:21:16 by gtertysh ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a +SRC = ft_memset.c \ + ft_bzero.c \ + ft_memcpy.c \ + ft_memccpy.c \ + ft_memmove.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_strlen.c \ + ft_strdup.c \ + ft_strcpy.c \ + ft_strncpy.c \ + ft_strcat.c \ + ft_strncat.c \ + ft_strlcat.c \ + ft_strchr.c \ + ft_strrchr.c \ + ft_strstr.c \ + ft_strnstr.c \ + ft_strcmp.c \ + ft_strncmp.c \ + ft_atoi.c \ + ft_isalpha.c \ + ft_isdigit.c \ + ft_isalnum.c \ + ft_isascii.c \ + ft_isprint.c \ + ft_toupper.c \ + ft_tolower.c \ + \ + ft_memalloc.c \ + ft_memdel.c \ + ft_strnew.c \ + ft_strdel.c \ + ft_strclr.c \ + ft_striter.c \ + ft_striteri.c \ + ft_strmap.c \ + ft_strmap.c \ + ft_strmapi.c \ + ft_strequ.c \ + ft_strnequ.c \ + ft_strsub.c \ + ft_strjoin.c \ + ft_strtrim.c \ + ft_strsplit.c \ + ft_itoa.c \ + ft_putchar.c \ + ft_putstr.c \ + ft_putendl.c \ + ft_putnbr.c \ + ft_putchar_fd.c \ + ft_putstr_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + \ + ft_lstnew.c \ + ft_lstdelone.c \ + ft_lstdel.c \ + ft_lstadd.c \ + ft_lstiter.c \ + ft_lstmap.c \ + ft_lststrsplit.c \ + ft_lstfind.c \ + ft_lst_at.c \ + ft_lstadd_back.c \ + ft_lstpop.c \ + \ + ft_realloc.c + +OBJ = $(SRC:.c=.o) +INC = -I libft.h +CC = clang +FLAGS = -Werror -Wextra -Wall + +all: $(NAME) + +$(NAME): $(OBJ) + ar rcs $(NAME) $(OBJ) + +%.o: %.c libft.h + $(CC) $(FLAGS) $(INC) -c $< -o $@ + +clean: + rm -rf $(OBJ) + +fclean: clean + rm -rf $(NAME) + +re: clean all diff --git a/libft/author b/libft/author new file mode 100644 index 0000000..913d4c1 --- /dev/null +++ b/libft/author @@ -0,0 +1 @@ +gtertysh diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100644 index 0000000..4a3c0ae --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/10/31 10:27:11 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 19:51:54 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_atoi(const char *str) +{ + int numb; + int is_negative; + + numb = 0; + is_negative = 0; + while (*str == ' ' || *str == '\n' || *str == '\t' || + *str == '\r' || *str == '\f' || *str == '\v') + str++; + if (*str == '-' && str++) + is_negative = 1; + else if (*str == '+') + str++; + while (*str >= '0' && *str <= '9' && *str != '\0') + { + numb = numb * 10 + *str - '0'; + str++; + } + if (is_negative) + return (-numb); + return (numb); +} diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c new file mode 100644 index 0000000..5241350 --- /dev/null +++ b/libft/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 16:19:32 by gtertysh #+# #+# */ +/* Updated: 2016/11/28 16:39:45 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c new file mode 100644 index 0000000..0cb1c1c --- /dev/null +++ b/libft/ft_isalnum.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 15:11:05 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 15:17:24 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + if (ft_isalpha(c) || ft_isdigit(c)) + return (1); + return (0); +} diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c new file mode 100644 index 0000000..eb57722 --- /dev/null +++ b/libft/ft_isalpha.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 14:58:04 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 15:05:30 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z')) + return (1); + return (0); +} diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c new file mode 100644 index 0000000..c303ae8 --- /dev/null +++ b/libft/ft_isascii.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 15:18:01 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 15:21:12 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100644 index 0000000..4e221d9 --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 15:08:32 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 15:09:31 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c new file mode 100644 index 0000000..4ea94b2 --- /dev/null +++ b/libft/ft_isprint.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 15:22:01 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 15:24:46 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c new file mode 100644 index 0000000..78a580d --- /dev/null +++ b/libft/ft_itoa.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/02 19:59:03 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:12:59 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void help_help(int *n, char *ret, int *i) +{ + if (*n < 0) + { + ret[0] = '-'; + *i = *i + 1; + *n = -(*n); + } + if (*n == 0) + ret[0] = '0'; +} + +static char *help(int n, long *div) +{ + int len; + char *ret; + + ret = NULL; + len = 0; + while (n) + { + n /= 10; + *div = *div * 10; + len++; + } + if (n < 0 || n == 0) + len++; + ret = ft_strnew(len); + return (ret); +} + +char *ft_itoa(int n) +{ + char *ret; + long div; + int i; + int exception; + + exception = 0; + div = 1; + i = 0; + if (n == -2147483648) + { + n = n / 10; + exception = 1; + } + if (!(ret = help(n, &div))) + return (NULL); + help_help(&n, ret, &i); + while (div > 1) + { + div /= 10; + ret[i++] = n / div + '0'; + n = n % div; + } + if (exception) + ret[i] = '8'; + return (ret); +} diff --git a/libft/ft_lst_at.c b/libft/ft_lst_at.c new file mode 100644 index 0000000..126a9ed --- /dev/null +++ b/libft/ft_lst_at.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_at.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/06 17:17:21 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:49:09 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lst_at(t_list *l, unsigned int at) +{ + while (at-- && l) + l = l->next; + return (l); +} diff --git a/libft/ft_lstadd.c b/libft/ft_lstadd.c new file mode 100644 index 0000000..f059109 --- /dev/null +++ b/libft/ft_lstadd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 19:01:42 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 19:12:10 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd(t_list **alst, t_list *new) +{ + if (alst) + { + new->next = *alst; + *alst = new; + } +} diff --git a/libft/ft_lstadd_back.c b/libft/ft_lstadd_back.c new file mode 100644 index 0000000..c8bfb60 --- /dev/null +++ b/libft/ft_lstadd_back.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/06 17:45:28 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 20:06:23 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **l, void *content, size_t size) +{ + if (!l) + return ; + if (*l == NULL) + (*l) = ft_lstnew(content, size); + else + { + while ((*l)->next) + l = &(*l)->next; + (*l)->next = ft_lstnew(content, size); + } +} diff --git a/libft/ft_lstdel.c b/libft/ft_lstdel.c new file mode 100644 index 0000000..49903f0 --- /dev/null +++ b/libft/ft_lstdel.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 18:48:08 by gtertysh #+# #+# */ +/* Updated: 2016/12/05 18:50:39 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) +{ + t_list *tmp; + + tmp = NULL; + if (alst) + { + while (*alst) + { + tmp = (*alst)->next; + del((*alst)->content, (*alst)->content_size); + free(*alst); + *alst = tmp; + } + *alst = NULL; + } +} diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c new file mode 100644 index 0000000..371c65d --- /dev/null +++ b/libft/ft_lstdelone.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 18:30:18 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:46:45 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) +{ + if (alst && *alst && del) + { + del((*alst)->content, (*alst)->content_size); + free(*alst); + *alst = NULL; + } +} diff --git a/libft/ft_lstfind.c b/libft/ft_lstfind.c new file mode 100644 index 0000000..21c42be --- /dev/null +++ b/libft/ft_lstfind.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstfind.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/06 16:56:43 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:47:14 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstfind(t_list *lst, void *data, size_t size) +{ + while (lst) + { + if ((ft_memcmp(lst->content, data, size) == 0)) + return (lst); + lst = lst->next; + } + return (0); +} diff --git a/libft/ft_lstiter.c b/libft/ft_lstiter.c new file mode 100644 index 0000000..148fc0f --- /dev/null +++ b/libft/ft_lstiter.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 19:13:25 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:47:50 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + while (lst && f) + { + f(lst); + lst = lst->next; + } +} diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c new file mode 100644 index 0000000..ca591fb --- /dev/null +++ b/libft/ft_lstmap.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 19:18:15 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 18:08:53 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *new; + + new = NULL; + if (lst && f) + { + new = f(lst); + new->next = ft_lstmap(lst->next, f); + } + return (new); +} diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c new file mode 100644 index 0000000..2514624 --- /dev/null +++ b/libft/ft_lstnew.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 15:59:21 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:45:44 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *new; + + new = NULL; + if ((new = (t_list *)malloc(sizeof(t_list)))) + { + if (!content) + { + new->content = NULL; + new->content_size = 0; + } + else + { + if (!(new->content = (char *)malloc(content_size))) + { + free(new); + return (NULL); + } + ft_memcpy(new->content, content, content_size); + new->content_size = content_size; + } + new->next = NULL; + } + return (new); +} diff --git a/libft/ft_lstpop.c b/libft/ft_lstpop.c new file mode 100644 index 0000000..dc7af32 --- /dev/null +++ b/libft/ft_lstpop.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 18:30:18 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:46:45 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstpop(t_list **alst) +{ + t_list *first; + + first = NULL; + if (alst) + { + first = *alst; + *alst = (*alst)->next; + } + return (first); +} diff --git a/libft/ft_lststrsplit.c b/libft/ft_lststrsplit.c new file mode 100644 index 0000000..4960770 --- /dev/null +++ b/libft/ft_lststrsplit.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lststrsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/06 15:32:39 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 20:07:14 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lststrsplit(const char *s, char c) +{ + char **arr_table; + t_list *lst_table; + t_list *tmp; + int i; + + i = 0; + lst_table = NULL; + arr_table = ft_strsplit(s, c); + if (arr_table[i]) + { + lst_table = ft_lstnew(arr_table[i], ft_strlen(arr_table[i]) + 1); + tmp = lst_table; + i++; + while (arr_table[i]) + { + tmp->next = ft_lstnew(arr_table[i], ft_strlen(arr_table[i]) + 1); + tmp = tmp->next; + i++; + } + } + return (lst_table); +} diff --git a/libft/ft_memalloc.c b/libft/ft_memalloc.c new file mode 100644 index 0000000..9364e94 --- /dev/null +++ b/libft/ft_memalloc.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 16:07:55 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 16:49:22 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memalloc(size_t size) +{ + void *ret; + + ret = NULL; + if ((ret = malloc(size))) + ft_bzero(ret, size); + return (ret); +} diff --git a/libft/ft_memccpy.c b/libft/ft_memccpy.c new file mode 100644 index 0000000..4e71b3f --- /dev/null +++ b/libft/ft_memccpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/29 13:35:32 by gtertysh #+# #+# */ +/* Updated: 2016/12/05 13:12:20 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memccpy(void *dst, const void *src, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + *((char *)dst + i) = *((const char *)src + i); + if (*((unsigned char *)src + i) == (unsigned char)c) + return ((dst + i + 1)); + i++; + } + return (NULL); +} diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c new file mode 100644 index 0000000..eb05917 --- /dev/null +++ b/libft/ft_memchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/29 15:55:30 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 18:51:40 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + while (n--) + { + if (*(unsigned char *)s == (unsigned char)c) + return ((unsigned char *)s); + s++; + } + return (NULL); +} diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c new file mode 100644 index 0000000..6b92874 --- /dev/null +++ b/libft/ft_memcmp.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/29 16:31:56 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 14:07:47 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + while ((int)n--) + { + if ((*(unsigned char *)s1 != *(unsigned char *)s2)) + return (*(unsigned char *)s1 - *(unsigned char *)s2); + s1++; + s2++; + } + return (0); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100644 index 0000000..52df83c --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 17:19:13 by gtertysh #+# #+# */ +/* Updated: 2016/11/29 14:52:27 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t len) +{ + size_t i; + + i = 0; + while (i < len) + { + *((char *)dst + i) = *((const char *)src + i); + i++; + } + return (dst); +} diff --git a/libft/ft_memdel.c b/libft/ft_memdel.c new file mode 100644 index 0000000..e1ba7a0 --- /dev/null +++ b/libft/ft_memdel.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 16:50:09 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 17:18:12 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_memdel(void **ap) +{ + if (ap) + { + free(*ap); + *ap = NULL; + } +} diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c new file mode 100644 index 0000000..d0a749e --- /dev/null +++ b/libft/ft_memmove.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/29 14:55:05 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 16:38:06 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + unsigned char *tmp; + + tmp = dst; + if (src < dst) + { + src += len; + dst += len; + while (len--) + *((char *)--dst) = *((const char *)--src); + } + else + { + while (len--) + *(char *)dst++ = *(const char *)src++; + } + return (tmp); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c new file mode 100644 index 0000000..ac837d1 --- /dev/null +++ b/libft/ft_memset.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 12:06:20 by gtertysh #+# #+# */ +/* Updated: 2016/11/28 16:47:35 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *b, int c, size_t len) +{ + unsigned char *s; + + s = (unsigned char *)b; + while (len--) + *s++ = (unsigned char)c; + return (b); +} diff --git a/libft/ft_putchar.c b/libft/ft_putchar.c new file mode 100644 index 0000000..daac03e --- /dev/null +++ b/libft/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 14:29:56 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:04:42 by gtertysh ### ########.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..2349170 --- /dev/null +++ b/libft/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 15:03:56 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:04:30 by gtertysh ### ########.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..e9d9f3d --- /dev/null +++ b/libft/ft_putendl.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 14:57:59 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:24:37 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl(char const *s) +{ + if (s) + while (*s) + write(1, s++, 1); + write(1, "\n", 1); +} diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c new file mode 100644 index 0000000..45c78a7 --- /dev/null +++ b/libft/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 15:17:18 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:24:55 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char const *s, int fd) +{ + if (s) + while (*s) + write(fd, s++, 1); + write(fd, "\n", 1); +} diff --git a/libft/ft_putnbr.c b/libft/ft_putnbr.c new file mode 100644 index 0000000..0a59078 --- /dev/null +++ b/libft/ft_putnbr.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/10/31 09:45:39 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:26:28 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void ft_putnbr_print(int nb, long div, int exeption) +{ + if (nb < 0) + { + nb = -nb; + ft_putchar('-'); + } + if (div == 1) + ft_putchar('0'); + else + { + while (div > 1) + { + div = div / 10; + ft_putchar(nb / div + '0'); + nb = nb % div; + } + } + if (exeption) + ft_putchar('8'); +} + +void ft_putnbr(int nb) +{ + int temp; + long div; + int exeption; + + div = 1; + exeption = 0; + temp = nb; + if (nb == -2147483648) + { + nb = nb / 10; + temp = temp / 10; + exeption = 1; + } + while (temp) + { + temp = temp / 10; + div = div * 10; + } + ft_putnbr_print(nb, div, exeption); +} diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..7c1ad10 --- /dev/null +++ b/libft/ft_putnbr_fd.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 15:19:18 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:25:42 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void ft_putnbr_print(int nb, long div, int exeption, int fd) +{ + if (nb < 0) + { + nb = -nb; + ft_putchar_fd('-', fd); + } + if (div == 1) + ft_putchar_fd('0', fd); + else + { + while (div > 1) + { + div = div / 10; + ft_putchar_fd(nb / div + '0', fd); + nb = nb % div; + } + } + if (exeption) + ft_putchar_fd('8', fd); +} + +void ft_putnbr_fd(int nb, int fd) +{ + int temp; + long div; + int exeption; + + div = 1; + exeption = 0; + temp = nb; + if (nb == -2147483648) + { + nb = nb / 10; + temp = temp / 10; + exeption = 1; + } + while (temp) + { + temp = temp / 10; + div = div * 10; + } + ft_putnbr_print(nb, div, exeption, fd); +} diff --git a/libft/ft_putstr.c b/libft/ft_putstr.c new file mode 100644 index 0000000..bbe80cb --- /dev/null +++ b/libft/ft_putstr.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 14:55:00 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:25:12 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + if (s) + while (*s) + write(1, s++, 1); +} diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c new file mode 100644 index 0000000..7f7f74f --- /dev/null +++ b/libft/ft_putstr_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/04 15:14:02 by gtertysh #+# #+# */ +/* Updated: 2016/12/04 15:26:07 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char const *s, int fd) +{ + if (s) + while (*s) + write(fd, s++, 1); +} diff --git a/libft/ft_realloc.c b/libft/ft_realloc.c new file mode 100644 index 0000000..2ad3311 --- /dev/null +++ b/libft/ft_realloc.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_realloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/06 18:57:55 by gtertysh #+# #+# */ +/* Updated: 2016/12/07 17:16:48 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_realloc(void *old, long new_size, long old_size) +{ + void *new; + + new = NULL; + if ((new = ft_memalloc(new_size))) + { + if (old) + { + if (old_size) + ft_memcpy(new, old, old_size); + free(old); + old = NULL; + } + return (new); + } + return (old); +} diff --git a/libft/ft_strcat.c b/libft/ft_strcat.c new file mode 100644 index 0000000..ebe3ef6 --- /dev/null +++ b/libft/ft_strcat.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 14:58:16 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 15:02:50 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *s1, const char *s2) +{ + char *s; + + s = s1; + while (*s1) + s1++; + while (*s2) + *s1++ = *s2++; + *s1 = '\0'; + return (s); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100644 index 0000000..7b48bf9 --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 18:14:04 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 18:29:27 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s && *s != (char)c) + s++; + if (*s == (char)c) + return ((char *)s); + return (NULL); +} diff --git a/libft/ft_strclr.c b/libft/ft_strclr.c new file mode 100644 index 0000000..f03da5a --- /dev/null +++ b/libft/ft_strclr.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 17:21:04 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 17:30:26 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strclr(char *s) +{ + if (s) + ft_bzero(s, ft_strlen(s)); +} diff --git a/libft/ft_strcmp.c b/libft/ft_strcmp.c new file mode 100644 index 0000000..a8d73ee --- /dev/null +++ b/libft/ft_strcmp.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 13:40:16 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:52:50 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + while (*s1 && *s2 && *(unsigned char *)s1 == *(unsigned char *)s2) + { + s1++; + s2++; + } + if (*(unsigned char *)s1 != *(unsigned char *)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..9b09b72 --- /dev/null +++ b/libft/ft_strcpy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 13:03:43 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 13:09:52 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dst, const char *src) +{ + size_t i; + + i = 0; + while (*src) + *(dst + i++) = *src++; + *(dst + i) = '\0'; + return (dst); +} diff --git a/libft/ft_strdel.c b/libft/ft_strdel.c new file mode 100644 index 0000000..ecb0905 --- /dev/null +++ b/libft/ft_strdel.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 17:15:34 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 17:17:50 by gtertysh ### ########.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..3a8468f --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 12:51:34 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 18:31:11 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *new; + size_t size; + + size = ft_strlen(s1); + if (!(new = (char *)malloc(sizeof(char) * (size + 1)))) + return (0); + ft_memcpy(new, s1, size); + *(new + size) = '\0'; + return (new); +} diff --git a/libft/ft_strequ.c b/libft/ft_strequ.c new file mode 100644 index 0000000..f257cd9 --- /dev/null +++ b/libft/ft_strequ.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:51:03 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 19:00:02 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strequ(char const *s1, char const *s2) +{ + if (s1 && s2) + { + while (*s1 && *s2 && *s1 == *s2) + { + s1++; + s2++; + } + if (*s1 != *s2) + return (0); + } + return (1); +} diff --git a/libft/ft_striter.c b/libft/ft_striter.c new file mode 100644 index 0000000..1ab2c28 --- /dev/null +++ b/libft/ft_striter.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 17:36:39 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 17:37:33 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striter(char *s, void (*f)(char *)) +{ + if (s && f) + while (*s) + f(s++); +} diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c new file mode 100644 index 0000000..0aacc9b --- /dev/null +++ b/libft/ft_striteri.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 17:31:23 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 17:38:48 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + unsigned int i; + + i = 0; + if (s && f) + while (*s) + f(i++, s++); +} diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100644 index 0000000..378e595 --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 19:53:30 by gtertysh #+# #+# */ +/* Updated: 2016/12/02 16:18:10 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *ret; + char *tmp; + + ret = NULL; + if (s1 && + s2 && + (ret = ft_strnew(ft_strlen(s1) + ft_strlen(s2))) && + (tmp = ret)) + { + while (*s1) + *tmp++ = *s1++; + while (*s2) + *tmp++ = *s2++; + } + return (ret); +} diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c new file mode 100644 index 0000000..e613c1b --- /dev/null +++ b/libft/ft_strlcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 15:16:55 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 18:13:07 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t dst_size; + size_t src_size; + + dst_size = ft_strlen(dst); + src_size = ft_strlen(src); + while (*dst) + dst++; + if (size > dst_size) + { + while (*src && (size-- - dst_size - 1)) + *dst++ = *src++; + *dst = '\0'; + return (src_size + dst_size); + } + return (src_size + size); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100644 index 0000000..65ff048 --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 12:37:23 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 12:48:50 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t len; + + len = 0; + while (*(unsigned char *)s++) + len++; + return (len); +} diff --git a/libft/ft_strmap.c b/libft/ft_strmap.c new file mode 100644 index 0000000..a8b2b57 --- /dev/null +++ b/libft/ft_strmap.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 17:39:51 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 19:53:31 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + char *ret; + size_t i; + + i = 0; + ret = NULL; + if (s) + if ((ret = (char *)ft_strnew(ft_strlen(s)))) + while (*(s + i)) + { + *(ret + i) = f(*(s + i)); + i++; + } + return (ret); +} diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c new file mode 100644 index 0000000..d532b4e --- /dev/null +++ b/libft/ft_strmapi.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:47:40 by gtertysh #+# #+# */ +/* Updated: 2016/12/02 16:18:37 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *ret; + unsigned int i; + + i = 0; + ret = NULL; + if (s) + if ((ret = (char *)ft_strnew(ft_strlen(s)))) + while (*(s + i)) + { + *(ret + i) = f(i, *(s + i)); + i++; + } + return (ret); +} diff --git a/libft/ft_strncat.c b/libft/ft_strncat.c new file mode 100644 index 0000000..881fa8a --- /dev/null +++ b/libft/ft_strncat.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 15:03:18 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 15:11:23 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncat(char *s1, const char *s2, size_t n) +{ + char *s; + size_t s1_size; + + s = s1; + s1_size = sizeof(s1); + while (*s1) + s1++; + while (*s2 && s1_size - 1 && n--) + *s1++ = *s2++; + *s1 = '\0'; + return (s); +} diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c new file mode 100644 index 0000000..7976e67 --- /dev/null +++ b/libft/ft_strncmp.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 14:33:11 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 14:23:45 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + while (i < n && (*s1 || *s2)) + { + if (*(unsigned char *)s1 != *(unsigned char *)s2) + return (*(unsigned char *)s1 - *(unsigned char *)s2); + s1++; + s2++; + i++; + } + return (0); +} diff --git a/libft/ft_strncpy.c b/libft/ft_strncpy.c new file mode 100644 index 0000000..f88f84e --- /dev/null +++ b/libft/ft_strncpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 13:12:41 by gtertysh #+# #+# */ +/* Updated: 2016/11/30 14:39:28 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncpy(char *dst, const char *src, size_t len) +{ + char *new_dest; + + new_dest = dst; + while (len && *src) + { + *new_dest++ = *src++; + if (!len) + return (dst); + len--; + } + while (len-- > 0) + *(new_dest++) = '\0'; + return (dst); +} diff --git a/libft/ft_strnequ.c b/libft/ft_strnequ.c new file mode 100644 index 0000000..21219fc --- /dev/null +++ b/libft/ft_strnequ.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 19:00:40 by gtertysh #+# #+# */ +/* Updated: 2016/12/02 19:00:55 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + if (s1 && s2) + while (n--) + if (ft_tolower(*s1++) != ft_tolower(*s2++)) + return (0); + return (1); +} diff --git a/libft/ft_strnew.c b/libft/ft_strnew.c new file mode 100644 index 0000000..699a6f5 --- /dev/null +++ b/libft/ft_strnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 17:09:46 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 17:14:53 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnew(size_t size) +{ + char *ret; + + ret = NULL; + if ((ret = (char *)malloc(size + 1))) + { + ft_bzero(ret, size + 1); + } + return (ret); +} diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100644 index 0000000..52cbca2 --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 20:26:05 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 13:22:59 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t l; + size_t b; + + l = ft_strlen(little); + b = ft_strlen(big); + if (*little == '\0') + return ((char *)big); + while (len >= l && b) + { + if (ft_memcmp(big, little, l) == 0) + return ((char *)big); + big++; + len--; + b--; + } + return (NULL); +} diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c new file mode 100644 index 0000000..bed8e70 --- /dev/null +++ b/libft/ft_strrchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 18:32:21 by gtertysh #+# #+# */ +/* Updated: 2016/12/02 16:19:20 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + const char *str; + + str = s; + while (*s) + s++; + while (s != str && *s != (char)c) + s--; + if (*s == (char)c) + return ((char *)s); + return (NULL); +} diff --git a/libft/ft_strsplit.c b/libft/ft_strsplit.c new file mode 100644 index 0000000..a5d83ed --- /dev/null +++ b/libft/ft_strsplit.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/02 17:36:40 by gtertysh #+# #+# */ +/* Updated: 2016/12/07 17:24:37 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int count_words(char const *str, char c) +{ + int words; + int has_word; + + words = 0; + has_word = 0; + while (*str) + { + while (*str == c) + str++; + while (*str != c && *str) + { + str++; + has_word = 1; + } + if (has_word) + words++; + has_word = 0; + } + return (words); +} + +static int fill_table(char const *str, char **table, char c) +{ + int chars; + int char_position; + int word_position; + + word_position = 0; + while (*str) + { + chars = 0; + char_position = 0; + while (*str == c) + str++; + while (*str != c && *str && ++chars) + str++; + if (chars != 0) + { + if (!(table[word_position] = malloc(sizeof(char) * (chars + 1)))) + return (1); + while (chars) + table[word_position][char_position++] = *(str - chars--); + table[word_position++][char_position] = '\0'; + } + } + table[word_position] = 0; + return (0); +} + +char **ft_strsplit(char const *s, char c) +{ + char **table; + int words; + + table = NULL; + if (!s) + return (NULL); + words = count_words(s, c); + if (!words) + { + if (!(table = (char **)malloc(sizeof(char *)))) + return (NULL); + } + else if (!(table = (char **)malloc(sizeof(char *) * (words + 1)))) + return (NULL); + if (fill_table(s, table, c)) + return (table); + return (table); +} diff --git a/libft/ft_strstr.c b/libft/ft_strstr.c new file mode 100644 index 0000000..1dc9ce3 --- /dev/null +++ b/libft/ft_strstr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 19:32:43 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 14:23:54 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strstr(const char *big, const char *little) +{ + size_t l; + + l = ft_strlen(little); + if (!*little) + return ((char *)big); + while (*big) + { + if (!(ft_memcmp(big, little, l))) + return ((char *)big); + big++; + } + return (NULL); +} diff --git a/libft/ft_strsub.c b/libft/ft_strsub.c new file mode 100644 index 0000000..5c01d12 --- /dev/null +++ b/libft/ft_strsub.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 19:02:56 by gtertysh #+# #+# */ +/* Updated: 2016/12/02 16:21:00 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *ret; + char *tmp; + + ret = NULL; + if (s) + if ((ret = ft_strnew(len)) && + (tmp = ret)) + while (len--) + *tmp++ = *(s++ + start); + return (ret); +} diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100644 index 0000000..948e1db --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 20:25:11 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 14:24:05 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s) +{ + char *ret; + char *tmp; + const char *start; + const char *end; + + ret = NULL; + if (s) + { + while (*s == ' ' || *s == '\n' || *s == '\t') + s++; + start = s; + while (*s && *(s + 1)) + s++; + while (*s == ' ' || *s == '\n' || *s == '\t') + s--; + end = s; + if (!(ret = ft_strnew(end - start + 1))) + return (ret); + tmp = ret; + while (start <= end) + *tmp++ = *start++; + } + return (ret); +} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c new file mode 100644 index 0000000..9c823bb --- /dev/null +++ b/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 15:58:52 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 15:59:14 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c + 32); + return (c); +} diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c new file mode 100644 index 0000000..b9fc06f --- /dev/null +++ b/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 15:26:03 by gtertysh #+# #+# */ +/* Updated: 2016/12/01 15:58:00 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c - 32); + return (c); +} diff --git a/libft/libft.h b/libft/libft.h new file mode 100644 index 0000000..476c6e7 --- /dev/null +++ b/libft/libft.h @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 12:21:43 by gtertysh #+# #+# */ +/* Updated: 2016/12/06 20:08:35 by gtertysh ### ########.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_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t len); +void *ft_memccpy(void *dst, const void *src, int c, size_t n); +void *ft_memmove(void *dst, const void *src, size_t len); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +size_t ft_strlen(const char *s); +char *ft_strdup(const char *s1); +char *ft_strcpy(char *dst, const char *src); +char *ft_strncpy(char *dst, const char *src, size_t len); +char *ft_strcat(char *s1, const char *s2); +char *ft_strncat(char *s1, const char *s2, size_t n); +size_t ft_strlcat(char *dst, const char *src, size_t size); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strstr(const char *big, const char *little); +char *ft_strnstr(const char *big, const char *little, + size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_atoi(const char *str); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalpha(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +int ft_tolower(int c); +int ft_toupper(int c); + +void *ft_memalloc(size_t size); +void ft_memdel(void **ap); +char *ft_strnew(size_t size); +void ft_strdel(char **as); +void ft_strclr(char *s); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +int ft_strequ(char const *s1, char const *s2); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s); +char **ft_strsplit(char const *s, char c); +char *ft_itoa(int c); +void ft_putchar(char c); +void ft_putstr(char const *s); +void ft_putendl(char const *s); +void ft_putnbr(int n); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char const *s, int fd); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr_fd(int n, int fd); + +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); +void ft_lstadd(t_list **alst, t_list *new); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); +t_list *ft_lstpop(t_list **alst); + +t_list *ft_lststrsplit(const char *s, char c); +t_list *ft_lstfind(t_list *lst, void *data, size_t size); +t_list *ft_lst_at(t_list *l, unsigned int at); +void ft_lstadd_back(t_list **l, void *data, size_t size); +void *ft_realloc(void *old, long new_size, + long old_size); + +#endif diff --git a/obj/.gitkeep b/obj/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/algorithm.c b/src/algorithm.c new file mode 100644 index 0000000..0a754b7 --- /dev/null +++ b/src/algorithm.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* algorithm.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/26 13:40:58 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 13:42:55 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int ft_sqrt_ceil(int num) +{ + int i; + + i = 1; + while (i * i < num) + i++; + return (i); +} + +void algorithm(int *types, int amount) +{ + int size; + t_node *r; + t_node **cols_arr; + t_node **ans; + int go; + + go = 1; + ans = (t_node**)malloc(sizeof(t_node*) * amount); + size = ft_sqrt_ceil(amount * 4); + while (go) + { + r = init_root(size); + cols_arr = add_cols(r, (size * size + amount)); + add_rows(types, amount, size, cols_arr); + search(r, ans, 0, amount); + size++; + } +} diff --git a/src/check.c b/src/check.c new file mode 100644 index 0000000..a64c573 --- /dev/null +++ b/src/check.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/26 15:30:15 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 15:30:19 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +void check_endline(char *str) +{ + int i; + + i = 4; + while (i < 20) + { + if (!(str[i] == '\n')) + error(); + i += 5; + } +} + +void check_char(char ch) +{ + if (!(ch == '.' || ch == '\n' || ch == '#')) + error(); +} + +void check_string(char *string_ttrs) +{ + int i; + int j; + + i = 0; + j = 0; + while (string_ttrs[i]) + { + check_char(string_ttrs[i]); + if (i == j) + check_endline((string_ttrs + i)); + if ((i >= 20) && (i % (20 + j)) == 0) + { + if (string_ttrs[i] == '\n' && string_ttrs[i - 1] == '\n') + if (string_ttrs[i + 1] == '\0') + error(); + else + j += 21; + else + error(); + } + i++; + } +} + +void check_table(char **ttr) +{ + int amount; + int match; + int i; + int j; + + j = 0; + amount = 0; + match = 0; + while (ttr[amount]) + amount++; + while (j < amount) + { + i = 0; + while (i < 19) + if (ft_strcmp(ttr[j], g_templates[i++].t) == 0) + { + match++; + break ; + } + j++; + } + if (amount != match) + error(); +} diff --git a/src/dancing_links.c b/src/dancing_links.c new file mode 100644 index 0000000..57f7d05 --- /dev/null +++ b/src/dancing_links.c @@ -0,0 +1,125 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dancing_links.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/20 19:21:42 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 14:41:17 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +void fill_map(char *map, int size, t_node *ttr, int letter) +{ + int i; + char a; + + a = 'A' + letter - 1; + i = 0; + while (i < 4) + { + ft_memcpy((map + (ttr->coord.y + g_templates[ttr->type].c[i].y) * size + + ttr->coord.x + g_templates[ttr->type].c[i].x), &a, 1); + i++; + } +} + +void print_square(t_node **solution, int amount, int size) +{ + char *map; + int map_size; + int i; + + map_size = size * size + 1; + map = malloc(sizeof(char) * map_size); + map[map_size] = 0; + i = 0; + while (i++ < amount) + fill_map(map, size, solution[i - 1], i); + i = 0; + while (i < map_size - 1) + { + if (map[i] >= 'A' && map[i] <= 'Z') + ft_putchar(map[i]); + else + ft_putchar('.'); + if ((i + 1) % size == 0) + ft_putchar('\n'); + i++; + } + exit(0); +} + +int cover(t_node *to_cover) +{ + static t_node *step_vert; + static t_node *step_horiz; + + to_cover->left->right = to_cover->right; + to_cover->right->left = to_cover->left; + step_vert = to_cover->down; + while (step_vert != to_cover) + { + step_horiz = step_vert->right; + while (step_horiz != step_vert) + { + step_horiz->down->up = step_horiz->up; + step_horiz->up->down = step_horiz->down; + step_horiz = step_horiz->right; + } + step_vert = step_vert->down; + } + return (1); +} + +int uncover(t_node *to_uncover) +{ + static t_node *step_vert; + static t_node *step_horiz; + + step_vert = to_uncover->up; + while (step_vert != to_uncover) + { + step_horiz = step_vert->left; + while (step_horiz != step_vert) + { + step_horiz->down->up = step_horiz; + step_horiz->up->down = step_horiz; + step_horiz = step_horiz->left; + } + step_vert = step_vert->up; + } + to_uncover->left->right = to_uncover; + to_uncover->right->left = to_uncover; + return (1); +} + +void search(t_node *root, t_node **solution, int k, int amount) +{ + t_node *current_col; + t_node *row; + t_node *j; + + if (k == amount) + print_square(solution, amount, root->size); + current_col = root->right; + cover(current_col); + row = current_col->down; + while (row != current_col) + { + solution[k] = row; + j = row->right; + while (j != row && cover(j->column)) + j = j->right; + search(root, solution, k + 1, amount); + j = row->left; + while (j != row && uncover(j->column)) + j = j->left; + row = row->down; + } + uncover(current_col); + return ; +} diff --git a/src/dancing_links_initialization.c b/src/dancing_links_initialization.c new file mode 100644 index 0000000..a81e475 --- /dev/null +++ b/src/dancing_links_initialization.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dancing_links_initialization.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/26 14:40:31 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 14:40:34 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +t_node *init_root(int size) +{ + t_node *new; + + new = (t_node*)malloc(sizeof(t_node)); + new->left = new; + new->right = new; + new->down = new; + new->up = new; + new->column = NULL; + new->size = size; + return (new); +} + +t_node *add_column(t_node *root) +{ + t_node *new_col; + + new_col = (t_node*)malloc(sizeof(t_node)); + new_col->down = new_col; + new_col->up = new_col; + new_col->left = root->left; + new_col->right = root; + root->left->right = new_col; + root->left = new_col; + new_col->column = NULL; + return (new_col); +} + +t_node *add_node_with_coord(t_node *col, t_coord coord, int type) +{ + t_node *new_node; + + new_node = (t_node*)malloc(sizeof(t_node)); + new_node->coord = coord; + new_node->type = type; + new_node->column = col; + new_node->up = col->up; + new_node->down = col; + col->up->down = new_node; + col->up = new_node; + return (new_node); +} + +t_node *add_node(t_node *col) +{ + t_node *new_node; + + new_node = (t_node*)malloc(sizeof(t_node)); + new_node->column = col; + new_node->up = col->up; + new_node->down = col; + col->up->down = new_node; + col->up = new_node; + return (new_node); +} + +t_node **add_cols(t_node *root, int number) +{ + t_node **array; + int i; + + array = (t_node**)malloc(number * sizeof(t_node*)); + i = 0; + while (i < number) + array[i++] = add_column(root); + return (array); +} diff --git a/src/dancing_links_initialization_2.c b/src/dancing_links_initialization_2.c new file mode 100644 index 0000000..3d505fb --- /dev/null +++ b/src/dancing_links_initialization_2.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dancing_links_initialization_2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/26 15:16:08 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 15:16:11 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int *get_coords(t_coord coord, int amount, int type, int size) +{ + int *result; + int i; + t_coord *start; + + result = (int *)malloc(sizeof(int) * 5); + start = g_templates[type].c; + i = 0; + while (i < 4) + { + result[i + 1] = (coord.x + coord.y * size + + start[i].x + start[i].y * size) + amount; + i++; + } + return (result); +} + +void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type) +{ + add_node_with_coord(cols_arr[col_numbers[0]], coord, type); + add_node(cols_arr[col_numbers[1]]); + add_node(cols_arr[col_numbers[2]]); + add_node(cols_arr[col_numbers[3]]); + add_node(cols_arr[col_numbers[4]]); +} + +void link_row(int *col_nums, t_node **cols_arr) +{ + cols_arr[col_nums[0]]->up->left = cols_arr[col_nums[4]]->up; + cols_arr[col_nums[0]]->up->right = cols_arr[col_nums[1]]->up; + cols_arr[col_nums[1]]->up->left = cols_arr[col_nums[0]]->up; + cols_arr[col_nums[1]]->up->right = cols_arr[col_nums[2]]->up; + cols_arr[col_nums[2]]->up->left = cols_arr[col_nums[1]]->up; + cols_arr[col_nums[2]]->up->right = cols_arr[col_nums[3]]->up; + cols_arr[col_nums[3]]->up->left = cols_arr[col_nums[2]]->up; + cols_arr[col_nums[3]]->up->right = cols_arr[col_nums[4]]->up; + cols_arr[col_nums[4]]->up->left = cols_arr[col_nums[3]]->up; + cols_arr[col_nums[4]]->up->right = cols_arr[col_nums[0]]->up; +} + +void add_rows(int *types, int amount, int size, t_node **cols_arr) +{ + int i; + int k; + int l; + int *col_num; + t_coord coord; + + i = 0; + while (i < amount) + { + l = 0; + while (l <= size - g_templates[types[i]].height) + { + k = 0; + while (k <= size - g_templates[types[i]].width) + { + coord = (t_coord){l, k++}; + col_num = get_coords(coord, amount, types[i], size); + col_num[0] = i; + add_row(col_num, cols_arr, coord, types[i]); + link_row(col_num, cols_arr); + } + l++; + } + i++; + } +} diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..6562715 --- /dev/null +++ b/src/error.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/26 13:39:43 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 13:39:46 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +void error(void) +{ + ft_putstr("error\n"); + exit(1); +} diff --git a/src/get_amount.c b/src/get_amount.c new file mode 100644 index 0000000..69d4498 --- /dev/null +++ b/src/get_amount.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_amount.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/26 15:21:24 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 15:21:27 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int get_amount(char **ttr) +{ + int amount; + + amount = 0; + while (ttr[amount]) + amount++; + return (amount); +} diff --git a/src/get_types.c b/src/get_types.c new file mode 100644 index 0000000..2fcb287 --- /dev/null +++ b/src/get_types.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_types.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/26 15:55:32 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 15:55:35 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +int *get_types(char **ttr) +{ + int *types; + int i; + int j; + int size; + + i = 0; + size = 0; + while (ttr[size]) + size++; + types = malloc(sizeof(int) * size); + while (i < size) + { + j = 0; + while (j < 19) + { + if (ft_strcmp(ttr[i], g_templates[j].t) == 0) + { + types[i] = j; + break ; + } + j++; + } + i++; + } + return (types); +} diff --git a/src/glue_figure.c b/src/glue_figure.c new file mode 100644 index 0000000..414cbdd --- /dev/null +++ b/src/glue_figure.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* glue_figure.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/14 17:12:30 by gtertysh #+# #+# */ +/* Updated: 2016/12/20 20:06:22 by ykolomie ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +/* +** This function creates string table in which one line represent one figure +** by "glueing" evry 4 strings from table +*/ + +char **glue_figure(char **table) +{ + int i; + int j; + int size; + char **ttr_table; + + size = 0; + i = 0; + j = 0; + while (table[size]) + size++; + ttr_table = ft_memalloc(sizeof(char *) * (size / 4 + 1)); + while (i < size / 4) + ttr_table[i++] = ft_memalloc(sizeof(char) * 17); + i = 0; + while (i < size / 4) + { + ft_strcat(ttr_table[i], table[j++]); + if (j % 4 == 0) + i++; + } + i = 0; + while (table[i]) + free(table[i++]); + free(table); + return (ttr_table); +} diff --git a/src/init.c b/src/init.c new file mode 100644 index 0000000..bec929d --- /dev/null +++ b/src/init.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/26 12:55:28 by gtertysh #+# #+# */ +/* Updated: 2016/12/26 12:55:36 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +void init(char *path) +{ + char *string; + char **table; + char **ttr; + int *types; + int amount; + + string = read_file(path); + check_string(string); + table = to_table(&string); + ttr = glue_figure(table); + move_up_left(ttr); + check_table(ttr); + types = get_types(ttr); + amount = get_amount(ttr); + algorithm(types, amount); +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..4ab9df8 --- /dev/null +++ b/src/main.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 16:07:25 by gtertysh #+# #+# */ +/* Updated: 2016/12/23 14:26:30 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +/* +** definition of global variable +** that contains all possible tetrominoes +** inside 4x4 square with initial coordinates at [0][0] +*/ + +t_ttrmn g_templates[19] = +{ + {"#...#...#...#...", 1, 4, {{0, 0}, {1, 0}, {2, 0}, {3, 0}}}, + {"####............", 4, 1, {{0, 0}, {0, 1}, {0, 2}, {0, 3}}}, + {"#...#...##......", 2, 3, {{0, 0}, {1, 0}, {2, 0}, {2, 1}}}, + {"..#.###.........", 3, 2, {{0, 2}, {1, 0}, {1, 1}, {1, 2}}}, + {"##...#...#......", 2, 3, {{0, 0}, {0, 1}, {1, 1}, {2, 1}}}, + {"###.#...........", 3, 2, {{0, 0}, {0, 1}, {0, 2}, {1, 0}}}, + {".#...#..##......", 2, 3, {{0, 1}, {1, 1}, {2, 0}, {2, 1}}}, + {"###...#.........", 3, 2, {{0, 0}, {0, 1}, {0, 2}, {1, 2}}}, + {"##..#...#.......", 2, 3, {{0, 0}, {0, 1}, {1, 0}, {2, 0}}}, + {"#...###.........", 3, 2, {{0, 0}, {1, 0}, {1, 1}, {1, 2}}}, + {"###..#..........", 3, 2, {{0, 0}, {0, 1}, {0, 2}, {1, 1}}}, + {"#...##..#.......", 2, 3, {{0, 0}, {1, 0}, {1, 1}, {2, 0}}}, + {".#..###.........", 3, 2, {{0, 1}, {1, 0}, {1, 1}, {1, 2}}}, + {".#..##...#......", 2, 3, {{0, 1}, {1, 0}, {1, 1}, {2, 1}}}, + {"##..##..........", 2, 2, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}}, + {"##...##.........", 3, 2, {{0, 0}, {0, 1}, {1, 1}, {1, 2}}}, + {".#..##..#.......", 2, 3, {{0, 1}, {1, 0}, {1, 1}, {2, 0}}}, + {".##.##..........", 3, 2, {{0, 1}, {0, 2}, {1, 0}, {1, 1}}}, + {"#...##...#......", 2, 3, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}} +}; + +int main(int argc, char **argv) +{ + if (argc != 2) + error(); + else + init(argv[1]); + return (0); +} diff --git a/src/move_up_left.c b/src/move_up_left.c new file mode 100644 index 0000000..31ac699 --- /dev/null +++ b/src/move_up_left.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move_up_left.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/14 17:13:59 by gtertysh #+# #+# */ +/* Updated: 2016/12/14 17:14:07 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +/* +** Moves figures to top left corner. +** table contains strings each represent one figure +*/ + +void move_up_left(char **ttr) +{ + int i; + int j; + int k; + char t[17]; + + i = 0; + while (ttr[i]) + { + ft_memcpy(t, ttr[i], 17); + j = 0; + while (ttr[i][j] != '#') + j++; + ft_memmove(t, ttr[i] + j, 17 - j); + k = 0; + while ((t[3] == '#' && t[4] == '#') || (t[7] == '#' && t[8] == '#') || + (t[11] == '#' && t[12] == '#')) + { + ft_memmove((t + k + 1), (t + k), 17 - j--); + *(t + k++) = '.'; + } + k = 15; + while (j--) + *(t + k--) = '.'; + ft_memcpy(ttr[i++], t, 17); + } +} diff --git a/src/read_file.c b/src/read_file.c new file mode 100644 index 0000000..0835b50 --- /dev/null +++ b/src/read_file.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/14 16:55:43 by gtertysh #+# #+# */ +/* Updated: 2016/12/14 16:55:45 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +/* +** read from file. Returns string. +*/ + +char *read_file(char *path) +{ + int fd; + long readed; + long old_size; + char buf[BUF_S + 1]; + char *string; + + if ((fd = open(path, O_RDONLY)) == -1) + error(); + string = ft_memalloc(sizeof(char) * BUF_S); + while ((readed = read(fd, buf, BUF_S))) + { + buf[readed] = '\0'; + old_size = ft_strlen(string); + string = ft_realloc(string, old_size + readed, old_size); + ft_strcat(string, buf); + } + return (string); +} diff --git a/src/string_to_table.c b/src/string_to_table.c new file mode 100644 index 0000000..f820b93 --- /dev/null +++ b/src/string_to_table.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* string_to_table.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/14 16:56:56 by gtertysh #+# #+# */ +/* Updated: 2016/12/14 16:56:57 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +/* +** Split string to table by newline character. +** Each string contain one line of the file +*/ + +char **to_table(char **string) +{ + char **table; + + table = ft_strsplit(*string, '\n'); + free(*string); + *string = NULL; + return (table); +}