diff --git a/Makefile b/Makefile index e50b29e..0b8307b 100755 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ SRCS += str/ft_len.c str/ft_lcpy.c str/ft_lcat.c str/ft_chr.c \ str/ft_rchr.c str/ft_ncmp.c str/ft_nstr.c str/ft_dup.c \ str/ft_sub.c str/ft_join.c str/ft_trim.c str/ft_split.c \ str/ft_rev.c str/ft_mapi.c str/ft_iteri.c str/ft_join_with.c \ - str/ft_new.c str/ft_append.c + str/ft_new.c str/ft_append.c str/ft_cmp.c SRCS += conv/ft_toupper.c conv/ft_tolower.c conv/ft_atoi.c conv/ft_itoa.c \ conv/ft_itox.c conv/ft_utoa.c diff --git a/libft.h b/libft.h index 14abb23..ce130ba 100755 --- a/libft.h +++ b/libft.h @@ -68,6 +68,7 @@ size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); size_t ft_strlen(const char *s); size_t ft_strlen_to(const char *str, char ch); int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_strcmp(const char *s1, const char *s2); char *ft_strnstr(const char *haystack, const char *needle, size_t len); char *ft_strrchr(const char *s, int c); void ft_strrev(char **str, unsigned int neg); diff --git a/printf/ft_printf.c b/printf/ft_printf.c new file mode 100644 index 0000000..e7cca64 --- /dev/null +++ b/printf/ft_printf.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */ +/* Updated: 2021/11/17 09:40:16 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "ft_printf.h" + +int ft_printf(const char *str, ...) +{ + va_list args; + size_t n; + size_t i; + const char *g_types = {"cspdiuxX%"}; + static int (*g_printf_fcts[9])(va_list arg, const char *str) = {\ + va_putchar, va_putstr, va_putptr, va_putnbr, va_putnbr, va_putunsign, \ + va_putx, va_putx_cap, va_putperc}; + + i = 0; + va_start (args, str); + while (*str) + { + n = -1; + if ((*str == '%' && str++) || !(++i && ft_putchar((int)*str))) + while (++n < 9) + if (g_types[n] == *str) + i += (g_printf_fcts[n])(args, str); + str++; + } + va_end(args); + return (i); +} diff --git a/printf/ft_putchars.c b/printf/ft_putchars.c new file mode 100644 index 0000000..7845979 --- /dev/null +++ b/printf/ft_putchars.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchars.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 09:17:03 by narnaud #+# #+# */ +/* Updated: 2021/11/17 09:57:53 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putchar(int ch) +{ + char c; + + c = (char)ch; + write(1, &c, 1); + return (1); +} + +int ft_putstr(char *str) +{ + int i; + + i = 0; + if (!str) + { + ft_putstr("(null)"); + return (6); + } + while (str[i]) + ft_putchar(str[i++]); + return (i); +} + +int va_putchar(va_list va_ch, const char *str) +{ + (void)*str; + ft_putchar(va_arg(va_ch, int)); + return (1); +} + +int va_putstr(va_list va_str, const char *str) +{ + (void)*str; + return (ft_putstr(va_arg(va_str, char *))); +} + +int va_putperc(va_list va, const char *str) +{ + (void)*str; + (void)va; + ft_putchar('%'); + return (1); +} diff --git a/printf/ft_putnbrs.c b/printf/ft_putnbrs.c new file mode 100644 index 0000000..8e4dfe6 --- /dev/null +++ b/printf/ft_putnbrs.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 09:34:01 by narnaud #+# #+# */ +/* Updated: 2021/12/21 10:06:28 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putnbr(int n, const char *str) +{ + char *n_str; + int ret; + + (void)*str; + n_str = ft_itoa(n); + ret = ft_putstr(n_str); + free(n_str); + return (ret); +} + +int ft_putunsign(unsigned int n, const char *str) +{ + char *n_str; + int ret; + + (void)*str; + n_str = ft_utoa(n); + ret = ft_putstr(n_str); + free(n_str); + return (ret); +} + +int va_putnbr(va_list va, const char *str) +{ + return (ft_putnbr(va_arg(va, int), str)); +} + +int va_putunsign(va_list va, const char *str) +{ + return (ft_putunsign(va_arg(va, unsigned int), str)); +} diff --git a/printf/ft_putptr.c b/printf/ft_putptr.c new file mode 100644 index 0000000..d4d3386 --- /dev/null +++ b/printf/ft_putptr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 10:13:07 by narnaud #+# #+# */ +/* Updated: 2021/12/21 09:50:41 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putptr(void *ptr) +{ + char *str; + int ret; + + str = ft_itox((unsigned long long int)ptr, "0123456789abcdef"); + ft_putstr("0x"); + ret = ft_putstr(str); + free(str); + return (ret + 2); +} + +int va_putptr(va_list va_ptr, const char *str) +{ + (void)*str; + return (ft_putptr(va_arg(va_ptr, void *))); +} diff --git a/printf/ft_putx.c b/printf/ft_putx.c new file mode 100644 index 0000000..7652079 --- /dev/null +++ b/printf/ft_putx.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 09:54:24 by narnaud #+# #+# */ +/* Updated: 2021/11/17 10:24:24 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putx(unsigned int n) +{ + char *str; + int ret; + + str = ft_itox((unsigned long int)n, "0123456789abcdef"); + ret = ft_putstr(str); + free(str); + return (ret); +} + +int ft_putx_cap(unsigned int n) +{ + char *str; + int ret; + + str = ft_itox((unsigned long int)n, "0123456789ABCDEF"); + ret = ft_putstr(str); + free(str); + return (ret); +} + +int va_putx(va_list va_uint, const char *str) +{ + (void)*str; + return (ft_putx(va_arg(va_uint, unsigned int))); +} + +int va_putx_cap(va_list va_uint, const char *str) +{ + (void)*str; + return (ft_putx_cap(va_arg(va_uint, unsigned int))); +} diff --git a/str/ft_cmp.c b/str/ft_cmp.c new file mode 100644 index 0000000..fd5669c --- /dev/null +++ b/str/ft_cmp.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/18 16:16:37 by narnaud #+# #+# */ +/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + size_t i; + int ret; + unsigned char *s1cp; + unsigned char *s2cp; + + i = 0; + s1cp = (unsigned char *)s1; + s2cp = (unsigned char *)s2; + ret = s1cp[i] - s2cp[i]; + while ((!ret && s1[i] && s2[i])) + { + ret = s1cp[i] - s2cp[i]; + i++; + } + return (ret); +}