diff options
author | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-01-17 19:34:53 +0100 |
---|---|---|
committer | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-01-17 19:34:53 +0100 |
commit | a287db1124beda38507739f892c085bd3654ebd7 (patch) | |
tree | c439a25efe0309de08087d439a597f84583b257f /libft | |
parent | Removed libft (diff) | |
download | 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.tar.gz 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.tar.bz2 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.tar.xz 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.tar.zst 42-cub3d-a287db1124beda38507739f892c085bd3654ebd7.zip |
Added libft
Diffstat (limited to 'libft')
93 files changed, 4012 insertions, 0 deletions
diff --git a/libft/.gitignore b/libft/.gitignore new file mode 100644 index 0000000..68ff059 --- /dev/null +++ b/libft/.gitignore @@ -0,0 +1,112 @@ +# Created by https://www.gitignore.io/api/c,emacs +# Edit at https://www.gitignore.io/?templates=c,emacs + +### C ### +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data + + +# End of https://www.gitignore.io/api/c,emacs + +main.c +*.txt diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..051fcd7 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,175 @@ +# **************************************************************************** # +# LE - / # +# / # +# Makefile .:: .:/ . .:: # +# +:+:+ +: +: +:+:+ # +# By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ # +# #+# #+ #+ #+# # +# Created: 2019/12/11 13:14:43 by rbousset #+# ## ## #+# # +# Updated: 2019/12/11 18:34:05 by rbousset ### #+. /#+ ###.fr # +# / # +# / # +# **************************************************************************** # + +default: all +#==============================================================================# +#--------------------------------- Shell --------------------------------------# +#==============================================================================# +SHELL := /bin/sh +#==============================================================================# +#------------------------------ Directories -----------------------------------# +#==============================================================================# +INCS_DIR = inc/ +SRCS_DIR = src/ +OBJS_DIR = obj/ +#==============================================================================# +#--------------------------------- Files --------------------------------------# +#==============================================================================# +INCS = libft.h +#------------------------------------------------------------------------------# +SRCS_NAME = ft_memset.c +SRCS_NAME += ft_bzero.c +SRCS_NAME += ft_memcpy.c +SRCS_NAME += ft_memccpy.c +SRCS_NAME += ft_memmove.c +SRCS_NAME += ft_memchr.c +SRCS_NAME += ft_memlchr.c +SRCS_NAME += ft_memcmp.c +SRCS_NAME += ft_strlen.c +SRCS_NAME += ft_isalpha.c +SRCS_NAME += ft_isdigit.c +SRCS_NAME += ft_isalnum.c +SRCS_NAME += ft_isascii.c +SRCS_NAME += ft_isprint.c +SRCS_NAME += ft_ischarset.c +SRCS_NAME += ft_tolower.c +SRCS_NAME += ft_toupper.c +SRCS_NAME += ft_strchr.c +SRCS_NAME += ft_strrchr.c +SRCS_NAME += ft_strlchr.c +SRCS_NAME += ft_strncmp.c +SRCS_NAME += ft_strlcpy.c +SRCS_NAME += ft_strlcat.c +SRCS_NAME += ft_strnstr.c +SRCS_NAME += ft_atoi.c +SRCS_NAME += ft_calloc.c +SRCS_NAME += ft_nrealloc.c +SRCS_NAME += ft_strdup.c +SRCS_NAME += ft_substr.c +SRCS_NAME += ft_strjoin.c +SRCS_NAME += ft_strtrim.c +SRCS_NAME += ft_split.c +SRCS_NAME += ft_itoa.c +SRCS_NAME += ft_itoa_base.c +SRCS_NAME += ft_uitoa.c +SRCS_NAME += ft_uitoa_base.c +SRCS_NAME += ft_strmapi.c +SRCS_NAME += ft_putchar_fd.c +SRCS_NAME += ft_putstr_fd.c +SRCS_NAME += ft_putendl_fd.c +SRCS_NAME += ft_putnbr_fd.c +SRCS_NAME += ft_lstnew.c +SRCS_NAME += ft_lstadd_front.c +SRCS_NAME += ft_lstsize.c +SRCS_NAME += ft_lstlast.c +SRCS_NAME += ft_lstadd_back.c +SRCS_NAME += ft_lstdelone.c +SRCS_NAME += ft_lstclear.c +SRCS_NAME += ft_lstiter.c +SRCS_NAME += ft_lstmap.c +SRCS_NAME += ft_putchar.c +SRCS_NAME += ft_putnchar.c +SRCS_NAME += ft_putstr.c +SRCS_NAME += ft_putendl.c +SRCS_NAME += ft_putnbr.c +SRCS_NAME += ft_putnbr_base.c +SRCS_NAME += ft_strnlen.c +SRCS_NAME += ft_strcat.c +SRCS_NAME += ft_strcmp.c +SRCS_NAME += ft_isspace.c +SRCS_NAME += ft_sqrt.c +SRCS_NAME += ft_intlen.c +SRCS_NAME += ft_intlen_base.c +SRCS_NAME += ft_uintlen.c +SRCS_NAME += ft_uintlen_base.c +SRCS_NAME += ft_nstr.c +SRCS_NAME += ft_memdel.c +SRCS_NAME += ft_kernel_panic.c +SRCS_NAME += get_next_line.c +SRCS_NAME += ft_printf.c +SRCS_NAME += ft_dprintf.c +SRCS_NAME += ft_sprintf.c +SRCS_NAME += ft_printf_init_struct.c +SRCS_NAME += ft_printf_get_flags.c +SRCS_NAME += ft_printf_use_flags.c +SRCS_NAME += ft_printf_get_width_nstr.c +SRCS_NAME += ft_printf_treat_flags.c +SRCS_NAME += ft_printf_parts.c +SRCS_NAME += ft_printf_put_width.c +SRCS_NAME += ft_printf_put_precision.c +SRCS_NAME += ft_printf_put_char.c +SRCS_NAME += ft_printf_put_str.c +SRCS_NAME += ft_printf_put_ptr.c +SRCS_NAME += ft_printf_put_int.c +SRCS_NAME += ft_printf_put_hex.c +SRCS_NAME += ft_printf_put_none.c +SRCS_NAME += ft_printf_get_s_putlen.c +SRCS_NAME += ft_printf_process.c +SRCS_NAME += ft_printf_cat_output.c +SRCS_NAME += ft_printf_flag_to_atoi.c +#------------------------------------------------------------------------------# +SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) +#------------------------------------------------------------------------------# +OBJS = $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS}) +#==============================================================================# +#-------------------------------- Compiler ------------------------------------# +#==============================================================================# +CC = clang +CFLAGS = -std=c89 +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Werror +CFLAGS += -pedantic +ifdef ASAN + CFLAGS += ${DEBUG} + CFLAGS += ${FSANITIZE} +endif +#------------------------------------------------------------------------------# +DEBUG = -glldb +FSANITIZE = -fsanitize=address +#------------------------------------------------------------------------------# +NAME = libft.a +#==============================================================================# +#-------------------------------- Library -------------------------------------# +#==============================================================================# +AR = ar rcs +#==============================================================================# +#--------------------------------- UNIX ---------------------------------------# +#==============================================================================# +MKDIR = mkdir -p +RM = rm -rf +#==============================================================================# +#--------------------------------- Rules --------------------------------------# +#==============================================================================# +${OBJS_DIR}%.o: ${SRCS_DIR}%.c ${INCS_DIR}${INCS} + @${MKDIR} ${OBJS_DIR} + ${CC} -c ${CFLAGS} -I${INCS_DIR} -o $@ $< +#------------------------------------------------------------------------------# +${NAME}: ${OBJS} + ${AR} ${NAME} ${OBJS} +#------------------------------------------------------------------------------# +all: ${NAME} +#------------------------------------------------------------------------------# +clean: + ${RM} ${OBJS_DIR} +#------------------------------------------------------------------------------# +fclean: clean + ${RM} ${NAME} +#------------------------------------------------------------------------------# +re: fclean all +#------------------------------------------------------------------------------# +run: ${OBJS} + ${CC} ${CFLAGS} -Iinc/ -o a.out ${OBJS} ${SRCS_DIR}main.c +#------------------------------------------------------------------------------# +.PHONY: all clean clean fclean re run default +#==================================== EOF =====================================# diff --git a/libft/inc/libft.h b/libft/inc/libft.h new file mode 100644 index 0000000..58dbbc2 --- /dev/null +++ b/libft/inc/libft.h @@ -0,0 +1,204 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* libft.h .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 18:15:13 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 14:17:02 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +# ifndef LIBFT_H +# define LIBFT_H + +#include <stddef.h> +#include <stdarg.h> +#include <inttypes.h> + +# define FT_MIN_HEX_BASE "0123456789abcdef" +# define FT_MAJ_HEX_BASE "0123456789ABCDEF" +# define FT_PRINTF_CONV_CHARSET "cspdiuxX%" +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 72 +# endif + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +typedef struct s_printflist +{ + int putlen; + char conv; + char actconv; + char *fullflag; + size_t flaglen; + int width; + int precision; + uint8_t isreverse; + uint8_t isneg; + uint8_t isaz; + int8_t isaspace; + uint8_t isaplus; + int8_t lh; + uint8_t zflag; + uint8_t issharp; + uint8_t isminus; + int fulllen; + char *output; +} t_printflist; + +/* +** VOID +*/ + +void ft_bzero(void *s, size_t n); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_lstadd_front(t_list **alst, t_list *new); +void ft_lstadd_back(t_list **alst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +void ft_kernel_panic(void); +void ft_putnbr(long nb); +void ft_putnbr_base(long nb, char *base); +void ft_memdel(void *ptr); +void *ft_memset(void *b, int c, size_t len); +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); +void *ft_calloc(size_t count, size_t size); +void *ft_nrealloc(void *ptr, size_t oldsize, size_t newsize); + +/* +** CHAR +*/ + +char *ft_strcat(char *s1, const char *s2); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strnstr(const char *haystack, const char *needle, + size_t len); +char *ft_strdup(const char *s1); +char *ft_substr(const char *s, unsigned int start, + size_t len); +char *ft_strjoin(const char *s1, const char *s2); +char *ft_strtrim(const char *s1, const char *set); +char *ft_itoa(long n); +char *ft_itoa_base(long n, char *base); +char *ft_uitoa(unsigned long n); +char *ft_uitoa_base(unsigned long n, char *base); +char *ft_strmapi(const char *s, + char (*f)(unsigned int, char)); +char *ft_nstr(size_t size); +char **ft_split(const char *s, char c); + +/* +** INT +*/ + +uint8_t ft_isspace(int c); +uint8_t ft_ischarset(const char *charset, int c); +uint8_t ft_intlen(long n); +uint8_t ft_intlen_base(long n, char *base); +uint8_t ft_uintlen(unsigned long n); +uint8_t ft_uintlen_base(unsigned long n, char *base); +int ft_memcmp(const void *s1, const void *s2, size_t n); +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); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_lstsize(t_list *lst); +int ft_atoi(const char *str); +int ft_putchar(int c); +int ft_putnchar(int c, const size_t n); +int ft_putstr(const char *s); +int ft_putendl(const char *s); +int ft_putchar_fd(char c, int fd); +int ft_putstr_fd(char *s, int fd); +int ft_strcmp(const char *s1, const char *s2); +int get_next_line(int fd, char **line); +long ft_memlchr(const void *s, int c, size_t n); +long ft_strlchr(const char *s, int c); +size_t ft_strlen(const char *s); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlcat(char *dst, const char *src, size_t size); +size_t ft_strnlen(const char *s, size_t size); +double ft_sqrt(double x); + +/* +** LIST +*/ + +t_list *ft_lstnew(void *content); +t_list *ft_lstlast(t_list *lst); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), + void (*del)(void *)); +/* +** ft_printf +*/ + +void ft_printf_process(const char *format, + va_list arg, + t_printflist *pflist); +void ft_printf_reinit_struct(t_printflist *pflist); +void ft_printf_cat_output(char *src, + size_t len, + t_printflist *format); +void ft_printf_treat_flags(va_list arg, + t_printflist *pflist); +void ft_printf_put_c(va_list arg, t_printflist *pflist); +void ft_printf_put_s(va_list arg, t_printflist *pflist); +void ft_printf_put_p(va_list arg, t_printflist *pflist); +void ft_printf_put_d(va_list arg, t_printflist *pflist); +void ft_printf_put_u(va_list arg, t_printflist *pflist); +void ft_printf_put_x(va_list arg, t_printflist *pflist); +void ft_printf_put_big_x(va_list arg, t_printflist *pflist); +void ft_printf_put_perc(va_list arg, t_printflist *pflist); +void ft_printf_put_none(t_printflist *pflist); +void ft_printf_put_width_pre(t_printflist *pflist); +void ft_printf_put_width_post(t_printflist *pflist); +char *ft_printf_get_flags(const char *format, + int pos, + t_printflist *pflist); +char *ft_printf_flag_to_atoi(char *str); +char *ft_printf_get_width_nstr(char *str, + t_printflist *pflist); +uint8_t ft_printf_is_multiwrite(const char *format); +int ft_printf_flags(const char *format, + int pos, + va_list arg, + t_printflist *pflist); +int ft_printf(const char *format, + ...) __attribute__((format(printf,1,2))); +int ft_dprintf(int fd, + const char *format, + ...) __attribute__((format(printf,2,3))); +int ft_sprintf(char *str, + const char *format, + ...) __attribute__((format(printf,2,3))); +int ft_printf_get_partlen(const char *format); +int ft_printf_putpart(const char *format, + int start, + int len, + t_printflist *pflist); +int ft_printf_put_precision(t_printflist *pflist); +int ft_printf_get_s_putlen(char *str, t_printflist *pflist); +int ft_printf_fetch_width(va_list arg, + char *nstr, + t_printflist *pflist); +t_printflist *ft_printf_init_struct(const char *format); + +# endif diff --git a/libft/src/ft_atoi.c b/libft/src/ft_atoi.c new file mode 100644 index 0000000..4459c1d --- /dev/null +++ b/libft/src/ft_atoi.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_atoi.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/10 05:32:13 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/15 02:16:20 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <inttypes.h> + +static int8_t + ft_setsign(const char c) +{ + int8_t sign; + + sign = 1; + if (c == '-') + sign = -1; + return (sign); +} + +static uint8_t + ft_seti(const char *str) +{ + uint8_t i; + + i = 0; + while (ft_isspace(str[i])) + i++; + return (i); +} + +int + ft_atoi(const char *str) +{ + uint8_t i; + int8_t sign; + long nb; + + i = ft_seti(str); + nb = 0; + sign = 1; + if (str[i] == '+' || str[i] == '-') + sign = ft_setsign(str[i++]); + while (ft_isdigit(str[i])) + { + if (nb * 10 + (str[i] - 48) < nb) + { + if (sign < 0) + return (0); + return (-1); + } + nb = nb * 10 + (str[i] - 48); + i++; + } + nb *= sign; + return ((int)nb); +} diff --git a/libft/src/ft_bzero.c b/libft/src/ft_bzero.c new file mode 100644 index 0000000..1f09fbe --- /dev/null +++ b/libft/src/ft_bzero.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_bzero.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/08 14:04:55 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:45:28 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +void + ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/src/ft_calloc.c b/libft/src/ft_calloc.c new file mode 100644 index 0000000..4ec518e --- /dev/null +++ b/libft/src/ft_calloc.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_calloc.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/11 02:47:15 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:43:53 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> + +void + *ft_calloc(size_t count, size_t size) +{ + void *ptr; + + ptr = 0; + ptr = malloc(count * size); + if (!ptr) + return (NULL); + ft_bzero(ptr, count * size); + return (ptr); +} diff --git a/libft/src/ft_dprintf.c b/libft/src/ft_dprintf.c new file mode 100644 index 0000000..d9f69c1 --- /dev/null +++ b/libft/src/ft_dprintf.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_dprintf.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2020/01/17 14:38:00 by rbousset #+# ## ## #+# */ +/* Updated: 2020/01/17 14:38:01 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> +#include <stdarg.h> +#include <unistd.h> + +static int + ft_printf_return(int fd, t_printflist *pflist) +{ + int ret; + + ret = write(fd, pflist->output, pflist->fulllen); + ft_memdel(pflist->output); + ft_memdel(pflist->fullflag); + ft_memdel(pflist); + return (ret); +} + +int + ft_dprintf(int fd, const char *format, ...) +{ + t_printflist *pflist; + va_list arg; + int pos; + + if (!format) + return (-1); + if ((pos = ft_strlchr(format, '%')) < 0) + return (write(fd, format, ft_strlen(format))); + else + { + pflist = ft_printf_init_struct(format); + ft_printf_putpart(format, 0, pos, pflist); + va_start(arg, format); + while (pos >= 0) + { + pos = ft_printf_flags(format, pos, arg, pflist); + ft_printf_reinit_struct(pflist); + } + va_end(arg); + return (ft_printf_return(fd, pflist)); + } + return (0); +} diff --git a/libft/src/ft_intlen.c b/libft/src/ft_intlen.c new file mode 100644 index 0000000..1ed6725 --- /dev/null +++ b/libft/src/ft_intlen.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_intlen.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/07 02:24:29 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/07 02:24:30 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <inttypes.h> + +uint8_t + ft_intlen(long n) +{ + uint8_t len; + + len = 0; + if (!n) + return (1); + if (n < 0) + len = 1; + while (n != 0) + { + n /= 10; + len++; + } + return (len); +} diff --git a/libft/src/ft_intlen_base.c b/libft/src/ft_intlen_base.c new file mode 100644 index 0000000..a590f3a --- /dev/null +++ b/libft/src/ft_intlen_base.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_intlen_base.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/19 17:22:26 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/19 17:22:27 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <inttypes.h> + +uint8_t + ft_intlen_base(long n, char *base) +{ + uint8_t len; + uint8_t size; + + size = ft_strlen(base); + len = 0; + if (!n) + return (1); + if (n < 0) + len = 1; + while (n != 0) + { + n /= size; + len++; + } + return (len); +} diff --git a/libft/src/ft_isalnum.c b/libft/src/ft_isalnum.c new file mode 100644 index 0000000..1ab9ca2 --- /dev/null +++ b/libft/src/ft_isalnum.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_isalnum.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 18:07:44 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:45:57 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +int + ft_isalnum(int c) +{ + if (ft_isalpha(c) || ft_isdigit(c)) + return (1); + return (0); +} diff --git a/libft/src/ft_isalpha.c b/libft/src/ft_isalpha.c new file mode 100644 index 0000000..9e95bd6 --- /dev/null +++ b/libft/src/ft_isalpha.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_isalpha.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 16:45:42 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:42:03 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +static int + ft_isupper(int c) +{ + if (c >= 65 && c <= 90) + return (1); + return (0); +} + +static int + ft_islower(int c) +{ + if (c >= 97 && c <= 122) + return (1); + return (0); +} + +int + ft_isalpha(int c) +{ + if (ft_isupper(c) || ft_islower(c)) + return (1); + return (0); +} diff --git a/libft/src/ft_isascii.c b/libft/src/ft_isascii.c new file mode 100644 index 0000000..937ead5 --- /dev/null +++ b/libft/src/ft_isascii.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_isascii.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 18:18:31 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:41:32 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +int + ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/libft/src/ft_ischarset.c b/libft/src/ft_ischarset.c new file mode 100644 index 0000000..8eea456 --- /dev/null +++ b/libft/src/ft_ischarset.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_ischarset.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/22 16:52:31 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/22 16:52:32 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <inttypes.h> + +uint8_t + ft_ischarset(const char *charset, int c) +{ + size_t i; + + i = 0; + while (charset[i]) + { + if (charset[i] == c) + return (1); + i++; + } + return (0); +} diff --git a/libft/src/ft_isdigit.c b/libft/src/ft_isdigit.c new file mode 100644 index 0000000..52ededc --- /dev/null +++ b/libft/src/ft_isdigit.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_isdigit.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 17:46:41 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:41:27 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +int + ft_isdigit(int c) +{ + if (c >= 48 && c <= 57) + return (1); + return (0); +} diff --git a/libft/src/ft_isprint.c b/libft/src/ft_isprint.c new file mode 100644 index 0000000..d82ded0 --- /dev/null +++ b/libft/src/ft_isprint.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_isprint.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 18:23:39 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:41:21 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +int + ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/libft/src/ft_isspace.c b/libft/src/ft_isspace.c new file mode 100644 index 0000000..c9cf255 --- /dev/null +++ b/libft/src/ft_isspace.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_isspace.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/10 05:57:19 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:44:18 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <inttypes.h> + +uint8_t + ft_isspace(int c) +{ + if (c == '\t' || + c == '\n' || + c == '\v' || + c == '\f' || + c == '\r' || + c == ' ') + return (1); + return (0); +} diff --git a/libft/src/ft_itoa.c b/libft/src/ft_itoa.c new file mode 100644 index 0000000..f70f889 --- /dev/null +++ b/libft/src/ft_itoa.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_itoa.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 02:22:48 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 13:35:46 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <inttypes.h> +#include <stdlib.h> + +char + *ft_itoa(long n) +{ + char *s; + long nb; + uint8_t i; + + i = ft_intlen(n) - 1; + if (!(s = (char*)malloc((i + 2) * sizeof(char)))) + return (NULL); + if (!n) + s[i] = '0'; + nb = n; + if (n < 0) + { + s[0] = '-'; + nb = -n; + } + s[i + 1] = '\0'; + while (nb > 0) + { + s[i] = 48 + (nb % 10); + nb = nb / 10; + i--; + } + return (s); +} diff --git a/libft/src/ft_itoa_base.c b/libft/src/ft_itoa_base.c new file mode 100644 index 0000000..37b792b --- /dev/null +++ b/libft/src/ft_itoa_base.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_itoa_base.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:28:40 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:28:43 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +char + *ft_itoa_base(long n, char *base) +{ + char *s; + long nb; + uint8_t i; + + i = ft_intlen_base(n, base) - 1; + if (!(s = (char*)malloc((i + 2) * sizeof(char)))) + return (NULL); + if (!n) + s[i] = '0'; + if (n < 0) + { + s[0] = '-'; + nb = -n; + } + s[i + 1] = '\0'; + while (n > 0) + { + nb = n; + if (nb >= (long)ft_strlen(base)) + nb = n % ft_strlen(base); + s[i] = base[nb]; + n /= ft_strlen(base); + i--; + } + return (s); +} diff --git a/libft/src/ft_kernel_panic.c b/libft/src/ft_kernel_panic.c new file mode 100644 index 0000000..06ea43e --- /dev/null +++ b/libft/src/ft_kernel_panic.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_kernel_panic.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/11 18:26:08 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/11 18:26:09 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +void + ft_kernel_panic(void) +{ + int *ptr; + + while (1) + ptr = (int *)ft_calloc((1024 * 1024) * sizeof(int), 1); + free(ptr); +} diff --git a/libft/src/ft_lstadd_back.c b/libft/src/ft_lstadd_back.c new file mode 100644 index 0000000..1c8aeeb --- /dev/null +++ b/libft/src/ft_lstadd_back.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstadd_back.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 10:04:16 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 10:11:08 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +void + ft_lstadd_back(t_list **alst, t_list *new) +{ + t_list *tmp; + + if (!alst || !new) + return ; + if (!*alst) + *alst = new; + else + { + tmp = ft_lstlast(*alst); + tmp->next = new; + } +} diff --git a/libft/src/ft_lstadd_front.c b/libft/src/ft_lstadd_front.c new file mode 100644 index 0000000..796740b --- /dev/null +++ b/libft/src/ft_lstadd_front.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstadd_front.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 09:31:45 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 10:11:10 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +void + ft_lstadd_front(t_list **alst, t_list *new) +{ + if (!alst || !new) + return ; + new->next = *alst; + *alst = new; +} diff --git a/libft/src/ft_lstclear.c b/libft/src/ft_lstclear.c new file mode 100644 index 0000000..936672d --- /dev/null +++ b/libft/src/ft_lstclear.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstclear.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 10:19:53 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 13:52:03 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +void + ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + t_list *renext; + + if (!lst) + return ; + tmp = *lst; + while (tmp) + { + renext = tmp->next; + del(tmp->content); + free(tmp); + tmp = renext; + } + *lst = NULL; +} diff --git a/libft/src/ft_lstdelone.c b/libft/src/ft_lstdelone.c new file mode 100644 index 0000000..47f0669 --- /dev/null +++ b/libft/src/ft_lstdelone.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstdelone.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 10:11:20 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 10:18:40 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +void + ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (lst) + { + del(lst->content); + free(lst); + } +} diff --git a/libft/src/ft_lstiter.c b/libft/src/ft_lstiter.c new file mode 100644 index 0000000..2570cf0 --- /dev/null +++ b/libft/src/ft_lstiter.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstiter.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 10:30:22 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 11:01:22 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +void + ft_lstiter(t_list *lst, void (*f)(void *)) +{ + if (!lst) + return ; + while (lst != NULL) + { + (*f)(lst->content); + lst = lst->next; + } +} diff --git a/libft/src/ft_lstlast.c b/libft/src/ft_lstlast.c new file mode 100644 index 0000000..0ac542a --- /dev/null +++ b/libft/src/ft_lstlast.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstlast.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 09:53:13 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 10:11:09 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +t_list + *ft_lstlast(t_list *lst) +{ + if (!lst) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} diff --git a/libft/src/ft_lstmap.c b/libft/src/ft_lstmap.c new file mode 100644 index 0000000..9aed5b9 --- /dev/null +++ b/libft/src/ft_lstmap.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstmap.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 10:36:15 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 11:06:32 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> + +t_list + *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *nlst; + t_list *new; + + if (!lst) + return (NULL); + nlst = ft_lstnew((*f)(lst->content)); + if (!nlst) + return (NULL); + lst = lst->next; + while (lst != NULL) + { + new = ft_lstnew((*f)(lst->content)); + (*del)(lst->content); + if (!new) + return (NULL); + lst = lst->next; + ft_lstadd_back(&nlst, new); + } + return (nlst); +} diff --git a/libft/src/ft_lstnew.c b/libft/src/ft_lstnew.c new file mode 100644 index 0000000..f4a9908 --- /dev/null +++ b/libft/src/ft_lstnew.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstnew.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 09:25:56 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 10:56:17 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +t_list + *ft_lstnew(void *content) +{ + t_list *nlst; + + nlst = (t_list*)ft_calloc(1, sizeof(t_list)); + if (!nlst) + return (NULL); + nlst->content = content; + nlst->next = NULL; + return (nlst); +} diff --git a/libft/src/ft_lstsize.c b/libft/src/ft_lstsize.c new file mode 100644 index 0000000..0c71a69 --- /dev/null +++ b/libft/src/ft_lstsize.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_lstsize.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 09:45:10 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 09:51:16 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +int + ft_lstsize(t_list *lst) +{ + int i; + + if (!lst) + return (0); + i = 0; + while (lst != NULL) + { + lst = lst->next; + i++; + } + return (i); +} diff --git a/libft/src/ft_memccpy.c b/libft/src/ft_memccpy.c new file mode 100644 index 0000000..a029e7d --- /dev/null +++ b/libft/src/ft_memccpy.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memccpy.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/08 14:59:46 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:40:44 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stddef.h> +#include <stdio.h> + +void + *ft_memccpy(void *dst, const void *src, int c, size_t n) +{ + unsigned char *dst_ptr; + const unsigned char *src_ptr; + + dst_ptr = (unsigned char*)dst; + src_ptr = (unsigned char*)src; + if (!*dst_ptr && !*src_ptr && n) + return (NULL); + if (n) + { + while (n) + { + if ((*dst_ptr++ = *src_ptr++) == (unsigned char)c) + return (dst_ptr); + n--; + } + } + return (NULL); +} diff --git a/libft/src/ft_memchr.c b/libft/src/ft_memchr.c new file mode 100644 index 0000000..8dfb477 --- /dev/null +++ b/libft/src/ft_memchr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memchr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/08 19:14:54 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/10 18:34:21 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stddef.h> + +void + *ft_memchr(const void *s, int c, size_t n) +{ + unsigned char *s_ptr; + unsigned char c_char; + + s_ptr = (unsigned char*)s; + c_char = (unsigned char)c; + while (n > 0) + { + if (*s_ptr == c_char) + return ((void*)s_ptr); + s_ptr++; + n--; + } + return (NULL); +} diff --git a/libft/src/ft_memcmp.c b/libft/src/ft_memcmp.c new file mode 100644 index 0000000..96f21c4 --- /dev/null +++ b/libft/src/ft_memcmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memcmp.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/08 19:23:43 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:40:10 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stddef.h> + +int + ft_memcmp(const void *s1, const void *s2, size_t n) +{ + const unsigned char *s1_ptr; + const unsigned char *s2_ptr; + + s1_ptr = (unsigned char*)s1; + s2_ptr = (unsigned char*)s2; + while (n) + { + if (*s1_ptr != *s2_ptr) + return (*s1_ptr - *s2_ptr); + s1_ptr++; + s2_ptr++; + n--; + } + return (0); +} diff --git a/libft/src/ft_memcpy.c b/libft/src/ft_memcpy.c new file mode 100644 index 0000000..49a1ff7 --- /dev/null +++ b/libft/src/ft_memcpy.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memcpy.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/08 14:17:11 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:39:01 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdio.h> + +void + *ft_memcpy(void *dst, const void *src, size_t n) +{ + unsigned char *dst_ptr; + unsigned char *src_ptr; + size_t i; + + dst_ptr = (unsigned char*)dst; + src_ptr = (unsigned char*)src; + i = 0; + if (src_ptr == NULL && dst_ptr == NULL && n != 0) + return (NULL); + while (i < n) + { + dst_ptr[i] = src_ptr[i]; + i++; + } + return (dst_ptr); +} diff --git a/libft/src/ft_memdel.c b/libft/src/ft_memdel.c new file mode 100644 index 0000000..05c2982 --- /dev/null +++ b/libft/src/ft_memdel.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memdel.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2020/01/09 15:58:16 by rbousset #+# ## ## #+# */ +/* Updated: 2020/01/09 15:58:17 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stdlib.h> + +void + ft_memdel(void *ptr) +{ + free(ptr); + ptr = NULL; +} diff --git a/libft/src/ft_memlchr.c b/libft/src/ft_memlchr.c new file mode 100644 index 0000000..1123743 --- /dev/null +++ b/libft/src/ft_memlchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memlchr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/11 16:50:58 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/11 16:50:59 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +long + ft_memlchr(const void *s, int c, size_t n) +{ + const size_t len = ft_strlen((const char *)s); + const size_t rem = ft_strlen((const char *)ft_memchr(s, c, n)); + + if (len - rem >= ft_strlen((const char *)s)) + return (-1); + return (len - rem); +} diff --git a/libft/src/ft_memmove.c b/libft/src/ft_memmove.c new file mode 100644 index 0000000..dee44f3 --- /dev/null +++ b/libft/src/ft_memmove.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memmove.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/08 18:57:44 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:38:47 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +void + *ft_memmove(void *dst, const void *src, size_t len) +{ + const char *from = (const char*)src; + char *to; + int i; + size_t j; + + to = (char*)dst; + if (to > from && to - from < (int)len) + { + i = len; + while (--i >= 0) + to[i] = from[i]; + return (dst); + } + if (from > to && from - to < (int)len) + { + j = -1; + while (++j < len) + to[j] = from[j]; + return (dst); + } + ft_memcpy(dst, src, len); + return (dst); +} diff --git a/libft/src/ft_memset.c b/libft/src/ft_memset.c new file mode 100644 index 0000000..1498ae6 --- /dev/null +++ b/libft/src/ft_memset.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memset.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/08 13:41:09 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:38:10 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stddef.h> + +void + *ft_memset(void *b, int c, size_t len) +{ + unsigned char *str; + + str = b; + if (!len) + return (b); + while (len > 0) + { + *str = (unsigned char)c; + str++; + len--; + } + return (b); +} diff --git a/libft/src/ft_nrealloc.c b/libft/src/ft_nrealloc.c new file mode 100644 index 0000000..96d5ad1 --- /dev/null +++ b/libft/src/ft_nrealloc.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_nrealloc.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2020/01/09 15:58:24 by rbousset #+# ## ## #+# */ +/* Updated: 2020/01/09 15:58:25 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> + +void + *ft_nrealloc(void *ptr, size_t oldsize, size_t newsize) +{ + void *nptr; + + if (!ptr) + { + if (!(ptr = malloc(newsize))) + return (NULL); + return (ptr); + } + else if (!newsize) + { + free(ptr); + return (NULL); + } + if (!(nptr = malloc(newsize))) + return (ptr); + ft_memcpy(nptr, ptr, oldsize); + free(ptr); + return (nptr); +} diff --git a/libft/src/ft_nstr.c b/libft/src/ft_nstr.c new file mode 100644 index 0000000..8919e64 --- /dev/null +++ b/libft/src/ft_nstr.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_nstr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/07 04:23:30 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/07 04:23:32 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include <stddef.h> + +char + *ft_nstr(size_t size) +{ + char *str; + size_t i; + + i = 0; + size += 1; + if (!(str = (char*)malloc((size) * sizeof(char)))) + return (NULL); + while (i < size) + { + str[i] = '\0'; + i++; + } + return (str); +} diff --git a/libft/src/ft_printf.c b/libft/src/ft_printf.c new file mode 100644 index 0000000..747f516 --- /dev/null +++ b/libft/src/ft_printf.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/15 06:17:50 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/15 06:17:51 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> +#include <stdarg.h> +#include <unistd.h> + +static int + ft_printf_return(t_printflist *pflist) +{ + int ret; + + ret = write(1, pflist->output, pflist->fulllen); + ft_memdel(pflist->output); + ft_memdel(pflist->fullflag); + ft_memdel(pflist); + return (ret); +} + +int + ft_printf(const char *format, ...) +{ + t_printflist *pflist; + va_list arg; + int pos; + + if (!format) + return (-1); + if ((pos = ft_strlchr(format, '%')) < 0) + return (ft_putstr(format)); + else + { + pflist = ft_printf_init_struct(format); + ft_printf_putpart(format, 0, pos, pflist); + va_start(arg, format); + while (pos >= 0) + { + pos = ft_printf_flags(format, pos, arg, pflist); + ft_printf_reinit_struct(pflist); + } + va_end(arg); + return (ft_printf_return(pflist)); + } + return (0); +} diff --git a/libft/src/ft_printf_cat_output.c b/libft/src/ft_printf_cat_output.c new file mode 100644 index 0000000..19e6cf1 --- /dev/null +++ b/libft/src/ft_printf_cat_output.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_cat_output.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:18 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:19 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <unistd.h> + +void + ft_printf_cat_output(char *src, size_t len, t_printflist *pflist) +{ + size_t dst_len; + + dst_len = pflist->fulllen; + pflist->output = (char*)ft_nrealloc(pflist->output, + (dst_len + 1) * sizeof(char), + (dst_len + len + 1) * sizeof(char)); + ft_memcpy(pflist->output + dst_len, src, len); + *(pflist->output + dst_len + len) = '\0'; + pflist->fulllen += len; +} diff --git a/libft/src/ft_printf_flag_to_atoi.c b/libft/src/ft_printf_flag_to_atoi.c new file mode 100644 index 0000000..f75a7f7 --- /dev/null +++ b/libft/src/ft_printf_flag_to_atoi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_flag_to_atoi.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:21 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:22 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> + +char + *ft_printf_flag_to_atoi(char *str) +{ + char *nstr; + char *nnstr; + int len; + + while (*str != '.' && *str) + str++; + while (*str != '-' && *str) + str--; + len = ft_strlchr(str, ' '); + nnstr = ft_substr(str, 0, len); + nstr = ft_strjoin(nnstr, str + len + 1); + ft_memdel(nnstr); + return (nstr); +} diff --git a/libft/src/ft_printf_get_flags.c b/libft/src/ft_printf_get_flags.c new file mode 100644 index 0000000..80ee47c --- /dev/null +++ b/libft/src/ft_printf_get_flags.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_get_flags.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:22 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:23 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> + +/* +** Copies everything between '%' and any +** FT_PRINTF_CONV_CHARSET "cspdiuxX%" +** into the fullflag string and returns it. +** Also puts actual conv into pflist->actconv +*/ + +char + *ft_printf_get_flags(const char *format, int pos, t_printflist *pflist) +{ + int i; + char *nstr; + char *fullflag; + + i = 0; + while (!ft_ischarset(FT_PRINTF_CONV_CHARSET, *(format + pos + i + 1)) + && *(format + pos + i + 1)) + i++; + if (!(fullflag = (char*)malloc((i + 1) * sizeof(char)))) + return (NULL); + ft_memcpy(fullflag, nstr = ft_substr(format, pos + 1, i), i); + ft_memdel(nstr); + fullflag[i] = '\0'; + pflist->actconv = *(format + pos + i + 1); + return (fullflag); +} diff --git a/libft/src/ft_printf_get_s_putlen.c b/libft/src/ft_printf_get_s_putlen.c new file mode 100644 index 0000000..5d0dfdf --- /dev/null +++ b/libft/src/ft_printf_get_s_putlen.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_get_s_putlen.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:24 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:25 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +int + ft_printf_get_s_putlen(char *str, t_printflist *pflist) +{ + int ret; + + if (pflist->precision > 0 + && (int)ft_strlen((const char*)str) > pflist->precision) + ret = pflist->precision; + else if (pflist->precision == -1) + ret = 0; + else + ret = ft_strlen((const char*)str); + return (ret); +} diff --git a/libft/src/ft_printf_get_width_nstr.c b/libft/src/ft_printf_get_width_nstr.c new file mode 100644 index 0000000..15b9f98 --- /dev/null +++ b/libft/src/ft_printf_get_width_nstr.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_get_width_nstr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2020/01/02 15:58:53 by rbousset #+# ## ## #+# */ +/* Updated: 2020/01/02 15:58:56 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdarg.h> + +int + ft_printf_fetch_width(va_list arg, char *nstr, t_printflist *pflist) +{ + int ret; + char *str; + + if (ft_strlchr(nstr, '*') >= 0) + { + (ft_strlchr(pflist->fullflag, '-') >= 0) ? (pflist->isminus = 1) : 0; + (pflist->isminus) ? (pflist->isreverse = 1) : 0; + ret = va_arg(arg, int); + } + else if ((pflist->isaspace = ft_strlchr(pflist->fullflag, ' ')) >= 0) + { + str = ft_printf_flag_to_atoi(pflist->fullflag); + ret = ft_atoi(str); + ft_memdel(str); + } + else + { + if (*nstr == '-' && ft_strlen(nstr) >= 2) + { + nstr += 1; + pflist->isreverse = 1; + } + ret = ft_atoi(nstr); + } + return (ret); +} + +char + *ft_printf_get_width_nstr(char *str, t_printflist *pflist) +{ + (ft_strlchr(pflist->fullflag, '+') >= 0) ? (pflist->isaplus = 1) : 0; + (*(pflist->fullflag) == '+' && *(pflist->fullflag + 1)) ? (str += 1) : 0; + (*(pflist->fullflag) == '0' && *(pflist->fullflag + 1)) ? (str += 1) : 0; + (*(pflist->fullflag) == '#' && *(pflist->fullflag + 1) + && (pflist->actconv == 'x' || pflist->actconv == 'X')) ? (str += 1) : 0; + return (str); +} diff --git a/libft/src/ft_printf_init_struct.c b/libft/src/ft_printf_init_struct.c new file mode 100644 index 0000000..44c9931 --- /dev/null +++ b/libft/src/ft_printf_init_struct.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_init_struct.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:28 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:29 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> + +t_printflist + *ft_printf_init_struct(const char *format) +{ + t_printflist *pflist; + + (void)format; + if (!(pflist = (t_printflist*)malloc(sizeof(t_printflist)))) + return (NULL); + if (!(pflist->fullflag = (char *)ft_calloc(1, sizeof(char)))) + return (NULL); + ft_printf_reinit_struct(pflist); + pflist->fulllen = 0; + if (!(pflist->output = (char*)ft_calloc(1, sizeof(char)))) + return (NULL); + return (pflist); +} + +void + ft_printf_reinit_struct(t_printflist *pflist) +{ + pflist->conv = 0; + pflist->actconv = 0; + pflist->flaglen = 0; + pflist->width = 0; + pflist->precision = 0; + pflist->isreverse = 0; + pflist->isneg = 0; + pflist->isaz = 0; + pflist->isaspace = -1; + pflist->isaplus = 0; + pflist->putlen = 0; + pflist->lh = 0; + pflist->zflag = 0; + pflist->issharp = 0; + pflist->isminus = 0; +} diff --git a/libft/src/ft_printf_parts.c b/libft/src/ft_printf_parts.c new file mode 100644 index 0000000..55a48dd --- /dev/null +++ b/libft/src/ft_printf_parts.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_parts.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:35 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:37 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> + +int + ft_printf_get_partlen(const char *format) +{ + int ret; + + ret = ft_strlchr(format, '%'); + if (ret < 0) + return (ft_strlen(format)); + else + return (ret); +} + +int + ft_printf_putpart(const char *format, + int start, + int len, + t_printflist *pflist) +{ + char *nstr; + int ret; + + ft_printf_cat_output(nstr = ft_substr(format, start, len), len, pflist); + ft_memdel(nstr); + ret = 0; + return (ret); +} diff --git a/libft/src/ft_printf_process.c b/libft/src/ft_printf_process.c new file mode 100644 index 0000000..6979ad6 --- /dev/null +++ b/libft/src/ft_printf_process.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_process.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/13 00:32:59 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/13 00:33:04 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdarg.h> +#include <inttypes.h> +#include <limits.h> + +/* +** calls the corresponding function to +** the right conv using: +** FT_PRINTF_CONV_CHARSET "cspdiuxX%" +*/ + +void + ft_printf_process(const char *format, va_list arg, t_printflist *pflist) +{ + int conv; + void (*fun_ptr[9])(va_list, t_printflist *); + + fun_ptr[0] = ft_printf_put_c; + fun_ptr[1] = ft_printf_put_s; + fun_ptr[2] = ft_printf_put_p; + fun_ptr[3] = ft_printf_put_d; + fun_ptr[4] = ft_printf_put_d; + fun_ptr[5] = ft_printf_put_u; + fun_ptr[6] = ft_printf_put_x; + fun_ptr[7] = ft_printf_put_big_x; + fun_ptr[8] = ft_printf_put_perc; + if ((conv = ft_strlchr(FT_PRINTF_CONV_CHARSET, + *(format + pflist->flaglen + 1))) >= 0) + (*fun_ptr[conv])(arg, pflist); + else + ft_printf_put_none(pflist); +} diff --git a/libft/src/ft_printf_put_char.c b/libft/src/ft_printf_put_char.c new file mode 100644 index 0000000..111ffa8 --- /dev/null +++ b/libft/src/ft_printf_put_char.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_char.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:40 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:41 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> +#include <stdarg.h> +#include <inttypes.h> +#include <unistd.h> + +void + ft_printf_put_c(va_list arg, t_printflist *pflist) +{ + char c; + + c = va_arg(arg, int); + pflist->putlen += 1; + ft_printf_put_width_pre(pflist); + ft_printf_cat_output(&c, 1, pflist); + ft_printf_put_width_post(pflist); +} + +void + ft_printf_put_perc(va_list arg, t_printflist *pflist) +{ + (void)arg; + pflist->putlen += 1; + (pflist->isaz) ? (ft_printf_put_precision(pflist)) : 0; + ft_printf_put_width_pre(pflist); + ft_printf_cat_output("%", 1, pflist); + ft_printf_put_width_post(pflist); +} diff --git a/libft/src/ft_printf_put_hex.c b/libft/src/ft_printf_put_hex.c new file mode 100644 index 0000000..bbf9821 --- /dev/null +++ b/libft/src/ft_printf_put_hex.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_hex.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:42 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:43 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> +#include <stdarg.h> +#include <inttypes.h> + +static unsigned long + ft_printf_get_xlh(va_list arg, int8_t lh, uint8_t zflag) +{ + if (zflag == 1) + return (va_arg(arg, size_t)); + else if (lh == 0) + return (va_arg(arg, unsigned int)); + else if (lh == -2) + return ((unsigned char)va_arg(arg, unsigned int)); + else if (lh == -1) + return ((unsigned short)va_arg(arg, unsigned int)); + else if (lh == 1) + return (va_arg(arg, unsigned long)); + else if (lh == 2) + return (va_arg(arg, unsigned long)); + return (va_arg(arg, unsigned int)); +} + +void + ft_printf_put_x(va_list arg, t_printflist *pflist) +{ + unsigned long x; + char *str; + + x = ft_printf_get_xlh(arg, pflist->lh, pflist->zflag); + pflist->conv = 'd'; + pflist->putlen += ft_uintlen_base(x, FT_MIN_HEX_BASE); + (pflist->issharp && x != 0) ? (pflist->putlen += 2) : 0; + (x == 0 && pflist->precision == -1 && pflist->width > 0) + ? (pflist->putlen = 0) : 0; + ft_printf_put_width_pre(pflist); + if (pflist->issharp && x != 0) + ft_printf_cat_output("0x", 2, pflist); + ft_printf_put_precision(pflist); + if (!(pflist->precision < 0 && x == 0)) + { + str = ft_uitoa_base(x, FT_MIN_HEX_BASE); + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } + ft_printf_put_width_post(pflist); +} + +void + ft_printf_put_big_x(va_list arg, t_printflist *pflist) +{ + unsigned long x; + char *str; + + x = ft_printf_get_xlh(arg, pflist->lh, pflist->zflag); + pflist->conv = 'd'; + pflist->putlen += ft_uintlen_base(x, FT_MAJ_HEX_BASE); + (pflist->issharp && x != 0) ? (pflist->putlen += 2) : 0; + (x == 0 && pflist->precision == -1 && pflist->width > 0) + ? (pflist->putlen = 0) : 0; + ft_printf_put_width_pre(pflist); + if (pflist->issharp && x != 0) + ft_printf_cat_output("0X", 2, pflist); + ft_printf_put_precision(pflist); + if (!(pflist->precision < 0 && x == 0)) + { + str = ft_uitoa_base(x, FT_MAJ_HEX_BASE); + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } + ft_printf_put_width_post(pflist); +} diff --git a/libft/src/ft_printf_put_int.c b/libft/src/ft_printf_put_int.c new file mode 100644 index 0000000..ddf06b7 --- /dev/null +++ b/libft/src/ft_printf_put_int.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_int.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:45 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:45 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> +#include <stdarg.h> +#include <inttypes.h> + +static long + ft_printf_get_dlh(va_list arg, int8_t lh) +{ + if (lh == 0) + return (va_arg(arg, int)); + else if (lh == -2) + return ((char)va_arg(arg, int)); + else if (lh == -1) + return ((short)va_arg(arg, int)); + else if (lh == 1) + return (va_arg(arg, long int)); + else if (lh == 2) + return (va_arg(arg, long int)); + return (va_arg(arg, int)); +} + +static unsigned long + ft_printf_get_ulh(va_list arg, int8_t lh, uint8_t zflag) +{ + if (zflag == 1) + return (va_arg(arg, size_t)); + else if (lh == 0) + return (va_arg(arg, unsigned int)); + else if (lh == -2) + return ((unsigned char)va_arg(arg, unsigned int)); + else if (lh == -1) + return ((unsigned short)va_arg(arg, unsigned int)); + else if (lh == 1) + return (va_arg(arg, unsigned long int)); + else if (lh == 2) + return (va_arg(arg, unsigned long int)); + return (va_arg(arg, unsigned int)); +} + +static long + ft_printf_put_plus_minus(long d, t_printflist *pflist) +{ + if (pflist->precision >= pflist->putlen && pflist->isneg == 1) + { + ft_printf_cat_output("-", 1, pflist); + pflist->putlen -= 1; + d = -d; + } + else if (pflist->isaplus && !pflist->isneg) + ft_printf_cat_output("+", 1, pflist); + return (d); +} + +void + ft_printf_put_d(va_list arg, t_printflist *pflist) +{ + long d; + char *str; + + d = ft_printf_get_dlh(arg, pflist->lh); + (d < 0) ? (pflist->isneg = 1) + : (pflist->isneg = 0); + pflist->conv = 'd'; + pflist->putlen += ft_intlen(d); + if (pflist->isaplus && !pflist->isneg) + pflist->putlen += 1; + (d == 0 && pflist->precision == -1 && pflist->width > 0) + ? (pflist->putlen = 0) : 0; + ft_printf_put_width_pre(pflist); + d = ft_printf_put_plus_minus(d, pflist); + ft_printf_put_precision(pflist); + if (!(pflist->precision < 0 && d == 0)) + { + str = ft_itoa(d); + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } + ft_printf_put_width_post(pflist); +} + +void + ft_printf_put_u(va_list arg, t_printflist *pflist) +{ + unsigned long d; + char *str; + + d = ft_printf_get_ulh(arg, pflist->lh, pflist->zflag); + pflist->conv = 'd'; + pflist->putlen += ft_uintlen(d); + (d == 0 && pflist->precision == -1 && pflist->width > 0) + ? (pflist->putlen = 0) : 0; + ft_printf_put_width_pre(pflist); + ft_printf_put_precision(pflist); + if (!(pflist->precision < 0 && d == 0)) + { + str = ft_uitoa(d); + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } + ft_printf_put_width_post(pflist); +} diff --git a/libft/src/ft_printf_put_none.c b/libft/src/ft_printf_put_none.c new file mode 100644 index 0000000..871ce2b --- /dev/null +++ b/libft/src/ft_printf_put_none.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_none.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:47 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:48 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +void + ft_printf_put_none(t_printflist *pflist) +{ + ft_printf_put_width_pre(pflist); + ft_printf_put_width_post(pflist); +} diff --git a/libft/src/ft_printf_put_precision.c b/libft/src/ft_printf_put_precision.c new file mode 100644 index 0000000..7898102 --- /dev/null +++ b/libft/src/ft_printf_put_precision.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_precision.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:49 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:50 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +static int + ft_printf_get_len(t_printflist *pflist) +{ + if (pflist->conv == 'p' && pflist->precision > pflist->putlen) + return (pflist->precision - pflist->putlen + 2); + else if (pflist->precision > pflist->putlen && pflist->isneg == 0 + && pflist->isaplus == 0) + return (pflist->precision - pflist->putlen); + else if (pflist->precision >= pflist->putlen && pflist->isneg == 1) + { + if (pflist->isaz == 1) + return (pflist->precision - pflist->putlen - 1); + else + return (pflist->precision - pflist->putlen); + } + else if (pflist->precision >= pflist->putlen && pflist->isneg == 0 + && pflist->isaplus == 1) + { + if (pflist->isaz == 1) + return (pflist->precision - pflist->putlen); + else + return (pflist->precision - pflist->putlen + 1); + } + return (0); +} + +/* +** Puts the correct amount of 0's when +** needed using pflist->precision. +** Used for %p, %d, %i, %u, %x and %X +*/ + +int + ft_printf_put_precision(t_printflist *pflist) +{ + char *str; + int len; + + len = 0; + if (pflist->precision >= pflist->putlen) + { + len = ft_printf_get_len(pflist); + if (!(str = (char*)malloc((len + 1) * sizeof(char)))) + return (1); + str = ft_memset(str, '0', len); + *(str + len) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } + return (0); +} diff --git a/libft/src/ft_printf_put_ptr.c b/libft/src/ft_printf_put_ptr.c new file mode 100644 index 0000000..409847d --- /dev/null +++ b/libft/src/ft_printf_put_ptr.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_ptr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:51 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:52 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdarg.h> +#include <inttypes.h> + +/* +** For GCC: if (!p) { ft_putstr("(nil)"); return ; } +*/ + +void + ft_printf_put_p(va_list arg, t_printflist *pflist) +{ + unsigned long p; + char *str; + + p = va_arg(arg, unsigned long); + pflist->conv = 'p'; + pflist->putlen += 2 + ft_uintlen_base(p, FT_MIN_HEX_BASE); + ft_printf_put_width_pre(pflist); + ft_printf_cat_output("0x", 2, pflist); + if (pflist->precision > pflist->putlen - 2) + ft_printf_put_precision(pflist); + if (!(pflist->precision < 0 && p == 0)) + { + str = ft_uitoa_base(p, FT_MIN_HEX_BASE); + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } + ft_printf_put_width_post(pflist); +} diff --git a/libft/src/ft_printf_put_str.c b/libft/src/ft_printf_put_str.c new file mode 100644 index 0000000..a11128b --- /dev/null +++ b/libft/src/ft_printf_put_str.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_str.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:54 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:54 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdarg.h> +#include <inttypes.h> + +void + ft_printf_put_s(va_list arg, t_printflist *pflist) +{ + char *str; + char *nstr; + + pflist->conv = 's'; + str = va_arg(arg, char*); + if (!str) + str = "(null)"; + pflist->putlen += ft_printf_get_s_putlen(str, pflist); + ft_printf_put_width_pre(pflist); + if ((int)ft_strlen(str) > pflist->putlen) + { + ft_printf_cat_output(nstr = ft_substr(str, 0, pflist->putlen), + pflist->putlen, pflist); + ft_memdel(nstr); + } + else + ft_printf_cat_output(str, pflist->putlen, pflist); + ft_printf_put_width_post(pflist); +} diff --git a/libft/src/ft_printf_put_width.c b/libft/src/ft_printf_put_width.c new file mode 100644 index 0000000..30ae97f --- /dev/null +++ b/libft/src/ft_printf_put_width.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_put_width.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:40:56 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:40:56 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +static int + ft_printf_get_len(t_printflist *pflist) +{ + int len; + int width; + int prec; + + width = pflist->width; + prec = pflist->precision; + if (pflist->conv == 'd' && prec > pflist->putlen && !pflist->isneg) + len = (width - pflist->putlen - (prec - pflist->putlen)); + else if (pflist->conv == 'd' && prec >= pflist->putlen && (pflist->isneg)) + len = (width - pflist->putlen - 1 - (prec - pflist->putlen)); + else if (pflist->conv == 'p' && prec > pflist->putlen) + len = (width - pflist->putlen - (prec - pflist->putlen + 2)); + else if (pflist->conv == 's' && prec > pflist->putlen && pflist->isaz == 1) + len = (width - pflist->putlen - (prec - pflist->putlen)); + else + len = (width - pflist->putlen); + if (pflist->conv == 'd' && pflist->isaspace == 1 && !pflist->isaplus + && (pflist->putlen < 3 && !pflist->isneg)) + len -= 1; + (len < 0) ? (len = 0) : 0; + return (len); +} + +/* +** Puts the (correct) amount of spaces +** when needed using pflist->width +*/ + +void + ft_printf_put_width_pre(t_printflist *pflist) +{ + char *str; + + if (pflist->conv == 'd' && pflist->isaspace == 1 && !pflist->isaplus + && (pflist->putlen < 3 && !pflist->isneg)) + ft_printf_cat_output(" ", 1, pflist); + if (pflist->width - pflist->putlen > 0 && pflist->width - pflist->putlen + - (pflist->precision - pflist->putlen) >= 0 && !pflist->isreverse) + { + if (!(str = malloc((ft_printf_get_len(pflist) + 1) * sizeof(char)))) + return ; + str = ft_memset(str, ' ', ft_printf_get_len(pflist)); + *(str + ft_printf_get_len(pflist)) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } + else if (pflist->conv == 's' && pflist->width - pflist->putlen > 0 + && !pflist->isreverse) + { + if (!(str = malloc((ft_printf_get_len(pflist) + 1) * sizeof(char)))) + return ; + str = ft_memset(str, ' ', ft_printf_get_len(pflist)); + *(str + ft_printf_get_len(pflist)) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } +} + +void + ft_printf_put_width_post(t_printflist *pflist) +{ + char *str; + + if (pflist->width - pflist->putlen > 0 && pflist->width - pflist->putlen + - (pflist->precision - pflist->putlen) >= 0 && pflist->isreverse) + { + if (!(str = (char*)malloc((ft_printf_get_len(pflist) + 1) + * sizeof(char)))) + return ; + str = ft_memset(str, ' ', ft_printf_get_len(pflist)); + *(str + ft_printf_get_len(pflist)) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } + else if (pflist->conv == 's' && pflist->width - pflist->putlen > 0 + && pflist->isreverse) + { + if (!(str = (char*)malloc((ft_printf_get_len(pflist) + 1) + * sizeof(char)))) + return ; + str = ft_memset(str, ' ', ft_printf_get_len(pflist)); + *(str + ft_printf_get_len(pflist)) = '\0'; + ft_printf_cat_output(str, ft_strlen(str), pflist); + ft_memdel(str); + } +} diff --git a/libft/src/ft_printf_treat_flags.c b/libft/src/ft_printf_treat_flags.c new file mode 100644 index 0000000..cb60c19 --- /dev/null +++ b/libft/src/ft_printf_treat_flags.c @@ -0,0 +1,147 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_treat_flags.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:41:01 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:41:01 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdarg.h> + +static int + ft_printf_get_width(va_list arg, t_printflist *pflist) +{ + char *nstr; + char *nstr_ptr; + int ret; + + if (ft_strlchr(pflist->fullflag, '.') >= 0) + nstr = ft_substr(pflist->fullflag, 0, + ft_strlchr(pflist->fullflag, '.')); + else + nstr = ft_strdup(pflist->fullflag); + nstr_ptr = nstr; + nstr = ft_printf_get_width_nstr(nstr, pflist); + ret = ft_printf_fetch_width(arg, nstr, pflist); + ft_memdel(nstr_ptr); + (ret < 0) ? (pflist->isreverse = 1) : 0; + (ret < 0) ? (ret = -ret) : 0; + return (ret); +} + +static int + ft_printf_get_precision(va_list arg, int pos, t_printflist *pflist) +{ + int ret; + char *ptr; + + ptr = pflist->fullflag; + if (pflist->isaz && ft_strlchr(pflist->fullflag, '+') >= 0) + { + pflist->isaplus = 1; + if (ft_strlen(ptr) < pflist->flaglen) + ptr += 1; + } + if (*(ptr + pos + 1) == '*') + { + ret = va_arg(arg, int); + (ret < 0) ? (ret = 0) : 0; + return (ret); + } + else + ret = ft_atoi(ptr + pos + 1); + if (ret == 0) + return (-1); + return (ret); +} + +/* +** Corresponding l ll hh h +** in pflist->lh +** hh = -2 +** h = -1 +** l = 1 +** ll = 2 +*/ + +static void + ft_printf_get_lh(t_printflist *pflist) +{ + int pos; + + if ((pos = ft_strlchr(pflist->fullflag, 'z')) >= 0) + pflist->zflag = 1; + else if ((pos = ft_strlchr(pflist->fullflag, 'l')) >= 0) + { + if (pos + 2 <= (int)pflist->flaglen && + *(pflist->fullflag + pos + 1) == 'l') + pflist->lh = 2; + else + pflist->lh = 1; + } + else if ((pos = ft_strlchr(pflist->fullflag, 'h')) >= 0) + { + if (pos + 2 <= (int)pflist->flaglen && + *(pflist->fullflag + pos + 1) == 'h') + pflist->lh = -2; + else + pflist->lh = -1; + } +} + +static uint8_t + ft_printf_check_z(t_printflist *pflist) +{ + char *ptr; + + ptr = pflist->fullflag; + while (!ft_isdigit(*ptr) && *ptr) + ptr++; + if ((ptr - pflist->fullflag) <= (long)ft_strlen(pflist->fullflag) + && (*ptr == '0') && ft_strlchr(pflist->fullflag, '.') < 0) + return (1); + return (0); +} + +/* +** Uses pflist->fullflag to put +** width and precision in the list +** also l ll hh h +*/ + +void + ft_printf_treat_flags(va_list arg, t_printflist *pflist) +{ + int pos; + char c; + + pflist->flaglen = ft_strlen(pflist->fullflag); + if (((pflist->isaz = ft_printf_check_z(pflist)) == 1 + && ft_strlchr(pflist->fullflag, '.') < 0 + && ft_strlchr(pflist->fullflag, '-') < 0)) + pflist->precision = ft_printf_get_precision(arg, 0, pflist); + else + { + pflist->width = ft_printf_get_width(arg, pflist); + (pflist->isaspace >= 0) ? (pflist->isaspace = 1) : 0; + if ((pos = ft_strlchr(pflist->fullflag, '.')) >= 0) + pflist->precision = ft_printf_get_precision(arg, pos, pflist); + } + (pflist->isaz && pflist->width) ? (pflist->isaz = 0) : 0; + (ft_strlchr(pflist->fullflag, '#') >= 0) ? (pflist->issharp = 1) : 0; + c = pflist->actconv; + if (ft_strlchr(pflist->fullflag, '.') < 0 && pflist->isaz + && pflist->precision < 0 && c != 'c' && c != 's' && c != '%') + { + pflist->precision = 0; + pflist->isaz = 0; + } + ft_printf_get_lh(pflist); +} diff --git a/libft/src/ft_printf_use_flags.c b/libft/src/ft_printf_use_flags.c new file mode 100644 index 0000000..7b1299e --- /dev/null +++ b/libft/src/ft_printf_use_flags.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_printf_use_flags.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:41:02 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:41:03 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +static void + ft_printf_noflags(const char *format, + int pos, + va_list arg, + t_printflist *pflist) +{ + ft_printf_process(format + pos, arg, pflist); +} + +static void + ft_printf_withflags(const char *format, + int pos, + va_list arg, + t_printflist *pflist) +{ + ft_memdel(pflist->fullflag); + pflist->fullflag = ft_printf_get_flags(format, pos, pflist); + ft_printf_treat_flags(arg, pflist); + ft_printf_process(format + pos, arg, pflist); +} + +int + ft_printf_flags(const char *format, + int pos, + va_list arg, + t_printflist *pflist) +{ + int plen; + + plen = 0; + if (ft_ischarset(FT_PRINTF_CONV_CHARSET, *(format + pos + 1))) + { + ft_printf_noflags(format, pos, arg, pflist); + plen = ft_printf_get_partlen(format + pos + 2); + ft_printf_putpart(format, pos + 2, plen, pflist); + if (plen == (int)ft_strlen(format + pos + 2)) + return (-1); + else + return (pos + plen + 2); + } + else + { + ft_printf_withflags(format, pos, arg, pflist); + plen = ft_printf_get_partlen(format + pos + pflist->flaglen + 2); + ft_printf_putpart(format, pos + pflist->flaglen + 2, + plen, pflist); + if (plen == (int)ft_strlen(format + pos + pflist->flaglen + 2)) + return (-1); + else + return (pos + plen + pflist->flaglen + 2); + } +} diff --git a/libft/src/ft_putchar.c b/libft/src/ft_putchar.c new file mode 100644 index 0000000..b558ead --- /dev/null +++ b/libft/src/ft_putchar.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putchar.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/12 15:02:55 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/15 01:10:32 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <unistd.h> + +int + ft_putchar(int c) +{ + return (write(1, &c, 1)); +} diff --git a/libft/src/ft_putchar_fd.c b/libft/src/ft_putchar_fd.c new file mode 100644 index 0000000..a8fed8f --- /dev/null +++ b/libft/src/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putchar_fd.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 08:27:19 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:49:34 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <unistd.h> + +int + ft_putchar_fd(char c, int fd) +{ + return (write(fd, &c, 1)); +} diff --git a/libft/src/ft_putendl.c b/libft/src/ft_putendl.c new file mode 100644 index 0000000..e2ab518 --- /dev/null +++ b/libft/src/ft_putendl.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putendl.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/15 05:19:52 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/15 05:19:53 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <unistd.h> + +int + ft_putendl(const char *s) +{ + return (write(1, s, ft_strlen(s)) + ft_putchar('\n')); +} diff --git a/libft/src/ft_putendl_fd.c b/libft/src/ft_putendl_fd.c new file mode 100644 index 0000000..9b0f495 --- /dev/null +++ b/libft/src/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putendl_fd.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 08:52:34 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 09:01:41 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +void + ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/libft/src/ft_putnbr.c b/libft/src/ft_putnbr.c new file mode 100644 index 0000000..9812f75 --- /dev/null +++ b/libft/src/ft_putnbr.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putnbr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/08/08 12:22:48 by rbousset #+# ## ## #+# */ +/* Updated: 2019/08/12 17:54:48 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <unistd.h> +#include <inttypes.h> + +void + ft_putnbr(long nb) +{ + long i; + + i = nb; + if (nb < 0) + { + ft_putchar('-'); + i = -nb; + } + if (i > 9) + { + ft_putnbr(i / 10); + ft_putchar((i % 10) + '0'); + } + else + ft_putchar(i + '0'); +} diff --git a/libft/src/ft_putnbr_base.c b/libft/src/ft_putnbr_base.c new file mode 100644 index 0000000..a8aaf7e --- /dev/null +++ b/libft/src/ft_putnbr_base.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putnbr_base.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/19 15:00:38 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/19 15:00:40 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <inttypes.h> + +void + ft_putnbr_base(long nb, char *base) +{ + long i; + uint8_t size; + + i = nb; + size = ft_strlen(base); + if (nb < 0) + { + ft_putchar('-'); + i = -i; + } + if (i >= size) + { + ft_putnbr_base(i / size, base); + ft_putnbr_base(i % size, base); + } + else + ft_putchar(base[nb]); +} diff --git a/libft/src/ft_putnbr_fd.c b/libft/src/ft_putnbr_fd.c new file mode 100644 index 0000000..3246976 --- /dev/null +++ b/libft/src/ft_putnbr_fd.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putnbr_fd.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 08:57:19 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 12:13:09 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +void + ft_putnbr_fd(int n, int fd) +{ + unsigned int i; + + i = n; + if (n < 0) + { + ft_putchar_fd('-', fd); + i = -n; + } + if (i > 9) + { + ft_putnbr_fd(i / 10, fd); + ft_putchar_fd((i % 10) + 48, fd); + } + else + ft_putchar_fd(i + 48, fd); +} diff --git a/libft/src/ft_putnchar.c b/libft/src/ft_putnchar.c new file mode 100644 index 0000000..45fb7b2 --- /dev/null +++ b/libft/src/ft_putnchar.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putnchar.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/22 16:52:25 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/22 16:52:26 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +int + ft_putnchar(int c, const size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + ft_putchar(c); + i++; + } + return (i); +} diff --git a/libft/src/ft_putstr.c b/libft/src/ft_putstr.c new file mode 100644 index 0000000..572e989 --- /dev/null +++ b/libft/src/ft_putstr.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putstr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 09:05:27 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 09:07:45 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <unistd.h> + +int + ft_putstr(const char *s) +{ + return (write(1, s, ft_strlen(s))); +} diff --git a/libft/src/ft_putstr_fd.c b/libft/src/ft_putstr_fd.c new file mode 100644 index 0000000..068d959 --- /dev/null +++ b/libft/src/ft_putstr_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putstr_fd.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 08:48:25 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 09:00:42 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <unistd.h> + +int + ft_putstr_fd(char *s, int fd) +{ + return (write(fd, s, ft_strlen(s))); +} diff --git a/libft/src/ft_split.c b/libft/src/ft_split.c new file mode 100644 index 0000000..f0cac95 --- /dev/null +++ b/libft/src/ft_split.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_split.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/12 19:24:20 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:37:16 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> +#include <inttypes.h> + +static uint8_t + ft_check(int c, char sep) +{ + if (c == sep) + return (1); + return (0); +} + +static size_t + ft_strlen_plus(const char *str, char c) +{ + size_t i; + size_t count; + uint8_t ibool; + + i = 0; + count = 0; + ibool = 1; + while (str[i]) + { + while (ft_check(str[i], c) && str[i]) + i++; + while (!ft_check(str[i], c) && str[i]) + { + if (ibool == 1) + count++; + ibool = 0; + i++; + } + ibool = 1; + } + return (count); +} + +static size_t + ft_strlen_again(const char *str, char c) +{ + size_t i; + + i = 0; + while (!ft_check(str[i], c) && str[i]) + i++; + return (i); +} + +static char + *ft_strdup_plus(const char *src, char c) +{ + size_t i; + size_t slen; + char *nstr; + + i = 0; + slen = ft_strlen_again(src, c) + 1; + if (!(nstr = (char*)ft_calloc(slen, sizeof(char)))) + return (NULL); + while (!ft_check(src[i], c) && src[i]) + { + nstr[i] = src[i]; + i++; + } + nstr[i] = '\0'; + return (nstr); +} + +char + **ft_split(const char *s, char c) +{ + size_t i; + size_t j; + char **best_split; + + i = 0; + j = 0; + if (!(best_split = (char **)ft_calloc(ft_strlen_plus(s, c) + 1, + sizeof(char *)))) + return (NULL); + while (s[i]) + { + while (ft_check(s[i], c) && s[i]) + i++; + while (!ft_check(s[i], c) && s[i]) + { + best_split[j] = ft_strdup_plus(&s[i], c); + i += ft_strlen_again(&s[i], c); + j++; + } + } + best_split[j] = 0; + return (best_split); +} diff --git a/libft/src/ft_sprintf.c b/libft/src/ft_sprintf.c new file mode 100644 index 0000000..0cf5dc2 --- /dev/null +++ b/libft/src/ft_sprintf.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_sprintf.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2020/01/17 15:09:24 by rbousset #+# ## ## #+# */ +/* Updated: 2020/01/17 15:09:26 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> +#include <stdarg.h> +#include <unistd.h> + +static int + ft_printf_return(char *str, t_printflist *pflist) +{ + int ret; + + ft_strlcpy(str, pflist->output, pflist->fulllen + 1); + ret = pflist->fulllen; + ft_memdel(pflist->output); + ft_memdel(pflist->fullflag); + ft_memdel(pflist); + return (ret); +} + +int + ft_sprintf(char *str, const char *format, ...) +{ + t_printflist *pflist; + va_list arg; + int pos; + + if (!format) + return (-1); + if ((pos = ft_strlchr(format, '%')) < 0) + return (ft_strlcpy(str, format, ft_strlen(format) + 1)); + else + { + pflist = ft_printf_init_struct(format); + ft_printf_putpart(format, 0, pos, pflist); + va_start(arg, format); + while (pos >= 0) + { + pos = ft_printf_flags(format, pos, arg, pflist); + ft_printf_reinit_struct(pflist); + } + va_end(arg); + return (ft_printf_return(str, pflist)); + } + return (0); +} diff --git a/libft/src/ft_sqrt.c b/libft/src/ft_sqrt.c new file mode 100644 index 0000000..3003e6d --- /dev/null +++ b/libft/src/ft_sqrt.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_sqrt.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/08/10 21:34:14 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/14 23:26:06 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <limits.h> + +double + ft_sqrt(double x) +{ + double i; + + i = 0.0000001; + while (i * i < x && i * i < INT_MAX) + { + i += 0.0000001; + } + return (i); +} diff --git a/libft/src/ft_strcat.c b/libft/src/ft_strcat.c new file mode 100644 index 0000000..59084c2 --- /dev/null +++ b/libft/src/ft_strcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strcat.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/12 16:31:34 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:44:34 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +char + *ft_strcat(char *s1, const char *s2) +{ + int i; + int j; + + i = 0; + j = 0; + while (s1[i] != '\0') + i++; + while (s2[j] != '\0') + { + s1[i] = s2[j]; + i++; + j++; + } + s1[i] = '\0'; + return (s1); +} diff --git a/libft/src/ft_strchr.c b/libft/src/ft_strchr.c new file mode 100644 index 0000000..f3be716 --- /dev/null +++ b/libft/src/ft_strchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strchr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 18:39:17 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:36:43 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stddef.h> + +char + *ft_strchr(const char *s, int c) +{ + char ch; + + ch = (char)c; + while (s) + { + if (*s == ch) + return ((char *)s); + if (*s == '\0') + return (NULL); + s++; + } + return (NULL); +} diff --git a/libft/src/ft_strcmp.c b/libft/src/ft_strcmp.c new file mode 100644 index 0000000..ca5cf60 --- /dev/null +++ b/libft/src/ft_strcmp.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strcmp.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 13:55:24 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 13:56:42 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +int + ft_strcmp(const char *s1, const char *s2) +{ + size_t i; + + i = 0; + while (s1[i] == s2[i] && i < ft_strlen(s1) - 1) + { + if (!s1[i] && !s2[i]) + return (0); + i++; + } + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/libft/src/ft_strdup.c b/libft/src/ft_strdup.c new file mode 100644 index 0000000..830c965 --- /dev/null +++ b/libft/src/ft_strdup.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strdup.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/11 05:45:32 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:36:24 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +char + *ft_strdup(const char *s1) +{ + char *n_str; + size_t slen; + size_t i; + + slen = ft_strlen(s1); + if (!(n_str = (char*)malloc((slen + 1) * sizeof(char)))) + return (NULL); + i = 0; + while (s1[i]) + { + n_str[i] = s1[i]; + i++; + } + n_str[i] = '\0'; + return (n_str); +} diff --git a/libft/src/ft_strjoin.c b/libft/src/ft_strjoin.c new file mode 100644 index 0000000..2e00daf --- /dev/null +++ b/libft/src/ft_strjoin.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strjoin.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/12 16:35:23 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:36:17 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +char + *ft_strjoin(const char *s1, const char *s2) +{ + char *str; + size_t i; + size_t j; + size_t len; + + if (!s1 || !s2) + return (NULL); + len = ft_strlen(s1) + ft_strlen(s2); + if (!(str = ft_nstr(len))) + return (NULL); + i = 0; + j = 0; + while (s1[i] != '\0') + { + str[i] = s1[i]; + i++; + } + while (s2[j] != '\0') + { + str[i] = s2[j]; + i++; + j++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft/src/ft_strlcat.c b/libft/src/ft_strlcat.c new file mode 100644 index 0000000..4b6f036 --- /dev/null +++ b/libft/src/ft_strlcat.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strlcat.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/09 06:44:30 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:36:04 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +size_t + ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t dst_len; + + dst_len = ft_strnlen(dst, size); + if (dst_len == size) + return (dst_len + ft_strlen(src)); + return (dst_len + ft_strlcpy(dst + dst_len, src, size - dst_len)); +} diff --git a/libft/src/ft_strlchr.c b/libft/src/ft_strlchr.c new file mode 100644 index 0000000..80d1308 --- /dev/null +++ b/libft/src/ft_strlchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strlchr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:37:19 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:37:20 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +long + ft_strlchr(const char *s, int c) +{ + const size_t len = ft_strlen(s); + const size_t rem = ft_strlen(ft_strchr(s, c)); + + if (len - rem >= ft_strlen(s)) + return (-1); + return (len - rem); +} diff --git a/libft/src/ft_strlcpy.c b/libft/src/ft_strlcpy.c new file mode 100644 index 0000000..9331f1c --- /dev/null +++ b/libft/src/ft_strlcpy.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strlcpy.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/08 20:44:40 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:35:51 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +size_t + ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t src_len; + + if (!dst || !src) + return (0); + src_len = ft_strlen(src); + if (src_len + 1 < size) + ft_memcpy(dst, src, src_len + 1); + else if (size != 0) + { + ft_memcpy(dst, src, size - 1); + dst[size - 1] = 0; + } + return (src_len); +} diff --git a/libft/src/ft_strlen.c b/libft/src/ft_strlen.c new file mode 100644 index 0000000..0964c64 --- /dev/null +++ b/libft/src/ft_strlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strlen.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 16:32:28 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:35:37 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stddef.h> + +size_t + ft_strlen(const char *s) +{ + const char *ptr; + + ptr = s; + while (ptr && *ptr) + ptr++; + return (ptr - s); +} diff --git a/libft/src/ft_strmapi.c b/libft/src/ft_strmapi.c new file mode 100644 index 0000000..8a47875 --- /dev/null +++ b/libft/src/ft_strmapi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strmapi.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/13 05:57:35 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:35:12 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +char + *ft_strmapi(const char *s, char (*f)(unsigned int, char)) +{ + unsigned int i; + char *nstr; + + if (!s) + return (NULL); + i = 0; + nstr = (char *)ft_calloc(ft_strlen(s) + 1, sizeof(char)); + if (!nstr) + return (NULL); + while (s[i]) + { + nstr[i] = (*f)(i, s[i]); + i++; + } + return (nstr); +} diff --git a/libft/src/ft_strncmp.c b/libft/src/ft_strncmp.c new file mode 100644 index 0000000..3bccfd5 --- /dev/null +++ b/libft/src/ft_strncmp.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strncmp.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 20:25:31 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:35:01 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stddef.h> + +int + ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + while (s1[i] == s2[i] && i < n - 1) + { + if (!s1[i] && !s2[i]) + return (0); + i++; + } + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/libft/src/ft_strnlen.c b/libft/src/ft_strnlen.c new file mode 100644 index 0000000..d9d20a1 --- /dev/null +++ b/libft/src/ft_strnlen.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strnlen.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/30 14:28:01 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/30 14:28:03 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stddef.h> + +size_t + ft_strnlen(const char *s, size_t size) +{ + const char *ptr = s; + + while (size > 0 && *ptr) + { + size--; + ptr++; + } + return (ptr - s); +} diff --git a/libft/src/ft_strnstr.c b/libft/src/ft_strnstr.c new file mode 100644 index 0000000..711629d --- /dev/null +++ b/libft/src/ft_strnstr.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strnstr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/10 00:23:15 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:34:35 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <inttypes.h> + +char + *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + unsigned long i; + unsigned long j; + char *hay_ptr; + char *nee_ptr; + + hay_ptr = (char*)haystack; + nee_ptr = (char*)needle; + i = 0; + if (!nee_ptr[0]) + return (hay_ptr); + while (hay_ptr[i] && i < len) + { + j = 0; + while (nee_ptr[j] == hay_ptr[i + j] && (i + j) < len) + { + if (!nee_ptr[j + 1]) + return (hay_ptr + i); + j++; + } + if (!hay_ptr[i + 1] && !j) + return (0); + i++; + } + return (0); +} diff --git a/libft/src/ft_strrchr.c b/libft/src/ft_strrchr.c new file mode 100644 index 0000000..ada7470 --- /dev/null +++ b/libft/src/ft_strrchr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strrchr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 19:07:53 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:34:19 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +char + *ft_strrchr(const char *s, int c) +{ + size_t i; + + i = ft_strlen(s); + while (s[i] != c) + { + if (!i) + return (NULL); + i--; + } + return ((char*)&s[i]); +} diff --git a/libft/src/ft_strtrim.c b/libft/src/ft_strtrim.c new file mode 100644 index 0000000..48fdad5 --- /dev/null +++ b/libft/src/ft_strtrim.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_strtrim.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/12 17:30:33 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:34:03 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> + +char + *ft_strtrim(const char *s1, const char *set) +{ + size_t len; + + len = 0; + if (!s1 || !set) + return (NULL); + while (*s1 && ft_strchr(set, *s1)) + s1++; + len = ft_strlen(s1); + while (len > 0 && ft_strchr(set, s1[len - 1])) + len--; + return (ft_substr(s1, 0, len)); +} diff --git a/libft/src/ft_substr.c b/libft/src/ft_substr.c new file mode 100644 index 0000000..0cb9733 --- /dev/null +++ b/libft/src/ft_substr.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_substr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/12 15:37:02 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:33:09 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stddef.h> + +char + *ft_substr(const char *s, unsigned int start, size_t len) +{ + char *nstr; + size_t i; + + i = 0; + if (!(nstr = (char*)malloc((len + 1) * sizeof(char)))) + return (NULL); + while (s[start + i] && i < len) + { + nstr[i] = s[start + i]; + i++; + } + nstr[i] = '\0'; + return (nstr); +} diff --git a/libft/src/ft_tolower.c b/libft/src/ft_tolower.c new file mode 100644 index 0000000..48c065e --- /dev/null +++ b/libft/src/ft_tolower.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_tolower.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 18:32:59 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:32:22 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +static int + ft_isupper(int c) +{ + if (c >= 65 && c <= 90) + return (1); + return (0); +} + +int + ft_tolower(int c) +{ + if (ft_isupper(c)) + return (c + 32); + return (c); +} diff --git a/libft/src/ft_toupper.c b/libft/src/ft_toupper.c new file mode 100644 index 0000000..3346cc3 --- /dev/null +++ b/libft/src/ft_toupper.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_toupper.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/07 18:25:57 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/13 08:32:15 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +static int + ft_islower(int c) +{ + if (c >= 97 && c <= 122) + return (1); + return (0); +} + +int + ft_toupper(int c) +{ + if (ft_islower(c)) + return (c - 32); + return (c); +} diff --git a/libft/src/ft_uintlen.c b/libft/src/ft_uintlen.c new file mode 100644 index 0000000..b788ed7 --- /dev/null +++ b/libft/src/ft_uintlen.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_uintlen.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:37:05 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:37:06 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <inttypes.h> + +uint8_t + ft_uintlen(unsigned long n) +{ + uint8_t len; + + len = 0; + if (!n) + return (1); + while (n != 0) + { + n /= 10; + len++; + } + return (len); +} diff --git a/libft/src/ft_uintlen_base.c b/libft/src/ft_uintlen_base.c new file mode 100644 index 0000000..5d0261e --- /dev/null +++ b/libft/src/ft_uintlen_base.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_uintlen_base.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:37:28 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:37:29 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <inttypes.h> + +uint8_t + ft_uintlen_base(unsigned long n, char *base) +{ + uint8_t len; + uint8_t size; + + size = ft_strlen(base); + len = 0; + if (!n) + return (1); + while (n != 0) + { + n /= size; + len++; + } + return (len); +} diff --git a/libft/src/ft_uitoa.c b/libft/src/ft_uitoa.c new file mode 100644 index 0000000..87b2866 --- /dev/null +++ b/libft/src/ft_uitoa.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_uitoa.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:37:37 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:37:38 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <inttypes.h> +#include <stdlib.h> + +char + *ft_uitoa(unsigned long n) +{ + char *s; + unsigned long nb; + uint8_t i; + + i = ft_uintlen(n) - 1; + if (!(s = (char*)malloc((i + 2) * sizeof(char)))) + return (NULL); + if (!n) + s[i] = '0'; + nb = n; + s[i + 1] = '\0'; + while (nb > 0) + { + s[i] = 48 + (nb % 10); + nb = nb / 10; + i--; + } + return (s); +} diff --git a/libft/src/ft_uitoa_base.c b/libft/src/ft_uitoa_base.c new file mode 100644 index 0000000..698ff6e --- /dev/null +++ b/libft/src/ft_uitoa_base.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_uitoa_base.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/31 14:37:55 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/31 14:37:56 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +char + *ft_uitoa_base(unsigned long n, char *base) +{ + char *s; + unsigned long nb; + uint8_t i; + + i = ft_uintlen_base(n, base) - 1; + if (!(s = (char*)malloc((i + 2) * sizeof(char)))) + return (NULL); + if (!n) + s[i] = '0'; + s[i + 1] = '\0'; + while (n > 0) + { + nb = n; + if (nb >= (unsigned long)ft_strlen(base)) + nb = n % ft_strlen(base); + s[i] = base[nb]; + n /= ft_strlen(base); + i--; + } + return (s); +} diff --git a/libft/src/get_next_line.c b/libft/src/get_next_line.c new file mode 100644 index 0000000..e50e5fd --- /dev/null +++ b/libft/src/get_next_line.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* get_next_line.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/29 00:37:39 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/29 00:37:41 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> + +static uint8_t + ft_linecheck(const char *str) +{ + size_t i; + + i = 0; + while (str && str[i]) + { + if (str[i] == '\n') + return (1); + i++; + } + return (0); +} + +static size_t + ft_linelen(const char *str) +{ + size_t i; + + i = 0; + while (str[i] != '\n' && str[i] != '\0') + i++; + return (i); +} + +static int + ft_linedup(int fd, char **str) +{ + char *buff; + char *tmp; + int ret; + + if (!(buff = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char)))) + return (0); + ret = 1; + while (!ft_linecheck(*str) && (ret = read(fd, buff, BUFFER_SIZE)) > 0) + { + buff[ret] = '\0'; + tmp = *str; + *str = ft_strjoin(tmp, buff); + free(tmp); + } + if (ret < 0) + { + free(*str); + free(buff); + return (-1); + } + free(buff); + return (ret); +} + +static int + ft_errchck(int fd, char **line, char **str) +{ + if (*str == NULL) + *str = ft_nstr(0); + if (fd < 0 || !line || BUFFER_SIZE <= 0) + { + free(*str); + return (-1); + } + return (0); +} + +int + get_next_line(int fd, char **line) +{ + static char *str; + char *tmp; + size_t i; + int ret; + + if (ft_errchck(fd, line, &str) == -1) + return (-1); + ret = ft_linedup(fd, &str); + if (ret < 0) + return (-1); + i = ft_linelen(str); + *line = ft_substr(str, 0, i); + tmp = str; + if (tmp[0] != '\0' && tmp) + str = ft_strdup(tmp + i + (tmp[i] == '\n')); + if (ret == 0 && ((str == NULL || str[0] == '\0') || !ft_linecheck(str))) + { + i = tmp[i]; + free(tmp); + tmp = NULL; + return (i == '\n'); + } + free(tmp); + return (1); +} |