diff --git a/Makefile b/Makefile index 06d79bb..2528e47 100644 --- a/Makefile +++ b/Makefile @@ -1,41 +1,48 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: yochered +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2018/10/26 13:59:12 by yochered #+# #+# # -# Updated: 2018/11/30 15:03:04 by yochered ### ########.fr # -# # -# **************************************************************************** # - -NAME = libft.a -SRC = ft_memset.c ft_bzero.c ft_memcpy.c ft_memccpy.c ft_memmove.c ft_memchr.c\ - ft_memcmp.c ft_strlen.c ft_strdup.c ft_strcpy.c ft_strncpy.c ft_strcat.c\ - ft_strncat.c ft_strlcat.c ft_strchr.c ft_strrchr.c ft_strstr.c ft_strnstr.c\ - ft_strcmp.c ft_strncmp.c ft_atoi.c ft_isalpha.c ft_isdigit.c ft_isalnum.c\ - ft_isascii.c ft_isprint.c ft_toupper.c ft_tolower.c ft_memalloc.c\ - ft_memdel.c ft_strnew.c ft_strdel.c ft_strclr.c ft_striter.c ft_striteri.c\ - ft_strmap.c ft_strmapi.c ft_strequ.c ft_strnequ.c ft_strsub.c ft_strjoin.c\ - ft_strtrim.c ft_strsplit.c ft_itoa.c ft_putchar.c ft_putstr.c ft_putendl.c\ - ft_putnbr.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c\ - ft_lstnew.c ft_lstdelone.c ft_lstdel.c ft_lstadd.c ft_lstiter.c ft_lstmap.c\ - ft_itoa_base.c ft_lstrev.c ft_cycle_detector.c ft_print_memory.c -BINARY = $(SRC:.c=.o) - -all: $(NAME) - -$(NAME): $(BINARY) - ar rc $(NAME) $(BINARY) - -%.o: %.c - gcc -Wall -W -Werror -c -o $@ $< - -clean: - /bin/rm -f $(BINARY) - -fclean: clean - /bin/rm -f $(NAME) - -re: fclean all +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: yochered +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2018/10/26 13:59:12 by yochered #+# #+# # +# Updated: 2018/12/14 10:04:18 by yochered ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a + +SRC = ft_memset.c ft_bzero.c ft_memcpy.c ft_memccpy.c ft_memmove.c ft_memchr.c\ + ft_memcmp.c ft_strlen.c ft_strdup.c ft_strcpy.c ft_strncpy.c ft_strcat.c\ + ft_strncat.c ft_strlcat.c ft_strchr.c ft_strrchr.c ft_strstr.c ft_strnstr.c\ + ft_strcmp.c ft_strncmp.c ft_atoi.c ft_isalpha.c ft_isdigit.c ft_isalnum.c\ + ft_isascii.c ft_isprint.c ft_toupper.c ft_tolower.c ft_memalloc.c\ + ft_memdel.c ft_strnew.c ft_strdel.c ft_strclr.c ft_striter.c ft_striteri.c\ + ft_strmap.c ft_strmapi.c ft_strequ.c ft_strnequ.c ft_strsub.c ft_strjoin.c\ + ft_strtrim.c ft_strsplit.c ft_count_digits.c ft_itoa.c ft_putchar.c ft_putstr.c\ + ft_putendl.c ft_putnbr.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c\ + ft_putnbr_fd.c ft_lstnew.c ft_lstdelone.c ft_lstdel.c ft_lstadd.c ft_lstiter.c\ + ft_lstmap.c ft_itoa_base.c ft_lstrev.c ft_cycle_detector.c ft_print_memory.c\ + get_next_line.c ft_printf.c $(addprefix ./printf_src/, $(PRINTF)) + +PRINTF = convert_to_str.c handlers.c function_array.c print_int.c\ + print_pointer.c print_str.c str_functions.c mem_funcs.c allowed_symbols.c\ + print_uint.c print_float.c bonus_types.c print_funcs.c + +BINARY = $(SRC:.c=.o) + +all: $(NAME) + +$(NAME): $(BINARY) + ar rc $(NAME) $(BINARY) + +%.o: %.c + gcc -Wall -W -Werror -c -o $@ $< -I ./includes/ + +clean: + /bin/rm -f $(BINARY) + +fclean: clean + /bin/rm -f $(NAME) + +re: fclean all diff --git a/author b/author index 3e13c33..6b800ba 100644 --- a/author +++ b/author @@ -1 +1 @@ -yochered +yochered diff --git a/ft_atoi.c b/ft_atoi.c index c859cb9..53c9492 100644 --- a/ft_atoi.c +++ b/ft_atoi.c @@ -1,49 +1,44 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_atoi.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 11:42:06 by yochered #+# #+# */ -/* Updated: 2018/10/26 11:42:07 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -static int ft_isspace(int c) -{ - unsigned char s; - - s = (unsigned char)c; - if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' - || c == '\r') - return (1); - return (0); -} - -int ft_atoi(const char *str) -{ - int sign; - long res; - - sign = 1; - res = 0; - while (ft_isspace(*str) == 1) - str++; - if (*str == '+' || *str == '-') - { - if (*str == '-') - sign = -1; - str++; - } - while (ft_isdigit(*str) == 1) - { - if (9223372036854775807 - res < *str - 48) - return (sign == -1 ? 0 : -1); - res = res * 10 + (*str - 48); - str++; - } - return (res * sign); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:42:06 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:42:07 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int ft_isspace(int c) +{ + return (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' + || c == '\r'); +} + +int ft_atoi(const char *str) +{ + int sign; + long res; + + sign = 1; + res = 0; + while (ft_isspace(*str) == 1) + str++; + if (*str == '+' || *str == '-') + { + if (*str == '-') + sign = -1; + str++; + } + while (ft_isdigit(*str) == 1) + { + if (9223372036854775807 - res < *str - 48) + return (sign == -1 ? 0 : -1); + res = res * 10 + (*str - 48); + str++; + } + return (res * sign); +} diff --git a/ft_bzero.c b/ft_bzero.c index ddff3f3..8f3ec63 100644 --- a/ft_bzero.c +++ b/ft_bzero.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* bzero.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 12:30:01 by yochered #+# #+# */ -/* Updated: 2018/10/25 12:30:04 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_bzero(void *s, size_t n) -{ - ft_memset(s, 0, n); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 12:30:01 by yochered #+# #+# */ +/* Updated: 2018/10/25 12:30:04 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/ft_count_digits.c b/ft_count_digits.c new file mode 100644 index 0000000..2066407 --- /dev/null +++ b/ft_count_digits.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_count_digits.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/14 10:49:32 by yochered #+# #+# */ +/* Updated: 2018/12/14 10:49:33 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_count_digits(int nbr, int base) +{ + int digits; + + digits = 0; + if (nbr <= 0) + digits++; + while (nbr) + { + digits++; + nbr /= base; + } + return (digits); +} diff --git a/ft_cycle_detector.c b/ft_cycle_detector.c index 8ea318f..61de60c 100644 --- a/ft_cycle_detector.c +++ b/ft_cycle_detector.c @@ -1,36 +1,36 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_cycle_detector.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 17:47:52 by yochered #+# #+# */ -/* Updated: 2018/10/29 17:47:53 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -int ft_cycle_detector(t_list *list) -{ - t_list *hare; - t_list *tortoise; - - if (!list) - return (1); - tortoise = list; - hare = list; - while (1) - { - tortoise = tortoise->next; - if (hare->next != NULL) - hare = hare->next->next; - else - return (0); - if (!hare || !tortoise) - return (0); - if (hare == tortoise) - return (1); - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cycle_detector.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 17:47:52 by yochered #+# #+# */ +/* Updated: 2018/10/29 17:47:53 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_cycle_detector(t_list *list) +{ + t_list *hare; + t_list *tortoise; + + if (!list) + return (1); + tortoise = list; + hare = list; + while (1) + { + tortoise = tortoise->next; + if (hare->next != NULL) + hare = hare->next->next; + else + return (0); + if (!hare || !tortoise) + return (0); + if (hare == tortoise) + return (1); + } +} diff --git a/ft_isalnum.c b/ft_isalnum.c index 2bf268a..2dbcf2f 100644 --- a/ft_isalnum.c +++ b/ft_isalnum.c @@ -1,20 +1,20 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isalnum.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 12:02:27 by yochered #+# #+# */ -/* Updated: 2018/10/26 12:02:28 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -int ft_isalnum(int c) -{ - if (ft_isalpha(c) == 1 || ft_isdigit(c) == 1) - return (1); - return (0); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:02:27 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:02:28 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_isalnum(int c) +{ + if (ft_isalpha(c) == 1 || ft_isdigit(c) == 1) + return (1); + return (0); +} diff --git a/ft_isalpha.c b/ft_isalpha.c index 7165ae8..034db26 100644 --- a/ft_isalpha.c +++ b/ft_isalpha.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isalpha.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 11:47:15 by yochered #+# #+# */ -/* Updated: 2018/10/26 11:47:16 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isalpha(int c) -{ - if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) - return (1); - return (0); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:47:15 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:47:16 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalpha(int c) +{ + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return (1); + return (0); +} diff --git a/ft_isascii.c b/ft_isascii.c index 568ccd4..c458161 100644 --- a/ft_isascii.c +++ b/ft_isascii.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isascii.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 12:13:52 by yochered #+# #+# */ -/* Updated: 2018/10/26 12:13:54 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isascii(int c) -{ - if (c >= 0 && c <= 127) - return (1); - return (0); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:13:52 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:13:54 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/ft_isdigit.c b/ft_isdigit.c index 263e71a..0bd897f 100644 --- a/ft_isdigit.c +++ b/ft_isdigit.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isdigit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 11:59:47 by yochered #+# #+# */ -/* Updated: 2018/10/26 11:59:48 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isdigit(int c) -{ - if (c >= '0' && c <= '9') - return (1); - return (0); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:59:47 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:59:48 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/ft_isprint.c b/ft_isprint.c index 4e1b1a6..7c06166 100644 --- a/ft_isprint.c +++ b/ft_isprint.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isprint.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 12:18:11 by yochered #+# #+# */ -/* Updated: 2018/10/26 12:18:12 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isprint(int c) -{ - if (c >= 32 && c <= 126) - return (1); - return (0); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:18:11 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:18:12 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/ft_itoa.c b/ft_itoa.c index 990861b..735def0 100644 --- a/ft_itoa.c +++ b/ft_itoa.c @@ -1,57 +1,42 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_itoa.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 11:31:35 by yochered #+# #+# */ -/* Updated: 2018/10/24 11:32:05 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include "libft.h" - -static int count_digits(int nbr) -{ - int res; - - res = 0; - if (nbr <= 0) - res++; - while (nbr != 0) - { - nbr /= 10; - res++; - } - return (res); -} - -char *ft_itoa(int n) -{ - char *res; - int len; - int start; - - len = count_digits(n); - start = 0; - res = (char *)malloc((len + 1) * sizeof(char)); - if (!res) - return (NULL); - if (n < 0) - { - start++; - res[0] = '-'; - } - res[len--] = '\0'; - while (len >= start) - { - if (n > 0) - res[len--] = n % 10 + 48; - else - res[len--] = -(n % 10) + 48; - n /= 10; - } - return (res); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 11:31:35 by yochered #+# #+# */ +/* Updated: 2018/10/24 11:32:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_itoa(int n) +{ + char *res; + int len; + int start; + + len = ft_count_digits(n, 10); + start = 0; + res = (char *)malloc((len + 1) * sizeof(char)); + if (!res) + return (NULL); + if (n < 0) + { + start++; + res[0] = '-'; + } + res[len--] = '\0'; + while (len >= start) + { + if (n > 0) + res[len--] = n % 10 + 48; + else + res[len--] = -(n % 10) + 48; + n /= 10; + } + return (res); +} diff --git a/ft_itoa_base.c b/ft_itoa_base.c index fef9801..c67e0bd 100644 --- a/ft_itoa_base.c +++ b/ft_itoa_base.c @@ -1,57 +1,57 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_itoa_base.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 16:17:41 by yochered #+# #+# */ -/* Updated: 2018/10/29 16:17:42 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -static int count_digits(int value, int base) -{ - int digits; - - digits = 0; - if (value < 0 && base == 10) - digits++; - if (value == 0) - digits++; - while (value != 0) - { - digits++; - value /= base; - } - return (digits); -} - -char *ft_itoa_base(int value, int base) -{ - char *res; - char *base_digits; - int digits; - - if (!(base_digits = (char *)malloc(16 * sizeof(char)))) - return (NULL); - base_digits = "0123456789ABCDEF"; - digits = count_digits(value, base); - if (!(res = (char *)malloc((digits + 1) * sizeof(char)))) - return (NULL); - if (value < 0 && base == 10) - res[0] = '-'; - if (value == 0) - res[0] = '0'; - res[digits] = '\0'; - while (value && digits-- > 0) - { - res[digits] = value < 0 ? base_digits[-(value % base)] : - base_digits[(value % base)]; - value /= base; - } - return (res); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 16:17:41 by yochered #+# #+# */ +/* Updated: 2018/10/29 16:17:42 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static int count_digits(int value, int base) +{ + int digits; + + digits = 0; + if (value < 0 && base == 10) + digits++; + if (value == 0) + digits++; + while (value != 0) + { + digits++; + value /= base; + } + return (digits); +} + +char *ft_itoa_base(int value, int base) +{ + char *res; + char *base_digits; + int digits; + + if (!(base_digits = (char *)malloc(16 * sizeof(char)))) + return (NULL); + base_digits = "0123456789ABCDEF"; + digits = count_digits(value, base); + if (!(res = (char *)malloc((digits + 1) * sizeof(char)))) + return (NULL); + if (value < 0 && base == 10) + res[0] = '-'; + if (value == 0) + res[0] = '0'; + res[digits] = '\0'; + while (value && digits-- > 0) + { + res[digits] = value < 0 ? base_digits[-(value % base)] : + base_digits[(value % base)]; + value /= base; + } + return (res); +} diff --git a/ft_lstadd.c b/ft_lstadd.c index 0877a5d..9d38df5 100644 --- a/ft_lstadd.c +++ b/ft_lstadd.c @@ -1,21 +1,21 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstadd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 10:52:34 by yochered #+# #+# */ -/* Updated: 2018/10/29 10:52:35 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_lstadd(t_list **alst, t_list *new) -{ - if (!alst || !new) - return ; - new->next = *alst; - *alst = new; -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 10:52:34 by yochered #+# #+# */ +/* Updated: 2018/10/29 10:52:35 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_lstadd(t_list **alst, t_list *new) +{ + if (!alst || !new) + return ; + new->next = *alst; + *alst = new; +} diff --git a/ft_lstdel.c b/ft_lstdel.c index c09731a..64a5cb7 100644 --- a/ft_lstdel.c +++ b/ft_lstdel.c @@ -1,32 +1,32 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstdel.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 11:04:10 by yochered #+# #+# */ -/* Updated: 2018/10/29 11:04:12 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) -{ - t_list *node; - t_list *next_node; - - if (!alst || !del) - return ; - node = *alst; - while (node) - { - next_node = node->next; - ft_lstdelone(&node, del); - *alst = (*alst)->next; - node = next_node; - } - *alst = NULL; -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 11:04:10 by yochered #+# #+# */ +/* Updated: 2018/10/29 11:04:12 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) +{ + t_list *node; + t_list *next_node; + + if (!alst || !del) + return ; + node = *alst; + while (node) + { + next_node = node->next; + ft_lstdelone(&node, del); + *alst = (*alst)->next; + node = next_node; + } + *alst = NULL; +} diff --git a/ft_lstdelone.c b/ft_lstdelone.c index 484e473..daed829 100644 --- a/ft_lstdelone.c +++ b/ft_lstdelone.c @@ -1,26 +1,26 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstdelone.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 10:34:51 by yochered #+# #+# */ -/* Updated: 2018/10/29 10:34:52 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) -{ - t_list *node; - - if (!alst || !del) - return ; - node = *alst; - del(node->content, node->content_size); - free(node); - *alst = NULL; -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 10:34:51 by yochered #+# #+# */ +/* Updated: 2018/10/29 10:34:52 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) +{ + t_list *node; + + if (!alst || !del) + return ; + node = *alst; + del(node->content, node->content_size); + free(node); + *alst = NULL; +} diff --git a/ft_lstiter.c b/ft_lstiter.c index c0e6f54..cd20cd6 100644 --- a/ft_lstiter.c +++ b/ft_lstiter.c @@ -1,24 +1,24 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstiter.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 11:14:33 by yochered #+# #+# */ -/* Updated: 2018/10/29 11:14:44 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) -{ - if (!lst) - return ; - while (lst) - { - f(lst); - lst = lst->next; - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 11:14:33 by yochered #+# #+# */ +/* Updated: 2018/10/29 11:14:44 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + if (!lst) + return ; + while (lst) + { + f(lst); + lst = lst->next; + } +} diff --git a/ft_lstmap.c b/ft_lstmap.c index e33c31b..2d4844d 100644 --- a/ft_lstmap.c +++ b/ft_lstmap.c @@ -1,27 +1,27 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstmap.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 11:18:59 by yochered #+# #+# */ -/* Updated: 2018/10/29 11:19:00 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) -{ - t_list *new_list; - - if (lst) - { - new_list = f(lst); - new_list->next = ft_lstmap(lst->next, f); - return (new_list); - } - return (NULL); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 11:18:59 by yochered #+# #+# */ +/* Updated: 2018/10/29 11:19:00 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *new_list; + + if (lst) + { + new_list = f(lst); + new_list->next = ft_lstmap(lst->next, f); + return (new_list); + } + return (NULL); +} diff --git a/ft_lstnew.c b/ft_lstnew.c index f0c45d5..cc37c7d 100644 --- a/ft_lstnew.c +++ b/ft_lstnew.c @@ -1,40 +1,40 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstnew.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 09:57:18 by yochered #+# #+# */ -/* Updated: 2018/10/29 09:57:20 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -t_list *ft_lstnew(void const *content, size_t content_size) -{ - t_list *node; - void *data; - size_t size; - - if (!(node = (t_list *)malloc(sizeof(t_list)))) - return (NULL); - data = (void *)content; - size = content_size; - if (!content) - { - node->content = NULL; - node->content_size = 0; - } - else - { - if (!(node->content = malloc(ft_strlen((char *)content)))) - return (NULL); - node->content = ft_memcpy(node->content, data, size); - node->content_size = size; - } - node->next = NULL; - return (node); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 09:57:18 by yochered #+# #+# */ +/* Updated: 2018/10/29 09:57:20 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *node; + void *data; + size_t size; + + if (!(node = (t_list *)malloc(sizeof(t_list)))) + return (NULL); + data = (void *)content; + size = content_size; + if (!content) + { + node->content = NULL; + node->content_size = 0; + } + else + { + if (!(node->content = malloc(ft_strlen((char *)content)))) + return (NULL); + node->content = ft_memcpy(node->content, data, size); + node->content_size = size; + } + node->next = NULL; + return (node); +} diff --git a/ft_lstrev.c b/ft_lstrev.c index 269d4ae..8aa5d1f 100644 --- a/ft_lstrev.c +++ b/ft_lstrev.c @@ -1,33 +1,33 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstrev.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/29 17:13:30 by yochered #+# #+# */ -/* Updated: 2018/10/29 17:13:31 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_lstrev(t_list **begin_list) -{ - t_list *prev; - t_list *cur; - t_list *next; - - if (!begin_list) - return ; - prev = NULL; - cur = *begin_list; - while (cur) - { - next = cur->next; - cur->next = prev; - prev = cur; - cur = next; - } - *begin_list = prev; -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstrev.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 17:13:30 by yochered #+# #+# */ +/* Updated: 2018/10/29 17:13:31 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_lstrev(t_list **begin_list) +{ + t_list *prev; + t_list *cur; + t_list *next; + + if (!begin_list) + return ; + prev = NULL; + cur = *begin_list; + while (cur) + { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + } + *begin_list = prev; +} diff --git a/ft_memalloc.c b/ft_memalloc.c index ddac108..91629ef 100644 --- a/ft_memalloc.c +++ b/ft_memalloc.c @@ -1,25 +1,25 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memalloc.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 15:16:03 by yochered #+# #+# */ -/* Updated: 2018/10/25 11:16:22 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include "libft.h" - -void *ft_memalloc(size_t size) -{ - void *mem_area; - - if (!(mem_area = (unsigned char *)malloc(size * sizeof(unsigned char)))) - return (NULL); - mem_area = (unsigned char *)ft_memset(mem_area, 0, size); - return (mem_area); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 15:16:03 by yochered #+# #+# */ +/* Updated: 2018/10/25 11:16:22 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +void *ft_memalloc(size_t size) +{ + void *mem_area; + + if (!(mem_area = (unsigned char *)malloc(size * sizeof(unsigned char)))) + return (NULL); + mem_area = (unsigned char *)ft_memset(mem_area, 0, size); + return (mem_area); +} diff --git a/ft_memccpy.c b/ft_memccpy.c index 9c39681..7188fef 100644 --- a/ft_memccpy.c +++ b/ft_memccpy.c @@ -1,32 +1,32 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memccpy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 16:03:29 by yochered #+# #+# */ -/* Updated: 2018/10/25 16:03:30 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -void *ft_memccpy(void *dst, const void *src, int c, size_t n) -{ - size_t i; - unsigned char *dst_tmp; - unsigned char *src_tmp; - - i = 0; - dst_tmp = (unsigned char *)dst; - src_tmp = (unsigned char *)src; - while (i < n) - { - dst_tmp[i] = src_tmp[i]; - if (src_tmp[i] == (unsigned char)c) - return (dst + i + 1); - i++; - } - return (NULL); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 16:03:29 by yochered #+# #+# */ +/* Updated: 2018/10/25 16:03:30 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memccpy(void *dst, const void *src, int c, size_t n) +{ + size_t i; + unsigned char *dst_tmp; + unsigned char *src_tmp; + + i = 0; + dst_tmp = (unsigned char *)dst; + src_tmp = (unsigned char *)src; + while (i < n) + { + dst_tmp[i] = src_tmp[i]; + if (src_tmp[i] == (unsigned char)c) + return (dst + i + 1); + i++; + } + return (NULL); +} diff --git a/ft_memchr.c b/ft_memchr.c index 7742025..557212c 100644 --- a/ft_memchr.c +++ b/ft_memchr.c @@ -1,27 +1,27 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memchr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 15:30:22 by yochered #+# #+# */ -/* Updated: 2018/10/25 15:30:23 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -void *ft_memchr(const void *s, int c, size_t n) -{ - unsigned char *str; - - str = (unsigned char *)s; - while (n--) - { - if (*str == (unsigned char)c) - return (str); - str++; - } - return (NULL); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 15:30:22 by yochered #+# #+# */ +/* Updated: 2018/10/25 15:30:23 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memchr(const void *s, int c, size_t n) +{ + unsigned char *str; + + str = (unsigned char *)s; + while (n--) + { + if (*str == (unsigned char)c) + return (str); + str++; + } + return (NULL); +} diff --git a/ft_memcmp.c b/ft_memcmp.c index b6bf7be..b8ea386 100644 --- a/ft_memcmp.c +++ b/ft_memcmp.c @@ -1,30 +1,30 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memcmp.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 15:40:33 by yochered #+# #+# */ -/* Updated: 2018/10/25 15:40:34 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -int ft_memcmp(const void *s1, const void *s2, size_t n) -{ - unsigned char *s1_tmp; - unsigned char *s2_tmp; - - s1_tmp = (unsigned char *)s1; - s2_tmp = (unsigned char *)s2; - while (n--) - { - if (*s1_tmp != *s2_tmp) - return (*s1_tmp - *s2_tmp); - s1_tmp++; - s2_tmp++; - } - return (0); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 15:40:33 by yochered #+# #+# */ +/* Updated: 2018/10/25 15:40:34 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *s1_tmp; + unsigned char *s2_tmp; + + s1_tmp = (unsigned char *)s1; + s2_tmp = (unsigned char *)s2; + while (n--) + { + if (*s1_tmp != *s2_tmp) + return (*s1_tmp - *s2_tmp); + s1_tmp++; + s2_tmp++; + } + return (0); +} diff --git a/ft_memcpy.c b/ft_memcpy.c index f6ee42c..6d15bb8 100644 --- a/ft_memcpy.c +++ b/ft_memcpy.c @@ -1,25 +1,25 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memcpy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 13:45:23 by yochered #+# #+# */ -/* Updated: 2018/10/25 13:45:24 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -void *ft_memcpy(void *dst, const void *src, size_t n) -{ - unsigned char *dst_tmp; - unsigned char *src_tmp; - - dst_tmp = (unsigned char *)dst; - src_tmp = (unsigned char *)src; - while (n--) - *dst_tmp++ = *src_tmp++; - return (dst); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 13:45:23 by yochered #+# #+# */ +/* Updated: 2018/10/25 13:45:24 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + unsigned char *dst_tmp; + unsigned char *src_tmp; + + dst_tmp = (unsigned char *)dst; + src_tmp = (unsigned char *)src; + while (n--) + *dst_tmp++ = *src_tmp++; + return (dst); +} diff --git a/ft_memdel.c b/ft_memdel.c index 55ec587..68ec0f8 100644 --- a/ft_memdel.c +++ b/ft_memdel.c @@ -1,23 +1,23 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memdel.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 17:40:50 by yochered #+# #+# */ -/* Updated: 2018/10/24 17:40:51 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -void ft_memdel(void **ap) -{ - if (ap) - { - free(*ap); - *ap = NULL; - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 17:40:50 by yochered #+# #+# */ +/* Updated: 2018/10/24 17:40:51 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void ft_memdel(void **ap) +{ + if (ap) + { + free(*ap); + *ap = NULL; + } +} diff --git a/ft_memmove.c b/ft_memmove.c index 25b451a..e11d734 100644 --- a/ft_memmove.c +++ b/ft_memmove.c @@ -1,35 +1,35 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memmove.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 13:30:49 by yochered #+# #+# */ -/* Updated: 2018/10/25 13:30:52 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void *ft_memmove(void *dst, const void *src, size_t len) -{ - unsigned char *dst_tmp; - unsigned char *src_tmp; - size_t i; - - i = -1; - dst_tmp = (unsigned char *)dst; - src_tmp = (unsigned char *)src; - if (src_tmp < dst_tmp) - { - while (len--) - *(dst_tmp + len) = *(src_tmp + len); - } - else - { - while (++i < len) - *(dst_tmp + i) = *(src_tmp + i); - } - return (dst); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 13:30:49 by yochered #+# #+# */ +/* Updated: 2018/10/25 13:30:52 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + unsigned char *dst_tmp; + unsigned char *src_tmp; + size_t i; + + i = -1; + dst_tmp = (unsigned char *)dst; + src_tmp = (unsigned char *)src; + if (src_tmp < dst_tmp) + { + while (len--) + *(dst_tmp + len) = *(src_tmp + len); + } + else + { + while (++i < len) + *(dst_tmp + i) = *(src_tmp + i); + } + return (dst); +} diff --git a/ft_memset.c b/ft_memset.c index 5c0bedb..fe3d7fb 100644 --- a/ft_memset.c +++ b/ft_memset.c @@ -1,26 +1,26 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memset.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 16:16:00 by yochered #+# #+# */ -/* Updated: 2018/10/24 16:16:01 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -void *ft_memset(void *b, int c, size_t len) -{ - unsigned char *ptr; - - ptr = (unsigned char *)b; - if (len) - { - while (len--) - *ptr++ = (unsigned char)c; - } - return (b); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 16:16:00 by yochered #+# #+# */ +/* Updated: 2018/10/24 16:16:01 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memset(void *b, int c, size_t len) +{ + unsigned char *ptr; + + ptr = (unsigned char *)b; + if (len) + { + while (len--) + *ptr++ = (unsigned char)c; + } + return (b); +} diff --git a/ft_print_memory.c b/ft_print_memory.c index af95d03..b62e66e 100644 --- a/ft_print_memory.c +++ b/ft_print_memory.c @@ -1,82 +1,82 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_print_memory.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/30 14:30:22 by yochered #+# #+# */ -/* Updated: 2018/10/30 14:30:23 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -static void print_hex(unsigned char c) -{ - char hex_digits[16]; - int i; - char n; - - i = 0; - n = '0'; - while (n <= '9') - hex_digits[i++] = n++; - n = 'a'; - while (n <= 'f') - hex_digits[i++] = n++; - hex_digits[i] = '\0'; - ft_putchar(hex_digits[c / 16]); - ft_putchar(hex_digits[c % 16]); -} - -static void print_ascii(unsigned char c) -{ - if (c >= 32 && c <= 126) - ft_putchar(c); - else - ft_putchar('.'); -} - -static void print_line(unsigned char *str, size_t min, size_t max) -{ - size_t i; - - i = min; - while (i < min + 16 && i < max) - { - print_hex(str[i]); - if (i % 2 != 0) - ft_putchar(' '); - i++; - } - while (i < min + 16) - { - ft_putchar(' '); - ft_putchar(' '); - if (i % 2 != 0) - ft_putchar(' '); - i++; - } - i = min; - while (i < min + 16 && i < max) - { - print_ascii(str[i]); - i++; - } - ft_putchar('\n'); -} - -void ft_print_memory(const void *addr, size_t size) -{ - size_t c; - unsigned char *str; - - c = 0; - str = (unsigned char *)addr; - while (c < size) - { - print_line(str, c, size); - c += 16; - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_memory.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/30 14:30:22 by yochered #+# #+# */ +/* Updated: 2018/10/30 14:30:23 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static void print_hex(unsigned char c) +{ + char hex_digits[16]; + int i; + char n; + + i = 0; + n = '0'; + while (n <= '9') + hex_digits[i++] = n++; + n = 'a'; + while (n <= 'f') + hex_digits[i++] = n++; + hex_digits[i] = '\0'; + ft_putchar(hex_digits[c / 16]); + ft_putchar(hex_digits[c % 16]); +} + +static void print_ascii(unsigned char c) +{ + if (c >= 32 && c <= 126) + ft_putchar(c); + else + ft_putchar('.'); +} + +static void print_line(unsigned char *str, size_t min, size_t max) +{ + size_t i; + + i = min; + while (i < min + 16 && i < max) + { + print_hex(str[i]); + if (i % 2 != 0) + ft_putchar(' '); + i++; + } + while (i < min + 16) + { + ft_putchar(' '); + ft_putchar(' '); + if (i % 2 != 0) + ft_putchar(' '); + i++; + } + i = min; + while (i < min + 16 && i < max) + { + print_ascii(str[i]); + i++; + } + ft_putchar('\n'); +} + +void ft_print_memory(const void *addr, size_t size) +{ + size_t c; + unsigned char *str; + + c = 0; + str = (unsigned char *)addr; + while (c < size) + { + print_line(str, c, size); + c += 16; + } +} diff --git a/ft_printf.c b/ft_printf.c new file mode 100644 index 0000000..2a4aafc --- /dev/null +++ b/ft_printf.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/26 10:14:10 by yochered #+# #+# */ +/* Updated: 2018/11/26 10:14:13 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int handle_expression(va_list ap, const char **format) +{ + t_params params; + int ret; + int index; + int (*f[10])(va_list, t_params*); + + ret = 0; + fill_function_arr(f); + (*format)++; + if (!**format || !allowed_symbols((char*)*format)) + return (0); + init_params(¶ms); + reach_type(format, ¶ms, ap); + index = type_id(**format, ¶ms); + if (index != -1 || **format == '%' || ft_isalpha(**format)) + { + ret += index == -1 ? + print_percent(**format, ¶ms) : f[index](ap, ¶ms); + (*format)++; + } + else + ret += print_percent(0, ¶ms); + return (ret); +} + +int ft_printf(const char *format, ...) +{ + va_list ap; + int ret; + + ret = 0; + va_start(ap, format); + while (*format) + { + if (*format == '%') + ret += handle_expression(ap, &format); + else + { + ft_putchar(*format); + format++; + ret++; + } + } + va_end(ap); + return (ret); +} diff --git a/ft_putchar.c b/ft_putchar.c index b0bb263..9b7a896 100644 --- a/ft_putchar.c +++ b/ft_putchar.c @@ -1,19 +1,19 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putchar.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 12:28:23 by yochered #+# #+# */ -/* Updated: 2018/10/24 12:28:26 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -void ft_putchar(char c) -{ - ft_putchar_fd(c, 1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 12:28:23 by yochered #+# #+# */ +/* Updated: 2018/10/24 12:28:26 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void ft_putchar(char c) +{ + ft_putchar_fd(c, 1); +} diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c index e4d274c..09e65a1 100644 --- a/ft_putchar_fd.c +++ b/ft_putchar_fd.c @@ -1,20 +1,20 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putchar_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 12:19:57 by yochered #+# #+# */ -/* Updated: 2018/10/24 12:19:59 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -void ft_putchar_fd(char c, int fd) -{ - if (fd < 0) - return ; - write(fd, &c, 1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 12:19:57 by yochered #+# #+# */ +/* Updated: 2018/10/24 12:19:59 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar_fd(char c, int fd) +{ + if (fd < 0) + return ; + write(fd, &c, 1); +} diff --git a/ft_putendl.c b/ft_putendl.c index 029460b..26d6635 100644 --- a/ft_putendl.c +++ b/ft_putendl.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putendl.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 18:19:58 by yochered #+# #+# */ -/* Updated: 2018/10/24 18:20:00 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_putendl(char const *s) -{ - ft_putendl_fd(s, 1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 18:19:58 by yochered #+# #+# */ +/* Updated: 2018/10/24 18:20:00 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putendl(char const *s) +{ + ft_putendl_fd(s, 1); +} diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c index 9bd0b42..9aca331 100644 --- a/ft_putendl_fd.c +++ b/ft_putendl_fd.c @@ -1,19 +1,19 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putendl_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 16:43:22 by yochered #+# #+# */ -/* Updated: 2018/10/27 16:43:23 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_putendl_fd(char const *s, int fd) -{ - ft_putstr_fd(s, fd); - ft_putchar_fd('\n', fd); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 16:43:22 by yochered #+# #+# */ +/* Updated: 2018/10/27 16:43:23 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putendl_fd(char const *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/ft_putnbr.c b/ft_putnbr.c index 05668b6..2cf6871 100644 --- a/ft_putnbr.c +++ b/ft_putnbr.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putnbr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 10:38:49 by yochered #+# #+# */ -/* Updated: 2018/10/24 10:47:55 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_putnbr(int n) -{ - ft_putnbr_fd(n, 1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 10:38:49 by yochered #+# #+# */ +/* Updated: 2018/10/24 10:47:55 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putnbr(int n) +{ + ft_putnbr_fd(n, 1); +} diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c index 09e9dd1..41c6adf 100644 --- a/ft_putnbr_fd.c +++ b/ft_putnbr_fd.c @@ -1,31 +1,31 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putnbr_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 16:40:54 by yochered #+# #+# */ -/* Updated: 2018/10/27 16:40:55 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_putnbr_fd(int n, int fd) -{ - if (n < 0) - { - ft_putchar_fd('-', fd); - if (n <= -10) - ft_putnbr_fd(n / -10, fd); - ft_putchar_fd(-(n % 10) + 48, fd); - } - else if (n >= 10) - { - ft_putnbr_fd(n / 10, fd); - ft_putchar_fd(n % 10 + 48, fd); - } - else - ft_putchar_fd(n % 10 + 48, fd); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 16:40:54 by yochered #+# #+# */ +/* Updated: 2018/10/27 16:40:55 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putnbr_fd(int n, int fd) +{ + if (n < 0) + { + ft_putchar_fd('-', fd); + if (n <= -10) + ft_putnbr_fd(n / -10, fd); + ft_putchar_fd(-(n % 10) + 48, fd); + } + else if (n >= 10) + { + ft_putnbr_fd(n / 10, fd); + ft_putchar_fd(n % 10 + 48, fd); + } + else + ft_putchar_fd(n % 10 + 48, fd); +} diff --git a/ft_putstr.c b/ft_putstr.c index 8edb47e..df9a67d 100644 --- a/ft_putstr.c +++ b/ft_putstr.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putstr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 10:27:02 by yochered #+# #+# */ -/* Updated: 2018/10/24 10:27:11 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_putstr(char const *s) -{ - ft_putstr_fd(s, 1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 10:27:02 by yochered #+# #+# */ +/* Updated: 2018/10/24 10:27:11 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putstr(char const *s) +{ + ft_putstr_fd(s, 1); +} diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c index ab544cd..edb6123 100644 --- a/ft_putstr_fd.c +++ b/ft_putstr_fd.c @@ -1,24 +1,24 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putstr_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 12:28:37 by yochered #+# #+# */ -/* Updated: 2018/10/24 12:28:38 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -void ft_putstr_fd(char const *s, int fd) -{ - if (!s) - return ; - while (*s) - { - ft_putchar_fd(*s, fd); - s++; - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 12:28:37 by yochered #+# #+# */ +/* Updated: 2018/10/24 12:28:38 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putstr_fd(char const *s, int fd) +{ + if (!s) + return ; + while (*s) + { + ft_putchar_fd(*s, fd); + s++; + } +} diff --git a/ft_quick_sort.c b/ft_quick_sort.c index 0ca4e10..b19ad5c 100644 --- a/ft_quick_sort.c +++ b/ft_quick_sort.c @@ -1,54 +1,54 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_quick_sort.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/30 15:01:25 by yochered #+# #+# */ -/* Updated: 2018/10/30 15:01:26 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -static void ft_swap(int *a, int *b) -{ - int tmp; - - tmp = *a; - *a = *b; - *b = tmp; -} - -static int partition(int *tab, int low, int high) -{ - int pivot; - int i; - int j; - - pivot = tab[high]; - j = low; - i = low - 1; - while (j <= high - 1) - { - if (tab[j] <= pivot) - { - i++; - ft_swap(&tab[i], &tab[j]); - } - j++; - } - ft_swap(&tab[i + 1], &tab[high]); - return (i + 1); -} - -void ft_quick_sort(int **tab, int low, int high) -{ - int pivot; - - if (low < high) - { - pivot = partition(*tab, low, high); - ft_quick_sort(tab, low, pivot - 1); - ft_quick_sort(tab, pivot + 1, high); - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_quick_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/30 15:01:25 by yochered #+# #+# */ +/* Updated: 2018/10/30 15:01:26 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +static void ft_swap(int *a, int *b) +{ + int tmp; + + tmp = *a; + *a = *b; + *b = tmp; +} + +static int partition(int *tab, int low, int high) +{ + int pivot; + int i; + int j; + + pivot = tab[high]; + j = low; + i = low - 1; + while (j <= high - 1) + { + if (tab[j] <= pivot) + { + i++; + ft_swap(&tab[i], &tab[j]); + } + j++; + } + ft_swap(&tab[i + 1], &tab[high]); + return (i + 1); +} + +void ft_quick_sort(int **tab, int low, int high) +{ + int pivot; + + if (low < high) + { + pivot = partition(*tab, low, high); + ft_quick_sort(tab, low, pivot - 1); + ft_quick_sort(tab, pivot + 1, high); + } +} diff --git a/ft_strcat.c b/ft_strcat.c index 916a565..fca8a06 100644 --- a/ft_strcat.c +++ b/ft_strcat.c @@ -1,25 +1,25 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strcat.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 16:43:12 by yochered #+# #+# */ -/* Updated: 2018/10/25 16:43:13 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strcat(char *s1, const char *s2) -{ - char *s1_tmp; - - s1_tmp = s1; - s1_tmp += ft_strlen(s1); - while (*s2) - *s1_tmp++ = *s2++; - *s1_tmp = '\0'; - return (s1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 16:43:12 by yochered #+# #+# */ +/* Updated: 2018/10/25 16:43:13 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strcat(char *s1, const char *s2) +{ + char *s1_tmp; + + s1_tmp = s1; + s1_tmp += ft_strlen(s1); + while (*s2) + *s1_tmp++ = *s2++; + *s1_tmp = '\0'; + return (s1); +} diff --git a/ft_strchr.c b/ft_strchr.c index 1e29083..4958f2d 100644 --- a/ft_strchr.c +++ b/ft_strchr.c @@ -1,29 +1,29 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strchr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 17:50:17 by yochered #+# #+# */ -/* Updated: 2018/10/25 17:50:18 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strchr(const char *s, int c) -{ - char *str; - - str = (char *)s; - if ((char)c == '\0') - return (str + ft_strlen(str)); - while (*str) - { - if (*str == (char)c) - return (str); - str++; - } - return (NULL); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 17:50:17 by yochered #+# #+# */ +/* Updated: 2018/10/25 17:50:18 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strchr(const char *s, int c) +{ + char *str; + + str = (char *)s; + if ((char)c == '\0') + return (str + ft_strlen(str)); + while (*str) + { + if (*str == (char)c) + return (str); + str++; + } + return (NULL); +} diff --git a/ft_strclr.c b/ft_strclr.c index 4a1ff1d..d7d4984 100644 --- a/ft_strclr.c +++ b/ft_strclr.c @@ -1,21 +1,21 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strclr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 12:57:03 by yochered #+# #+# */ -/* Updated: 2018/10/27 12:57:04 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -void ft_strclr(char *s) -{ - if (!s) - return ; - while (*s) - *s++ = '\0'; -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 12:57:03 by yochered #+# #+# */ +/* Updated: 2018/10/27 12:57:04 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_strclr(char *s) +{ + if (!s) + return ; + while (*s) + *s++ = '\0'; +} diff --git a/ft_strcmp.c b/ft_strcmp.c index 6e66940..d4d84ed 100644 --- a/ft_strcmp.c +++ b/ft_strcmp.c @@ -1,21 +1,21 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strcmp.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 11:32:04 by yochered #+# #+# */ -/* Updated: 2018/10/26 11:32:05 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_strcmp(const char *s1, const char *s2) -{ - int i; - - i = 0; - while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') - i++; - return ((unsigned char)s1[i] - (unsigned char)s2[i]); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:32:04 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:32:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcmp(const char *s1, const char *s2) +{ + int i; + + i = 0; + while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/ft_strcpy.c b/ft_strcpy.c index e181f80..3a1b57f 100644 --- a/ft_strcpy.c +++ b/ft_strcpy.c @@ -1,24 +1,24 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strcpy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 15:42:03 by yochered #+# #+# */ -/* Updated: 2018/10/24 15:42:05 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strcpy(char *dst, const char *src) -{ - char *dst_tmp; - - dst_tmp = dst; - while (*src) - *dst_tmp++ = *src++; - *dst_tmp = '\0'; - return (dst); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 15:42:03 by yochered #+# #+# */ +/* Updated: 2018/10/24 15:42:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strcpy(char *dst, const char *src) +{ + char *dst_tmp; + + dst_tmp = dst; + while (*src) + *dst_tmp++ = *src++; + *dst_tmp = '\0'; + return (dst); +} diff --git a/ft_strdel.c b/ft_strdel.c index ad253f0..d64d179 100644 --- a/ft_strdel.c +++ b/ft_strdel.c @@ -1,23 +1,23 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strdel.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 13:07:06 by yochered #+# #+# */ -/* Updated: 2018/10/27 13:07:07 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -void ft_strdel(char **as) -{ - if (as) - { - free(*as); - *as = NULL; - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:07:06 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:07:07 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void ft_strdel(char **as) +{ + if (as) + { + free(*as); + *as = NULL; + } +} diff --git a/ft_strdup.c b/ft_strdup.c index 9433c14..468e0d7 100644 --- a/ft_strdup.c +++ b/ft_strdup.c @@ -1,31 +1,31 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strdup.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 15:32:48 by yochered #+# #+# */ -/* Updated: 2018/10/24 15:32:50 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include "libft.h" - -char *ft_strdup(const char *s1) -{ - char *dest; - char *dest_tmp; - int len; - - len = ft_strlen(s1); - dest = (char *)malloc((len + 1) * sizeof(char)); - if (!dest) - return (NULL); - dest_tmp = dest; - while (*s1) - *dest++ = *s1++; - *dest = '\0'; - return (dest_tmp); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 15:32:48 by yochered #+# #+# */ +/* Updated: 2018/10/24 15:32:50 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strdup(const char *s1) +{ + char *dest; + char *dest_tmp; + int len; + + len = ft_strlen(s1); + dest = (char *)malloc((len + 1) * sizeof(char)); + if (!dest) + return (NULL); + dest_tmp = dest; + while (*s1) + *dest++ = *s1++; + *dest = '\0'; + return (dest_tmp); +} diff --git a/ft_strequ.c b/ft_strequ.c index c6f5d18..7695820 100644 --- a/ft_strequ.c +++ b/ft_strequ.c @@ -1,25 +1,25 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strequ.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 14:58:04 by yochered #+# #+# */ -/* Updated: 2018/10/24 14:58:05 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_strequ(char const *s1, char const *s2) -{ - int i; - - i = 0; - if (!s1 || !s2) - return (0); - while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') - i++; - if (s1[i] != '\0' || s2[i] != '\0') - return (0); - return (1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 14:58:04 by yochered #+# #+# */ +/* Updated: 2018/10/24 14:58:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strequ(char const *s1, char const *s2) +{ + int i; + + i = 0; + if (!s1 || !s2) + return (0); + while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') + i++; + if (s1[i] != '\0' || s2[i] != '\0') + return (0); + return (1); +} diff --git a/ft_striter.c b/ft_striter.c index 818585f..c4d4fd5 100644 --- a/ft_striter.c +++ b/ft_striter.c @@ -1,22 +1,22 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_striter.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 13:04:52 by yochered #+# #+# */ -/* Updated: 2018/10/27 13:04:52 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -void ft_striter(char *s, void (*f)(char *)) -{ - if (!s || !f) - return ; - while (*s) - { - f(&*s); - s++; - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:04:52 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:04:52 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_striter(char *s, void (*f)(char *)) +{ + if (!s || !f) + return ; + while (*s) + { + f(&*s); + s++; + } +} diff --git a/ft_striteri.c b/ft_striteri.c index 68cec02..9630519 100644 --- a/ft_striteri.c +++ b/ft_striteri.c @@ -1,25 +1,25 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_striteri.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 13:11:47 by yochered #+# #+# */ -/* Updated: 2018/10/27 13:11:48 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -void ft_striteri(char *s, void (*f)(unsigned int, char *)) -{ - int i; - - i = 0; - if (!s || !f) - return ; - while (s[i] != '\0') - { - f(i, &s[i]); - i++; - } -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:11:47 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:11:48 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + int i; + + i = 0; + if (!s || !f) + return ; + while (s[i] != '\0') + { + f(i, &s[i]); + i++; + } +} diff --git a/ft_strjoin.c b/ft_strjoin.c index 5a86c5c..a640f0a 100644 --- a/ft_strjoin.c +++ b/ft_strjoin.c @@ -1,33 +1,33 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strjoin.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 13:44:36 by yochered #+# #+# */ -/* Updated: 2018/10/27 13:44:37 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include "libft.h" - -char *ft_strjoin(char const *s1, char const *s2) -{ - char *res; - char *res_tmp; - - if (!s1 || !s2) - return (NULL); - res = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char)); - if (!res) - return (NULL); - res_tmp = res; - while (*s1) - *res_tmp++ = *s1++; - while (*s2) - *res_tmp++ = *s2++; - *res_tmp = '\0'; - return (res); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:44:36 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:44:37 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *res; + char *res_tmp; + + if (!s1 || !s2) + return (NULL); + res = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char)); + if (!res) + return (NULL); + res_tmp = res; + while (*s1) + *res_tmp++ = *s1++; + while (*s2) + *res_tmp++ = *s2++; + *res_tmp = '\0'; + return (res); +} diff --git a/ft_strlcat.c b/ft_strlcat.c index 782ecb9..96005fc 100644 --- a/ft_strlcat.c +++ b/ft_strlcat.c @@ -1,32 +1,32 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strlcat.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 17:17:28 by yochered #+# #+# */ -/* Updated: 2018/10/25 17:17:29 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -size_t ft_strlcat(char *dst, const char *src, size_t dstsize) -{ - size_t i; - size_t dstlength; - size_t srclength; - - i = 0; - dstlength = ft_strlen(dst); - srclength = ft_strlen(src); - if (dstsize <= dstlength) - return (srclength + dstsize); - while (dst[i] != '\0' && i < dstsize - 1) - i++; - while (*src && i < dstsize - 1) - dst[i++] = *src++; - dst[i] = '\0'; - return (dstlength + srclength); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 17:17:28 by yochered #+# #+# */ +/* Updated: 2018/10/25 17:17:29 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_strlcat(char *dst, const char *src, size_t dstsize) +{ + size_t i; + size_t dstlength; + size_t srclength; + + i = 0; + dstlength = ft_strlen(dst); + srclength = ft_strlen(src); + if (dstsize <= dstlength) + return (srclength + dstsize); + while (dst[i] != '\0' && i < dstsize - 1) + i++; + while (*src && i < dstsize - 1) + dst[i++] = *src++; + dst[i] = '\0'; + return (dstlength + srclength); +} diff --git a/ft_strlen.c b/ft_strlen.c index 0dce382..63e4e7f 100644 --- a/ft_strlen.c +++ b/ft_strlen.c @@ -1,23 +1,23 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strlen.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 11:34:29 by yochered #+# #+# */ -/* Updated: 2018/10/24 11:34:30 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -size_t ft_strlen(const char *string) -{ - size_t len; - - len = 0; - while (string[len] != '\0') - len++; - return (len); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 11:34:29 by yochered #+# #+# */ +/* Updated: 2018/10/24 11:34:30 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_strlen(const char *string) +{ + size_t len; + + len = 0; + while (string[len] != '\0') + len++; + return (len); +} diff --git a/ft_strmap.c b/ft_strmap.c index 43e894e..a6cb182 100644 --- a/ft_strmap.c +++ b/ft_strmap.c @@ -1,36 +1,36 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strmap.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 13:13:12 by yochered #+# #+# */ -/* Updated: 2018/10/27 13:13:13 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -char *ft_strmap(char const *s, char (*f)(char)) -{ - char *fresh_str; - char *fresh_str_tmp; - char *s_tmp; - - if (!s) - return (NULL); - s_tmp = (char *)s; - fresh_str = (char *)malloc((ft_strlen(s_tmp) + 1) * sizeof(char)); - if (!fresh_str) - return (NULL); - fresh_str_tmp = fresh_str; - while (*s_tmp) - { - *fresh_str++ = f(*s_tmp); - s_tmp++; - } - *fresh_str = '\0'; - return (fresh_str_tmp); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:13:12 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:13:13 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strmap(char const *s, char (*f)(char)) +{ + char *fresh_str; + char *fresh_str_tmp; + char *s_tmp; + + if (!s) + return (NULL); + s_tmp = (char *)s; + fresh_str = (char *)malloc((ft_strlen(s_tmp) + 1) * sizeof(char)); + if (!fresh_str) + return (NULL); + fresh_str_tmp = fresh_str; + while (*s_tmp) + { + *fresh_str++ = f(*s_tmp); + s_tmp++; + } + *fresh_str = '\0'; + return (fresh_str_tmp); +} diff --git a/ft_strmapi.c b/ft_strmapi.c index 9ba010c..5531403 100644 --- a/ft_strmapi.c +++ b/ft_strmapi.c @@ -1,36 +1,36 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strmapi.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 13:15:20 by yochered #+# #+# */ -/* Updated: 2018/10/27 13:15:22 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) -{ - char *fresh_str; - char *s_tmp; - int i; - - i = 0; - if (!s) - return (NULL); - s_tmp = (char *)s; - fresh_str = (char *)malloc((ft_strlen(s_tmp) + 1) * sizeof(char)); - if (!fresh_str) - return (NULL); - while (s_tmp[i] != '\0') - { - fresh_str[i] = f(i, s_tmp[i]); - i++; - } - fresh_str[i] = '\0'; - return (fresh_str); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:15:20 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:15:22 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *fresh_str; + char *s_tmp; + int i; + + i = 0; + if (!s) + return (NULL); + s_tmp = (char *)s; + fresh_str = (char *)malloc((ft_strlen(s_tmp) + 1) * sizeof(char)); + if (!fresh_str) + return (NULL); + while (s_tmp[i] != '\0') + { + fresh_str[i] = f(i, s_tmp[i]); + i++; + } + fresh_str[i] = '\0'; + return (fresh_str); +} diff --git a/ft_strncat.c b/ft_strncat.c index 11d18d3..a1a2ca4 100644 --- a/ft_strncat.c +++ b/ft_strncat.c @@ -1,25 +1,25 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strncat.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 17:10:34 by yochered #+# #+# */ -/* Updated: 2018/10/25 17:10:36 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strncat(char *s1, const char *s2, size_t n) -{ - char *s1_tmp; - - s1_tmp = s1; - s1_tmp += ft_strlen(s1); - while (*s2 && n--) - *s1_tmp++ = *s2++; - *s1_tmp = '\0'; - return (s1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 17:10:34 by yochered #+# #+# */ +/* Updated: 2018/10/25 17:10:36 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strncat(char *s1, const char *s2, size_t n) +{ + char *s1_tmp; + + s1_tmp = s1; + s1_tmp += ft_strlen(s1); + while (*s2 && n--) + *s1_tmp++ = *s2++; + *s1_tmp = '\0'; + return (s1); +} diff --git a/ft_strncmp.c b/ft_strncmp.c index 6dba04e..296a2fe 100644 --- a/ft_strncmp.c +++ b/ft_strncmp.c @@ -1,25 +1,25 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strncmp.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 11:38:08 by yochered #+# #+# */ -/* Updated: 2018/10/26 11:38:09 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -int ft_strncmp(const char *s1, const char *s2, size_t n) -{ - size_t i; - - i = 0; - if (n == 0) - return (0); - while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0' && i < n - 1) - i++; - return ((unsigned char)s1[i] - (unsigned char)s2[i]); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:38:08 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:38:09 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + if (n == 0) + return (0); + while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0' && i < n - 1) + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/ft_strncpy.c b/ft_strncpy.c index 8d76398..c1baedd 100644 --- a/ft_strncpy.c +++ b/ft_strncpy.c @@ -1,32 +1,32 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strncpy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/25 14:23:04 by yochered #+# #+# */ -/* Updated: 2018/10/25 14:23:05 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strncpy(char *dst, const char *src, size_t len) -{ - size_t src_len; - size_t cpy_len; - - src_len = 0; - cpy_len = len; - while (src[src_len] != '\0' && len--) - src_len++; - if (src_len < cpy_len) - { - ft_memcpy(dst, src, src_len); - ft_memset(dst + src_len, 0, cpy_len - src_len); - } - else - ft_memcpy(dst, src, cpy_len); - return (dst); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 14:23:04 by yochered #+# #+# */ +/* Updated: 2018/10/25 14:23:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strncpy(char *dst, const char *src, size_t len) +{ + size_t src_len; + size_t cpy_len; + + src_len = 0; + cpy_len = len; + while (src[src_len] != '\0' && len--) + src_len++; + if (src_len < cpy_len) + { + ft_memcpy(dst, src, src_len); + ft_memset(dst + src_len, 0, cpy_len - src_len); + } + else + ft_memcpy(dst, src, cpy_len); + return (dst); +} diff --git a/ft_strnequ.c b/ft_strnequ.c index e4e6479..a34f61b 100644 --- a/ft_strnequ.c +++ b/ft_strnequ.c @@ -1,32 +1,32 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strnequ.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 15:08:08 by yochered #+# #+# */ -/* Updated: 2018/10/30 18:13:56 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -int ft_strnequ(char const *s1, char const *s2, size_t n) -{ - size_t i; - - i = 0; - if (!s1 || !s2 || !n) - return (1); - while (n > 0) - { - if (!s1[i] && !s2[i]) - return (1); - if (s1[i] != s2[i]) - return (0); - n--; - i++; - } - return (1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 15:08:08 by yochered #+# #+# */ +/* Updated: 2018/10/30 18:13:56 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + size_t i; + + i = 0; + if (!s1 || !s2 || !n) + return (1); + while (n > 0) + { + if (!s1[i] && !s2[i]) + return (1); + if (s1[i] != s2[i]) + return (0); + n--; + i++; + } + return (1); +} diff --git a/ft_strnew.c b/ft_strnew.c index 50af776..c6fa347 100644 --- a/ft_strnew.c +++ b/ft_strnew.c @@ -1,29 +1,29 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strnew.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 18:03:37 by yochered #+# #+# */ -/* Updated: 2018/10/24 18:03:38 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -char *ft_strnew(size_t size) -{ - char *res; - size_t i; - - res = (char *)malloc((size + 1) * sizeof(char)); - i = 0; - if (!res) - return (NULL); - while (i < size) - res[i++] = '\0'; - res[i] = '\0'; - return (res); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 18:03:37 by yochered #+# #+# */ +/* Updated: 2018/10/24 18:03:38 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strnew(size_t size) +{ + char *res; + size_t i; + + res = (char *)malloc((size + 1) * sizeof(char)); + i = 0; + if (!res) + return (NULL); + while (i < size) + res[i++] = '\0'; + res[i] = '\0'; + return (res); +} diff --git a/ft_strnstr.c b/ft_strnstr.c index ece06df..0a6f736 100644 --- a/ft_strnstr.c +++ b/ft_strnstr.c @@ -1,31 +1,31 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strnstr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 11:10:30 by yochered #+# #+# */ -/* Updated: 2018/10/26 11:10:32 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strnstr(const char *haystack, const char *needle, size_t len) -{ - char *str; - size_t substr_len; - - substr_len = ft_strlen(needle); - str = (char *)haystack; - if (substr_len == 0) - return (str); - while (*str && len-- >= substr_len) - { - if (ft_memcmp(str, needle, substr_len) == 0) - return (str); - str++; - } - return (NULL); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:10:30 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:10:32 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + char *str; + size_t substr_len; + + substr_len = ft_strlen(needle); + str = (char *)haystack; + if (substr_len == 0) + return (str); + while (*str && len-- >= substr_len) + { + if (ft_memcmp(str, needle, substr_len) == 0) + return (str); + str++; + } + return (NULL); +} diff --git a/ft_strrchr.c b/ft_strrchr.c index 0cc61c3..2e3b079 100644 --- a/ft_strrchr.c +++ b/ft_strrchr.c @@ -1,31 +1,31 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strrchr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 10:53:14 by yochered #+# #+# */ -/* Updated: 2018/10/27 10:53:25 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strrchr(const char *s, int c) -{ - char *str; - char *last_s; - - last_s = NULL; - str = (char *)s; - if ((char)c == '\0') - return (str + ft_strlen(str)); - while (*str) - { - if (*str == (char)c) - last_s = str; - str++; - } - return (last_s); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 10:53:14 by yochered #+# #+# */ +/* Updated: 2018/10/27 10:53:25 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strrchr(const char *s, int c) +{ + char *str; + char *last_s; + + last_s = NULL; + str = (char *)s; + if ((char)c == '\0') + return (str + ft_strlen(str)); + while (*str) + { + if (*str == (char)c) + last_s = str; + str++; + } + return (last_s); +} diff --git a/ft_strsplit.c b/ft_strsplit.c index 1627211..4119bed 100644 --- a/ft_strsplit.c +++ b/ft_strsplit.c @@ -1,68 +1,68 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strsplit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 15:14:46 by yochered #+# #+# */ -/* Updated: 2018/10/27 15:14:51 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include "libft.h" - -static int count_words(char const *s, char c) -{ - int words; - int len; - int i; - - words = 0; - i = -1; - len = ft_strlen(s); - while (++i < len) - { - if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0')) - words++; - } - return (words); -} - -static int count_len(char const *s, char c) -{ - int len; - - len = 0; - while (*s == c) - s++; - while (s[len] != c && s[len] != '\0') - len++; - return (len); -} - -char **ft_strsplit(char const *s, char c) -{ - int i; - int j; - int k; - char **res; - - if (!s || !(res = (char **)malloc(sizeof(*res) * (count_words(s, c) + 1)))) - return (NULL); - i = -1; - j = 0; - while (++i < count_words(s, c)) - { - k = 0; - if (!(res[i] = ft_strnew(count_len(&s[j], c)))) - res[i] = NULL; - while (s[j] == c) - j++; - while (s[j] != c && s[j]) - res[i][k++] = s[j++]; - } - res[i] = 0; - return (res); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 15:14:46 by yochered #+# #+# */ +/* Updated: 2018/10/27 15:14:51 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static int count_words(char const *s, char c) +{ + int words; + int len; + int i; + + words = 0; + i = -1; + len = ft_strlen(s); + while (++i < len) + { + if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0')) + words++; + } + return (words); +} + +static int count_len(char const *s, char c) +{ + int len; + + len = 0; + while (*s == c) + s++; + while (s[len] != c && s[len] != '\0') + len++; + return (len); +} + +char **ft_strsplit(char const *s, char c) +{ + int i; + int j; + int k; + char **res; + + if (!s || !(res = (char **)malloc(sizeof(*res) * (count_words(s, c) + 1)))) + return (NULL); + i = -1; + j = 0; + while (++i < count_words(s, c)) + { + k = 0; + if (!(res[i] = ft_strnew(count_len(&s[j], c)))) + res[i] = NULL; + while (s[j] == c) + j++; + while (s[j] != c && s[j]) + res[i][k++] = s[j++]; + } + res[i] = 0; + return (res); +} diff --git a/ft_strstr.c b/ft_strstr.c index ed21d04..38de47b 100644 --- a/ft_strstr.c +++ b/ft_strstr.c @@ -1,31 +1,31 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strstr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 10:24:59 by yochered #+# #+# */ -/* Updated: 2018/10/26 10:25:01 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strstr(const char *haystack, const char *needle) -{ - char *str; - int substr_len; - - str = (char *)haystack; - substr_len = ft_strlen(needle); - if (substr_len == 0) - return (str); - while (*str) - { - if (ft_memcmp(str, needle, substr_len) == 0) - return (str); - str++; - } - return (NULL); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 10:24:59 by yochered #+# #+# */ +/* Updated: 2018/10/26 10:25:01 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strstr(const char *haystack, const char *needle) +{ + char *str; + int substr_len; + + str = (char *)haystack; + substr_len = ft_strlen(needle); + if (substr_len == 0) + return (str); + while (*str) + { + if (ft_memcmp(str, needle, substr_len) == 0) + return (str); + str++; + } + return (NULL); +} diff --git a/ft_strsub.c b/ft_strsub.c index de957ab..91e8d96 100644 --- a/ft_strsub.c +++ b/ft_strsub.c @@ -1,31 +1,31 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strsub.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 13:27:50 by yochered #+# #+# */ -/* Updated: 2018/10/27 13:27:52 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -char *ft_strsub(char const *s, unsigned int start, size_t len) -{ - char *fresh_str; - char *str; - size_t i; - - i = 0; - fresh_str = (char *)malloc((len + 1) * sizeof(char)); - if (!fresh_str || !s) - return (NULL); - str = (char *)s; - while (str[start] != '\0' && i < len) - fresh_str[i++] = str[start++]; - fresh_str[i] = '\0'; - return (fresh_str); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:27:50 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:27:52 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *fresh_str; + char *str; + size_t i; + + i = 0; + fresh_str = (char *)malloc((len + 1) * sizeof(char)); + if (!fresh_str || !s) + return (NULL); + str = (char *)s; + while (str[start] != '\0' && i < len) + fresh_str[i++] = str[start++]; + fresh_str[i] = '\0'; + return (fresh_str); +} diff --git a/ft_strtrim.c b/ft_strtrim.c index 7db75ce..386821d 100644 --- a/ft_strtrim.c +++ b/ft_strtrim.c @@ -1,58 +1,58 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strim.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/27 13:52:56 by yochered #+# #+# */ -/* Updated: 2018/10/27 13:52:57 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -static int is_space(char c) -{ - if (c == ' ' || c == ',' || c == '\n' || c == '\t') - return (1); - return (0); -} - -static int count_len(char const *s) -{ - int len; - int i; - - len = ft_strlen(s); - while (is_space(s[len - 1])) - len--; - i = -1; - while (is_space(s[++i])) - len--; - if (len < 0) - len = 0; - return (len); -} - -char *ft_strtrim(char const *s) -{ - int i; - int len; - char *res; - - if (!s) - return (NULL); - i = -1; - len = count_len(s); - res = (char *)malloc((len + 1) * sizeof(char)); - if (!res) - return (NULL); - while (is_space(*s)) - s++; - while (++i < len) - res[i] = *s++; - res[i] = '\0'; - return (res); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:52:56 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:52:57 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static int is_space(char c) +{ + if (c == ' ' || c == ',' || c == '\n' || c == '\t') + return (1); + return (0); +} + +static int count_len(char const *s) +{ + int len; + int i; + + len = ft_strlen(s); + while (is_space(s[len - 1])) + len--; + i = -1; + while (is_space(s[++i])) + len--; + if (len < 0) + len = 0; + return (len); +} + +char *ft_strtrim(char const *s) +{ + int i; + int len; + char *res; + + if (!s) + return (NULL); + i = -1; + len = count_len(s); + res = (char *)malloc((len + 1) * sizeof(char)); + if (!res) + return (NULL); + while (is_space(*s)) + s++; + while (++i < len) + res[i] = *s++; + res[i] = '\0'; + return (res); +} diff --git a/ft_tolower.c b/ft_tolower.c index 1d15d6a..7ece8c1 100644 --- a/ft_tolower.c +++ b/ft_tolower.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_tolower.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 12:33:32 by yochered #+# #+# */ -/* Updated: 2018/10/26 12:33:34 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_tolower(int c) -{ - if (c >= 65 && c <= 90) - c += 32; - return (c); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:33:32 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:33:34 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_tolower(int c) +{ + if (c >= 65 && c <= 90) + c += 32; + return (c); +} diff --git a/ft_toupper.c b/ft_toupper.c index 5a14c43..67014e5 100644 --- a/ft_toupper.c +++ b/ft_toupper.c @@ -1,18 +1,18 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_toupper.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/26 12:33:22 by yochered #+# #+# */ -/* Updated: 2018/10/26 12:33:24 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_toupper(int c) -{ - if (c >= 97 && c <= 122) - c -= 32; - return (c); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:33:22 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:33:24 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + c -= 32; + return (c); +} diff --git a/get_next_line.c b/get_next_line.c new file mode 100644 index 0000000..d95eda3 --- /dev/null +++ b/get_next_line.c @@ -0,0 +1,121 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/03 10:59:02 by yochered #+# #+# */ +/* Updated: 2018/12/14 10:04:53 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +static char *get_line(char *src, t_gnl *gnl, int size) +{ + char *src_tmp; + char *res; + char *res_tmp; + + CHECK((res = ft_strnew((size_t)(size + gnl->r_size)))); + res_tmp = res; + src_tmp = src; + while (size--) + *res_tmp++ = *src_tmp++; + free(src); + while (gnl->buf[gnl->i] != '\n' && gnl->i < gnl->r_size) + *res_tmp++ = gnl->buf[gnl->i++]; + return (res); +} + +static char *read_file(char *res, t_gnl *gnl) +{ + while ((gnl->r_size = (int)read(gnl->fd, gnl->buf, BUFF_SIZE))) + { + gnl->i = 0; + CHECK((res = get_line(res, gnl, (int)ft_strlen(res)))); + if (gnl->i < gnl->r_size) + break ; + } + return (res); +} + +static t_gnl *del_create_node(t_gnl *gnl, int fd) +{ + t_gnl *node; + + if (fd == -1) + { + if (gnl->prev) + gnl->prev->next = gnl->next; + if (gnl->next) + gnl->next->prev = gnl->prev; + free(gnl); + return (NULL); + } + CHECK((node = (t_gnl *)malloc(sizeof(t_gnl)))); + node->fd = fd; + node->i = 0; + node->r_size = 0; + node->next = NULL; + node->prev = gnl ? gnl : NULL; + return (node); +} + +static t_gnl *find_fd(t_gnl *search_fd, int fd) +{ + t_gnl *tmp; + t_gnl *cur; + + cur = search_fd; + while (cur && cur->fd != fd) + { + tmp = cur; + cur = cur->prev; + } + while (!cur && search_fd && search_fd->fd != fd) + { + tmp = search_fd; + search_fd = search_fd->next; + } + search_fd = cur ? cur : search_fd; + if (!search_fd) + { + CHECK((tmp->next = del_create_node(tmp, fd))); + return (tmp->next); + } + if (read(fd, 0, 0) < 0) + del_create_node(search_fd, -1); + return (search_fd); +} + +int get_next_line(const int fd, char **line) +{ + char *res; + static t_gnl *gnl; + + if (!line || fd < 0 || (!gnl && !(gnl = del_create_node(NULL, fd)))) + return (-1); + if (!(gnl = find_fd(gnl, fd)) || read(gnl->fd, gnl->buf, 0) < 0 + || !(res = ft_strnew(0))) + return (-1); + if (gnl->i < gnl->r_size) + { + gnl->i++; + if (!(res = get_line(res, gnl, 0))) + return (-1); + } + if (gnl->i >= gnl->r_size && !(res = read_file(res, gnl))) + return (-1); + if (!gnl->r_size && !(ft_strlen(res))) + { + *line = NULL; + free(res); + return (0); + } + *line = res; + return (1); +} diff --git a/includes/ft_printf.h b/includes/ft_printf.h new file mode 100644 index 0000000..1ae2397 --- /dev/null +++ b/includes/ft_printf.h @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/26 10:14:23 by yochered #+# #+# */ +/* Updated: 2018/11/26 10:14:24 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H +# include +# include +# include +# include +# include "libft.h" +# define FLAGS "+-0 #" +# define ABS(x) (((x) < 0) ? -(x) : (x)) +# define RED(string) "\x1b[31m" string "\x1b[0m" +# define BRED(string) "\x1b[1;31m" string "\x1b[0m" +# define GREEN(string) "\x1b[0;32m" string "\x1b[0m" +# define BGREEN(string) "\x1b[1;32m" string "\x1b[0m" +# define YELLOW(string) "\x1b[0;33m" string "\x1b[0m" +# define BYELLOW(string) "\x1b[01;33m" string "\x1b[0m" +# define BLUE(string) "\x1b[0;34m" string "\x1b[0m" +# define BBLUE(string) "\x1b[1;34m" string "\x1b[0m" +# define MAGENTA(string) "\x1b[0;35m" string "\x1b[0m" +# define BMAGENTA(string) "\x1b[1;35m" string "\x1b[0m" +# define CYAN(string) "\x1b[0;36m" string "\x1b[0m" +# define BCYAN(string) "\x1b[1;36m" string "\x1b[0m" + +typedef struct s_params +{ + int width; + int precision; + int flag; + char type; + enum { + plus = 1, + minus = 2, + zero = 4, + space = 8, + hash = 16, + precision = 32, + width = 64 + } e_flags; + enum { + none, + hh, + h, + l, + ll, + j, + z, + L + } e_convert; +} t_params; + +void reach_type(const char **format, + t_params *params, va_list ap); +void handle_length(char **format, t_params *params); +void handle_precision(char **format, + t_params *params, va_list ap); +void handle_width(char **format, t_params *params, va_list ap); +void handle_flags(char **format, t_params *params); +int ft_va_putchar(va_list ap, t_params *params); +int ft_va_putstr(va_list ap, t_params *params); +int ft_va_putnbr(va_list ap, t_params *params); +int ft_va_putunbr(va_list ap, t_params *params); +int ft_va_putoctal(va_list ap, t_params *params); +int ft_va_puthex(va_list ap, t_params *params); +int ft_va_putpointer(va_list ap, t_params *params); +int ft_va_putfloat(va_list ap, t_params *params); +int ft_va_putbinary(va_list ap, t_params *params); +int ft_va_putnonprint(va_list ap, t_params *params); +void ft_putnstr(char *str, int n); +void print_padding(int size, char c); +int ft_format_str(char *s, t_params *params); +int print_percent(char c, t_params *params); +intmax_t ft_power(intmax_t nb, int power); +double ft_pow(double nbr, int pow); +int flag_list(char c); +int convert_list(char c); +int allowed_symbols(char *format); +int count_unsigned_digits(uintmax_t value, int base); +int count_signed_digits(intmax_t value, int base); +void uint_to_str(char *res, uintmax_t nbr, + int base, t_params *params); +void int_to_str(char *res, intmax_t nbr, + int base, t_params *params); +char *ft_strcp(char *dst, const char *src); +char *ft_strjoin_free(char *s1, char *s2); +void ft_strrev(char *str, int len); +void str_toupper(char *str); +uintmax_t convert_unsigned_arg(va_list ap, t_params *params); +void init_params(t_params *params); +void fill_function_arr(int (**f)(va_list ap, t_params *params)); +int type_id(char c, t_params *params); + +#endif diff --git a/libft.h b/includes/libft.h similarity index 88% rename from libft.h rename to includes/libft.h index 86c95b3..6a97169 100644 --- a/libft.h +++ b/includes/libft.h @@ -1,89 +1,106 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* libft.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: yochered +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/24 10:27:20 by yochered #+# #+# */ -/* Updated: 2018/10/24 10:27:56 by yochered ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef LIBFT_H -# define LIBFT_H -# include - -typedef struct s_list -{ - void *content; - size_t content_size; - struct s_list *next; -} t_list; - -void *ft_memset(void *b, int c, size_t len); -void ft_bzero(void *s, size_t n); -void *ft_memcpy(void *dst, const void *src, size_t n); -void *ft_memccpy(void *dst, const void *src, int c, size_t n); -void *ft_memmove(void *dst, const void *src, size_t len); -void *ft_memchr(const void *s, int c, size_t n); -int ft_memcmp(const void *s1, const void *s2, size_t n); -size_t ft_strlen(const char *s); -char *ft_strdup(const char *s1); -char *ft_strcpy(char *dst, const char *src); -char *ft_strncpy(char *dst, const char *src, size_t len); -char *ft_strcat(char *s1, const char *s2); -char *ft_strncat(char *s1, const char *s2, size_t n); -size_t ft_strlcat(char *dst, const char *src, size_t dstsize); -char *ft_strchr(const char *s, int c); -char *ft_strrchr(const char *s, int c); -char *ft_strstr(const char *haystack, const char *needle); -char *ft_strnstr(const char *haystack, const char *needle, -size_t len); -int ft_strcmp(const char *s1, const char *s2); -int ft_strncmp(const char *s1, const char *s2, size_t n); -int ft_atoi(const char *str); -int ft_isalpha(int c); -int ft_isdigit(int c); -int ft_isalnum(int c); -int ft_isascii(int c); -int ft_isprint(int c); -int ft_toupper(int c); -int ft_tolower(int c); -void *ft_memalloc(size_t size); -void ft_memdel(void **ap); -char *ft_strnew(size_t size); -void ft_strdel(char **as); -void ft_strclr(char *s); -void ft_striter(char *s, void (*f)(char *)); -void ft_striteri(char *s, void (*f)(unsigned int, char *)); -char *ft_strmap(char const *s, char (*f)(char)); -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); -int ft_strequ(char const *s1, char const *s2); -int ft_strnequ(char const *s1, char const *s2, size_t n); -char *ft_strsub(char const *s, unsigned int start, size_t len); -char *ft_strjoin(char const *s1, char const *s2); -char *ft_strtrim(char const *s); -char **ft_strsplit(char const *s, char c); -char *ft_itoa(int n); -void ft_putchar(char c); -void ft_putstr(char const *s); -void ft_putendl(char const *s); -void ft_putnbr(int n); -void ft_putchar_fd(char c, int fd); -void ft_putstr_fd(char const *s, int fd); -void ft_putendl_fd(char const *s, int fd); -void ft_putnbr_fd(int n, int fd); -t_list *ft_lstnew(void const *content, size_t content_size); -void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); -void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); -void ft_lstadd(t_list **alst, t_list *new); -void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); -t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); -char *ft_itoa_base(int value, int base); -void ft_lstrev(t_list **begin_list); -int ft_cycle_detector(t_list *list); -void ft_print_memory(const void *addr, size_t size); -void ft_quick_sort(int **tab, int low, int high); - -#endif +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 10:27:20 by yochered #+# #+# */ +/* Updated: 2018/10/24 10:27:56 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# include +# include +# include "ft_printf.h" +# define BUFF_SIZE 150 +# define CHECK(x) if (!x)return (NULL) + +typedef struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +} t_list; + +typedef struct s_gnl +{ + int fd; + char buf[BUFF_SIZE]; + int i; + int r_size; + struct s_gnl *next; + struct s_gnl *prev; +} t_gnl; + +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memccpy(void *dst, const void *src, int c, size_t n); +void *ft_memmove(void *dst, const void *src, size_t len); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +size_t ft_strlen(const char *s); +char *ft_strdup(const char *s1); +char *ft_strcpy(char *dst, const char *src); +char *ft_strncpy(char *dst, const char *src, size_t len); +char *ft_strcat(char *s1, const char *s2); +char *ft_strncat(char *s1, const char *s2, size_t n); +size_t ft_strlcat(char *dst, const char *src, size_t dstsize); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strstr(const char *haystack, const char *needle); +char *ft_strnstr(const char *haystack, const char *needle, +size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_atoi(const char *str); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +int ft_toupper(int c); +int ft_tolower(int c); +void *ft_memalloc(size_t size); +void ft_memdel(void **ap); +char *ft_strnew(size_t size); +void ft_strdel(char **as); +void ft_strclr(char *s); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +int ft_strequ(char const *s1, char const *s2); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s); +char **ft_strsplit(char const *s, char c); +char *ft_itoa(int n); +int ft_count_digits(int nbr, int base); +void ft_putchar(char c); +void ft_putstr(char const *s); +void ft_putendl(char const *s); +void ft_putnbr(int n); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char const *s, int fd); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr_fd(int n, int fd); +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); +void ft_lstadd(t_list **alst, t_list *new); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); +char *ft_itoa_base(int value, int base); +void ft_lstrev(t_list **begin_list); +int ft_cycle_detector(t_list *list); +void ft_print_memory(const void *addr, size_t size); +void ft_quick_sort(int **tab, int low, int high); +int get_next_line(const int fd, char **line); +int ft_printf(const char *format, ...); + +#endif diff --git a/printf_src/allowed_symbols.c b/printf_src/allowed_symbols.c new file mode 100644 index 0000000..b83c49f --- /dev/null +++ b/printf_src/allowed_symbols.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* allowed_symbols.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/04 15:19:30 by yochered #+# #+# */ +/* Updated: 2018/12/04 15:19:31 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int flag_list(char c) +{ + return (c == '+' || c == '-' || c == ' ' || c == '0' || c == '#'); +} + +int convert_list(char c) +{ + return (c == 'h' || c == 'l' || c == 'j' || c == 'L' || c == 'z'); +} + +int allowed_symbols(char *format) +{ + if (!flag_list(*format) && *format != '*' && *format != '.' + && type_id(*format, NULL) == -1 && !convert_list(*format) + && !ft_isdigit(*format) && *format != '%') + return (0); + return (1); +} diff --git a/printf_src/bonus_types.c b/printf_src/bonus_types.c new file mode 100644 index 0000000..b90b70e --- /dev/null +++ b/printf_src/bonus_types.c @@ -0,0 +1,141 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bonus_types.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/08 11:55:48 by yochered #+# #+# */ +/* Updated: 2018/12/08 11:55:49 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_va_putbinary(va_list ap, t_params *params) +{ + uintmax_t nbr; + int len; + char *res; + char *res_tmp; + + nbr = convert_unsigned_arg(ap, params); + len = count_unsigned_digits(nbr, 2); + if (params->flag & precision) + len = len < params->precision ? params->precision : len; + res = ft_strnew((size_t)len); + if (!(!nbr && (params->flag & precision && !params->precision))) + uint_to_str(res, nbr, 2, params); + res_tmp = res; + if (params->flag & hash && nbr > 0) + { + res = ft_strnew(2); + ft_strcp(res, "0b"); + res = ft_strjoin_free(res, res_tmp); + } + if (params->flag & zero && params->flag & precision) + params->flag = params->flag & ~(1 << (3 - 1)); + return (ft_format_str(res, params)); +} + +char *get_str_22_to_32(char c) +{ + if (c == 22) + return ("[syn]"); + else if (c == 23) + return ("[etb]"); + else if (c == 24) + return ("[can]"); + else if (c == 25) + return ("[em]"); + else if (c == 26) + return ("[sub]"); + else if (c == 27) + return ("[esc]"); + else if (c == 28) + return ("[fs]"); + else if (c == 29) + return ("[gs]"); + else if (c == 30) + return ("[rs]"); + else if (c == 31) + return ("[us]"); + else if (c == 32) + return ("[sp]"); + else if (c == 127) + return ("[del]"); + return (NULL); +} + +char *get_str_11_to_21(char c) +{ + if (c == 11) + return ("[vt]"); + else if (c == 12) + return ("[np]"); + else if (c == 13) + return ("[cr]"); + else if (c == 14) + return ("[so]"); + else if (c == 15) + return ("[si]"); + else if (c == 16) + return ("[dle]"); + else if (c == 17) + return ("[dc1]"); + else if (c == 18) + return ("[dc2]"); + else if (c == 19) + return ("[dc3]"); + else if (c == 20) + return ("[dc4]"); + else if (c == 21) + return ("[nak]"); + else + return (get_str_22_to_32(c)); +} + +char *get_str(char c) +{ + if (c == 0) + return ("[nul]"); + else if (c == 1) + return ("[soh]"); + else if (c == 2) + return ("[stx]"); + else if (c == 3) + return ("[etx]"); + else if (c == 4) + return ("[eot]"); + else if (c == 5) + return ("[enq]"); + else if (c == 6) + return ("[ack]"); + else if (c == 7) + return ("[bel]"); + else if (c == 8) + return ("[bs]"); + else if (c == 9) + return ("[ht]"); + else if (c == 10) + return ("[nl]"); + else + return (get_str_11_to_21(c)); +} + +int ft_va_putnonprint(va_list ap, t_params *params) +{ + char *s; + char *res; + int len; + + s = va_arg(ap, char*); + len = (int)ft_strlen(s); + res = ft_strnew(1); + while (len--) + { + res = ft_strjoin_free(res, ft_strdup(get_str(*s))); + s++; + } + return (ft_format_str(res, params)); +} diff --git a/printf_src/convert_to_str.c b/printf_src/convert_to_str.c new file mode 100644 index 0000000..3e61dc0 --- /dev/null +++ b/printf_src/convert_to_str.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* convert_to_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/29 10:05:07 by yochered #+# #+# */ +/* Updated: 2018/11/29 10:05:08 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int count_unsigned_digits(uintmax_t value, int base) +{ + int digits; + + digits = 0; + if (value == 0) + return (1); + while (value) + { + digits++; + value /= base; + } + return (digits); +} + +int count_signed_digits(intmax_t value, int base) +{ + int digits; + + digits = 0; + if (value <= 0) + digits++; + while (value) + { + digits++; + value /= base; + } + return (digits); +} + +void uint_to_str(char *res, uintmax_t nbr, int base, t_params *params) +{ + char *base_digits; + int i; + + base_digits = "0123456789abcdef"; + i = 0; + if (nbr == 0 && !(params->flag & precision)) + res[i++] = '0'; + while (nbr) + { + res[i++] = base_digits[nbr % base]; + nbr /= base; + } + while (i < params->precision && params->flag & precision) + res[i++] = '0'; + ft_strrev(res, i); +} + +void int_to_str(char *res, intmax_t nbr, int base, t_params *params) +{ + char *base_digits; + int i; + intmax_t nbr_tmp; + + nbr_tmp = nbr; + base_digits = "0123456789abcdef"; + i = 0; + if (nbr == 0 && params && !(params->flag & precision)) + res[i++] = '0'; + while (nbr) + { + res[i++] = base_digits[(nbr < 0 ? -(nbr % base) : nbr % base)]; + nbr /= base; + } + while (params && i < params->precision && params->flag & precision) + res[i++] = '0'; + if (nbr_tmp < 0) + res[i++] = '-'; + ft_strrev(res, i); +} diff --git a/printf_src/function_array.c b/printf_src/function_array.c new file mode 100644 index 0000000..898fd72 --- /dev/null +++ b/printf_src/function_array.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* function_array.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/30 10:50:43 by yochered #+# #+# */ +/* Updated: 2018/11/30 10:50:44 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void fill_function_arr(int (**f)(va_list ap, t_params *params)) +{ + f[0] = &ft_va_putchar; + f[1] = &ft_va_putstr; + f[2] = &ft_va_putnbr; + f[3] = &ft_va_putunbr; + f[4] = &ft_va_putoctal; + f[5] = &ft_va_puthex; + f[6] = &ft_va_putpointer; + f[7] = &ft_va_putfloat; + f[8] = &ft_va_putbinary; + f[9] = &ft_va_putnonprint; +} + +int type_id(char c, t_params *params) +{ + char *str; + int i; + + i = 0; + str = "csduoxpfbr"; + while (str[i]) + { + if (c == str[i] || (c == 'i' && str[i] == 'd') || + (c == 'X' && str[i] == 'x') || (c == 'F' && str[i] == 'f')) + { + if (params) + params->type = c; + return (i); + } + i++; + } + return (-1); +} diff --git a/printf_src/handlers.c b/printf_src/handlers.c new file mode 100644 index 0000000..d332115 --- /dev/null +++ b/printf_src/handlers.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handlers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/11 16:31:32 by yochered #+# #+# */ +/* Updated: 2018/12/11 16:31:38 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void handle_flags(char **format, t_params *params) +{ + char *flags; + int i; + + flags = FLAGS; + while (flag_list(**format)) + { + i = -1; + while (flags[++i]) + if (flags[i] == **format) + params->flag |= ft_power(2, i); + (*format)++; + } +} + +void handle_width(char **format, t_params *params, va_list ap) +{ + if (**format == '*') + { + params->width = va_arg(ap, int); + params->flag |= width; + (*format)++; + } + else if (ft_isdigit(**format)) + { + params->width = ft_atoi(*format); + params->flag |= width; + while (ft_isdigit(**format)) + (*format)++; + } + if (params->flag & width && params->width < 0) + { + params->flag |= minus; + params->width = -params->width; + } +} + +void handle_precision(char **format, t_params *params, va_list ap) +{ + if (**format == '.' && *(*format + 1) == '*') + { + params->precision = va_arg(ap, int); + params->flag |= params->precision >= 0 ? precision : 0; + (*format) += 2; + } + else if (**format == '.' && ft_isdigit(*(*format + 1))) + { + (*format)++; + params->precision = ft_atoi(*format); + params->flag |= params->precision >= 0 ? precision : 0; + while (ft_isdigit(**format)) + (*format)++; + } + else if (**format == '.' && !ft_isdigit(*(*format + 1))) + { + (*format)++; + params->precision = 0; + params->flag |= params->precision >= 0 ? precision : 0; + } +} + +void handle_length(char **format, t_params *params) +{ + if (**format == 'h' && *(*format + 1) != 'h' && params->e_convert < h) + params->e_convert = h; + else if (**format == 'h' && *(*format + 1) == 'h') + params->e_convert = hh; + else if (**format == 'l' && *(*format + 1) != 'l' && params->e_convert < l) + params->e_convert = l; + else if (**format == 'l' && *(*format + 1) == 'l' && params->e_convert < ll) + params->e_convert = ll; + else if (**format == 'j' && params->e_convert < j) + params->e_convert = j; + else if (**format == 'z' && params->e_convert < z) + params->e_convert = z; + else if (**format == 'L') + params->e_convert = L; + (*format) += params->e_convert == hh || params->e_convert == ll ? 2 : 1; +} + +void reach_type(const char **format, t_params *params, va_list ap) +{ + while (allowed_symbols((char*)*format) && + type_id(**format, NULL) == -1 && **format != '%') + { + if (flag_list(**format)) + handle_flags((char**)format, params); + else if (ft_isdigit(**format) || **format == '*') + handle_width((char**)format, params, ap); + else if (**format == '.') + handle_precision((char**)format, params, ap); + else if (convert_list(**format)) + handle_length((char**)format, params); + } +} diff --git a/printf_src/mem_funcs.c b/printf_src/mem_funcs.c new file mode 100644 index 0000000..22d23f5 --- /dev/null +++ b/printf_src/mem_funcs.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_funcs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/11 16:36:21 by yochered #+# #+# */ +/* Updated: 2018/12/11 16:36:22 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strjoin_free(char *s1, char *s2) +{ + char *str_tmp; + char *res_tmp; + char *res; + + res = ft_strnew(ft_strlen(s1) + ft_strlen(s2)); + str_tmp = s1; + res_tmp = res; + while (*s1) + *res++ = *s1++; + if (str_tmp) + free(str_tmp); + str_tmp = s2; + while (*s2) + *res++ = *s2++; + if (str_tmp) + free(str_tmp); + *res = '\0'; + return (res_tmp); +} + +void init_params(t_params *params) +{ + params->e_convert = 0; + params->e_flags = 0; + params->flag = 0; +} + +intmax_t ft_power(intmax_t nb, int power) +{ + intmax_t res; + + res = 1; + if (power <= 0) + return (1); + while (power--) + res *= nb; + return (res); +} + +double ft_pow(double nbr, int pow) +{ + double res; + + res = 1; + while (pow) + { + res = pow < 0 ? res / nbr : res * nbr; + pow += pow < 0 ? 1 : -1; + } + return (res); +} diff --git a/printf_src/print_float.c b/printf_src/print_float.c new file mode 100644 index 0000000..e4c4c0b --- /dev/null +++ b/printf_src/print_float.c @@ -0,0 +1,107 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_float.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/04 18:25:19 by yochered #+# #+# */ +/* Updated: 2018/12/04 18:25:20 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void get_limit(char *res, long double nbr, t_params *params) +{ + if (nbr != nbr) + ft_strcp(res, "nan"); + else if (nbr == 1.0 / 0.0) + ft_strcp(res, "inf"); + else if (nbr == -1.0 / 0.0) + ft_strcp(res, "inf"); + *(res + 3) = '\0'; + if (params->type == 'F') + str_toupper(res); + if (params->flag & zero) + params->flag = params->flag & ~(1 << (3 - 1)); +} + +int ft_int_to_str(intmax_t nbr, char *res, int precision) +{ + int i; + + i = 0; + while (nbr) + { + res[i++] = (char)((nbr < 0 ? -(nbr % 10) : nbr % 10) + '0'); + nbr /= 10; + } + while (i < precision) + res[i++] = '0'; + ft_strrev(res, i); + return (i); +} + +void ft_dtoa(long double nbr, char *res, int precision) +{ + int i; + int k; + + nbr += 0.5 / ft_pow(10, precision); + i = ft_int_to_str((intmax_t)nbr, res, 1); + nbr -= (intmax_t)nbr; + k = -1; + if (precision) + { + res[i++] = '.'; + while (++k < precision) + { + nbr *= 10; + res[i++] = (char)((int)nbr + 48); + nbr -= (int)nbr; + } + } + res[i] = '\0'; +} + +char *putfloat(long double nbr, t_params *params) +{ + char *res; + + res = ft_strnew(((size_t)count_signed_digits((intmax_t)nbr, 10) + + 1 + (params->flag & precision ? params->precision : 6))); + if ((nbr < 0 || (!nbr && 1.0 / nbr == 1.0 / -0.0) || params->flag & plus + || params->flag & space) && nbr == nbr) + { + if (nbr < 0 || (!nbr && 1.0 / nbr == 1.0 / -0.0)) + *res++ = '-'; + else + *res++ = (char)(params->flag & space + && !(params->flag & plus) ? ' ' : '+'); + } + if (nbr != nbr || nbr == 1.0 / 0.0 || nbr == -1.0 / 0.0) + get_limit(res, nbr, params); + else + ft_dtoa(ABS(nbr), res, params->flag & precision + ? params->precision : 6); + if (params->flag & precision && !params->precision && params->flag & hash) + *(res + ft_strlen(res)) = '.'; + if ((nbr < 0 || (!nbr && 1.0 / nbr == 1.0 / -0.0) || params->flag & plus + || params->flag & space) && nbr == nbr) + res--; + return (res); +} + +int ft_va_putfloat(va_list ap, t_params *params) +{ + long double nbr; + char *res; + + if (params->e_convert == L) + nbr = va_arg(ap, long double); + else + nbr = va_arg(ap, double); + res = putfloat(nbr, params); + return (ft_format_str(res, params)); +} diff --git a/printf_src/print_funcs.c b/printf_src/print_funcs.c new file mode 100644 index 0000000..545a72d --- /dev/null +++ b/printf_src/print_funcs.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_funcs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/10 11:56:28 by yochered #+# #+# */ +/* Updated: 2018/12/10 11:56:29 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putnstr(char *str, int n) +{ + while (*str && n--) + { + ft_putchar(*str); + str++; + } +} + +void print_padding(int size, char c) +{ + while (size-- > 0) + ft_putchar(c); +} + +int print_percent(char c, t_params *params) +{ + int len; + + len = c ? 1 : 0; + if (params->flag & width) + len = params->width > len ? params->width : len; + if (params->flag & width && !(params->flag & minus)) + print_padding(params->width - 1, params->flag & zero ? '0' : ' '); + if (c) + ft_putchar(c); + if (params->flag & width && params->flag & minus) + print_padding(params->width - 1, ' '); + return (len); +} diff --git a/printf_src/print_int.c b/printf_src/print_int.c new file mode 100644 index 0000000..e286d57 --- /dev/null +++ b/printf_src/print_int.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/04 16:19:04 by yochered #+# #+# */ +/* Updated: 2018/12/04 16:19:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +intmax_t convert_signed_arg(va_list ap, t_params *params) +{ + intmax_t nbr; + + nbr = va_arg(ap, intmax_t); + if (params->e_convert == hh) + nbr = (char)nbr; + else if (params->e_convert == h) + nbr = (short)nbr; + else if (params->e_convert == l) + nbr = (long)nbr; + else if (params->e_convert == ll) + nbr = (long long)nbr; + else if (params->e_convert == j) + nbr = (intmax_t)nbr; + else if (params->e_convert == z) + nbr = (size_t)nbr; + else + nbr = (int)nbr; + return (nbr); +} + +int ft_va_putnbr(va_list ap, t_params *params) +{ + char *str; + char *str_tmp; + intmax_t nbr; + size_t len; + + nbr = convert_signed_arg(ap, params); + len = (size_t)count_signed_digits(nbr, 10); + if (params->flag & precision) + len = len < (size_t)params->precision ? (size_t)params->precision : len; + if (((params->flag & plus || params->flag & space) && nbr >= 0) || nbr < 0) + str = ft_strnew(len + 1); + else + str = ft_strnew(len); + str_tmp = str; + if (params->flag & space && !(params->flag & plus) && nbr >= 0) + *str_tmp++ = ' '; + if (params->flag & plus && nbr >= 0) + *str_tmp++ = '+'; + if (!(!nbr && (params->flag & precision && !params->precision))) + int_to_str(str_tmp, nbr, 10, params); + if (params->flag & hash) + params->flag = params->flag & ~(1 << (5 - 1)); + if (params->flag & zero && params->flag & precision) + params->flag = params->flag & ~(1 << (3 - 1)); + return (ft_format_str(str, params)); +} diff --git a/printf_src/print_pointer.c b/printf_src/print_pointer.c new file mode 100644 index 0000000..7c8aff1 --- /dev/null +++ b/printf_src/print_pointer.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_pointer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/30 11:24:52 by yochered #+# #+# */ +/* Updated: 2018/11/30 11:24:53 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_va_putpointer(va_list ap, t_params *params) +{ + uintmax_t addr; + char *res; + size_t len; + char *res_tmp; + + addr = va_arg(ap, uintmax_t); + len = (size_t)count_unsigned_digits(addr, 16); + len = params->flag & precision && params->precision > (int)len ? + params->precision : (int)len; + res_tmp = ft_strnew(len); + uint_to_str(res_tmp, addr, 16, params); + if (!addr && params->flag & precision && !params->precision) + ft_strclr(res_tmp); + res = ft_strnew(sizeof(char) * (len + 2)); + ft_strcp(res, "0x"); + ft_strcp(res + 2, res_tmp); + free(res_tmp); + res_tmp = res; + while (*res_tmp++) + if (*res_tmp >= 'A' && *res_tmp <= 'F') + *res_tmp += 32; + return (ft_format_str(res, params)); +} diff --git a/printf_src/print_str.c b/printf_src/print_str.c new file mode 100644 index 0000000..4965454 --- /dev/null +++ b/printf_src/print_str.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/29 10:08:38 by yochered #+# #+# */ +/* Updated: 2018/11/29 10:08:39 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void skip_flag_symbols(t_params *params, char **s) +{ + int len; + + len = 0; + if ((params->flag & hash && (params->type == 'o')) + || **s == '+' || **s == '-' || **s == ' ') + len = 1; + if (*(*s + 1) == 'x' || *(*s + 1) == 'X' || *(*s + 1) == 'b') + len = 2; + ft_putnstr(*s, len); + params->width -= len; + *s += len; +} + +int ft_format_str(char *s, t_params *params) +{ + int len; + char *str_tmp; + + len = (int)ft_strlen(s); + len = params->flag & width && params->width > len ? params->width : len; + str_tmp = s; + if (params->flag & width && !(params->flag & minus) + && !(params->flag & zero)) + print_padding(params->width - (int)ft_strlen(s), ' '); + if (params->flag & width && !(params->flag & minus) && params->flag & zero) + { + if (params->flag & hash || *s == '+' || *s == '-' || *s == ' ' + || params->type == 'p') + skip_flag_symbols(params, &s); + print_padding(params->width - (int)ft_strlen(s), '0'); + } + ft_putstr(s); + if (params->flag & width && params->flag & minus) + print_padding(params->width - (int)ft_strlen(s), ' '); + free(str_tmp); + return (len); +} + +int ft_va_putchar(va_list ap, t_params *params) +{ + int len; + char c; + + c = (char)va_arg(ap, int); + len = 1; + if (params->flag & width) + len = params->width > len ? params->width : len; + if (params->flag & width && !(params->flag & minus)) + print_padding(params->width - 1, params->flag & zero ? '0' : ' '); + ft_putchar(c); + if (params->flag & width && params->flag & minus) + print_padding(params->width - 1, ' '); + return (len); +} + +int ft_va_putstr(va_list ap, t_params *params) +{ + char *s; + char *res; + + s = va_arg(ap, char*); + if (!s) + s = "(null)"; + res = ft_strdup(s); + if (params->flag & precision && params->precision < (int)ft_strlen(s)) + { + s = ft_strsub(res, 0, (size_t)params->precision); + free(res); + res = s; + } + if (params->flag & hash) + params->flag = params->flag & ~(1 << (5 - 1)); + return (ft_format_str(res, params)); +} diff --git a/printf_src/print_uint.c b/printf_src/print_uint.c new file mode 100644 index 0000000..0d67e12 --- /dev/null +++ b/printf_src/print_uint.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_uint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/29 10:04:14 by yochered #+# #+# */ +/* Updated: 2018/11/29 10:04:16 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +uintmax_t convert_unsigned_arg(va_list ap, t_params *params) +{ + uintmax_t nbr; + + nbr = va_arg(ap, uintmax_t); + if (params->e_convert == hh) + nbr = (unsigned char)nbr; + else if (params->e_convert == h) + nbr = (unsigned short)nbr; + else if (params->e_convert == l) + nbr = (unsigned long)nbr; + else if (params->e_convert == ll) + nbr = (unsigned long long)nbr; + else if (params->e_convert == j) + nbr = (uintmax_t)nbr; + else if (params->e_convert == z) + nbr = (size_t)nbr; + else + nbr = (unsigned int)nbr; + return (nbr); +} + +int ft_va_putunbr(va_list ap, t_params *params) +{ + char *str; + uintmax_t nbr; + size_t len; + + nbr = convert_unsigned_arg(ap, params); + len = (size_t)count_unsigned_digits(nbr, 10); + if (params->flag & precision) + len = (int)len < params->precision ? params->precision : (int)len; + str = ft_strnew(len); + uint_to_str(str, nbr, 10, params); + if (params->flag & hash) + params->flag = params->flag & ~(1 << (5 - 1)); + if (params->flag & zero && params->flag & precision) + params->flag = params->flag & ~(1 << (3 - 1)); + return (ft_format_str(str, params)); +} + +int ft_va_putoctal(va_list ap, t_params *params) +{ + char *str; + char *str_tmp; + uintmax_t nbr; + size_t len; + + nbr = convert_unsigned_arg(ap, params); + len = (size_t)count_unsigned_digits(nbr, 8); + if (params->flag & precision) + len = (int)len < params->precision ? params->precision : (int)len; + str = ft_strnew(len); + str_tmp = str; + if (!(!nbr && (params->flag & precision && !params->precision))) + uint_to_str(str, nbr, 8, params); + if (params->flag & hash && (nbr > 0 || params->flag & precision) + && (!(params->flag & precision) + || params->precision <= count_unsigned_digits(nbr, 8))) + { + str = ft_strnew(1); + ft_strcp(str, "0"); + str = ft_strjoin_free(str, str_tmp); + } + if (params->flag & zero && params->flag & precision) + params->flag = params->flag & ~(1 << (3 - 1)); + return (ft_format_str(str, params)); +} + +int ft_va_puthex(va_list ap, t_params *params) +{ + char *str; + char *str_tmp; + uintmax_t nbr; + size_t len; + + nbr = convert_unsigned_arg(ap, params); + len = (size_t)count_unsigned_digits(nbr, 16); + if (params->flag & precision) + len = (int)len < params->precision ? params->precision : (int)len; + str = ft_strnew(len); + str_tmp = str; + if (!(!nbr && (params->flag & precision && !params->precision))) + uint_to_str(str, nbr, 16, params); + if (params->flag & hash && nbr > 0) + { + str = ft_strnew(2); + ft_strcp(str, "0x"); + str = ft_strjoin_free(str, str_tmp); + } + if (params->flag & zero && params->flag & precision) + params->flag = params->flag & ~(1 << (3 - 1)); + if (params->type == 'X') + str_toupper(str); + return (ft_format_str(str, params)); +} diff --git a/printf_src/str_functions.c b/printf_src/str_functions.c new file mode 100644 index 0000000..f54c89d --- /dev/null +++ b/printf_src/str_functions.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_functions.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/29 10:14:45 by yochered #+# #+# */ +/* Updated: 2018/11/29 10:14:46 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void str_toupper(char *str) +{ + while (*str) + { + if (*str >= 'a' && *str <= 'z') + *str -= 32; + str++; + } +} + +void ft_strrev(char *str, int len) +{ + char c; + int i; + + i = -1; + while (++i < --len) + { + c = str[i]; + str[i] = str[len]; + str[len] = c; + } +} + +char *ft_strcp(char *dst, const char *src) +{ + char *dst_tmp; + + dst_tmp = dst; + while (*src) + *dst_tmp++ = *src++; + return (dst); +}