diff --git a/ft_printf.c b/ft_printf.c deleted file mode 100644 index 81e21e6..0000000 --- a/ft_printf.c +++ /dev/null @@ -1,276 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: o_ :::::: ::: */ -/* ft_printf.c :+: / :+::+: :+: */ -/* +:+ > +:++:+ +:+ */ -/* By: whaffman +#+ +:+ +#++#++:++#++ */ -/* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/10/11 14:49:07 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/10/15 12:43:16 by whaffman ### ### ### ### / \ */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include "libft.h" -#include "libft/libft.h" -#include "ft_printf.h" - -int ft_isbase(char *str) -{ - int i; - int n; - - n = 0; - while (str[n]) - { - i = 1; - if (str[n] == '-' || str[n] == '+') - return (0); - while (str[n + i]) - { - if (str[n] == str[n + i]) - return (0); - i++; - } - n++; - } - return (n); -} - -void ft_write_str(char c, char *str) -{ - int i; - - i = 0; - while (str[i]) - { - i++; - } - str[i++] = c; - str[i] = '\0'; -} - -void ft_putnbr_base(unsigned long nbr, char *base, char *result) -{ - unsigned int b; - - b = ft_isbase(base); - if (b < 2) - return ; - if (nbr < b) - ft_write_str(base[nbr], result); - else if (nbr >= b) - { - ft_putnbr_base(nbr / b, base, result); - ft_putnbr_base(nbr % b, base, result); - } -} - -void ft_putnbr_signed(long nbr, char *base, char *result) -{ - if (nbr < 0) - { - *result = '-'; - ft_putnbr_base((unsigned long)(-nbr), base, result); - } - else - ft_putnbr_base((unsigned long) nbr, base, result); -} - -int print_char(va_list args) -{ - char c; - - c = va_arg(args, int); - return (write(1, &c, 1)); -} - -int print_string(va_list args) -{ - char *str; - size_t len; - - str = va_arg(args, char *); - if (!str) - { - write(1, "(null)", 6); - return (6); - } - len = ft_strlen(str); - return (write(1, str, len)); -} - -int print_number(va_list args) -{ - long n; - size_t len; - char *str; - int wrote; - - wrote = 0; - str = ft_calloc(sizeof(char), 12); - if (!str) - return (wrote); - n = va_arg(args, int); - ft_putnbr_signed(n, "0123456789", str); - len = ft_strlen(str); - wrote = write(1, str, len); - free(str); - return (wrote); -} - -int print_unumber(va_list args) -{ - long n; - size_t len; - char *str; - int wrote; - - wrote = 0; - str = ft_calloc(sizeof(char), 12); - if (!str) - return (wrote); - n = va_arg(args, unsigned int); - ft_putnbr_base(n, "0123456789", str); - len = ft_strlen(str); - wrote = write(1, str, len); - free(str); - return (wrote); -} - -int print_hex(va_list args, int uppercase) -{ - long n; - size_t len; - char *str; - int wrote; - - wrote = 0; - str = ft_calloc(sizeof(char), 20); - if (!str) - return (wrote); - n = va_arg(args, unsigned int); - if (uppercase) - ft_putnbr_base(n, "0123456789ABCDEF", str); - else - ft_putnbr_base(n, "0123456789abcdef", str); - len = ft_strlen(str); - wrote = write(1, str, len); - free(str); - return (wrote); -} - -int print_pointer(va_list args) -{ - long n; - size_t len; - char *str; - int wrote; - - wrote = 0; - n = va_arg(args, unsigned long); - if (!n) - { - write(1, "(nil)", 5); - return (5); - } - str = ft_calloc(sizeof(char), 20); - if (!str) - return (wrote); - - ft_strlcpy(str, "0x", 3); - ft_putnbr_base(n, "0123456789abcdef", str); - len = ft_strlen(str); - wrote = write(1, str, len); - free(str); - return (wrote); -} - -int parse_conversion(const char **format, va_list args) -{ - int wrote; - - wrote = 0; - if (**format == 'c') - wrote = print_char(args); - else if (**format == 's') - wrote = print_string(args); - else if (ft_strchr("di", **format)) - wrote = print_number(args); - else if (ft_strchr("u", **format)) - wrote = print_unumber(args); - else if (ft_strchr("p", **format)) - wrote = print_pointer(args); - else if (ft_strchr("x", **format)) - wrote = print_hex(args, 0); - else if (ft_strchr("X", **format)) - wrote = print_hex(args, 1); - return (wrote); -} - -int parse_placeholder(const char **format, va_list args) -{ - const char conversions[] = "cspdiuxX%"; - int wrote; - - (*format)++; - wrote = 0; - while (**format && !ft_strchr(conversions, **format)) - (*format)++; - if (**format == '%') - wrote = write(1, *format, 1); - else - wrote = parse_conversion(format, args); - return (wrote); -} - -int ft_printf(const char *format, ...) -{ - int wrote; - int result; - va_list args; - - result = 0; - va_start(args, format); - while (*format && result >= 0) - { - if (*format == '%') - wrote = parse_placeholder(&format, args); - else - wrote = write(1, format, 1); - if (wrote < 0) - { - result = wrote; - break; - } - result += wrote; - format++; - } - va_end(args); - return (result); -} - -/* -int main(void) -{ - const char str[] = "HOIe"; - - ft_printf("test:\n"); - ft_printf("%%%% = %%\n--\n"); - ft_printf("%%c = %c\n--\n", 't'); - ft_printf("%%s = %s\n--\n", str); - ft_printf("%%d = %d\n--\n", 255); - ft_printf("%%i = %i\n--\n", 255); - ft_printf("%%u = %u\n--\n", 255); - ft_printf("%%p = %p\n--\n", str); - ft_printf("%%x = %x\n--\n", 255); - ft_printf("%%X = %X\n--\n", 255); - - ft_printf(" %% %% %%\n"); - ft_printf(" %% %% %%\n"); - ft_printf(" %% %% %%\n"); - return (0); -} -*/ diff --git a/ft_printf/ft_isbase.c b/ft_printf/ft_isbase.c new file mode 100644 index 0000000..f39ae28 --- /dev/null +++ b/ft_printf/ft_isbase.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_isbase.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:18:39 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:19:43 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +int ft_isbase(char *str) +{ + int i; + int n; + + n = 0; + while (str[n]) + { + i = 1; + if (str[n] == '-' || str[n] == '+') + return (0); + while (str[n + i]) + { + if (str[n] == str[n + i]) + return (0); + i++; + } + n++; + } + return (n); +} diff --git a/ft_printf/ft_printf.c b/ft_printf/ft_printf.c new file mode 100644 index 0000000..0572b05 --- /dev/null +++ b/ft_printf/ft_printf.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_printf.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/11 14:49:07 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:37:46 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" +#include "ft_printf.h" + +int ft_printf(const char *format, ...) +{ + int wrote; + int result; + va_list args; + + result = 0; + va_start(args, format); + while (*format && result >= 0) + { + if (*format == '%') + wrote = parse_placeholder(&format, args); + else + wrote = write(1, format, 1); + if (wrote < 0) + { + result = wrote; + break ; + } + result += wrote; + format++; + } + va_end(args); + return (result); +} diff --git a/ft_printf.h b/ft_printf/ft_putnbr_base.c similarity index 50% rename from ft_printf.h rename to ft_printf/ft_putnbr_base.c index 2f241d9..f111ab2 100644 --- a/ft_printf.h +++ b/ft_printf/ft_putnbr_base.c @@ -1,32 +1,29 @@ /* ************************************************************************** */ /* */ /* ::: o_ :::::: ::: */ -/* ft_printf.h :+: / :+::+: :+: */ +/* ft_putnbr_base.c :+: / :+::+: :+: */ /* +:+ > +:++:+ +:+ */ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/10/15 13:04:31 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/10/15 13:07:34 by whaffman ### ### ### ### / \ */ +/* Created: 2024/10/27 16:21:37 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:22:22 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ -#ifndef FT_PRINTF_H -# define FT_PRINTF_H +#include "ft_printf.h" -# include +void ft_putnbr_base(unsigned long nbr, char *base, char *result) +{ + unsigned int b; -int ft_printf(const char *format, ...); -int ft_isbase(char *str); -void ft_write_str(char c, char *str); -void ft_putnbr_base(unsigned long nbr, char *base, char *result); -void ft_putnbr_signed(long nbr, char *base, char *result); -int print_char(va_list args); -int print_string(va_list args); -int print_number(va_list args); -int print_unumber(va_list args); -int print_hex(va_list args, int uppercase); -int print_pointer(va_list args); -int parse_conversion(const char **format, va_list args); -int parse_placeholder(const char **format, va_list args); - -#endif + b = ft_isbase(base); + if (b < 2) + return ; + if (nbr < b) + ft_write_str(base[nbr], result); + else if (nbr >= b) + { + ft_putnbr_base(nbr / b, base, result); + ft_putnbr_base(nbr % b, base, result); + } +} diff --git a/ft_printf/ft_putnbr_signed.c b/ft_printf/ft_putnbr_signed.c new file mode 100644 index 0000000..12e112d --- /dev/null +++ b/ft_printf/ft_putnbr_signed.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_putnbr_signed.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:22:39 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:23:02 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putnbr_signed(long nbr, char *base, char *result) +{ + if (nbr < 0) + { + *result = '-'; + ft_putnbr_base((unsigned long)(-nbr), base, result); + } + else + ft_putnbr_base((unsigned long) nbr, base, result); +} diff --git a/ft_printf/ft_write_str.c b/ft_printf/ft_write_str.c new file mode 100644 index 0000000..c6269cd --- /dev/null +++ b/ft_printf/ft_write_str.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_write_str.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:19:20 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:19:53 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +void ft_write_str(char c, char *str) +{ + int i; + + i = 0; + while (str[i]) + { + i++; + } + str[i++] = c; + str[i] = '\0'; +} diff --git a/ft_printf/parse_conversion.c b/ft_printf/parse_conversion.c new file mode 100644 index 0000000..1019cc8 --- /dev/null +++ b/ft_printf/parse_conversion.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* parse_conversion.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:34:43 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:34:49 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" +#include "ft_printf.h" + +int parse_conversion(const char **format, va_list args) +{ + int wrote; + + wrote = 0; + if (**format == 'c') + wrote = print_char(args); + else if (**format == 's') + wrote = print_string(args); + else if (ft_strchr("di", **format)) + wrote = print_number(args); + else if (ft_strchr("u", **format)) + wrote = print_unumber(args); + else if (ft_strchr("p", **format)) + wrote = print_pointer(args); + else if (ft_strchr("x", **format)) + wrote = print_hex(args, 0); + else if (ft_strchr("X", **format)) + wrote = print_hex(args, 1); + return (wrote); +} diff --git a/ft_printf/parse_placeholder.c b/ft_printf/parse_placeholder.c new file mode 100644 index 0000000..d5da9fd --- /dev/null +++ b/ft_printf/parse_placeholder.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* parse_placeholder.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:34:28 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:35:09 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" +#include "ft_printf.h" + +int parse_placeholder(const char **format, va_list args) +{ + const char conversions[] = "cspdiuxX%"; + int wrote; + + (*format)++; + wrote = 0; + while (**format && !ft_strchr(conversions, **format)) + (*format)++; + if (**format == '%') + wrote = write(1, *format, 1); + else + wrote = parse_conversion(format, args); + return (wrote); +} diff --git a/ft_printf/print_char.c b/ft_printf/print_char.c new file mode 100644 index 0000000..d2a2037 --- /dev/null +++ b/ft_printf/print_char.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_char.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:28:10 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:28:12 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include + +int print_char(va_list args) +{ + char c; + + c = va_arg(args, int); + return (write(1, &c, 1)); +} diff --git a/ft_printf/print_hex.c b/ft_printf/print_hex.c new file mode 100644 index 0000000..91d4196 --- /dev/null +++ b/ft_printf/print_hex.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_hex.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:29:01 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:30:00 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "libft.h" +#include "ft_printf.h" + +int print_hex(va_list args, int uppercase) +{ + long n; + size_t len; + char *str; + int wrote; + + wrote = 0; + str = ft_calloc(sizeof(char), 20); + if (!str) + return (wrote); + n = va_arg(args, unsigned int); + if (uppercase) + ft_putnbr_base(n, "0123456789ABCDEF", str); + else + ft_putnbr_base(n, "0123456789abcdef", str); + len = ft_strlen(str); + wrote = write(1, str, len); + free(str); + return (wrote); +} diff --git a/ft_printf/print_number.c b/ft_printf/print_number.c new file mode 100644 index 0000000..7f0511e --- /dev/null +++ b/ft_printf/print_number.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_number.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:31:23 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:31:37 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "libft.h" +#include "ft_printf.h" + +int print_number(va_list args) +{ + long n; + size_t len; + char *str; + int wrote; + + wrote = 0; + str = ft_calloc(sizeof(char), 12); + if (!str) + return (wrote); + n = va_arg(args, int); + ft_putnbr_signed(n, "0123456789", str); + len = ft_strlen(str); + wrote = write(1, str, len); + free(str); + return (wrote); +} diff --git a/ft_printf/print_pointer.c b/ft_printf/print_pointer.c new file mode 100644 index 0000000..d7875c3 --- /dev/null +++ b/ft_printf/print_pointer.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_pointer.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:26:58 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:31:19 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "../libft/libft.h" +#include "ft_printf.h" + +int print_pointer(va_list args) +{ + long n; + size_t len; + char *str; + int wrote; + + wrote = 0; + n = va_arg(args, unsigned long); + if (!n) + { + write(1, "(nil)", 5); + return (5); + } + str = ft_calloc(sizeof(char), 20); + if (!str) + return (wrote); + ft_strlcpy(str, "0x", 3); + ft_putnbr_base(n, "0123456789abcdef", str); + len = ft_strlen(str); + wrote = write(1, str, len); + free(str); + return (wrote); +} diff --git a/ft_printf/print_string.c b/ft_printf/print_string.c new file mode 100644 index 0000000..f9d820e --- /dev/null +++ b/ft_printf/print_string.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_string.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:31:48 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:32:23 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" +#include "ft_printf.h" + +int print_string(va_list args) +{ + char *str; + size_t len; + + str = va_arg(args, char *); + if (!str) + { + write(1, "(null)", 6); + return (6); + } + len = ft_strlen(str); + return (write(1, str, len)); +} diff --git a/ft_printf/print_unumber.c b/ft_printf/print_unumber.c new file mode 100644 index 0000000..c41f6de --- /dev/null +++ b/ft_printf/print_unumber.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_unumber.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/27 16:32:36 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/27 16:32:49 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "libft.h" +#include "ft_printf.h" + +int print_unumber(va_list args) +{ + long n; + size_t len; + char *str; + int wrote; + + wrote = 0; + str = ft_calloc(sizeof(char), 12); + if (!str) + return (wrote); + n = va_arg(args, unsigned int); + ft_putnbr_base(n, "0123456789", str); + len = ft_strlen(str); + wrote = write(1, str, len); + free(str); + return (wrote); +}