diff --git a/Makefile b/Makefile index 60c4fcb..bc1e13d 100644 --- a/Makefile +++ b/Makefile @@ -5,61 +5,48 @@ # +:+ # # By: aholster +#+ # # +#+ # -# Created: 2019/03/08 15:18:49 by aholster #+# #+# # +# Created: 2019/03/08 15:18:49 by aholster #+# #+# # # Updated: 2019/09/10 20:24:10 by lgutter ######## odam.nl # # # # **************************************************************************** # -FILES := main.c fillit.c solver.c shift_corner.c place_tet.c smallest_map.c\ -check_tet.c increment_offset.c remove_tet.c print_result.c read_tet.c\ +FILES := main.c fillit.c solver.c shift_corner.c place_tet.c\ +check_tet.c increment_offset.c remove_tet.c print_result.c parse_tet.c\ calc_empty.c HEADER := fillit.h -NORM := norminette $(FILES) $(HEADER) | grep -e "Error" -e "Warning" -B 1 +OBJS := $(FILES:%.c= %.o) -CC := gcc -g -Wall -Werror -Wextra +INCLUDES := -I ./libft + +CFLAGS := -Wall -Werror -Wextra NAME := fillit all: $(NAME) -assemble: - @${CC} $(FILES) -I ./libft -L ./libft -lft -o $(NAME) -$(NAME): - @make -C libft/ - @echo "\033[0;33mStarting assembly of $(NAME)...\033[0;00m" - @time make assemble +$(NAME): $(OBJS) + @$(MAKE) --no-print-directory -C libft/ + @${CC} $(OBJS) $(INCLUDES) -L ./libft -lft -o $(NAME) @echo "\033[0;32m$(NAME) successfully assembled!\033[0;00m\n" -clean: - @rm -rf *~ \#*\# .DS_Store - @make clean -C libft/ +%.o: %.c + @$(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@ + +lclean: + @rm -rf *~ \#*\# .DS_Store $(OBJS) @echo "\033[0;31mPests exterminated!\033[0;00m\n" -fclean: clean +clean: lclean + @$(MAKE) --no-print-directory clean -C libft/ + +fclean: lclean @rm -rf $(NAME) - @make fclean -C libft/ + @$(MAKE) --no-print-directory fclean -C libft/ @echo "\033[0;31mObituary of $(NAME): Deceased on $(shell date).\033[0;00m\n" -re: fclean all - -norm: - @echo "**+++=====*=====*=====*=====*{\033[0;31mOUT\033[0;00m}\ - *=====*=====*=====*=====+++**" - @$(NORM) || TRUE - @echo "**+++=====*=====*=====*=====*=====*=====*=====*=====*=====+++**" - -add: - @git add $(FILES) $(HEADER) Makefile author - git status +re: fclean + @$(MAKE) --no-print-directory all -push: -#ifdef MSG - @make norm - @git commit - git push -#else -# @echo "\033[0;31mUsage: make push MSG=\"Message here\"\033[0;00m" -#endif diff --git a/README.md b/README.md new file mode 100644 index 0000000..a4a970e --- /dev/null +++ b/README.md @@ -0,0 +1,98 @@ +# Fillit + +This project is part of the 42 curriculum as was available at Codam in 2019. +The goal of the project is to take a file containing tetrominoes (tetris pieces) +as input from a text file, and calculating plus displaying the smallest +possible square containing all tetrominoes, without using rotation, and respecting the original order as much as possible. + +Considering the quickly increasing complexity, and the chosen display format of +a letter for each tetromino, the maximum amount of tetrominoes is `26`. + +## Looking for a challenge +I built this project with my friend Arend, and when thinking about how we would +approach this project, we were talking to other students for ideas. One of them +jokingly made the ridiculous suggestion of doing everything with bitwise operations. +After initially laughing about this we thought "why not, it sounds like a challenge!" +and thus we made the decision that would cause us headaches and sleepless nights +for weeks... + +Since we only had a few months of programming experience, neither of us really +understood the purpose and power of bitwise operations, so our only plan was +"do ANYTHING we can using bitwise!". This led to some simply stupid ideas, but as +the project developed, our understand improved and so did our use of bitwise. + +The final product you find here has been refactored to make some stuff at least +remotely understandable, but since I did not change any of the actual logic, +the code is still nothing to be proud of... +It is however very fast thanks to some nice tricks we were able to do thanks to our use of bitwise. + +## Storing tetrominos +Every tetromino gets `32 bits` of storage. Why `32`? We need `24 bits` to store +a tetromino and its coordinates in a `16x16` map, but that is not an easily storable and processable amount. +The next available amount is the size of an `unsigned int`: `32 bits` or `4 bytes`, so we used that. + +How do store the tetromino inside these bits? Here is visualisation of the layout, per `byte`: +``` +| pos 1 & pos 2 | pos 3 & pos 4 | X coordinate | Y coordinate | +``` +### Storing the shape +The first `16 bits` are used to store the shape of the tetromino, using `4 bits` for the position of every block of the tetromino within a `4x4` grid, or a chain of `16` positions. (values `0` through `15`) +This method allows us to cast the value to normal variable if needed. + +### Storing the position +With a maximum of 26 tetrominoes, the largest amount of tetromino blocks we will need to handle is `26 * 4 = 104`, which would theoretically fit in an `11x11` square. But again, that does not result in an amount that is easily stored, so we allocate `1 byte` for the X coordinate, and `1 byte` for the Y coordinate, without any fancy bit manipulations. + +### Why store it this way? +Storing the tetrominoes like this allows us to be very efficient with memory, but also allows us to compare tetrominoes and their position with very basic math. For example, the `16 bits` that store the shape will always hold the same value when the shape is the same. We do have to make sure all tetrominoes are always positioned in the top left of the `4x4` grid, but this is a one-time operation. + +## Input parsing +A valid input file looks a little like this: +``` +.#.. +.#.. +.##. +.... + +.... +.##. +..#. +..#. + +#### +.... +.... +.... + +.... +.#.. +.### +.... + +``` +Every tetromino is provided in a 4x4 block, with `#` being part of the tetromino, +and `.` being blank spots. To seperate the tetromino blocks an extra `newline` is insterted. + +This blocks in this text file are then converted and stored in the format described above, shifted to the top left corner if needed, and checked for valid tetromino shapes. + +## A map to solve in +As said earlier, we need to be able to handle a map size of at least `11x11` to make sure we can solve any combination of tetrominoes. We want to use a single bit per position in the map, which means we need `11 * 11 = 121` bits. As usual, this is not an easy amount to store, especially since the largest datatype we have available is `64 bits`. So we decided to use `16 unsigned shorts`, which each consist of `16 bits`, giving us a `16x16` map to work with. More than we need, but much easier to work with. Even if we only need an `8x8` square, we always work within this `16x16` grid, so IF we need to try with a larger square size, we don't have to allocate anything. (in fact, we don't use malloc at all) + +## Solving + +### Map size +The first step in solving is determining the smallest possible square that could theoretically fit the amount of tetrominoes. This is essentially `√(n * 4)` rounded up to the nearest integer. So for `8` tetrominoes: +``` + 8 * 4 = 32 + √32 = 5.656854249 + round up = 6 + ``` +These values were pre-calculated since they are constant, but we take a few shortcuts based on simple tetromino shapes. Here we start using the fact that each unique shape has a numerical value. For example, a square has the value `325`. You can see this in `size_exceptions` in `solver.c:33`. + +### Calculating usable space +TBC + +### The almighty shortcut for duplicates +TBC + +### Recursive trial & error +TBC diff --git a/check_tet.c b/check_tet.c index 2625249..b57a4ea 100644 --- a/check_tet.c +++ b/check_tet.c @@ -12,11 +12,11 @@ #include "fillit.h" -int check_tet(unsigned int *tet) +int check_tet(t_tet_data *tet) { - unsigned int temp; - short check; - short count; + t_tet_data temp; + short check; + short count; count = 0; check = 12; diff --git a/fillit.c b/fillit.c index 1bbe7c7..b258976 100644 --- a/fillit.c +++ b/fillit.c @@ -5,38 +5,45 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/03/25 16:12:43 by lgutter #+# #+# */ +/* Created: 2019/03/25 16:12:43 by lgutter #+# #+# */ /* Updated: 2019/04/01 15:13:00 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ #include "fillit.h" +void ft_error(char *error) +{ + ft_putstr_fd("fillit: ", STDERR_FILENO); + ft_putendl_fd(error, STDERR_FILENO); + exit(-1); +} + void fillit(int fd) { - unsigned int tet[27]; - char buff[20]; - ssize_t ret; + t_tet_data tet[MAX_TETS + 1]; + char buff[20]; + ssize_t ret; - tet[26] = 0; + tet[TET_COUNT] = 0; ret = read(fd, buff, 20); - while (1) + while (true) { - tet[tet[26]] = 0; - if (read_tet(&tet[tet[26]], buff) != 1 || check_tet(&tet[tet[26]]) != 1) - ft_error(); - tet[26]++; + tet[tet[TET_COUNT]] = 0; + if (parse_tet(&tet[tet[TET_COUNT]], buff) != 1) + ft_error("parsing error!"); + tet[TET_COUNT]++; if (read(fd, buff, 1) == 1) { if (buff[0] != '\n') - ft_error(); + ft_error("tetromino definitions should be seperated by '\\n'"); ret = read(fd, buff, 20); - if (ret != 20 || tet[26] > 25) - ft_error(); + if (ret != 20 || tet[TET_COUNT] >= MAX_TETS) + ft_error(ret == 20 ? "too many tetrominos!" : "parsing error!"); } else break ; } close(fd); - map_control(&tet[0], tet[26]); + map_control(&tet[0], tet[TET_COUNT]); } diff --git a/fillit.h b/fillit.h index f123319..0bbefb9 100644 --- a/fillit.h +++ b/fillit.h @@ -5,7 +5,7 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/03/11 16:20:57 by aholster #+# #+# */ +/* Created: 2019/03/11 16:20:57 by aholster #+# #+# */ /* Updated: 2019/03/25 15:37:55 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -13,28 +13,50 @@ #ifndef FILLIT_H # define FILLIT_H -# define MASK2B 3 -# define MASK4B 15 -# define MASKBY 255 - # include "libft.h" # include # include # include -int read_tet(unsigned int *tet, char *buff); -void fillit(int fd); -void map_control(unsigned int *tet, short tetcount); -int place_tet(unsigned int *tet, unsigned short *map,\ -unsigned short di); -unsigned short smallest_map(short tetcount); -int check_tet(unsigned int *tet); -void shift_corner(unsigned int *tet); -void ft_error(void); -unsigned short calc_empty(unsigned short *map, unsigned short di); -int remove_tet(unsigned int *tet, unsigned short *map,\ +/* +** Masks for different parts of the int we store every tet and its info in. +*/ +# define MASK2BIT 3 +# define MASK4BIT 15 +# define MASKBYTE 255 +# define MASK2BYTE 65535 +# define TET_SHAPE 65535 + +/* +** Defines of number with a specific meaning +*/ +# define MAX_TETS 26 +# define TET_COUNT MAX_TETS +# define MAP_SIZE 16 +# define VERTICAL_BAR 1164 +# define HORIZONTAL_BAR 291 +# define SQUARE 325 + +/* +** Contains the shape of a tet in the first 2 bytes (4 bits for the position of +** each block of the tet), the x offset within the map in the third byte, and +** the y offset within the map in the fourth and last byte. +*/ +typedef unsigned int t_tet_data; + +int parse_tet(t_tet_data *tet, char *buff); +void fillit(int fd); +void map_control(t_tet_data *tet, short tetcount); +int place_tet(t_tet_data *tet, unsigned short *map,\ + unsigned short di); +unsigned short smallest_map(short tetcount); +int check_tet(t_tet_data *tet); +void shift_corner(t_tet_data *tet); +void ft_error(char *error); +unsigned short calc_empty(unsigned short *map, unsigned short di); +int remove_tet(t_tet_data *tet, unsigned short *map,\ unsigned short di); -int increment_offset(unsigned int *tet, unsigned short di); -void print_result(unsigned int *tet, unsigned short di); +int increment_offset(t_tet_data *tet, unsigned short di); +void print_result(t_tet_data *tet, unsigned short di); #endif diff --git a/increment_offset.c b/increment_offset.c index 642ab75..c28d78f 100644 --- a/increment_offset.c +++ b/increment_offset.c @@ -10,7 +10,9 @@ /* */ /* ************************************************************************** */ -int increment_offset(unsigned int *tet, unsigned short di) +#include "fillit.h" + +int increment_offset(t_tet_data *tet, unsigned short di) { unsigned char *offx; unsigned char *offy; diff --git a/libft/Makefile b/libft/Makefile index a4904de..696a388 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -3,86 +3,48 @@ # :::::::: # # Makefile :+: :+: # # +:+ # -# By: aholster +#+ # +# By: lgutter +#+ # # +#+ # -# Created: 2019/02/16 15:46:43 by aholster #+# #+# # -# Updated: 2019/04/03 18:28:58 by lgutter ######## odam.nl # +# Created: 2019/01/16 14:00:27 by lgutter #+# #+# # +# Updated: 2019/11/29 12:56:31 by lgutter ######## odam.nl # # # # **************************************************************************** # -BONOR := lstnew lstdelone lstdel lstadd lstiter lstmap +NAME := libft.a +include ./libftsources -ADVOR := memalloc memdel strnew strdel strclr striter striteri strmap\ - strmapi strequ strnequ strsub strjoin strtrim strsplit itoa putchar\ - putstr putendl putnbr putchar_fd putstr_fd putendl_fd putnbr_fd +CFLAGS := -Wall -Wextra -Werror -BASICOR := memset bzero memcpy memccpy memmove memchr memcmp\ - strlen strdup strcpy strncpy strcat strncat strlcat strchr strrchr\ - strstr strnstr strcmp strncmp\ - atoi isalpha isdigit isalnum isascii isprint toupper tolower +SRCS := $(LFTSOURCES:%= %.c) +OBJS := $(LFTSOURCES:%= %.o) -CUSOR:= nbrlen del lstaddend itoba iswhitespace isprime power putstrarr\ - putstrarr_fd strarrdel strarrtolst lsttostrarr lstleng min max constrain\ - segdefault factorial count_if range sqrt absneg swap lsttardest memjoin \ - memdup strarrnew textangle stralloc bitprint - - -FILEC = $(ADVOR:%=./ft_%.c) $(BONOR:%=./ft_%.c)\ - $(CUSOR:%=./ft_%.c) $(BASICOR:%=./ft_%.c) - -OBJ = $(ADVOR:%=./ft_%.o) $(BONOR:%=./ft_%.o)\ - $(CUSOR:%=./ft_%.o) $(BASICOR:%=./ft_%.o) - -HEAD = libft.h - -NAME = libft.a - -NORM = norminette $(FILEC) $(HEAD) | grep -e "Error" -e "Warning" -B 1 - -GCCC = ${CC} -c -CC = gcc -Wall -Werror -Wextra -AR = ar rcs +C_RESET = \033[0;00m +C_CLEAN = \033[38;5;159m +C_FCLEAN = \033[38;5;81m +C_OBJECTS = \033[38;5;75m +C_LIB = \033[38;5;33m +C_LINES = \033[38;5;250m all: $(NAME) -assemble: $(OBJ) - @$(AR) $(NAME) $(OBJ) +$(NAME): $(OBJS) + @ar rc $@ $^ @ranlib $(NAME) - -$(NAME): - @echo "\033[0;33mStarting assembly of libft.a...\033[0;00m" - @time make assemble - @echo "\033[0;32m$(NAME) successfully assembled!\033[0;00m\n" + @echo "$(C_LIB)Libft.a has been compiled$(C_RESET)" %.o: %.c - @$(GCCC) -o $@ $< + @$(CC) $(CFLAGS) -c $^ -o $@ clean: - @rm -rf $(OBJ) - @find "./" -type f \( -name '*~' -o -name '\#*\#' -o -name '.DS_Store' \)\ - -exec rm -rf {} \; + @rm -rf $(OBJS) + @echo "$(C_CLEAN)Libft object files removed$(C_RESET)" fclean: clean @rm -rf $(NAME) - @echo "\033[0;31mObituary of $(NAME): Deceased on $(shell date).\033[0;00m\n" - -re: fclean all - -norm: - @echo "**+++=====*=====*=====*=====*{\033[0;31mOUT\033[0;00m}\ - *=====*=====*=====*=====+++**" - @$(NORM) || TRUE - @echo "**+++=====*=====*=====*=====*=====*=====*===*=====*=====+++**" + @echo "$(C_FCLEAN)Libft.a removed$(C_RESET)" + @echo "$(C_LINES)- - - - - - - - - -$(C_RESET)" -add: - @git add $(FILEC) $(HEAD) Makefile author - git status +re: fclean + $(MAKE) all -push: norm -ifdef MSG - @git commit -m "$(MSG)" - git push -else - @git commit - git push -endif \ No newline at end of file +.PHONY: all objects clean fclean re diff --git a/libft/author b/libft/author index 91321f4..e2abfbf 100644 --- a/libft/author +++ b/libft/author @@ -1 +1 @@ -aholster +lgutter diff --git a/libft/ft_absneg.c b/libft/ft_absneg.c index a94d551..14eb0f2 100644 --- a/libft/ft_absneg.c +++ b/libft/ft_absneg.c @@ -3,16 +3,18 @@ /* :::::::: */ /* ft_absneg.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/31 22:14:45 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:01:54 by aholster ######## odam.nl */ +/* Created: 2019/01/31 19:35:02 by lgutter #+# #+# */ +/* Updated: 2019/01/31 19:35:03 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -long long ft_absneg(long long num) +long ft_absneg(long integer) { - if (num < 0) - return (num); - return (-(num)); + if (integer > 0) + { + return (integer * -1); + } + return (integer); } diff --git a/libft/ft_min.c b/libft/ft_abspos.c similarity index 64% rename from libft/ft_min.c rename to libft/ft_abspos.c index 636e2ba..18681e6 100644 --- a/libft/ft_min.c +++ b/libft/ft_abspos.c @@ -1,18 +1,20 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_min.c :+: :+: */ +/* ft_abspos.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/31 17:28:36 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:14:02 by aholster ######## odam.nl */ +/* Created: 2019/02/03 21:24:41 by lgutter #+# #+# */ +/* Updated: 2019/02/03 21:24:42 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -int ft_min(int val1, int val2) +long ft_abspos(long integer) { - if (val1 < val2) - return (val1); - return (val2); + if (integer < 0) + { + return (integer * -1); + } + return (integer); } diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c index 231cbc4..0fd584c 100644 --- a/libft/ft_atoi.c +++ b/libft/ft_atoi.c @@ -3,75 +3,64 @@ /* :::::::: */ /* ft_atoi.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/14 16:31:17 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:29:47 by aholster ######## odam.nl */ +/* Created: 2019/01/31 17:05:06 by lgutter #+# #+# */ +/* Updated: 2019/01/31 17:05:07 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -static int ft_isstrlower(const char *str, const char *str2, size_t n) +static unsigned long ft_exceptions(unsigned long integer, const char *string) { size_t index; index = 0; - while (index < n && str[index] != '\0') + if (*string == '+') { - if (!(str[index] <= str2[index])) + if (ft_isdigit(string[20]) == 1 && ft_isdigit(string[21]) == 1) return (0); - index++; + if (integer > 9223372036854775807) + return (0); + return (integer); } + if (*string == '-') + string++; + while (ft_isdigit(string[index]) != 0) + index++; + if (index > 19) + return (0); + if (integer > 9223372036854775807) + return (0); return (1); } -static int ft_exceptional(const char *str, const char sign) +int ft_atoi(const char *string) { - size_t index; + size_t index; + unsigned long result; index = 0; - while (ft_isdigit(str[index]) == 1 && index < 19) - { + result = 0; + while (ft_iswhitespace(*string) == 1) + string++; + if (string[index] == '+' || string[index] == '-') index++; - } - if (index == 19 && ft_isdigit(str[index]) == 1) - { - if (sign == '-') - return (0); - return (-1); - } - else if (index == 19) + while (ft_isdigit(string[index]) != 0) { - if (sign == '-' && ft_isstrlower(str, "9223372036854775808", 19) == 0) - return (0); - if (sign != '-' && ft_isstrlower(str, "9223372036854775807", 19) == 0) - return (-1); - } - return (42); -} - -int ft_atoi(const char *str) -{ - size_t i; - long long n; - - i = 0; - n = 0; - while (ft_iswhitespace(*str) == 1) - str++; - if (str[i] == '+' || str[i] == '-') - i++; - if (ft_exceptional(&str[i], str[0]) != 42) - return (ft_exceptional(&str[i], str[0])); - while (ft_isdigit(str[i]) == 1) - { - n = (str[i] - '0') + n; - if (ft_isdigit(str[i + 1]) == 1) - n = (n * 10); - i++; + result = result * 10; + result = result + (string[index] - 48); + if (index == 19) + { + if (ft_exceptions(result, string) == 0 && string[0] != '-') + return (-1); + if (ft_exceptions(result, string) == 0) + return (0); + } + index++; } - if (str[0] == '-') - n = -(n); - return ((int)n); + if (string[0] == '-') + return ((int)(result * -1)); + return ((int)result); } diff --git a/libft/ft_atoll.c b/libft/ft_atoll.c new file mode 100644 index 0000000..f3c95ac --- /dev/null +++ b/libft/ft_atoll.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_atoll.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/01/31 17:05:06 by lgutter #+# #+# */ +/* Updated: 2019/01/31 17:05:07 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static unsigned long long ft_exceptions(unsigned long long integer, + const char *string) +{ + size_t index; + + index = 0; + if (*string == '+') + { + if (ft_isdigit(string[20]) == 1 && ft_isdigit(string[21]) == 1) + return (0); + if (integer > 9223372036854775807) + return (0); + return (integer); + } + if (*string == '-') + string++; + while (ft_isdigit(string[index]) != 0) + index++; + if (index > 19) + return (0); + if (integer > 9223372036854775807) + return (0); + return (1); +} + +long long ft_atoll(const char *string) +{ + size_t index; + unsigned long long result; + + index = 0; + result = 0; + while (ft_iswhitespace(*string) == 1) + string++; + if (string[index] == '+' || string[index] == '-') + index++; + while (ft_isdigit(string[index]) != 0) + { + result = result * 10; + result = result + (string[index] - 48); + if (index == 19) + { + if (ft_exceptions(result, string) == 0 && string[0] != '-') + return (-1); + if (ft_exceptions(result, string) == 0) + return (0); + } + index++; + } + if (string[0] == '-') + return ((long long)(result * -1)); + return ((long long)result); +} diff --git a/libft/ft_bitprint.c b/libft/ft_bitprint.c index 79a6077..3135fea 100644 --- a/libft/ft_bitprint.c +++ b/libft/ft_bitprint.c @@ -5,7 +5,7 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/03/27 15:51:49 by aholster #+# #+# */ +/* Created: 2019/03/27 15:51:49 by aholster #+# #+# */ /* Updated: 2019/03/27 18:39:13 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c index 36a3263..80a5369 100644 --- a/libft/ft_bzero.c +++ b/libft/ft_bzero.c @@ -3,25 +3,16 @@ /* :::::::: */ /* ft_bzero.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 13:33:03 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:26:02 by aholster ######## odam.nl */ +/* Created: 2019/01/25 17:49:35 by lgutter #+# #+# */ +/* Updated: 2019/01/25 17:49:44 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_bzero(void *s, size_t n) +void ft_bzero(void *input, size_t len) { - char *str; - size_t index; - - index = 0; - str = s; - while (index < n) - { - str[index] = '\0'; - index++; - } + ft_memset(input, 0, len); } diff --git a/libft/ft_constrain.c b/libft/ft_constrain.c deleted file mode 100644 index 08296b5..0000000 --- a/libft/ft_constrain.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_constrain.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/31 17:30:59 by aholster #+# #+# */ -/* Updated: 2019/02/02 17:14:24 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -int ft_constrain(int in, int min, int max) -{ - if (min > max) - { - ft_swap(&min, &max); - } - if (in >= min && in <= max) - return (in); - else if (in < min) - return (min); - return (max); -} diff --git a/libft/ft_count_if.c b/libft/ft_count_if.c deleted file mode 100644 index 1b86774..0000000 --- a/libft/ft_count_if.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_count_if.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/31 21:31:16 by aholster #+# #+# */ -/* Updated: 2019/02/01 20:56:26 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -int ft_count_if(char **tab, int (*f)(char*)) -{ - int i; - int c; - - i = 0; - c = 0; - while (tab[i] != '\0') - { - if (((*f)(tab[i])) == 1) - c++; - i++; - } - return (c); -} diff --git a/libft/ft_factorial.c b/libft/ft_factorial.c index f4acbf4..421a306 100644 --- a/libft/ft_factorial.c +++ b/libft/ft_factorial.c @@ -5,7 +5,7 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/01/31 21:26:32 by aholster #+# #+# */ +/* Created: 2019/01/31 21:26:32 by aholster #+# #+# */ /* Updated: 2019/02/01 20:56:43 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/libft/ft_free_str_array.c b/libft/ft_free_str_array.c new file mode 100644 index 0000000..8a89f86 --- /dev/null +++ b/libft/ft_free_str_array.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_free_str_array.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2020/01/22 14:12:44 by lgutter #+# #+# */ +/* Updated: 2020/02/07 11:40:00 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_free_str_array(char **array) +{ + size_t index; + + index = 0; + if (array != NULL) + { + while (array[index] != NULL) + { + free(array[index]); + array[index] = NULL; + index++; + } + free(array); + } + return (index); +} diff --git a/libft/ft_getopt.c b/libft/ft_getopt.c new file mode 100644 index 0000000..2885ad1 --- /dev/null +++ b/libft/ft_getopt.c @@ -0,0 +1,132 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_getopt.c :+: :+: */ +/* +:+ */ +/* By: nloomans +#+ */ +/* +#+ */ +/* Created: 2020/02/12 18:03:23 by nloomans #+# #+# */ +/* Updated: 2020/02/12 18:03:24 by nloomans ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +/* +** ft_getopt parses the options for you. Example usage: +** +** int main(int argc, char **argv) +** { +** struct s_ft_getopt opt; +** bool opt_a; +** char *opt_b_arg; +** bool opt_c; +** +** opt = FT_GETOPT_DEFAULT; +** opt_a = false; +** opt_b_arg = NULL; +** opt_c = false; +** while (ft_getopt(&opt, argc, argv, "ab:c")) +** { +** if (opt.opt == 'a') +** opt_a = true; +** else if (opt.opt == 'b') +** opt_b_arg = opt.arg; +** else if (opt.opt == 'c') +** opt_c = true; +** } +** if (opt.illegal) +** return (1); +** // use options +** return (0); +** } +*/ + +static void print_error(char *argv0, char *msg, char opt) +{ + write(STDERR_FILENO, argv0, ft_strlen(argv0)); + write(STDERR_FILENO, msg, ft_strlen(msg)); + write(STDERR_FILENO, &opt, 1); + write(STDERR_FILENO, "\n", sizeof("\n")); +} + +static int process_arg(struct s_ft_getopt *opt, char **argv) +{ + if (argv[opt->index][opt->group_index + 1]) + opt->arg = argv[opt->index] + opt->group_index + 1; + else if (argv[opt->index + 1]) + { + opt->arg = argv[opt->index + 1]; + opt->index++; + } + else + return (-1); + opt->group_index = 1; + opt->index++; + return (0); +} + +static int process(struct s_ft_getopt *opt, + char **argv, char matched_opt, bool has_arg) +{ + opt->opt = matched_opt; + if (has_arg) + { + if (process_arg(opt, argv) == -1) + { + opt->illegal = true; + print_error(argv[0], ": option requires an argument -- ", + argv[opt->index][opt->group_index]); + return (-1); + } + } + else + { + if (argv[opt->index][opt->group_index + 1]) + opt->group_index++; + else + { + opt->group_index = 1; + opt->index++; + } + } + return (0); +} + +static bool should_stop(struct s_ft_getopt opt, int argc, char **argv) +{ + return (opt.index >= argc || argv[opt.index][0] != '-' || + ft_strcmp(argv[opt.index], "-") == 0); +} + +bool ft_getopt(struct s_ft_getopt *opt, int argc, char **argv, + const char *optstring) +{ + size_t i; + + if (should_stop(*opt, argc, argv)) + return (false); + if (argv[opt->index][1] == '-') + { + opt->index++; + return (false); + } + i = 0; + while (i < ft_strlen(optstring)) + { + if (ft_isalnum(optstring[i]) && + optstring[i] == argv[opt->index][opt->group_index]) + { + if (process(opt, argv, optstring[i], optstring[i + 1] == ':') == -1) + return (false); + return (true); + } + i++; + } + opt->illegal = true; + print_error(argv[0], ": illegal option -- ", + argv[opt->index][opt->group_index]); + return (false); +} diff --git a/libft/ft_nbrlen.c b/libft/ft_intlen.c similarity index 63% rename from libft/ft_nbrlen.c rename to libft/ft_intlen.c index 2297411..f071408 100644 --- a/libft/ft_nbrlen.c +++ b/libft/ft_intlen.c @@ -1,28 +1,30 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_nbrlen.c :+: :+: */ +/* ft_intlen.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/14 13:36:17 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:14:08 by aholster ######## odam.nl */ +/* Created: 2020/05/28 11:52:40 by lgutter #+# #+# */ +/* Updated: 2020/05/28 11:52:40 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -unsigned int ft_nbrlen(long long n, unsigned int base) +#include "libft.h" + +int ft_intlen(int n) { - unsigned int length; + int i; - length = 0; + i = 0; if (n == 0) return (1); if (n < 0) - length++; + i++; while (n != 0) { - n = n / base; - length++; + n /= 10; + i++; } - return (length); + return (i); } diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c index 2cf7410..eba28c4 100644 --- a/libft/ft_isalnum.c +++ b/libft/ft_isalnum.c @@ -3,18 +3,26 @@ /* :::::::: */ /* ft_isalnum.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/14 16:37:55 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:27:02 by aholster ######## odam.nl */ +/* Created: 2019/01/31 22:06:46 by lgutter #+# #+# */ +/* Updated: 2019/01/31 22:06:47 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "libft.h" - -int ft_isalnum(int c) +int ft_isalnum(int character) { - if (ft_isalpha(c) == 1 || ft_isdigit(c) == 1) + if (character <= 'Z' && character >= 'A') + { + return (1); + } + else if (character <= 'z' && character >= 'a') + { + return (1); + } + else if (character <= '9' && character >= '0') + { return (1); + } return (0); } diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c index 70216fc..c961b90 100644 --- a/libft/ft_isalpha.c +++ b/libft/ft_isalpha.c @@ -3,16 +3,22 @@ /* :::::::: */ /* ft_isalpha.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 19:47:08 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:02:48 by aholster ######## odam.nl */ +/* Created: 2019/01/31 21:56:26 by lgutter #+# #+# */ +/* Updated: 2019/01/31 21:56:26 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -int ft_isalpha(int c) +int ft_isalpha(int character) { - if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + if (character <= 'Z' && character >= 'A') + { return (1); + } + else if (character <= 'z' && character >= 'a') + { + return (1); + } return (0); } diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c index 92a40c8..81b7996 100644 --- a/libft/ft_isascii.c +++ b/libft/ft_isascii.c @@ -3,16 +3,18 @@ /* :::::::: */ /* ft_isascii.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 19:45:26 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:02:48 by aholster ######## odam.nl */ +/* Created: 2019/01/31 22:12:46 by lgutter #+# #+# */ +/* Updated: 2019/01/31 22:12:47 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -int ft_isascii(int c) +int ft_isascii(int character) { - if (c >= 0 && c <= 127) + if (character <= '\x7f' && character >= '\0') + { return (1); + } return (0); } diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c index ca3572e..b0600b6 100644 --- a/libft/ft_isdigit.c +++ b/libft/ft_isdigit.c @@ -3,16 +3,18 @@ /* :::::::: */ /* ft_isdigit.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 19:54:02 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:02:48 by aholster ######## odam.nl */ +/* Created: 2019/01/31 17:05:16 by lgutter #+# #+# */ +/* Updated: 2019/01/31 17:05:17 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -int ft_isdigit(int c) +int ft_isdigit(int character) { - if (c >= '0' && c <= '9') + if (character <= '9' && character >= '0') + { return (1); + } return (0); } diff --git a/libft/ft_isprime.c b/libft/ft_isprime.c deleted file mode 100644 index 7bb743b..0000000 --- a/libft/ft_isprime.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_isprime.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/31 19:14:10 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:02:48 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -unsigned int ft_isprime(unsigned int num) -{ - unsigned int counter; - - counter = 2; - while (counter <= num / counter) - { - if (num % counter == 0) - return (0); - counter++; - } - return (1); -} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c index fd8a034..000993d 100644 --- a/libft/ft_isprint.c +++ b/libft/ft_isprint.c @@ -3,16 +3,18 @@ /* :::::::: */ /* ft_isprint.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 19:42:14 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:02:48 by aholster ######## odam.nl */ +/* Created: 2019/01/31 22:25:07 by lgutter #+# #+# */ +/* Updated: 2019/01/31 22:25:08 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -int ft_isprint(int c) +int ft_isprint(int character) { - if (c >= 32 && c <= 126) + if (character <= '~' && character >= ' ') + { return (1); + } return (0); } diff --git a/libft/ft_isspace.c b/libft/ft_isspace.c new file mode 100644 index 0000000..9459dde --- /dev/null +++ b/libft/ft_isspace.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_isspace.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2020/05/28 11:48:01 by lgutter #+# #+# */ +/* Updated: 2020/05/28 11:48:01 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(int character) +{ + if (character == '\t' || character == '\n' || character == '\v' + || character == '\f' || character == '\r' || character == ' ') + { + return (1); + } + return (0); +} diff --git a/libft/ft_iswhitespace.c b/libft/ft_iswhitespace.c index 1261af8..e6bba98 100644 --- a/libft/ft_iswhitespace.c +++ b/libft/ft_iswhitespace.c @@ -3,17 +3,24 @@ /* :::::::: */ /* ft_iswhitespace.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/30 20:00:49 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:02:48 by aholster ######## odam.nl */ +/* Created: 2019/01/19 17:58:22 by lgutter #+# #+# */ +/* Updated: 2019/01/19 17:58:26 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -int ft_iswhitespace(int c) +#include "libft.h" + +int ft_iswhitespace(char character) { - if (c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v'\ - || c == ' ') + if (character == '\n' || character == '\t' || character == ' ' || + character == '\v' || character == '\f' || character == '\r') + { return (1); - return (0); + } + else + { + return (0); + } } diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c index aba665f..0ea59b8 100644 --- a/libft/ft_itoa.c +++ b/libft/ft_itoa.c @@ -3,36 +3,40 @@ /* :::::::: */ /* ft_itoa.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 16:43:03 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:03:03 by aholster ######## odam.nl */ +/* Created: 2019/01/23 12:11:07 by lgutter #+# #+# */ +/* Updated: 2019/01/23 12:11:23 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_itoa(int n) +char *ft_itoa(int integer) { - char *str; - unsigned int index; + size_t len; + char *ret; - if (n == -2147483648) + ret = NULL; + if (integer == -2147483648) return (ft_strdup("-2147483648")); - index = ft_nbrlen((long long)n, 10); - str = ft_strnew(index); - if (str == NULL) + len = ft_nbrlenbase((long)integer, 10); + ret = (char *)malloc(sizeof(char) * (len + 1)); + if (ret == NULL) return (NULL); - if (n < 0) + if (integer < 0) { - str[0] = '-'; - n = -(n); + ret[0] = '-'; + integer *= -1; } - while (index > 1 || (index > 0 && str[0] != '-')) + ret[len] = '\0'; + len--; + while (integer / 10 != 0) { - index--; - str[index] = ((n % 10) + '0'); - n = n / 10; + ret[len] = ((integer % 10) + 48); + integer /= 10; + len--; } - return (str); + ret[len] = (integer + 48); + return (ret); } diff --git a/libft/ft_itoa_base.c b/libft/ft_itoa_base.c new file mode 100644 index 0000000..34748fd --- /dev/null +++ b/libft/ft_itoa_base.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_itoa_base.c :+: :+: */ +/* +:+ */ +/* By: ivan-tey +#+ */ +/* +#+ */ +/* Created: 2019/09/25 15:44:36 by ivan-tey #+# #+# */ +/* Updated: 2019/11/25 16:57:34 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *ft_convert(signed long long n,\ + const unsigned int base,\ + char *str,\ + size_t i) +{ + int res; + char *bstr; + + bstr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + while (base == 1 && ((ssize_t)(i * -1) >= n)) + { + str[i] = '1'; + i++; + if ((ssize_t)(i * -1) == n) + return (str); + } + if (n != 0) + { + res = n % base; + res = res < 0 ? res * -1 : res; + str[i] = bstr[res]; + i++; + ft_convert(n / base, base, str, i); + } + return (str); +} + +char *ft_itoa_base(signed long long nb, const unsigned int base) +{ + int nb_len; + char *str; + + if (base == 0) + { + return (NULL); + } + if (nb == 0) + return (ft_strdup("0")); + nb_len = ft_nbrlenbase(nb, base); + str = (char *)ft_strnew(sizeof(char) * nb_len + 1); + if (str != NULL) + { + if (nb < 0) + { + str[0] = '-'; + } + else + nb = nb * -1; + str = ft_convert(nb, base, str, (str[0] == '-' ? 1 : 0)); + str[nb_len] = '\0'; + str = ft_strrev(str, (str[0] == '-' ? 1 : 0)); + } + return (str); +} diff --git a/libft/ft_itoba.c b/libft/ft_itoba.c deleted file mode 100644 index 9048431..0000000 --- a/libft/ft_itoba.c +++ /dev/null @@ -1,71 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_itoba.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/30 18:25:03 by aholster #+# #+# */ -/* Updated: 2019/01/30 19:59:42 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -static char *ft_base1(int n) -{ - char *str; - - str = malloc(sizeof(char) * n + 1); - if (str == NULL) - return (NULL); - str[n] = '\0'; - n--; - while (n >= 0) - { - str[n] = '1'; - n--; - } - return (str); -} - -static void ft_baseput(char *str, long long num, unsigned int base,\ -unsigned int index) -{ - char *legend; - - legend = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - while (index > 1 || (index > 0 && str[0] != '-')) - { - index--; - str[index] = legend[num % base]; - num = num / base; - } -} - -char *ft_itoba(int n, unsigned int base) -{ - char *str; - unsigned int index; - long long num; - - num = n; - if (base > 36 || base == 0) - { - ft_putendl("BASE NOT SUPPORTED"); - return (NULL); - } - if (base == 1) - return (ft_base1(n)); - index = ft_nbrlen(num, base); - str = ft_strnew(index); - if (str == NULL) - return (NULL); - if (n < 0) - { - str[0] = '-'; - num = -(num); - } - ft_baseput(str, num, base, index); - return (str); -} diff --git a/libft/ft_lstadd.c b/libft/ft_lstadd.c index 63eb856..b3a1f2b 100644 --- a/libft/ft_lstadd.c +++ b/libft/ft_lstadd.c @@ -3,17 +3,17 @@ /* :::::::: */ /* ft_lstadd.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/21 19:28:11 by aholster #+# #+# */ -/* Updated: 2019/02/22 16:51:16 by aholster ######## odam.nl */ +/* Created: 2019/02/02 21:36:29 by lgutter #+# #+# */ +/* Updated: 2019/02/02 21:36:30 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_lstadd(t_list **alst, t_list *new) +void ft_lstadd(t_list **start, t_list *new) { - new->next = *alst; - *alst = new; + new->next = *start; + *start = new; } diff --git a/libft/ft_lstaddback.c b/libft/ft_lstaddback.c new file mode 100644 index 0000000..b74d687 --- /dev/null +++ b/libft/ft_lstaddback.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstaddback.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/02/03 17:40:03 by lgutter #+# #+# */ +/* Updated: 2019/02/03 17:40:04 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstaddback(t_list **start, t_list *new) +{ + t_list *current; + + current = *start; + while (current->next != NULL) + { + current = current->next; + } + current->next = new; +} diff --git a/libft/ft_putstrarr.c b/libft/ft_lstaddnext.c similarity index 62% rename from libft/ft_putstrarr.c rename to libft/ft_lstaddnext.c index 855ea59..3b85857 100644 --- a/libft/ft_putstrarr.c +++ b/libft/ft_lstaddnext.c @@ -1,25 +1,23 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_putstrarr.c :+: :+: */ +/* ft_lstaddnext.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/31 20:51:13 by aholster #+# #+# */ -/* Updated: 2019/02/01 16:10:39 by aholster ######## odam.nl */ +/* Created: 2019/02/03 21:25:08 by lgutter #+# #+# */ +/* Updated: 2019/02/03 21:25:09 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putstrarr(char **strarr) +t_list *ft_lstaddnext(t_list *current, t_list *new) { - size_t index; - - index = 0; - while (strarr[index] != NULL) + while (current->next != NULL) { - ft_putendl(strarr[index]); - index++; + current = current->next; } + current->next = new; + return (new); } diff --git a/libft/ft_lstdel.c b/libft/ft_lstdel.c index 3628c48..38e5e6f 100644 --- a/libft/ft_lstdel.c +++ b/libft/ft_lstdel.c @@ -3,32 +3,35 @@ /* :::::::: */ /* ft_lstdel.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/22 17:40:24 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:06:09 by aholster ######## odam.nl */ +/* Created: 2019/02/02 21:36:33 by lgutter #+# #+# */ +/* Updated: 2019/02/02 21:36:34 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) +void ft_lstdel(t_list **start, void (*del)(void *, size_t)) { - t_list *holder; - t_list *current; + t_list *current; + t_list *temp; - if (alst == NULL || *alst == NULL || del == NULL) - return ; - current = *alst; + current = *start; while (current != NULL) { - if (current->next != NULL) - holder = current->next; - else - holder = NULL; - ft_lstdelone(¤t, del); + temp = current->next; + (*del)(current->content, current->content_size); free(current); - current = holder; + current = NULL; + if (temp != NULL) + { + current = temp; + } + else + { + *start = NULL; + return ; + } } - *alst = NULL; } diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c index df6621a..7acc96d 100644 --- a/libft/ft_lstdelone.c +++ b/libft/ft_lstdelone.c @@ -1,20 +1,23 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_lstdelone.c :+: :+: */ +/* ft_delone.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/22 16:41:14 by aholster #+# #+# */ -/* Updated: 2019/02/03 12:55:31 by aholster ######## odam.nl */ +/* Created: 2019/02/02 14:31:58 by lgutter #+# #+# */ +/* Updated: 2019/02/02 14:31:59 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_lstdelone(t_list **alst, void (*del)(void*, size_t)) +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) { - del((*alst)->content, (*alst)->content_size); + if (del != NULL) + { + (*del)((*alst)->content, (*alst)->content_size); + } free(*alst); *alst = NULL; } diff --git a/libft/ft_lstiter.c b/libft/ft_lstiter.c index 6e70e3f..8cfa06f 100644 --- a/libft/ft_lstiter.c +++ b/libft/ft_lstiter.c @@ -3,20 +3,21 @@ /* :::::::: */ /* ft_lstiter.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/24 13:04:26 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:28:32 by aholster ######## odam.nl */ +/* Created: 2019/02/03 17:12:32 by lgutter #+# #+# */ +/* Updated: 2019/02/03 17:12:33 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +void ft_lstiter(t_list *list, void (*f)(t_list *elem)) { - while (lst != NULL) + while (list->next != NULL) { - f(lst); - lst = lst->next; + (f)(list); + list = list->next; } + (f)(list); } diff --git a/libft/ft_putstrarr_fd.c b/libft/ft_lstlen.c similarity index 62% rename from libft/ft_putstrarr_fd.c rename to libft/ft_lstlen.c index 6ff782d..e4b99ca 100644 --- a/libft/ft_putstrarr_fd.c +++ b/libft/ft_lstlen.c @@ -1,25 +1,28 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_putstrarr_fd.c :+: :+: */ +/* ft_lstlen.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/31 21:00:21 by aholster #+# #+# */ -/* Updated: 2019/02/01 16:03:05 by aholster ######## odam.nl */ +/* Created: 2019/02/03 17:55:46 by lgutter #+# #+# */ +/* Updated: 2019/02/03 17:55:47 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putstrarr_fd(char **strarr, int fd) +size_t ft_lstlen(t_list **start) { + t_list *current; size_t index; - index = 0; - while (strarr[index] != NULL) + index = 1; + current = *start; + while (current->next != NULL) { - ft_putendl_fd(strarr[index], fd); + current = current->next; index++; } + return (index); } diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c index 1ada19b..b44b0b6 100644 --- a/libft/ft_lstmap.c +++ b/libft/ft_lstmap.c @@ -3,29 +3,26 @@ /* :::::::: */ /* ft_lstmap.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/24 13:04:13 by aholster #+# #+# */ -/* Updated: 2019/02/07 20:26:47 by aholster ######## odam.nl */ +/* Created: 2019/02/03 17:33:24 by lgutter #+# #+# */ +/* Updated: 2019/02/03 17:33:26 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +t_list *ft_lstmap(t_list *list, t_list *(*f)(t_list *elem)) { - t_list *head; t_list *new; - head = f(lst); - lst = lst->next; - while (lst != NULL) + new = (f)(list); + list = list->next; + while (list->next != NULL) { - new = f(lst); - if (new == NULL) - return (NULL); - ft_lstaddend(&head, new); - lst = lst->next; + ft_lstaddback(&new, (f)(list)); + list = list->next; } - return (head); + ft_lstaddback(&new, (f)(list)); + return (new); } diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c index d4d809b..ea63aa8 100644 --- a/libft/ft_lstnew.c +++ b/libft/ft_lstnew.c @@ -3,10 +3,10 @@ /* :::::::: */ /* ft_lstnew.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/21 19:28:30 by aholster #+# #+# */ -/* Updated: 2019/02/01 20:54:29 by aholster ######## odam.nl */ +/* Created: 2019/02/02 12:25:13 by lgutter #+# #+# */ +/* Updated: 2019/02/02 12:25:14 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,27 +14,28 @@ t_list *ft_lstnew(void const *content, size_t content_size) { - t_list *head; + t_list *new; - head = (t_list *)malloc(sizeof(t_list)); - if (head == NULL) + new = (t_list *)malloc(sizeof(t_list) * 1); + if (new == NULL) + { return (NULL); - if (!(content)) + } + if (content == NULL) { - head->content = NULL; - head->content_size = 0; + new->content = NULL; + new->content_size = 0; + new->next = NULL; + return (new); } - else + new->content = ft_memdup(content, content_size); + if (new->content == NULL) { - head->content = ft_memalloc(content_size); - if (head->content == NULL) - { - free(head); - return (NULL); - } - ft_memcpy(head->content, content, content_size); - head->content_size = content_size; + free(new); + new = NULL; + return (NULL); } - head->next = NULL; - return (head); + new->content_size = content_size; + new->next = NULL; + return (new); } diff --git a/libft/ft_lsttardest.c b/libft/ft_lsttardest.c deleted file mode 100644 index fdac083..0000000 --- a/libft/ft_lsttardest.c +++ /dev/null @@ -1,40 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_lsttardest.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/02/07 16:09:18 by aholster #+# #+# */ -/* Updated: 2019/04/03 17:55:58 by lgutter ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_lsttardest(t_list **lst, t_list **target,\ - void (*del)(void *, size_t)) -{ - t_list *trail; - t_list *hunter; - - if (target == NULL || *target == NULL || del == NULL) - return ; - hunter = *lst; - if (hunter == *target) - { - *lst = hunter->next; - ft_lstdelone(&hunter, del); - } - while (hunter != NULL) - { - trail = hunter; - hunter = hunter->next; - if (hunter == *target) - { - trail->next = hunter->next; - ft_lstdelone(target, del); - return ; - } - } -} diff --git a/libft/ft_lsttostrarr.c b/libft/ft_lsttostrarr.c deleted file mode 100644 index fa43947..0000000 --- a/libft/ft_lsttostrarr.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_lsttostrarr.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/31 21:26:14 by aholster #+# #+# */ -/* Updated: 2019/01/31 23:11:58 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char **ft_lsttostrarr(t_list *lst) -{ - char **ret; - size_t index; - - index = 0; - ret = (char **)malloc(sizeof(char *) * ft_lstleng(lst) + 1); - if (ret == NULL) - return (NULL); - while (lst != NULL) - { - ret[index] = ft_strnew(lst->content_size); - if (ret[index] == NULL) - { - ft_strarrdel(&ret); - return (NULL); - } - ret[index] = ft_memcpy(ret[index], lst->content, lst->content_size); - index++; - lst = lst->next; - } - ret[index] = NULL; - return (ret); -} diff --git a/libft/ft_max.c b/libft/ft_max.c deleted file mode 100644 index 9d920d5..0000000 --- a/libft/ft_max.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_max.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/31 17:28:38 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:12:28 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -int ft_max(int val1, int val2) -{ - if (val1 > val2) - return (val1); - return (val2); -} diff --git a/libft/ft_memalloc.c b/libft/ft_memalloc.c index c005957..df63f59 100644 --- a/libft/ft_memalloc.c +++ b/libft/ft_memalloc.c @@ -3,10 +3,10 @@ /* :::::::: */ /* ft_memalloc.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 14:56:57 by aholster #+# #+# */ -/* Updated: 2019/01/29 18:32:34 by aholster ######## odam.nl */ +/* Created: 2019/01/10 17:29:07 by lgutter #+# #+# */ +/* Updated: 2019/01/19 14:28:13 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,11 +14,15 @@ void *ft_memalloc(size_t size) { - void *output; + unsigned char *string; - output = (void *)malloc(sizeof(void *) * size); - if (output == NULL) + string = (unsigned char *)malloc(size); + if (!string) return (NULL); - ft_memset(output, '\0', size); - return (output); + while (size > 0) + { + size--; + string[size] = '\0'; + } + return ((void *)string); } diff --git a/libft/ft_memccpy.c b/libft/ft_memccpy.c index 5a28b21..15bc38a 100644 --- a/libft/ft_memccpy.c +++ b/libft/ft_memccpy.c @@ -3,35 +3,37 @@ /* :::::::: */ /* ft_memccpy.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 16:33:42 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:26:21 by aholster ######## odam.nl */ +/* Created: 2019/01/25 21:31:21 by lgutter #+# #+# */ +/* Updated: 2020/02/04 17:37:31 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memccpy(void *dst, void const *src, int c, size_t n) +void *ft_memccpy(void *dst, const void *src, int delim, size_t len) { - unsigned char *output; - unsigned char const *input; size_t index; + unsigned char *output; + unsigned const char *input; - index = 0; - output = dst; input = src; - while (index < n && input[index] != (unsigned char)c) + output = dst; + index = 0; + while (index < len && input[index] != (unsigned char)delim) { output[index] = input[index]; index++; } - if (index == n) + if (index == len) + { return (NULL); - if (input[index] == (unsigned char)c) + } + else { output[index] = input[index]; - return ((void *)&output[index + 1]); + index++; + return ((void *)&output[index]); } - return (NULL); } diff --git a/libft/ft_del.c b/libft/ft_memcdup.c similarity index 63% rename from libft/ft_del.c rename to libft/ft_memcdup.c index f4e2a07..b34d378 100644 --- a/libft/ft_del.c +++ b/libft/ft_memcdup.c @@ -1,20 +1,18 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_del.c :+: :+: */ +/* ft_memcdup.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/24 13:11:45 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:28:41 by aholster ######## odam.nl */ +/* Created: 2019/02/14 12:22:13 by lgutter #+# #+# */ +/* Updated: 2019/02/14 12:22:13 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_del(void *data, size_t size) +void *ft_memcdup(const void *source, int delim, size_t size) { - free(data); - data = NULL; - size = 0; + return (ft_memdup(source, ft_strlenc((char *)source, delim, size))); } diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c index 7b94a22..68b752d 100644 --- a/libft/ft_memchr.c +++ b/libft/ft_memchr.c @@ -3,26 +3,30 @@ /* :::::::: */ /* ft_memchr.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 17:42:54 by aholster #+# #+# */ -/* Updated: 2019/02/16 17:59:13 by aholster ######## odam.nl */ +/* Created: 2019/01/29 18:22:49 by lgutter #+# #+# */ +/* Updated: 2020/01/08 10:38:23 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memchr(void const *s, int c, size_t n) +void *ft_memchr(const void *source, int character, size_t len) { - unsigned char const *str; size_t index; + const unsigned char *input; - str = (unsigned char *)s; + input = source; index = 0; - while (index < n) + if (input == NULL) + return (NULL); + while (index < len) { - if (str[index] == (unsigned char)c) - return ((void *)&str[index]); + if (input[index] == (unsigned char)character) + { + return ((void *)&input[index]); + } index++; } return (NULL); diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c index 5e59a95..fd6d732 100644 --- a/libft/ft_memcmp.c +++ b/libft/ft_memcmp.c @@ -3,28 +3,30 @@ /* :::::::: */ /* ft_memcmp.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 18:57:47 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:26:35 by aholster ######## odam.nl */ +/* Created: 2019/01/29 20:41:10 by lgutter #+# #+# */ +/* Updated: 2019/01/29 20:41:11 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_memcmp(void const *s1, void const *s2, size_t n) +int ft_memcmp(const void *source1, const void *source2, size_t len) { - size_t index; - unsigned char *str1; - unsigned char *str2; + size_t index; + const unsigned char *first; + const unsigned char *second; index = 0; - str1 = (unsigned char *)s1; - str2 = (unsigned char *)s2; - while (index < n) + first = source1; + second = source2; + while (index < len) { - if (str1[index] != str2[index]) - return (str1[index] - str2[index]); + if (first[index] != second[index]) + { + return (first[index] - second[index]); + } index++; } return (0); diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c index 3df7f3d..cbd5e1f 100644 --- a/libft/ft_memcpy.c +++ b/libft/ft_memcpy.c @@ -3,27 +3,27 @@ /* :::::::: */ /* ft_memcpy.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 15:42:31 by aholster #+# #+# */ -/* Updated: 2019/02/16 17:59:13 by aholster ######## odam.nl */ +/* Created: 2019/01/25 19:00:57 by lgutter #+# #+# */ +/* Updated: 2019/01/25 19:01:02 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memcpy(void *dst, const void *src, size_t n) +void *ft_memcpy(void *dst, const void *src, size_t len) { size_t index; - unsigned char *input; - unsigned const char *output; + unsigned char *output; + unsigned const char *input; + output = dst; + input = src; index = 0; - input = (unsigned char *)dst; - output = (unsigned char const *)src; - while (index < n) + while (index < len) { - input[index] = output[index]; + output[index] = input[index]; index++; } return (dst); diff --git a/libft/ft_memdel.c b/libft/ft_memdel.c index 39bd3f0..c3a516b 100644 --- a/libft/ft_memdel.c +++ b/libft/ft_memdel.c @@ -3,17 +3,17 @@ /* :::::::: */ /* ft_memdel.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/16 19:47:11 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:27:09 by aholster ######## odam.nl */ +/* Created: 2019/01/13 16:43:15 by lgutter #+# #+# */ +/* Updated: 2019/01/13 16:43:17 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_memdel(void **ap) +void ft_memdel(void **pointer) { - free(*ap); - *ap = NULL; + free(*pointer); + *pointer = NULL; } diff --git a/libft/ft_memdelsize.c b/libft/ft_memdelsize.c new file mode 100644 index 0000000..4ce60cc --- /dev/null +++ b/libft/ft_memdelsize.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_memdelsize.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/02/02 14:34:16 by lgutter #+# #+# */ +/* Updated: 2019/02/02 14:34:18 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_memdelsize(void *pointer, size_t size) +{ + size_t index; + unsigned char *input; + + index = 0; + input = pointer; + while (index < size) + { + input[index] = 0; + index++; + } + free(pointer); + pointer = NULL; +} diff --git a/libft/ft_memdup.c b/libft/ft_memdup.c index 0c9bc90..3148fb9 100644 --- a/libft/ft_memdup.c +++ b/libft/ft_memdup.c @@ -3,26 +3,28 @@ /* :::::::: */ /* ft_memdup.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/02/18 16:02:34 by aholster #+# #+# */ -/* Updated: 2019/02/18 17:34:27 by aholster ######## odam.nl */ +/* Created: 2019/02/02 11:32:38 by lgutter #+# #+# */ +/* Updated: 2019/02/02 11:32:39 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memdup(void *src, size_t len) +void *ft_memdup(const void *source, size_t len) { - size_t index; - char *ret; - char *input; + size_t index; + unsigned char *ret; + const unsigned char *input; index = 0; - input = src; - ret = (char *)malloc(sizeof(char) * len); + input = (const unsigned char *)source; + ret = (void *)ft_memalloc(sizeof(unsigned char) * len); if (ret == NULL) + { return (NULL); + } while (index < len) { ret[index] = input[index]; diff --git a/libft/ft_power.c b/libft/ft_memexpand.c similarity index 55% rename from libft/ft_power.c rename to libft/ft_memexpand.c index c9cdb19..1f9fdca 100644 --- a/libft/ft_power.c +++ b/libft/ft_memexpand.c @@ -1,36 +1,31 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_power.c :+: :+: */ +/* ft_memexpand.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/31 20:20:36 by aholster #+# #+# */ -/* Updated: 2019/02/01 18:49:05 by aholster ######## odam.nl */ +/* Created: 2019/02/15 13:29:30 by lgutter #+# #+# */ +/* Updated: 2020/02/04 18:11:55 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_power(int num, unsigned int power) +void ft_memexpand(void **src, size_t *size1, const void *add, size_t size2) { - long long calc; + void *temp; - calc = 1; - if (power == 0) + temp = *src; + if (*src == NULL) { - return (1); + *src = ft_memdup(add, size2); + *size1 += size2; } - while (power > 0) + else if (add != NULL && size2 != 0) { - calc = num * calc; - if (calc > 2147483647 || calc < -2147483648) - return (0); - power--; + *src = ft_memjoin(*src, *size1, add, size2); + free(temp); + *size1 += size2; } - if (num < 0) - { - calc = (int)ft_absneg(calc); - } - return ((int)calc); } diff --git a/libft/ft_memjoin.c b/libft/ft_memjoin.c index 4a62988..30d60c5 100644 --- a/libft/ft_memjoin.c +++ b/libft/ft_memjoin.c @@ -3,39 +3,45 @@ /* :::::::: */ /* ft_memjoin.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/02/16 15:25:40 by aholster #+# #+# */ -/* Updated: 2019/02/18 17:34:47 by aholster ######## odam.nl */ +/* Created: 2019/02/14 12:43:36 by lgutter #+# #+# */ +/* Updated: 2019/02/14 12:43:37 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memjoin(void *mem1, size_t size1, void *mem2, size_t size2) +static size_t ft_memjoin_internal(unsigned char *result, + size_t judex, const unsigned char *src, size_t size) { - size_t index; - size_t judex; - char *ret; - char *src; + size_t index; index = 0; - src = (char *)mem1; - ret = (char *)malloc(sizeof(char) * size1 + size2); - if (ret == NULL) - return (NULL); - while (index < size1) - { - ret[index] = src[index]; - index++; - } - src = (char *)mem2; - judex = 0; - while (judex < size2) + while (index < size) { - ret[index] = src[judex]; + result[judex] = src[index]; index++; judex++; } - return (ret); + return (judex); +} + +void *ft_memjoin(const void *str1, size_t size1, + const void *str2, size_t size2) +{ + size_t judex; + const unsigned char *src1; + const unsigned char *src2; + unsigned char *result; + + judex = 0; + src1 = (unsigned char *)str1; + src2 = (unsigned char *)str2; + result = (unsigned char *)malloc(sizeof(unsigned char) * (size1 + size2)); + if (result == NULL) + return (NULL); + judex = ft_memjoin_internal(result, judex, src1, size1); + ft_memjoin_internal(result, judex, src2, size2); + return ((void *)result); } diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c index 8939ef2..ce99e5b 100644 --- a/libft/ft_memmove.c +++ b/libft/ft_memmove.c @@ -3,37 +3,24 @@ /* :::::::: */ /* ft_memmove.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 16:54:50 by aholster #+# #+# */ -/* Updated: 2019/02/16 17:58:24 by aholster ######## odam.nl */ +/* Created: 2019/01/28 12:22:38 by lgutter #+# #+# */ +/* Updated: 2019/01/28 12:22:39 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memmove(void *dst, void const *src, size_t len) +void *ft_memmove(void *dst, const void *src, size_t len) { - unsigned char *output; - unsigned char const *input; - size_t index; - - index = 0; - output = (unsigned char *)dst; - input = (unsigned char const *)src; - while (index < len) + if (dst < src) + { + dst = ft_memcpy(dst, src, len); + } + else { - if (&dst[0] == &input[index]) - { - while (len != 0) - { - len--; - output[len] = input[len]; - } - return (dst); - } - index++; + dst = ft_memrcpy(dst, src, len); } - dst = ft_memcpy(dst, src, len); return (dst); } diff --git a/libft/ft_memrcpy.c b/libft/ft_memrcpy.c new file mode 100644 index 0000000..5bb75ed --- /dev/null +++ b/libft/ft_memrcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_memrcpy.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/01/28 18:33:50 by lgutter #+# #+# */ +/* Updated: 2019/01/28 18:33:55 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memrcpy(void *dst, const void *src, size_t len) +{ + unsigned char *output; + unsigned const char *input; + + output = dst; + input = src; + while (len > 0) + { + output[len - 1] = input[len - 1]; + len--; + } + return (dst); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c index 645152e..aa17250 100644 --- a/libft/ft_memset.c +++ b/libft/ft_memset.c @@ -3,28 +3,40 @@ /* :::::::: */ /* ft_memset.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 15:05:30 by aholster #+# #+# */ -/* Updated: 2019/02/16 17:59:10 by aholster ######## odam.nl */ +/* Created: 2019/01/24 17:03:46 by lgutter #+# #+# */ +/* Updated: 2019/01/24 17:03:55 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memset(void *b, int c, size_t len) +void *ft_memset(void *target, int setvalue, size_t len) { - unsigned char character; - size_t index; - char *output; + size_t i; + size_t chunk_size; + unsigned long long chunk; - character = c; - index = 0; - output = (char *)b; - while (index < len) + chunk = 0; + chunk_size = sizeof(unsigned long long); + i = chunk_size / sizeof(unsigned char); + while (i > 0) { - output[index] = character; - index++; + chunk <<= 8; + chunk |= (unsigned long long)(unsigned char)setvalue; + i--; } - return (b); + i = target == NULL ? len : 0; + while (len >= chunk_size && i < (len - chunk_size)) + { + *(unsigned long long *)&(((unsigned char *)target)[i]) = chunk; + i += chunk_size; + } + while (i < len) + { + ((unsigned char *)target)[i] = (unsigned char)setvalue; + i++; + } + return (target); } diff --git a/libft/ft_nbrlenbase.c b/libft/ft_nbrlenbase.c new file mode 100644 index 0000000..9063ccf --- /dev/null +++ b/libft/ft_nbrlenbase.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_nbrlenbase.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/02/02 12:11:46 by lgutter #+# #+# */ +/* Updated: 2019/11/13 13:10:30 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_nbrlenbase(long long number, int base) +{ + size_t len; + + len = 1; + if (number < 0) + len++; + if (base == 1) + { + if (number < 0) + number = number * -1; + return ((size_t)number + len); + } + while (number / base != 0) + { + len++; + number /= base; + } + return (len); +} diff --git a/libft/ft_nbrlenbase_ull.c b/libft/ft_nbrlenbase_ull.c new file mode 100644 index 0000000..4ec1795 --- /dev/null +++ b/libft/ft_nbrlenbase_ull.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_nbrlenbase_ull.c :+: :+: */ +/* +:+ */ +/* By: ivan-tey +#+ */ +/* +#+ */ +/* Created: 2019/10/21 16:36:58 by ivan-tey #+# #+# */ +/* Updated: 2019/10/22 12:00:37 by ivan-tey ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_nbrlenbase_ull(unsigned long long number, int base) +{ + size_t len; + + len = 1; + if (base == 1) + return ((size_t)number + len); + while (number / base != 0) + { + len++; + number /= base; + } + return (len); +} diff --git a/libft/ft_putchar.c b/libft/ft_putchar.c index b9a68bc..758eb55 100644 --- a/libft/ft_putchar.c +++ b/libft/ft_putchar.c @@ -3,16 +3,16 @@ /* :::::::: */ /* ft_putchar.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/12 17:55:36 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:27:28 by aholster ######## odam.nl */ +/* Created: 2019/01/14 16:33:12 by lgutter #+# #+# */ +/* Updated: 2019/02/03 11:31:26 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putchar(char c) +void ft_putchar(char character) { - write(1, &c, 1); + write(1, &character, 1); } diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c index b0ca697..dd55712 100644 --- a/libft/ft_putchar_fd.c +++ b/libft/ft_putchar_fd.c @@ -3,16 +3,16 @@ /* :::::::: */ /* ft_putchar_fd.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/12 18:45:21 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:27:36 by aholster ######## odam.nl */ +/* Created: 2019/01/23 18:06:13 by lgutter #+# #+# */ +/* Updated: 2019/01/23 18:06:21 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putchar_fd(char c, int fd) +void ft_putchar_fd(char character, int filedes) { - write(fd, &c, 1); + write(filedes, &character, 1); } diff --git a/libft/ft_putendl.c b/libft/ft_putendl.c index 631d029..8881fd4 100644 --- a/libft/ft_putendl.c +++ b/libft/ft_putendl.c @@ -3,17 +3,17 @@ /* :::::::: */ /* ft_putendl.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/12 18:28:32 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:17:42 by aholster ######## odam.nl */ +/* Created: 2019/01/23 17:03:03 by lgutter #+# #+# */ +/* Updated: 2019/01/23 17:03:12 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putendl(char const *s) +void ft_putendl(char const *string) { - write(1, s, ft_strlen(s)); + write(1, string, ft_strlen(string)); write(1, "\n", 1); } diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c index d64f0b6..a536738 100644 --- a/libft/ft_putendl_fd.c +++ b/libft/ft_putendl_fd.c @@ -3,17 +3,17 @@ /* :::::::: */ /* ft_putendl_fd.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 11:47:43 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:15:22 by aholster ######## odam.nl */ +/* Created: 2019/01/23 20:17:09 by lgutter #+# #+# */ +/* Updated: 2019/01/23 20:17:16 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putendl_fd(char const *s, int fd) +void ft_putendl_fd(char const *string, int filedes) { - write(fd, s, ft_strlen(s)); - write(fd, "\n", 1); + write(filedes, string, ft_strlen(string)); + write(filedes, "\n", 1); } diff --git a/libft/ft_putnbr.c b/libft/ft_putnbr.c index 1e4fd2c..1c97211 100644 --- a/libft/ft_putnbr.c +++ b/libft/ft_putnbr.c @@ -3,32 +3,39 @@ /* :::::::: */ /* ft_putnbr.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/12 18:18:57 by aholster #+# #+# */ -/* Updated: 2019/01/29 19:29:29 by aholster ######## odam.nl */ +/* Created: 2019/01/23 17:50:10 by lgutter #+# #+# */ +/* Updated: 2019/01/23 17:50:22 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putnbr(int n) +void ft_putnbr(int number) { - if (n == -2147483648) - { - write(1, "-2", 2); - n = 147483648; - } - else if (n < 0) - { - write(1, "-", 1); - n = (-n); - } - if (n > 9) + char digit; + + digit = 'a'; + if (number == -2147483648) + write(1, "-2147483648", 11); + else { - ft_putnbr(n / 10); - ft_putchar((n % 10) + '0'); + if (number < 0) + { + write(1, "-", 1); + number *= -1; + } + if (number / 10 > 0) + { + ft_putnbr(number / 10); + digit = ((number % 10) + 48); + write(1, &digit, 1); + } + else + { + digit = number + 48; + write(1, &digit, 1); + } } - else - ft_putchar(n + '0'); } diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c index e2546fa..a991531 100644 --- a/libft/ft_putnbr_fd.c +++ b/libft/ft_putnbr_fd.c @@ -3,32 +3,39 @@ /* :::::::: */ /* ft_putnbr_fd.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 12:05:42 by aholster #+# #+# */ -/* Updated: 2019/02/01 20:52:24 by aholster ######## odam.nl */ +/* Created: 2019/01/23 20:52:48 by lgutter #+# #+# */ +/* Updated: 2019/01/23 20:52:57 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putnbr_fd(int n, int fd) +void ft_putnbr_fd(int number, int filedes) { - if (n == -2147483648) - { - write(fd, "-2", 2); - n = 147483648; - } - else if (n < 0) - { - write(fd, "-", 1); - n = (-n); - } - if (n > 9) + char digit; + + digit = 'a'; + if (number == -2147483648) + write(filedes, "-2147483648", 11); + else { - ft_putnbr_fd(n / 10, fd); - ft_putchar_fd(((n % 10) + '0'), fd); + if (number < 0) + { + write(filedes, "-", 1); + number *= -1; + } + if (number / 10 > 0) + { + ft_putnbr_fd(number / 10, filedes); + digit = ((number % 10) + 48); + write(filedes, &digit, 1); + } + else + { + digit = number + 48; + write(filedes, &digit, 1); + } } - else - ft_putchar_fd((n + '0'), fd); } diff --git a/libft/ft_putstr.c b/libft/ft_putstr.c index 1be8264..4d88c93 100644 --- a/libft/ft_putstr.c +++ b/libft/ft_putstr.c @@ -3,16 +3,16 @@ /* :::::::: */ /* ft_putstr.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/12 17:59:46 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:18:17 by aholster ######## odam.nl */ +/* Created: 2019/01/14 17:19:54 by lgutter #+# #+# */ +/* Updated: 2019/01/14 17:19:57 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putstr(char const *s) +void ft_putstr(char const *string) { - write(1, s, ft_strlen(s)); + write(1, string, ft_strlen(string)); } diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c index bc034d2..004d85b 100644 --- a/libft/ft_putstr_fd.c +++ b/libft/ft_putstr_fd.c @@ -3,16 +3,16 @@ /* :::::::: */ /* ft_putstr_fd.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/12 18:46:21 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:18:26 by aholster ######## odam.nl */ +/* Created: 2019/01/23 18:48:41 by lgutter #+# #+# */ +/* Updated: 2019/01/23 18:48:50 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_putstr_fd(char const *s, int fd) +void ft_putstr_fd(char const *string, int filedes) { - write(fd, s, ft_strlen(s)); + write(filedes, string, ft_strlen(string)); } diff --git a/libft/ft_segdefault.c b/libft/ft_segdefault.c deleted file mode 100644 index e4cbf59..0000000 --- a/libft/ft_segdefault.c +++ /dev/null @@ -1,34 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_segdefault.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/31 17:57:56 by aholster #+# #+# */ -/* Updated: 2019/03/27 16:20:40 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_segdefault(void) -{ - size_t index; - char *str; - - index = 0; - str = NULL; - ft_putendl("\e[0;31mWARNING: this is an intentional segfault caused \ -by segdefualt"); - ft_putstr("Inevitably many shall fault the hands of those who felled"); - ft_putstr(" , but segdefualt merely performs the duty of its"); - ft_putendl(" office, to further fear it is redundant."); - ft_putstr("those more sensible would place responsibility with those who"); - ft_putendl(" forced the hands of segdefualt."); - ft_putendl("\e[0;33mMay imperial justice account in all balance.\e[0;00m"); - while (str[index] != '\0') - { - index++; - } -} diff --git a/libft/ft_sqrt.c b/libft/ft_sqrt.c deleted file mode 100644 index e307263..0000000 --- a/libft/ft_sqrt.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_sqrt.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/31 21:43:31 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:16:49 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -int ft_sqrt(int nb) -{ - int count; - - if (nb < 0) - return (0); - count = 0; - while (count * count <= nb) - { - if (count * count == nb) - return (count); - count++; - } - return (0); -} diff --git a/libft/ft_lstleng.c b/libft/ft_str_arr_len.c similarity index 66% rename from libft/ft_lstleng.c rename to libft/ft_str_arr_len.c index 6086b6d..0592a3b 100644 --- a/libft/ft_lstleng.c +++ b/libft/ft_str_arr_len.c @@ -1,25 +1,24 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_lstleng.c :+: :+: */ +/* ft_str_arr_len.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/31 17:32:55 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:29:22 by aholster ######## odam.nl */ +/* Created: 2020/01/21 11:49:18 by lgutter #+# #+# */ +/* Updated: 2020/01/21 11:49:21 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -unsigned int ft_lstleng(t_list *lst) +size_t ft_str_arr_len(char **pointer_array) { - unsigned int index; + size_t index; index = 0; - while (lst != NULL) + while (pointer_array[index] != NULL) { - lst = lst->next; index++; } return (index); diff --git a/libft/ft_strarrtolst.c b/libft/ft_strarrtolst.c deleted file mode 100644 index ab1a768..0000000 --- a/libft/ft_strarrtolst.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_strarrtolst.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/01/31 21:26:16 by aholster #+# #+# */ -/* Updated: 2019/02/07 20:26:47 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -t_list *ft_strarrtolst(char **strarr) -{ - size_t index; - t_list *head; - t_list *holder; - t_list *new; - - index = 0; - head = ft_lstnew(strarr[index], ft_strlen(strarr[index]) + 1); - holder = head; - index++; - while (strarr[index] != NULL) - { - new = ft_lstnew(strarr[index], ft_strlen(strarr[index]) + 1); - if (new == NULL) - return (NULL); - ft_lstaddend(&holder, new); - holder = new; - index++; - } - return (head); -} diff --git a/libft/ft_strcat.c b/libft/ft_strcat.c index a2f8f03..17892ef 100644 --- a/libft/ft_strcat.c +++ b/libft/ft_strcat.c @@ -3,27 +3,32 @@ /* :::::::: */ /* ft_strcat.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/18 18:49:46 by aholster #+# #+# */ -/* Updated: 2019/01/29 19:30:54 by aholster ######## odam.nl */ +/* Created: 2019/01/30 15:28:00 by lgutter #+# #+# */ +/* Updated: 2019/01/30 15:28:02 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strcat(char *s1, char const *s2) +char *ft_strcat(char *string1, const char *string2) { - size_t index; - size_t judex; + size_t index; + size_t xedni; index = 0; - judex = ft_strlen(s1); - while (s2[index] != '\0') + xedni = 0; + while (string1[index] != '\0') { - s1[index + judex] = s2[index]; index++; } - s1[index + judex] = '\0'; - return (s1); + while (string2[xedni] != '\0') + { + string1[index] = string2[xedni]; + index++; + xedni++; + } + string1[index] = '\0'; + return (string1); } diff --git a/libft/ft_strcdup.c b/libft/ft_strcdup.c new file mode 100644 index 0000000..257d394 --- /dev/null +++ b/libft/ft_strcdup.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strcdup.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2020/01/10 17:47:04 by lgutter #+# #+# */ +/* Updated: 2020/01/21 19:50:31 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcdup(const char *string, char delim) +{ + size_t len; + + if (string == NULL) + { + return (NULL); + } + len = ft_strlenc(string, delim, ft_strlen(string)); + return (ft_strndup(string, len)); +} diff --git a/libft/ft_strcharexpand.c b/libft/ft_strcharexpand.c new file mode 100644 index 0000000..a06ee72 --- /dev/null +++ b/libft/ft_strcharexpand.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strcharexpand.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/10/27 12:30:20 by lgutter #+# #+# */ +/* Updated: 2019/10/27 17:49:46 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strcharexpand(char **source, const char addition) +{ + void *temp; + + temp = *source; + if (*source == NULL) + { + *source = ft_strnew(1); + if (*source != NULL) + { + *source[0] = addition; + } + } + else + { + *source = ft_strcharjoin(*source, addition); + free(temp); + } +} diff --git a/libft/ft_range.c b/libft/ft_strcharjoin.c similarity index 57% rename from libft/ft_range.c rename to libft/ft_strcharjoin.c index edb3b7c..b6234cc 100644 --- a/libft/ft_range.c +++ b/libft/ft_strcharjoin.c @@ -1,32 +1,32 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_range.c :+: :+: */ +/* ft_strcharjoin.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/31 21:33:15 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:29:30 by aholster ######## odam.nl */ +/* Created: 2019/10/27 12:13:40 by lgutter #+# #+# */ +/* Updated: 2019/11/16 15:35:45 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -int *ft_range(int min, int max) +char *ft_strcharjoin(char *str, char c) { - int *res; - int cur; + char *strnew; + size_t i; - cur = min; - if (min >= max) - return (0); - res = (int *)malloc(sizeof(int) * (max - min)); - if (res == NULL) + i = 0; + strnew = ft_strnew(ft_strlen(str) + 2); + if (strnew == NULL) return (NULL); - while (cur <= max) + while (i < ft_strlen(str)) { - res[cur - min] = cur; - cur++; + strnew[i] = str[i]; + i++; } - return (res); + strnew[i] = c; + strnew[i + 1] = '\0'; + return (strnew); } diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c index de74748..f8d2190 100644 --- a/libft/ft_strchr.c +++ b/libft/ft_strchr.c @@ -3,27 +3,28 @@ /* :::::::: */ /* ft_strchr.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/18 13:02:35 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:26:47 by aholster ######## odam.nl */ +/* Created: 2019/01/31 11:05:38 by lgutter #+# #+# */ +/* Updated: 2019/01/31 11:05:39 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strchr(char const *s, int c) +char *ft_strchr(const char *string, int character) { - size_t index; - - index = 0; - while (s[index] != '\0') + while (*string != '\0') + { + if (*string == character) + { + return ((char *)string); + } + string++; + } + if (character == '\0') { - if (s[index] == (unsigned char)c) - return ((char *)&s[index]); - index++; + return ((char *)string); } - if (s[index] == (unsigned char)c) - return ((char *)&s[index]); return (NULL); } diff --git a/libft/ft_strclr.c b/libft/ft_strclr.c index 8b6f186..05b67f8 100644 --- a/libft/ft_strclr.c +++ b/libft/ft_strclr.c @@ -3,23 +3,21 @@ /* :::::::: */ /* ft_strclr.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 10:55:10 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:27:15 by aholster ######## odam.nl */ +/* Created: 2019/01/14 14:07:28 by lgutter #+# #+# */ +/* Updated: 2019/01/14 14:07:30 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "libft.h" - -void ft_strclr(char *s) +void ft_strclr(char *string) { - size_t index; + int index; index = 0; - while (s[index] != '\0') + while (string[index] != '\0') { - s[index] = '\0'; + string[index] = '\0'; index++; } } diff --git a/libft/ft_strcmp.c b/libft/ft_strcmp.c index e1b9851..604da66 100644 --- a/libft/ft_strcmp.c +++ b/libft/ft_strcmp.c @@ -3,23 +3,32 @@ /* :::::::: */ /* ft_strcmp.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 12:25:24 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:26:54 by aholster ######## odam.nl */ +/* Created: 2019/01/31 14:34:52 by lgutter #+# #+# */ +/* Updated: 2019/01/31 14:34:53 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_strcmp(char const *s1, char const *s2) +int ft_strcmp(const char *string1, const char *string2) { - size_t index; + size_t index; index = 0; - while (s1[index] == s2[index] && s2[index] != '\0' && s1[index] != '\0') + if (string1 == NULL && string2 != NULL && + string1 != NULL && string2 == NULL) + return (1); + else if (string1 == NULL) + return (0); + while (string1[index] == string2[index]) { + if (string1[index] == '\0') + { + return (0); + } index++; } - return ((unsigned char)s1[index] - (unsigned char)s2[index]); + return ((unsigned char)string1[index] - (unsigned char)string2[index]); } diff --git a/libft/ft_strcpy.c b/libft/ft_strcpy.c index 21ee8c6..962e418 100644 --- a/libft/ft_strcpy.c +++ b/libft/ft_strcpy.c @@ -3,10 +3,10 @@ /* :::::::: */ /* ft_strcpy.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 11:12:58 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:26:41 by aholster ######## odam.nl */ +/* Created: 2019/01/30 12:01:52 by lgutter #+# #+# */ +/* Updated: 2019/01/30 12:05:02 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,14 +14,5 @@ char *ft_strcpy(char *dst, const char *src) { - size_t index; - - index = 0; - while (src[index] != '\0') - { - dst[index] = src[index]; - index++; - } - dst[index] = '\0'; - return (dst); + return (ft_memcpy(dst, src, ft_strlen(src) + 1)); } diff --git a/libft/ft_strdel.c b/libft/ft_strdel.c index 478c596..872629a 100644 --- a/libft/ft_strdel.c +++ b/libft/ft_strdel.c @@ -3,17 +3,17 @@ /* :::::::: */ /* ft_strdel.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 10:55:01 by aholster #+# #+# */ -/* Updated: 2019/01/25 16:29:19 by aholster ######## odam.nl */ +/* Created: 2019/01/14 13:46:05 by lgutter #+# #+# */ +/* Updated: 2019/01/14 13:46:09 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_strdel(char **as) +void ft_strdel(char **string) { - free(*as); - *as = NULL; + free(*string); + *string = NULL; } diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c index 73746de..ed7528f 100644 --- a/libft/ft_strdup.c +++ b/libft/ft_strdup.c @@ -3,21 +3,16 @@ /* :::::::: */ /* ft_strdup.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 10:35:30 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:23:59 by aholster ######## odam.nl */ +/* Created: 2019/01/29 21:17:53 by lgutter #+# #+# */ +/* Updated: 2020/01/10 17:50:38 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strdup(char const *s1) +char *ft_strdup(const char *string) { - char *output; - - if (!(output = (char *)malloc(sizeof(char) * (ft_strlen(s1) + 1)))) - return (NULL); - output = ft_strcpy(output, s1); - return (output); + return (ft_strcdup(string, '\0')); } diff --git a/libft/ft_strequ.c b/libft/ft_strequ.c index 8716bef..b917c24 100644 --- a/libft/ft_strequ.c +++ b/libft/ft_strequ.c @@ -3,16 +3,25 @@ /* :::::::: */ /* ft_strequ.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 15:33:29 by aholster #+# #+# */ -/* Updated: 2019/01/13 16:01:01 by aholster ######## odam.nl */ +/* Created: 2019/01/17 10:47:11 by lgutter #+# #+# */ +/* Updated: 2019/01/17 10:47:13 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "libft.h" - -int ft_strequ(char const *s1, char const *s2) +int ft_strequ(char const *string1, char const *string2) { - return (ft_strcmp(s1, s2) == 0); + int index; + + index = 0; + while (string1[index] != '\0' && string2[index] != '\0') + { + if (string1[index] != string2[index]) + return (0); + index++; + } + if (string1[index] != string2[index]) + return (0); + return (1); } diff --git a/libft/ft_lstaddend.c b/libft/ft_strexpand.c similarity index 58% rename from libft/ft_lstaddend.c rename to libft/ft_strexpand.c index d758018..49d61dd 100644 --- a/libft/ft_lstaddend.c +++ b/libft/ft_strexpand.c @@ -1,27 +1,29 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_lstaddend.c :+: :+: */ +/* ft_strexpand.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/25 13:07:44 by aholster #+# #+# */ -/* Updated: 2019/02/07 20:28:26 by aholster ######## odam.nl */ +/* Created: 2019/09/13 16:04:59 by lgutter #+# #+# */ +/* Updated: 2020/02/04 15:23:17 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_lstaddend(t_list **lst, t_list *new) +void ft_strexpand(char **source, const char *addition) { - if (*lst == NULL) + void *temp; + + temp = *source; + if (*source == NULL) { - *lst = new; - return ; + *source = ft_strdup(addition); } - while ((*lst)->next != NULL) + else if (addition != NULL) { - *lst = (*lst)->next; + *source = ft_strjoin(*source, addition); + free(temp); } - (*lst)->next = new; } diff --git a/libft/ft_striter.c b/libft/ft_striter.c index 51912f6..ccf1bdb 100644 --- a/libft/ft_striter.c +++ b/libft/ft_striter.c @@ -3,23 +3,21 @@ /* :::::::: */ /* ft_striter.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/16 21:06:47 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:27:22 by aholster ######## odam.nl */ +/* Created: 2019/01/14 15:07:53 by lgutter #+# #+# */ +/* Updated: 2019/01/16 15:29:19 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "libft.h" - -void ft_striter(char *s, void (*f)(char *)) +void ft_striter(char *string, void (*function)(char *)) { - size_t index; + unsigned int index; index = 0; - while (s[index] != '\0') + while (string[index] != '\0') { - ((*f)(&s[index])); + ((*function)(&string[index])); index++; } } diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c index 1de0ede..c73c3ac 100644 --- a/libft/ft_striteri.c +++ b/libft/ft_striteri.c @@ -3,21 +3,21 @@ /* :::::::: */ /* ft_striteri.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/16 21:06:41 by aholster #+# #+# */ -/* Updated: 2019/02/01 21:20:27 by aholster ######## odam.nl */ +/* Created: 2019/01/16 14:56:13 by lgutter #+# #+# */ +/* Updated: 2019/01/16 14:56:15 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ -void ft_striteri(char *s, void (*f)(unsigned int, char *)) +void ft_striteri(char *string, void (*function)(unsigned int, char *)) { - unsigned int index; + unsigned int index; index = 0; - while (s[index] != '\0') + while (string[index] != '\0') { - ((*f)(index, &s[index])); + ((*function)(index, &string[index])); index++; } } diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c index c5fb860..6d48253 100644 --- a/libft/ft_strjoin.c +++ b/libft/ft_strjoin.c @@ -3,39 +3,52 @@ /* :::::::: */ /* ft_strjoin.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/16 18:31:48 by aholster #+# #+# */ -/* Updated: 2019/02/16 15:46:07 by aholster ######## odam.nl */ +/* Created: 2019/01/18 16:52:17 by lgutter #+# #+# */ +/* Updated: 2020/02/07 08:39:53 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strjoin(char const *s1, char const *s2) +static size_t ft_strjoin_internal(char *ret, char const *str, size_t xedni) { - size_t index; - size_t judex; - char *str; + size_t index; - judex = 0; index = 0; - str = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); - if (str == NULL) - return (NULL); - while (s1[index] != '\0') + while (str[index] != '\0') { - str[judex] = s1[index]; - judex++; + ret[xedni] = str[index]; + xedni++; index++; } - index = 0; - while (s2[index] != '\0') + return (xedni); +} + +char *ft_strjoin(char const *string1, char const *string2) +{ + char *ret; + size_t in; + size_t dex; + + in = ft_strlen(string1); + dex = ft_strlen(string2); + ret = (char *)malloc(sizeof(char) * (in + dex + 1)); + if (!ret) { - str[judex] = s2[index]; - judex++; - index++; + return (NULL); + } + ret[in + dex] = '\0'; + if (string1 == NULL && string2 != NULL) + ret = ft_strncpy(ret, string2, dex); + else if (string1 != NULL && string2 == NULL) + ret = ft_strncpy(ret, string1, in); + else if (string1 != NULL && string2 != NULL) + { + in = 0; + in = ft_strjoin_internal(ret, string1, in); + ft_strjoin_internal(ret, string2, in); } - str[judex] = '\0'; - return (str); + return (ret); } diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c index 857a89f..9e6389e 100644 --- a/libft/ft_strlcat.c +++ b/libft/ft_strlcat.c @@ -3,36 +3,34 @@ /* :::::::: */ /* ft_strlcat.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/20 16:37:37 by aholster #+# #+# */ -/* Updated: 2019/02/01 20:52:24 by aholster ######## odam.nl */ +/* Created: 2019/01/30 19:39:45 by lgutter #+# #+# */ +/* Updated: 2019/01/30 19:39:46 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -size_t ft_strlcat(char *dst, char const *src, size_t size) +size_t ft_strlcat(char *dst, const char *src, size_t size) { - size_t output; - size_t sourcelen; - size_t index; + size_t index; + size_t xedni; + size_t ret; - index = 0; - output = 0; - sourcelen = ft_strlen(src); - while (dst[output] != '\0' && output < size) - output++; - if (output + 1 >= size) - return (output + sourcelen); - else + ret = ft_strlen(dst); + xedni = 0; + if (size <= ret) { - while (index + output < size - 1) - { - dst[index + output] = src[index]; - index++; - } - dst[size - 1] = '\0'; + return (size + ft_strlen(src)); } - return (output + sourcelen); + index = ret; + while (index < (size - 1) && src[xedni] != '\0') + { + dst[index] = src[xedni]; + index++; + xedni++; + } + dst[index] = '\0'; + return (ret + ft_strlen(src)); } diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c index 81e6968..df5bfdd 100644 --- a/libft/ft_strlen.c +++ b/libft/ft_strlen.c @@ -3,21 +3,23 @@ /* :::::::: */ /* ft_strlen.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/14 13:39:30 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:26:04 by aholster ######## odam.nl */ +/* Created: 2019/01/16 15:36:46 by lgutter #+# #+# */ +/* Updated: 2020/02/07 08:34:01 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -size_t ft_strlen(char const *s) +size_t ft_strlen(const char *string) { - size_t index; + size_t len; - index = 0; - while (s[index] != '\0') - index++; - return (index); + len = 0; + if (string == NULL) + return (0); + while (string[len] != '\0') + len++; + return (len); } diff --git a/libft/ft_strarrdel.c b/libft/ft_strlenc.c similarity index 61% rename from libft/ft_strarrdel.c rename to libft/ft_strlenc.c index 592dece..d525679 100644 --- a/libft/ft_strarrdel.c +++ b/libft/ft_strlenc.c @@ -1,27 +1,23 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_strarrdel.c :+: :+: */ +/* ft_strlenc.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/31 21:03:28 by aholster #+# #+# */ -/* Updated: 2019/01/31 21:25:04 by aholster ######## odam.nl */ +/* Created: 2019/02/14 12:25:27 by lgutter #+# #+# */ +/* Updated: 2019/02/14 12:25:28 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_strarrdel(char ***ap) +size_t ft_strlenc(const char *string, int delim, size_t size) { - size_t index; + size_t len; - index = 0; - while ((*ap)[index] != NULL) - { - ft_strdel(&((*ap)[index])); - index++; - } - free(*ap); - *ap = NULL; + len = 0; + while (string[len] != (char)delim && len < size) + len++; + return (len); } diff --git a/libft/ft_strmap.c b/libft/ft_strmap.c index 1d1ea29..d5e42cb 100644 --- a/libft/ft_strmap.c +++ b/libft/ft_strmap.c @@ -3,29 +3,29 @@ /* :::::::: */ /* ft_strmap.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/16 19:57:12 by aholster #+# #+# */ -/* Updated: 2019/04/01 16:51:38 by aholster ######## odam.nl */ +/* Created: 2019/01/16 15:32:17 by lgutter #+# #+# */ +/* Updated: 2019/01/16 15:32:18 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strmap(char const *s, char (*f)(char)) +char *ft_strmap(char const *string, char (*function)(char)) { - size_t index; - char *str; + int index; + char *ret; - index = 0; - str = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1)); - if (str == NULL) + ret = (char *)malloc(sizeof(char) * ft_strlen(string) + 1); + if (!ret) return (NULL); - while (s[index] != '\0') + index = 0; + while (string[index] != '\0') { - str[index] = ((*f)(s[index])); + ret[index] = (*function)(string[index]); index++; } - str[index] = '\0'; - return (str); + ret[index] = '\0'; + return (ret); } diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c index 6be3685..a38ade7 100644 --- a/libft/ft_strmapi.c +++ b/libft/ft_strmapi.c @@ -3,29 +3,29 @@ /* :::::::: */ /* ft_strmapi.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/16 19:57:09 by aholster #+# #+# */ -/* Updated: 2019/04/01 16:47:59 by aholster ######## odam.nl */ +/* Created: 2019/01/16 20:02:03 by lgutter #+# #+# */ +/* Updated: 2019/01/16 20:02:04 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +char *ft_strmapi(char const *string, char (*function)(unsigned int, char)) { unsigned int index; - char *str; + char *ret; index = 0; - str = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1)); - if (str == NULL) + ret = (char *)malloc(sizeof(char) * ft_strlen(string) + 1); + if (!ret) return (NULL); - while (s[index] != '\0') + while (string[index] != '\0') { - str[index] = ((*f)(index, s[index])); + ret[index] = (*function)(index, string[index]); index++; } - str[index] = '\0'; - return (str); + ret[index] = '\0'; + return (ret); } diff --git a/libft/ft_strncat.c b/libft/ft_strncat.c index b55df41..8c9c97c 100644 --- a/libft/ft_strncat.c +++ b/libft/ft_strncat.c @@ -3,27 +3,37 @@ /* :::::::: */ /* ft_strncat.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/18 19:04:20 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:26:51 by aholster ######## odam.nl */ +/* Created: 2019/01/30 17:21:50 by lgutter #+# #+# */ +/* Updated: 2019/01/30 17:21:51 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strncat(char *s1, char const *s2, size_t n) +char *ft_strncat(char *string1, const char *string2, size_t len) { - size_t index; - size_t judex; + size_t index; + size_t xedni; index = 0; - judex = ft_strlen(s1); - while (s2[index] != '\0' && index < n) + xedni = 0; + while (string1[index] != '\0') { - s1[index + judex] = s2[index]; index++; } - s1[index + judex] = '\0'; - return (s1); + while (string2[xedni] != '\0') + { + if (xedni == len) + { + string1[index] = '\0'; + return (string1); + } + string1[index] = string2[xedni]; + index++; + xedni++; + } + string1[index] = '\0'; + return (string1); } diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c index 8499d4f..e518594 100644 --- a/libft/ft_strncmp.c +++ b/libft/ft_strncmp.c @@ -3,29 +3,31 @@ /* :::::::: */ /* ft_strncmp.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 12:28:26 by aholster #+# #+# */ -/* Updated: 2019/01/30 15:13:46 by aholster ######## odam.nl */ +/* Created: 2019/01/31 14:35:02 by lgutter #+# #+# */ +/* Updated: 2019/01/31 14:35:03 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_strncmp(char const *s1, char const *s2, size_t n) +int ft_strncmp(const char *string1, const char *string2, size_t len) { size_t index; index = 0; - if (n == 0) + if (len == 0) + { return (0); - while (s1[index] != '\0' && s2[index] != '\0' && index < (n - 1)) + } + while (string1[index] == string2[index]) { - if (s1[index] != s2[index]) - return ((unsigned char)s1[index] - (unsigned char)s2[index]); + if (string1[index] == '\0' || index == (len - 1)) + { + return (0); + } index++; } - if (s1[index] != s2[index]) - return ((unsigned char)s1[index] - (unsigned char)s2[index]); - return (0); + return ((unsigned char)string1[index] - (unsigned char)string2[index]); } diff --git a/libft/ft_strncpy.c b/libft/ft_strncpy.c index 6590507..1062949 100644 --- a/libft/ft_strncpy.c +++ b/libft/ft_strncpy.c @@ -3,10 +3,10 @@ /* :::::::: */ /* ft_strncpy.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 11:13:03 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:27:09 by aholster ######## odam.nl */ +/* Created: 2019/01/30 12:05:07 by lgutter #+# #+# */ +/* Updated: 2019/01/30 12:05:08 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,18 +14,19 @@ char *ft_strncpy(char *dst, const char *src, size_t len) { - size_t index; + char *ret; + size_t index; - index = 0; - while (src[index] != '\0' && index < len) + index = ft_strlen(src) + 1; + if (len >= index) { - dst[index] = src[index]; - index++; + ret = ft_memcpy(dst, src, index); + while (index < len) + { + ret[index] = '\0'; + index++; + } + return (ret); } - while (index < len) - { - dst[index] = '\0'; - index++; - } - return (dst); + return (ft_memcpy(dst, src, len)); } diff --git a/libft/ft_stralloc.c b/libft/ft_strndup.c similarity index 57% rename from libft/ft_stralloc.c rename to libft/ft_strndup.c index 2f9b9b9..963daac 100644 --- a/libft/ft_stralloc.c +++ b/libft/ft_strndup.c @@ -1,31 +1,36 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_stralloc.c :+: :+: */ +/* ft_strndup.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/03/24 18:08:35 by aholster #+# #+# */ -/* Updated: 2019/03/24 18:15:06 by aholster ######## odam.nl */ +/* Created: 2020/01/21 19:46:22 by lgutter #+# #+# */ +/* Updated: 2020/01/21 19:47:55 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_stralloc(size_t size, char c) +char *ft_strndup(const char *string, size_t len) { - char *str; + char *ret; size_t index; index = 0; - str = (char *)malloc(sizeof(char) * size + 1); - if (str == NULL) + if (string == NULL) + { return (NULL); - str[size] = '\0'; - while (index < size) + } + ret = (char *)malloc(sizeof(char) * (len + 1)); + if (ret != NULL) { - str[index] = c; - index++; + while (index < len) + { + ret[index] = string[index]; + index++; + } + ret[index] = '\0'; } - return (str); + return (ret); } diff --git a/libft/ft_strnequ.c b/libft/ft_strnequ.c index 31c786b..b74db04 100644 --- a/libft/ft_strnequ.c +++ b/libft/ft_strnequ.c @@ -3,16 +3,36 @@ /* :::::::: */ /* ft_strnequ.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/13 15:33:31 by aholster #+# #+# */ -/* Updated: 2019/01/30 15:01:41 by aholster ######## odam.nl */ +/* Created: 2019/01/17 15:49:40 by lgutter #+# #+# */ +/* Updated: 2019/01/17 15:49:44 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_strnequ(char const *s1, char const *s2, size_t n) +int ft_strnequ(char const *str1, char const *str2, size_t len) { - return (ft_strncmp(s1, s2, n) == 0); + size_t index; + + index = 0; + if (len == 0) + return (1); + else + { + while (str1[index] != '\0' && str2[index] != '\0' && index < (len - 1)) + { + if (str1[index] != str2[index]) + { + return (0); + } + index++; + } + if (str1[index] != str2[index]) + { + return (0); + } + return (1); + } } diff --git a/libft/ft_strnew.c b/libft/ft_strnew.c index 6d91886..b5cbbd5 100644 --- a/libft/ft_strnew.c +++ b/libft/ft_strnew.c @@ -3,10 +3,10 @@ /* :::::::: */ /* ft_strnew.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/17 11:59:15 by aholster #+# #+# */ -/* Updated: 2019/03/24 18:09:35 by aholster ######## odam.nl */ +/* Created: 2019/01/13 20:40:25 by lgutter #+# #+# */ +/* Updated: 2019/11/19 14:14:50 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,11 +14,18 @@ char *ft_strnew(size_t size) { - char *str; + char *string; - str = (char *)malloc(sizeof(char) * size + 1); - if (str == NULL) + size++; + string = (char *)malloc(sizeof(char) * (size + 1)); + if (!string) return (NULL); - ft_bzero(str, size + 1); - return (str); + if (size > 0) + size++; + while (size > 0) + { + size--; + string[size] = '\0'; + } + return (string); } diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c index f8d50ab..70e376b 100644 --- a/libft/ft_strnstr.c +++ b/libft/ft_strnstr.c @@ -3,35 +3,50 @@ /* :::::::: */ /* ft_strnstr.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/18 14:27:22 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:34:00 by aholster ######## odam.nl */ +/* Created: 2019/01/31 12:30:58 by lgutter #+# #+# */ +/* Updated: 2019/01/31 12:30:59 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strnstr(char const *haystack, char const *needle, size_t len) +static int check_word(const char *string, const char *word, size_t len) { - size_t i; - size_t j; - char const *s; + size_t index; - i = 0; - s = haystack; - if (needle[0] == '\0') - return ((char *)haystack); - while (haystack[i] != '\0' && i < len) + index = 0; + while (word[index] != '\0') { - j = 0; - while (needle[j] == s[i + j] && s[i + j] != '\0' && (i + j) < len) + if (word[index] != string[index] || index >= len) { - j++; + return (0); } - if (needle[j] == '\0') - return ((char *)&haystack[i]); - i++; + index++; + } + return (1); +} + +char *ft_strnstr(const char *string, const char *word, size_t len) +{ + size_t index; + + index = 0; + if (word[0] == '\0') + { + return ((char *)string); + } + while (string[index] != '\0' && index < len) + { + if (string[index] == word[0]) + { + if (check_word(&string[index], word, len - index) == 1) + { + return ((char *)&string[index]); + } + } + index++; } return (NULL); } diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c index edc86b8..d865d17 100644 --- a/libft/ft_strrchr.c +++ b/libft/ft_strrchr.c @@ -3,27 +3,35 @@ /* :::::::: */ /* ft_strrchr.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/18 13:02:34 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:29:30 by aholster ######## odam.nl */ +/* Created: 2019/01/31 11:17:51 by lgutter #+# #+# */ +/* Updated: 2019/01/31 11:17:52 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strrchr(char const *s, int c) +char *ft_strrchr(const char *string, int character) { - size_t index; + size_t index; - index = ft_strlen(s); - while (index != 0) + index = ft_strlen(string); + if (character == '\0') { - if (s[index] == (unsigned char)c) - return ((char *)&s[index]); + return ((char *)&string[index]); + } + while (index > 0) + { + if (string[index] == character) + { + return ((char *)&string[index]); + } index--; } - if (s[index] == (unsigned char)c) - return ((char *)&s[index]); + if (string[index] == character) + { + return ((char *)&string[index]); + } return (NULL); } diff --git a/libft/ft_strarrnew.c b/libft/ft_strrev.c similarity index 61% rename from libft/ft_strarrnew.c rename to libft/ft_strrev.c index 0d27d59..2f46c50 100644 --- a/libft/ft_strarrnew.c +++ b/libft/ft_strrev.c @@ -1,24 +1,29 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* ft_strarrnew.c :+: :+: */ +/* ft_strrev.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: ivan-tey +#+ */ /* +#+ */ -/* Created: 2019/03/24 17:49:47 by aholster #+# #+# */ -/* Updated: 2019/03/24 17:56:30 by aholster ######## odam.nl */ +/* Created: 2019/09/27 13:40:07 by ivan-tey #+# #+# */ +/* Updated: 2019/10/22 12:00:46 by ivan-tey ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char **ft_strarrnew(size_t size) +char *ft_strrev(char *str, int start) { - char **str; + int i; + int len; - str = (char **)malloc(sizeof(char *) * size + 1); - if (str == NULL) - return (NULL); - str[size] = NULL; + i = start; + len = ft_strlen(str) - 1; + while (i < len) + { + ft_swap(&str[i], &str[len]); + i++; + len--; + } return (str); } diff --git a/libft/ft_strsplit.c b/libft/ft_strsplit.c index 58e8c0c..09344ed 100644 --- a/libft/ft_strsplit.c +++ b/libft/ft_strsplit.c @@ -3,94 +3,86 @@ /* :::::::: */ /* ft_strsplit.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/20 16:41:45 by aholster #+# #+# */ -/* Updated: 2019/02/01 18:58:56 by aholster ######## odam.nl */ +/* Created: 2019/01/21 18:29:22 by lgutter #+# #+# */ +/* Updated: 2019/01/21 18:29:32 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -static unsigned int ft_wordskip(char const *s, int c) +static size_t ft_wordcount(char const *string, char delim) { - unsigned int index; + size_t index; + size_t wordcount; index = 0; - while (s[index] != '\0') + wordcount = 0; + while (string[index] != '\0') { - if (s[index] == (char)c) - return (index); - index++; - } - return (index); -} - -static unsigned int ft_wordcount(char const *s, char c) -{ - size_t index; - unsigned int ret; - - index = 0; - ret = 0; - while (s[index] != '\0') - { - while (s[index] == c) + while (string[index] == delim) + { index++; - while (s[index] != '\0' && s[index] != c) + } + if (string[index] != '\0') + { + wordcount++; + } + while (string[index] != delim && string[index] != '\0') + { index++; - if ((s[index] == '\0' && s[index - 1] != c) || s[index] != '\0') - ret++; + } } - return (ret); + return (wordcount); } -static char **ft_insert(char const *s, char **array, char c,\ -unsigned int *judex) +static char *ft_writeword(char const *string, size_t index, char delim) { - while (s[*judex] == c) - *judex = *judex + 1; - if (s[*judex] != '\0') + size_t len; + char *ret; + + len = index; + while (string[len] != delim && string[len] != '\0') + len++; + ret = (char *)malloc(sizeof(char) * ((len - index) + 1)); + if (!ret) + return (NULL); + ret[len - index] = '\0'; + len = 0; + while (string[index] != delim && string[index] != '\0') { - array[0] = ft_strsub(s, *judex, ft_wordskip(&s[*judex], c)); - if (array[0] == NULL) - { - while (*array != NULL) - { - ft_strclr(*array); - ft_strdel(&*array); - array++; - } - return (NULL); - } - *judex = *judex + ft_wordskip(&s[*judex], c); + ret[len] = string[index]; + index++; + len++; } - return (array); + ret[len] = '\0'; + return (ret); } -char **ft_strsplit(char const *s, char c) +char **ft_strsplit(char const *string, char delim) { - char **ret; - unsigned int wordcount; - unsigned int index; - unsigned int judex; + char **ret; + size_t index; + size_t wordcount; + size_t wordindex; index = 0; - judex = 0; - wordcount = ft_wordcount(s, c); - ret = (char**)malloc(sizeof(char*) * (wordcount + 1)); - if (ret == NULL) + wordcount = ft_wordcount(string, delim); + wordindex = 0; + ret = (char **)malloc(sizeof(char *) * (wordcount + 1)); + if (!ret) return (NULL); - while (index < wordcount) + ret[wordcount] = NULL; + while (string[index] != '\0' && wordindex < wordcount) { - if (ft_insert(s, &ret[index], c, &judex) == NULL) - { - free(ret); - ret = NULL; - return (NULL); - } - index++; + while (string[index] == delim) + index++; + if (string[index] != '\0') + ret[wordindex] = ft_writeword(string, index, delim); + wordindex++; + while (string[index] != delim && string[index] != '\0') + index++; } - ret[index] = (NULL); return (ret); } diff --git a/libft/ft_strsplit_t_s.c b/libft/ft_strsplit_t_s.c new file mode 100644 index 0000000..89f67df --- /dev/null +++ b/libft/ft_strsplit_t_s.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strsplit_t_s.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/01/21 18:29:22 by lgutter #+# #+# */ +/* Updated: 2020/02/10 16:51:47 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_is_tab_space(int c) +{ + if (c == ' ' || c == '\t') + { + return (1); + } + return (0); +} + +static size_t ft_wordcount_t_s(char const *string) +{ + size_t index; + size_t wordcount; + + index = 0; + wordcount = 0; + while (string[index] != '\0') + { + while (ft_is_tab_space(string[index]) == 1) + { + index++; + } + if (string[index] != '\0') + { + wordcount++; + } + while (ft_is_tab_space(string[index]) == 0 && string[index] != '\0') + { + index++; + } + } + return (wordcount); +} + +static char *ft_writeword_t_s(char const *string, size_t index) +{ + size_t len; + char *ret; + + len = index; + while (ft_is_tab_space(string[len]) == 0 && string[len] != '\0') + len++; + ret = (char *)ft_memalloc(sizeof(char) * len + 1); + if (ret == NULL) + return (NULL); + len = 0; + while (ft_is_tab_space(string[index]) == 0 && string[index] != '\0') + { + ret[len] = string[index]; + index++; + len++; + } + ret[len] = '\0'; + return (ret); +} + +char **ft_strsplit_t_s(char const *string) +{ + char **ret; + size_t index; + size_t wordcount; + size_t wordindex; + + index = 0; + wordcount = ft_wordcount_t_s(string); + wordindex = 0; + ret = (char **)ft_memalloc(sizeof(char *) * (wordcount + 1)); + if (ret == NULL) + return (NULL); + ret[wordcount] = NULL; + while (string[index] != '\0' && wordindex < wordcount) + { + while (ft_is_tab_space(string[index]) == 1) + index++; + if (string[index] != '\0') + ret[wordindex] = ft_writeword_t_s(string, index); + wordindex++; + while (ft_is_tab_space(string[index]) == 0 && string[index] != '\0') + index++; + } + return (ret); +} diff --git a/libft/ft_strstr.c b/libft/ft_strstr.c index 01a7fbd..eb53ce8 100644 --- a/libft/ft_strstr.c +++ b/libft/ft_strstr.c @@ -3,33 +3,49 @@ /* :::::::: */ /* ft_strstr.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/18 14:27:21 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:30:55 by aholster ######## odam.nl */ +/* Created: 2019/01/31 11:51:53 by lgutter #+# #+# */ +/* Updated: 2019/01/31 11:51:54 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strstr(char const *haystack, char const *needle) +static int check_word(const char *string, const char *word) { - size_t index; - size_t judex; + size_t index; index = 0; - if (needle[0] == '\0') - return ((char *)haystack); - while (haystack[index] != '\0') + while (word[index] != '\0') { - judex = 0; - while (needle[judex] == haystack[index + judex] &&\ - haystack[index + judex] != '\0') + if (word[index] != string[index]) { - judex++; + return (0); + } + index++; + } + return (1); +} + +char *ft_strstr(const char *string, const char *word) +{ + size_t index; + + index = 0; + if (string == NULL || word == NULL || word[0] == '\0') + { + return ((char *)string); + } + while (string[index] != '\0') + { + if (string[index] == word[0]) + { + if (check_word(&string[index], word) == 1) + { + return ((char *)&string[index]); + } } - if (needle[judex] == '\0') - return ((char *)&haystack[index]); index++; } return (NULL); diff --git a/libft/ft_strsub.c b/libft/ft_strsub.c index e38aa90..f5d851e 100644 --- a/libft/ft_strsub.c +++ b/libft/ft_strsub.c @@ -3,29 +3,30 @@ /* :::::::: */ /* ft_strsub.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/14 19:18:45 by aholster #+# #+# */ -/* Updated: 2019/01/30 14:31:05 by aholster ######## odam.nl */ +/* Created: 2019/01/17 17:15:42 by lgutter #+# #+# */ +/* Updated: 2019/01/17 17:15:44 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strsub(char const *s, unsigned int start, size_t len) +char *ft_strsub(char const *string, unsigned int start, size_t len) { - char *str; + char *ret; size_t index; index = 0; - str = (char *)malloc(sizeof(char) * (len + 1)); - if (str == NULL) + ret = (char *)malloc(sizeof(char) * (len + 1)); + if (!ret) return (NULL); while (index < len) { - str[index] = s[start + index]; + ret[index] = string[start]; index++; + start++; } - str[index] = '\0'; - return (str); + ret[index] = '\0'; + return (ret); } diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c index 3b8ac88..fbec95e 100644 --- a/libft/ft_strtrim.c +++ b/libft/ft_strtrim.c @@ -3,31 +3,38 @@ /* :::::::: */ /* ft_strtrim.c :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/16 16:07:57 by aholster #+# #+# */ -/* Updated: 2019/01/30 17:14:58 by aholster ######## odam.nl */ +/* Created: 2019/01/19 14:47:49 by lgutter #+# #+# */ +/* Updated: 2020/02/07 14:32:50 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strtrim(char const *s) +char *ft_strtrim(char const *str) { - unsigned int index; - size_t judex; - char *str; + size_t end; + size_t index; + char *ret; + int empty; index = 0; - while ((s[index] == ' ' || s[index] == '\t' || s[index] == '\n')\ - && s[index] != '\0') + empty = 1; + while (str[index] == '\n' || str[index] == '\t' || str[index] == ' ') index++; - judex = (ft_strlen(s) - 1); - while ((s[judex] == ' ' || s[judex] == '\t' || s[judex] == '\n')\ - && judex >= index) - judex--; - str = ft_strsub(s, index, (judex - index) + 1); - if (str == NULL) + end = ft_strlen(str); + if (end > 0) + end--; + while ((str[end] == '\n' || str[end] == '\t' || str[end] == ' ') && end > 0) + end--; + if (end == 0 && (str[end] == '\n' || str[end] == '\t' || str[end] == ' ')) + { + index = 0; + empty = 0; + } + ret = ft_strsub(str, (unsigned int)index, (end - index) + empty); + if (!ret) return (NULL); - return (str); + return (ret); } diff --git a/libft/ft_swap.c b/libft/ft_swap.c index 5cee2ca..1c3634a 100644 --- a/libft/ft_swap.c +++ b/libft/ft_swap.c @@ -5,7 +5,7 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/01/31 22:23:56 by aholster #+# #+# */ +/* Created: 2019/01/31 22:23:56 by aholster #+# #+# */ /* Updated: 2019/02/01 21:25:54 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/libft/ft_textangle.c b/libft/ft_textangle.c deleted file mode 100644 index 142d8fc..0000000 --- a/libft/ft_textangle.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_textangle.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/03/24 18:02:21 by aholster #+# #+# */ -/* Updated: 2019/03/25 15:12:47 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char **ft_textangle(size_t x, size_t y, char c) -{ - char **rectangle; - size_t index; - - index = 0; - rectangle = (char **)malloc(sizeof(char *) * y + 1); - if (rectangle == NULL) - return (NULL); - rectangle[y] = NULL; - while (index < y) - { - rectangle[index] = ft_stralloc(x, c); - if (rectangle[index] == NULL) - { - ft_strarrdel(&rectangle); - return (NULL); - } - index++; - } - return (rectangle); -} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c index c2ea0b9..9f23497 100644 --- a/libft/ft_tolower.c +++ b/libft/ft_tolower.c @@ -5,7 +5,7 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/01/13 15:55:17 by aholster #+# #+# */ +/* Created: 2019/01/13 15:55:17 by aholster #+# #+# */ /* Updated: 2019/02/01 21:25:54 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c index 8cb56b3..e82177c 100644 --- a/libft/ft_toupper.c +++ b/libft/ft_toupper.c @@ -5,7 +5,7 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/01/13 15:55:12 by aholster #+# #+# */ +/* Created: 2019/01/13 15:55:12 by aholster #+# #+# */ /* Updated: 2019/02/01 21:23:26 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/libft/ft_ulltoa_base_low.c b/libft/ft_ulltoa_base_low.c new file mode 100644 index 0000000..8ed5bfb --- /dev/null +++ b/libft/ft_ulltoa_base_low.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_ulltoa_base_low.c :+: :+: */ +/* +:+ */ +/* By: ivan-tey +#+ */ +/* +#+ */ +/* Created: 2019/11/14 14:29:16 by ivan-tey #+# #+# */ +/* Updated: 2019/11/20 17:15:04 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *ft_convert(unsigned long long n, \ + const unsigned base, \ + char *str, size_t i) +{ + int res; + char *bstr; + + res = 0; + bstr = "0123456789abcdefghijklmnopqrstuvqxyz"; + while (base == 1 && (i <= ft_nbrlenbase_ull(n, base))) + { + str[i] = '1'; + i++; + if (i == ft_nbrlenbase_ull(n, base) - 1) + return (str); + } + if (n > 0) + { + res = n % base; + str[i] = bstr[res]; + i++; + ft_convert(n / base, base, str, i); + } + return (str); +} + +char *ft_ulltoa_base_low(unsigned long long nb, const unsigned base) +{ + int nb_len; + char *str; + + nb_len = 0; + if (base <= 0) + { + return (NULL); + } + if (nb == 0) + { + return (ft_strdup("0")); + } + nb_len = ft_nbrlenbase_ull(nb, base); + str = (char *)ft_strnew(sizeof(char) * (nb_len + 1)); + if (str != NULL) + { + str = ft_convert(nb, base, str, 0); + str[nb_len] = '\0'; + str = ft_strrev(str, 0); + } + return (str); +} diff --git a/libft/ft_ulltoa_base_upp.c b/libft/ft_ulltoa_base_upp.c new file mode 100644 index 0000000..cf56c24 --- /dev/null +++ b/libft/ft_ulltoa_base_upp.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_ulltoa_base_upp.c :+: :+: */ +/* +:+ */ +/* By: ivan-tey +#+ */ +/* +#+ */ +/* Created: 2019/10/21 15:14:42 by ivan-tey #+# #+# */ +/* Updated: 2019/11/20 17:14:53 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *ft_convert(unsigned long long n, \ + const unsigned base, \ + char *str, size_t i) +{ + int res; + char *bstr; + + res = 0; + bstr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + while (base == 1 && (i <= ft_nbrlenbase_ull(n, base))) + { + str[i] = '1'; + i++; + if (i == ft_nbrlenbase_ull(n, base) - 1) + return (str); + } + if (n > 0) + { + res = n % base; + str[i] = bstr[res]; + i++; + ft_convert(n / base, base, str, i); + } + return (str); +} + +char *ft_ulltoa_base_upp(unsigned long long nb, const unsigned base) +{ + int nb_len; + char *str; + + nb_len = 0; + if (base <= 0) + { + return (NULL); + } + if (nb == 0) + { + return (ft_strdup("0")); + } + nb_len = ft_nbrlenbase_ull(nb, base); + str = (char *)ft_strnew(sizeof(char) * (nb_len + 1)); + if (str != NULL) + { + str = ft_convert(nb, base, str, 0); + str[nb_len] = '\0'; + str = ft_strrev(str, 0); + } + return (str); +} diff --git a/libft/get_next_line.c b/libft/get_next_line.c new file mode 100644 index 0000000..5745b26 --- /dev/null +++ b/libft/get_next_line.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* get_next_line.c :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/02/14 14:37:52 by lgutter #+# #+# */ +/* Updated: 2020/02/04 18:17:35 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +static ssize_t cache_pull(int fd, t_cache **start, char **line, size_t *size) +{ + t_cache *current; + t_cache *previous; + + if (*start == NULL || (*start)->content == NULL || read(fd, line, 0) == -1) + return (-1); + current = *start; + previous = current; + while (current->next != NULL && current->fd != fd) + { + previous = current; + current = current->next; + } + if (current->fd != fd && current->next == NULL) + return (-1); + *line = ft_memdup(current->content, current->size); + *size = current->size; + if (previous != current) + previous->next = current->next; + free(current->content); + if (current == *start && current->next == NULL) + *start = NULL; + else if (current == *start && current->next != NULL) + *start = current->next; + free(current); + return (1); +} + +static int list_add_new(int fd, t_cache **start, char *buf, + ssize_t i) +{ + t_cache *new; + + if (buf == NULL || (ssize_t)ft_strlenc(buf, '\n', i - 1) >= (i - 1)) + return (0); + new = (t_cache *)malloc(sizeof(t_cache) * 1); + if (new == NULL) + return (-1); + new->fd = fd; + if (ft_memchr(buf, '\n', i) != NULL) + { + new->size = i - 1 - ft_strlenc(buf, '\n', i); + new->content = ft_memdup(&buf[ft_strlenc(buf, '\n', i) + 1], new->size); + } + else + { + new->size = i - 1; + new->content = ft_memdup(buf, new->size); + } + new->next = *start; + *start = new; + return (1); +} + +static int finalize(int fd, t_cache **start, char **line, size_t size) +{ + if (size == 0) + { + ft_memexpand((void **)line, &size, "\0", 1); + return (0); + } + if (list_add_new(fd, start, *line, size) == -1) + { + return (-1); + } + if (ft_memchr(*line, '\n', size) != NULL) + size = ft_strlenc(*line, '\n', size); + ft_memexpand((void **)line, &size, "\0", 1); + return (size); +} + +int get_next_line(const int fd, char **line) +{ + static t_cache *start = NULL; + ssize_t ret; + char *buffer; + size_t size; + + buffer = (char *)malloc(sizeof(char) * BUFF_SIZE); + if (buffer == NULL || line == NULL || read(fd, buffer, 0) == -1) + return (-1); + *line = NULL; + size = 0; + ret = cache_pull(fd, &start, line, &size); + if (line == NULL) + return (-1); + while (ft_memchr(*line, '\n', size) == NULL && ret != 0) + { + ret = read(fd, buffer, BUFF_SIZE); + ft_memexpand((void **)line, &size, buffer, (size_t)ret); + } + ft_memdel((void **)&buffer); + return (finalize(fd, &start, line, size)); +} diff --git a/libft/get_next_line.h b/libft/get_next_line.h new file mode 100644 index 0000000..70c4e65 --- /dev/null +++ b/libft/get_next_line.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* get_next_line.h :+: :+: */ +/* +:+ */ +/* By: lgutter +#+ */ +/* +#+ */ +/* Created: 2019/02/14 14:37:43 by lgutter #+# #+# */ +/* Updated: 2019/02/14 14:37:44 by lgutter ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# include "libft.h" + +# define BUFF_SIZE 32 + +typedef struct s_cache +{ + int fd; + char *content; + size_t size; + struct s_cache *next; +} t_cache; + +int get_next_line(const int fd, char **line); + +#endif diff --git a/libft/libft.h b/libft/libft.h index 5c4fab0..e1f1036 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -3,18 +3,21 @@ /* :::::::: */ /* libft.h :+: :+: */ /* +:+ */ -/* By: aholster +#+ */ +/* By: lgutter +#+ */ /* +#+ */ -/* Created: 2019/01/12 18:16:38 by aholster #+# #+# */ -/* Updated: 2019/09/09 21:21:34 by lgutter ######## odam.nl */ +/* Created: 2019/01/10 17:29:34 by lgutter #+# #+# */ +/* Updated: 2020/02/04 19:18:57 by lgutter ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef LIBFT_H # define LIBFT_H + # include -# include # include +# include +# include +# include "get_next_line.h" typedef struct s_list { @@ -23,118 +26,153 @@ typedef struct s_list struct s_list *next; } t_list; -/* -** mandatory functions -*/ -int ft_atoi(char const *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); -char *ft_itoa(int n); -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)); -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_lstnew(void const *content, size_t content_size); -void *ft_memalloc(size_t size); -void *ft_memccpy(void *dst, const void *src, int c, size_t n); -void *ft_memchr(void const *s, int c, size_t n); -int ft_memcmp(void const *s1, void const *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, void const *src, size_t len); -void *ft_memset(void *b, int c, size_t len); -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_fd(int n, int fd); -void ft_putnbr(int n); -void ft_putstr(char const *s); -void ft_putstr_fd(char const *s, int fd); -char *ft_strcat(char *s1, char const *s2); -char *ft_strchr(char const *s, int c); -void ft_strclr(char *s); -int ft_strcmp(char const *s1, char const *s2); -int ft_strncmp(char const *s1, char const *s2, size_t n); +struct s_ft_getopt +{ + char *arg; + int index; + int group_index; + char opt; + bool illegal; +}; + +void *ft_memset(void *target, int setvalue, size_t len); +void ft_bzero(void *input, size_t len); +void *ft_memcpy(void *dst, const void *src, size_t len); +void *ft_memccpy(void *dst, const void *src, int delim, + size_t len); +void *ft_memmove(void *dst, const void *src, size_t len); +void *ft_memchr(const void *source, int character, size_t len); +int ft_memcmp(const void *source1, const void *source2, + size_t len); +size_t ft_strlen(const char *string); +char *ft_strdup(const char *string); +char *ft_strcdup(const char *string, char delim); char *ft_strcpy(char *dst, const char *src); char *ft_strncpy(char *dst, const char *src, size_t len); -void ft_strdel(char **as); -char *ft_strdup(char const *s1); -int ft_strequ(char const *s1, char const *s2); -int ft_strnequ(char const *s1, char const *s2, size_t n); -void ft_striter(char *s, void (*f)(char *)); -void ft_striteri(char *s, void (*f)(unsigned int, char *)); -char *ft_strjoin(char const *s1, char const *s2); -size_t ft_strlcat(char *dst, char const *src, size_t size); -size_t ft_strlen(char const *s); -char *ft_strmap(char const *s, char (*f)(char)); -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); -char *ft_strnew(size_t size); -char *ft_strncat(char *s1, char const *s2, size_t n); -char *ft_strnstr(char const *haystack, char const *needle,\ -size_t len); -char *ft_strrchr(char const *s, int c); -char **ft_strsplit(char const *s, char c); -char *ft_strstr(char const *haystack, char const *needle); -char *ft_strsub(char const *s, unsigned int start, size_t len); -char *ft_strtrim(char const *s); -int ft_tolower(int c); -int ft_toupper(int c); - -// advanced math - -unsigned int ft_isprime(unsigned int num); -int ft_power(int num, unsigned int power); -int ft_sqrt(int nb); -int ft_factorial(int nb); - -// simple math - -char *ft_itoba(int n, unsigned int base); -unsigned int ft_nbrlen(long long n, unsigned int base); -int ft_max(int val1, int val2); -int ft_min(int val1, int val2); -int ft_constrain(int in, int min, int max); -long long ft_absneg(long long num); -int *ft_range(int min, int max); - -// lst handling +char *ft_strcat(char *string1, const char *string2); +char *ft_strncat(char *string1, const char *string2, size_t len); +size_t ft_strlcat(char *dst, const char *src, size_t size); +char *ft_strchr(const char *string, int character); +char *ft_strrchr(const char *string, int character); +char *ft_strstr(const char *string, const char *word); +char *ft_strnstr(const char *string, const char *word, + size_t len); +int ft_strcmp(const char *string1, const char *string2); +int ft_strncmp(const char *string1, const char *string2, + size_t len); +int ft_atoi(const char *string); +long long ft_atoll(const char *string); +int ft_intlen(int n); +int ft_isalpha(int character); +int ft_isdigit(int character); +int ft_isalnum(int character); +int ft_isascii(int character); +int ft_isprint(int character); +int ft_isspace(int character); +int ft_toupper(int character); +int ft_tolower(int character); -void ft_del(void *data, size_t size); -void ft_lstaddend(t_list **lst, t_list *new); -unsigned int ft_lstleng(t_list *lst); -void ft_lsttardest(t_list **lst, t_list **target,\ -void (*del)(void *, size_t)); -char **ft_lsttostrarr(t_list *lst); -t_list *ft_strarrtolst(char **strarr); - -// str handling - -void ft_putstrarr(char **strarr); -void ft_putstrarr_fd(char **strarr, int fd); -void ft_strarrdel(char ***ap); -char **ft_strarrnew(size_t size); -char **ft_textangle(size_t x, size_t y, char c); -char *ft_stralloc(size_t size, char c); +void *ft_memalloc(size_t size); +void ft_memdel(void **pointer); +char *ft_strnew(size_t size); +void ft_strdel(char **string); +void ft_strclr(char *string); +void ft_striter(char *string, void (*function)(char *)); +void ft_striteri(char *string, + void (*function)(unsigned int, char *)); +char *ft_strmap(char const *string, char (*function)(char)); +char *ft_strmapi(char const *string, + char (*function)(unsigned int, char)); +int ft_strequ(char const *string1, char const *string2); +int ft_strnequ(char const *str1, char const *str2, size_t len); +char *ft_strsub(char const *string, unsigned int start, + size_t len); +char *ft_strjoin(char const *string1, char const *string2); +char *ft_strtrim(char const *str); +char **ft_strsplit(char const *string, char delim); +char **ft_strsplit_t_s(char const *string); +char *ft_itoa(int integer); +void ft_putchar(char character); +void ft_putstr(char const *string); +void ft_putendl(char const *string); +void ft_putnbr(int number); +void ft_putchar_fd(char character, int filedes); +void ft_putstr_fd(char const *string, int filedes); +void ft_putendl_fd(char const *string, int filedes); +void ft_putnbr_fd(int number, int filedes); -// mem handling +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 **start, void (*del)(void *, size_t)); +void ft_lstadd(t_list **start, t_list *new); +void ft_lstiter(t_list *list, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *list, t_list *(*f)(t_list *elem)); + +long ft_absneg(long integer); +long ft_abspos(long integer); +int ft_iswhitespace(char character); +size_t ft_strlenc(const char *string, int delim, size_t size); +void *ft_memrcpy(void *dst, const void *src, size_t len); +void *ft_memdup(const void *source, size_t len); +void *ft_memcdup(const void *source, int delim, size_t size); +void *ft_memjoin(const void *str1, size_t size1, + const void *str2, size_t size2); +void ft_memexpand(void **src, size_t *size1, const void *add, + size_t size2); +void ft_memdelsize(void *pointer, size_t size); +size_t ft_nbrlenbase(long long number, int base); +size_t ft_lstlen(t_list **start); +void ft_lstaddback(t_list **start, t_list *new); +t_list *ft_lstaddnext(t_list *current, t_list *new); + +void ft_strexpand(char **source, const char *addition); +void ft_swap(char *a, char *b); +char *ft_strrev(char *str, int start); +char *ft_itoa_base(signed long long nb, const unsigned int base); +char *ft_ulltoa_base_upp(\ + unsigned long long nb, const unsigned base); +char *ft_ulltoa_base_low(\ + unsigned long long nb, const unsigned base); +size_t ft_nbrlenbase_ull(unsigned long long number, int base); +char *ft_strcharjoin(char *str, char c); +void ft_strcharexpand(char **source, const char addition); +bool ft_getopt(struct s_ft_getopt *opt, int argc, char **argv, + const char *optstring); -void *ft_memjoin(void *mem1, size_t size1, void *mem2,\ -size_t size2); -void *ft_memdup(void *src, size_t len); +/* +** Arguments: +** char **pointer_array: array of character pointers, NULL delimited. +** Returns: +** the amount of character pointers in the array. +*/ +size_t ft_str_arr_len(char **pointer_array); -void ft_bitprint(const void *addr, size_t size); +/* +** Arguments: +** char *string: string to be duplicated (partially). +** size_t len: the amount of characters to be copied. +** Returns: +** a fresh copy of the string up until len. +*/ +char *ft_strndup(const char *string, size_t len); -// utility +/* +** Arguments: +** char **array: the array of strings to be freed. +** frees every char * in the array, by iterating over it until it finds NULL. +** Returns: +** the amount of strings that were found and freed. +*/ +size_t ft_free_str_array(char **array); -int ft_iswhitespace(int c); -void ft_swap(int *a, int *b); -void ft_segdefault(void); -int ft_count_if(char **tab, int (*f)(char*)); +/* +** Arguments: +** char **array: the array of strings to be printed. +** prints every string in the array, followed by a newline. +** Returns: +** 0 on succes. +** -1 on failure. +*/ +int ft_print_str_array(char **array); #endif diff --git a/libft/libftsources b/libft/libftsources new file mode 100644 index 0000000..5e74113 --- /dev/null +++ b/libft/libftsources @@ -0,0 +1,90 @@ +LFTSOURCES := ft_memset \ +ft_bzero \ +ft_memcpy \ +ft_memccpy \ +ft_memmove \ +ft_memchr \ +ft_memcmp \ +ft_strlen \ +ft_str_arr_len \ +ft_strdup \ +ft_strcdup \ +ft_strndup \ +ft_strcpy \ +ft_strncpy \ +ft_strcat \ +ft_strncat \ +ft_strlcat \ +ft_strchr \ +ft_strrchr \ +ft_strstr \ +ft_strnstr \ +ft_strcmp \ +ft_strncmp \ +ft_atoi \ +ft_atoll \ +ft_intlen \ +ft_isalpha \ +ft_isdigit \ +ft_isalnum \ +ft_isascii \ +ft_isprint \ +ft_isspace \ +ft_toupper \ +ft_tolower \ +ft_memalloc \ +ft_memdel \ +ft_strnew \ +ft_strdel \ +ft_strclr \ +ft_striter \ +ft_striteri \ +ft_strmap \ +ft_strmapi \ +ft_strequ \ +ft_strnequ \ +ft_strsub \ +ft_strjoin \ +ft_strtrim \ +ft_strsplit \ +ft_strsplit_t_s \ +ft_itoa \ +ft_putchar \ +ft_putstr \ +ft_putendl \ +ft_putnbr \ +ft_putchar_fd \ +ft_putstr_fd \ +ft_putendl_fd \ +ft_putnbr_fd \ +ft_lstnew \ +ft_lstdelone \ +ft_lstdel \ +ft_lstadd \ +ft_lstiter \ +ft_lstmap \ +ft_absneg \ +ft_abspos \ +ft_iswhitespace \ +ft_strlenc \ +ft_memrcpy \ +ft_memdup \ +ft_memcdup \ +ft_memjoin \ +ft_memexpand \ +ft_memdelsize \ +ft_nbrlenbase \ +ft_lstlen \ +ft_lstaddback \ +ft_lstaddnext \ +get_next_line\ +ft_free_str_array\ +ft_strcharjoin \ +ft_getopt \ +ft_strexpand \ +ft_strrev \ +ft_swap \ +ft_itoa_base \ +ft_ulltoa_base_upp \ +ft_ulltoa_base_low \ +ft_nbrlenbase_ull diff --git a/main.c b/main.c index 36303d9..7966b3d 100644 --- a/main.c +++ b/main.c @@ -22,7 +22,7 @@ int main(int argc, char **argv) if (fd >= 0) fillit(fd); else - ft_error(); + ft_error("error opening source_file!"); } else ft_putendl("usage: ./fillit source_file"); diff --git a/read_tet.c b/parse_tet.c similarity index 83% rename from read_tet.c rename to parse_tet.c index da6dc74..487743a 100644 --- a/read_tet.c +++ b/parse_tet.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* read_tet.c :+: :+: */ +/* parse_tet.c :+: :+: */ /* +:+ */ /* By: lgutter +#+ */ /* +#+ */ @@ -10,14 +10,16 @@ /* */ /* ************************************************************************** */ -int read_tet(unsigned int *tet, char *buff) +#include "fillit.h" + +int parse_tet(t_tet_data *tet, char *buff) { - short i; - short hash; + short i; + short hash; i = 0; hash = 0; - if (buff[19] != '\n') + if (ft_strlen(buff) < 20 || buff[19] != '\n') return (-1); while (i < 20) { @@ -34,5 +36,5 @@ int read_tet(unsigned int *tet, char *buff) } if (hash != 4) return (-1); - return (1); + return (check_tet(tet)); } diff --git a/place_tet.c b/place_tet.c index c224176..3a20917 100644 --- a/place_tet.c +++ b/place_tet.c @@ -5,14 +5,14 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/03/22 16:51:36 by lgutter #+# #+# */ +/* Created: 2019/03/22 16:51:36 by lgutter #+# #+# */ /* Updated: 2019/03/31 19:09:10 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ #include "fillit.h" -static void place_final(unsigned int tet, unsigned short *map) +static void place_final(t_tet_data tet, unsigned short *map) { unsigned char y; unsigned char x; @@ -31,32 +31,32 @@ static void place_final(unsigned int tet, unsigned short *map) map[y] = map[y] | (1 << x); } -static int check_hash(unsigned int tet, unsigned short *map,\ +static int check_hash(t_tet_data tet, unsigned short *map,\ unsigned short di) { unsigned char y; unsigned char x; - y = (((tet >> 16) & 255) + ((tet >> (0) & 3))); + y = (((tet >> 16) & MASKBYTE) + ((tet >> (0) & 3))); x = ((tet >> 24) + ((tet >> ((0) + 2)) & 3)); if (x >= di || y >= di || ((map[y] >> x) & 1) != 0) return (-1); - y = (((tet >> 16) & 255) + ((tet >> (12) & 3))); + y = (((tet >> 16) & MASKBYTE) + ((tet >> (12) & 3))); x = ((tet >> 24) + ((tet >> ((12) + 2)) & 3)); if (x >= di || y >= di || ((map[y] >> x) & 1) != 0) return (-1); - y = (((tet >> 16) & 255) + ((tet >> (8) & 3))); + y = (((tet >> 16) & MASKBYTE) + ((tet >> (8) & 3))); x = ((tet >> 24) + ((tet >> ((8) + 2)) & 3)); if (x >= di || y >= di || ((map[y] >> x) & 1) != 0) return (-1); - y = (((tet >> 16) & 255) + ((tet >> (4) & 3))); + y = (((tet >> 16) & MASKBYTE) + ((tet >> (4) & 3))); x = ((tet >> 24) + ((tet >> ((4) + 2)) & 3)); if (x >= di || y >= di || ((map[y] >> x) & 1) != 0) return (-1); return (1); } -int place_tet(unsigned int *tet, unsigned short *map,\ +int place_tet(t_tet_data *tet, unsigned short *map,\ unsigned short di) { while (check_hash(*tet, map, di) != 1) diff --git a/print_result.c b/print_result.c index 4d54e45..9b1f1f7 100644 --- a/print_result.c +++ b/print_result.c @@ -5,35 +5,35 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/03/25 15:37:06 by aholster #+# #+# */ +/* Created: 2019/03/25 15:37:06 by aholster #+# #+# */ /* Updated: 2019/04/01 15:00:02 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ #include "fillit.h" -static void ft_converttet(unsigned int tet, unsigned int index,\ +static void ft_converttet(t_tet_data tet, unsigned short index,\ char arr[16][16]) { unsigned short offy; unsigned short offx; unsigned char offset; - offy = (tet >> 16) & MASKBY; + offy = (tet >> 16) & MASKBYTE; offx = (tet >> 24); offset = 0; while (offset <= 12) { - arr[offx + ((tet >> (offset + 2)) & MASK2B)]\ - [offy + ((tet >> offset) & MASK2B)] = 'A' + index; + arr[offx + ((tet >> (offset + 2)) & MASK2BIT)]\ + [offy + ((tet >> offset) & MASK2BIT)] = 'A' + index; offset += 4; } } static void ft_initsquare(unsigned short di, char arr[16][16]) { - unsigned int index; - unsigned int sudex; + unsigned short index; + unsigned short sudex; index = 0; while (index < di) @@ -49,10 +49,10 @@ static void ft_initsquare(unsigned short di, char arr[16][16]) } } -void print_result(unsigned int *tet, unsigned short di) +void print_result(t_tet_data *tet, unsigned short di) { char arr[16][16]; - unsigned int index; + unsigned short index; index = 0; ft_initsquare(di, arr); diff --git a/remove_tet.c b/remove_tet.c index 102be05..fe36210 100644 --- a/remove_tet.c +++ b/remove_tet.c @@ -5,14 +5,14 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/03/24 16:59:04 by lgutter #+# #+# */ +/* Created: 2019/03/24 16:59:04 by lgutter #+# #+# */ /* Updated: 2019/03/24 18:45:39 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ #include "fillit.h" -int remove_tet(unsigned int *tet, unsigned short *map, unsigned short di) +int remove_tet(t_tet_data *tet, unsigned short *map, unsigned short di) { unsigned char *offx; unsigned char *offy; @@ -24,7 +24,7 @@ int remove_tet(unsigned int *tet, unsigned short *map, unsigned short di) while (hash < 4) { map[(*offy + ((*tet >> (hash * 4)) & 3))] &= \ - (65535 - (1 << (*offx + ((*tet >> (hash * 4 + 2)) & 3)))); + (MASK2BYTE - (1 << (*offx + ((*tet >> (hash * 4 + 2)) & 3)))); hash++; } return (increment_offset(tet, di)); diff --git a/shift_corner.c b/shift_corner.c index 63013a1..eb539bc 100644 --- a/shift_corner.c +++ b/shift_corner.c @@ -10,21 +10,56 @@ /* */ /* ************************************************************************** */ -void shift_corner(unsigned int *tet) +#include "fillit.h" + +/* +** This function shifts the tet to the top left corner of the virtual grid it +** is stored in. We start by comparing the x position of the first 3 blocks. +** we can get the x position by using modulo 4, as the positions are not +** coordinates but positions as if the 4 rows were one long row. for example: +** with x:2 y: 1 , the position is (4 * y) + x = 6 (coordinates start at 0) +** the fourth block can not possibly be the leftmost block, so we don't need +** to check it. We set the variable "smallest" to the value of the left-most +** block, modulo 4. the modulo 4 automatically shifts it to the top row. +** to make sure we don't shift the block to high (if the 2nd or 3rd was the +** left-most block, the first block will now be in row -1), we set our "temp" +** variable to the value of the first block after rounding it down to the +** nearest multiple of 4 by abusing integer rounding. +** now we add the "temp" variable (now holding the minimum y) to the "smallest" +** variable (holding the minimum x) so we get the amount of positions every +** block needs to be shifted. Then all we need to do is shift every block and +** and them to the "temp" variable, after which we overwrite the original tet +** with our shifted version in "temp". +*/ + +/* +** Defines for the different blocks +*/ +#define FIRST_B 12 +#define SECOND_B 8 +#define THIRD_B 4 + +/* +** defines to clarify calculating with rows +*/ +#define ROW_WIDTH 4 +#define ROW_COUNT 4 + +void shift_corner(t_tet_data *tet) { unsigned short smallest; unsigned short temp; - smallest = (*tet >> 12) % 4; - if (smallest > ((*tet >> 8) & 15) % 4) - smallest = ((*tet >> 8) & 15) % 4; - if (smallest > ((*tet >> 4) & 15) % 4) - smallest = ((*tet >> 4) & 15) % 4; - temp = ((*tet >> 12) / 4) * 4; + smallest = (*tet >> FIRST_B) % ROW_WIDTH; + if (smallest > ((*tet >> SECOND_B) & MASK4BIT) % ROW_WIDTH) + smallest = ((*tet >> SECOND_B) & MASK4BIT) % ROW_WIDTH; + if (smallest > ((*tet >> THIRD_B) & MASK4BIT) % ROW_WIDTH) + smallest = ((*tet >> THIRD_B) & MASK4BIT) % ROW_WIDTH; + temp = ((*tet >> FIRST_B) / ROW_COUNT) * ROW_COUNT; smallest += temp; - temp = ((*tet >> 12) - smallest) << 12; - temp = ((((*tet >> 8) & 15) - smallest) << 8) | temp; - temp = ((((*tet >> 4) & 15) - smallest) << 4) | temp; - temp = ((*tet & 15) - smallest) | temp; + temp = ((*tet >> FIRST_B) - smallest) << FIRST_B; + temp = ((((*tet >> SECOND_B) & MASK4BIT) - smallest) << SECOND_B) | temp; + temp = ((((*tet >> THIRD_B) & MASK4BIT) - smallest) << THIRD_B) | temp; + temp = ((*tet & MASK4BIT) - smallest) | temp; *tet = temp; } diff --git a/smallest_map.c b/smallest_map.c deleted file mode 100644 index b055b15..0000000 --- a/smallest_map.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* smallest_map.c :+: :+: */ -/* +:+ */ -/* By: aholster +#+ */ -/* +#+ */ -/* Created: 2019/03/15 16:26:43 by lgutter #+# #+# */ -/* Updated: 2019/04/01 15:49:33 by aholster ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -unsigned short smallest_map(short tetcount) -{ - if (tetcount < 13) - { - if (tetcount < 5) - return ((tetcount < 4) ? tetcount + 1 : 4); - if (tetcount < 7) - return (5); - return ((tetcount < 10) ? 6 : 7); - } - if (tetcount < 21) - return ((tetcount < 17) ? 8 : 9); - return ((tetcount < 26) ? 10 : 11); -} diff --git a/solver.c b/solver.c index 0134e74..0d43a99 100644 --- a/solver.c +++ b/solver.c @@ -5,68 +5,93 @@ /* +:+ */ /* By: aholster +#+ */ /* +#+ */ -/* Created: 2019/03/22 16:40:34 by aholster #+# #+# */ +/* Created: 2019/03/22 16:40:34 by aholster #+# #+# */ /* Updated: 2019/03/31 19:05:41 by aholster ######## odam.nl */ /* */ /* ************************************************************************** */ #include "fillit.h" -void ft_error(void) -{ - ft_putendl("error"); - exit(-1); -} +/* +** this tiny function is 90% of our speed. essentially, we check if we've +** placed a tetromino with the same shape (for example a square) before, +** by iterating over the already placed tets and comparing the value of the +** first 2 bytes in the t_tet_data, which contains the shape. +** if a value matches, we simply duplicate that previous tet, skipping any +** invalid placements before that position. +*/ -static unsigned int duplicate_shortcut(unsigned int *tet,\ - short curtet, unsigned int current) +static t_tet_data duplicate_shortcut(t_tet_data *tet,\ + short curtet, t_tet_data current) { while (curtet > 0) { - if (current == (tet[curtet - 1] & 65535)) + if (current == (tet[curtet - 1] & MASK2BYTE)) return (tet[curtet - 1]); curtet--; } return (current); } -static unsigned short di_exceptions(unsigned short di,\ - unsigned int *tet, short tetcount) +/* +** Check the count of 3 specific tetromino shapes. If we have a certain amount +** of these, we should start 1 map size larger to save time. +** the unreadable if statement contains the following rules: +** - if the size is less then 8, and we have more bars than size, increase. +** - if size is an odd number, and our squares will take up more space than +** the area we would have if size was 1 smaller, increase. +** - if size is larger than 7, (so 2 bars will fit next to each other), and +** we have more bars than (size * 2), increase. +*/ + +static unsigned short size_exceptions(unsigned short size,\ + t_tet_data *tet, short tetcount) { - short lc; - short fc; - short sc; + short vertical_bars; + short horizontal_bars; + short squares; short i; i = 0; - sc = 0; - lc = 0; - fc = 0; + squares = 0; + vertical_bars = 0; + horizontal_bars = 0; while (i < tetcount) { - if (tet[i] == 1164) - lc++; - if (tet[i] == 291) - fc++; - if (tet[i] == 325) - sc++; + if (tet[i] == VERTICAL_BAR) + vertical_bars++; + if (tet[i] == HORIZONTAL_BAR) + horizontal_bars++; + if (tet[i] == SQUARE) + squares++; i++; } - if ((di < 8 && (di < lc || di < fc)) ||\ - (di % 2 == 1 && (sc * 4) > (di - 1) * (di - 1)) ||\ - (di >= 8 && (di + di < lc || di + di < fc))) - return (di + 1); - return (di); + if ((size < 8 && (size < vertical_bars || size < horizontal_bars)) ||\ + (size % 2 == 1 && (squares * 4) > (size - 1) * (size - 1)) ||\ + (size >= 8 && (size * 2 < vertical_bars || size * 2 < horizontal_bars))) + return (size + 1); + return (size); } -static int solve(unsigned int *tet, short curtet,\ +/* +** The main recursive solve function. first we have a stop condition: when our +** current count of tets is >= tet_count, we print result and exit. +** then we calculate if there is still enough usable space, otherwise we return +** then we check if we placed this shape before, if so, skip to last position. +** then we place the tet in the first available location, and call ourselves +** for the next tet. +** when solve returns, we try to move the tet the a next possible position. +** if there is no next possible position, we return. +*/ + +static int solve(t_tet_data *tet, short curtet,\ unsigned short *map, unsigned short di) { - if (curtet >= (short)tet[26]) + if (curtet >= (short)tet[TET_COUNT]) { print_result(tet, di); } - if (calc_empty(map, di) < ((tet[26] - curtet) * 4)) + if (calc_empty(map, di) < ((tet[TET_COUNT] - curtet) * 4)) { return (0); } @@ -80,23 +105,59 @@ static int solve(unsigned int *tet, short curtet,\ return (0); } -void map_control(unsigned int *tet, short tetcount) +/* +** this table contains the pre-calculated smallest map size based on the amount +** of tetrominoes. The index is the amount of tetrominoes, +** and the value is the size of the square map. so 8 tetrominoes needs a +** minimal size of 6x6. position 0 is simply to avoid needing to translate a +** count to an index, since that adds unneeded obscurity. +*/ +static const unsigned short g_smallest_size[27] = { + [0] = 0, + [1] = 2, + [2] = 3, + [3] = 4, + [4] = 4, + [5] = 5, + [6] = 5, + [7] = 6, + [8] = 6, + [9] = 6, + [10] = 7, + [11] = 7, + [12] = 7, + [13] = 8, + [14] = 8, + [15] = 8, + [16] = 8, + [17] = 9, + [18] = 9, + [19] = 9, + [20] = 9, + [21] = 10, + [22] = 10, + [23] = 10, + [24] = 10, + [25] = 10, + [26] = 11, +}; + +/* +** starts with the smallest possible map that could fit the current amount of +** tetrominoes, and keeps trying to solve with a larger map size until we hit +** the maximum size of 16. +*/ + +void map_control(t_tet_data *tet, short tetcount) { unsigned short di; - unsigned short map[16]; - unsigned short index; + unsigned short map[MAP_SIZE + 2]; - index = 0; - di = smallest_map(tetcount); - di = di_exceptions(di, tet, tetcount); - while (index < 16) - { - map[index] = 0; - index++; - } - while (solve(tet, 0, map, di) == 0 && di < 16) + ft_memset(map, 0, sizeof(map)); + di = size_exceptions(g_smallest_size[tetcount], tet, tetcount); + while (solve(tet, 0, map, di) == 0 && di < MAP_SIZE) { di++; } - ft_error(); + ft_error("Could not solve within 16x16 map!"); }