From 0cd1d2ef1bc57e427c90c20655fd410ce6a6fbd5 Mon Sep 17 00:00:00 2001 From: gbaconni Date: Mon, 18 Apr 2022 00:52:18 +0200 Subject: [PATCH] Move dispatcher to ft_vprintf_percent.c --- Makefile | 3 +- libftprintf/Makefile | 3 +- libftprintf/ft_vprintf.c | 19 ++----- libftprintf/ft_vprintf_char.c | 2 +- libftprintf/ft_vprintf_escape.c | 39 ++++++++++++++ libftprintf/ft_vprintf_other.c | 2 +- libftprintf/ft_vprintf_percent.c | 17 ++++-- libftprintf/libft/ft_strlen.c | 2 +- libftprintf/libftprintf.h | 3 +- main.c | 90 +++++++++++++++++++++++++++----- 10 files changed, 141 insertions(+), 39 deletions(-) create mode 100644 libftprintf/ft_vprintf_escape.c diff --git a/Makefile b/Makefile index 4daa681..313c37d 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # -# Updated: 2022/04/17 13:18:09 by gbaconni ### lausanne.ch # +# Updated: 2022/04/17 20:56:04 by gbaconni ### lausanne.ch # # # # **************************************************************************** # # @@ -99,6 +99,7 @@ test: clean $(NAME) @$(PREFIX) ./$(NAME) "%p" "" || true @$(PREFIX) ./$(NAME) "%p" "\n" || true @$(PREFIX) ./$(NAME) "Forty Two" "" || true + @$(PREFIX) ./$(NAME) "\t\r\n" "" || true test2: leak @$(MAKE) test LEAK=1 diff --git a/libftprintf/Makefile b/libftprintf/Makefile index e046c17..831109a 100644 --- a/libftprintf/Makefile +++ b/libftprintf/Makefile @@ -6,7 +6,7 @@ # By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # -# Updated: 2022/04/17 11:18:36 by gbaconni ### lausanne.ch # +# Updated: 2022/04/18 00:28:09 by gbaconni ### lausanne.ch # # # # **************************************************************************** # # @@ -40,6 +40,7 @@ SRC = \ ft_vprintf_decimal.c \ ft_vprintf_hexadecimal.c \ ft_vprintf_percent.c \ + ft_vprintf_escape.c \ ft_vprintf_other.c \ ft_vprintf.c \ ft_printf.c diff --git a/libftprintf/ft_vprintf.c b/libftprintf/ft_vprintf.c index 4d750b2..95c1125 100644 --- a/libftprintf/ft_vprintf.c +++ b/libftprintf/ft_vprintf.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:43:52 by gbaconni #+# #+# */ -/* Updated: 2022/04/16 01:14:44 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/17 22:45:28 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -20,20 +20,9 @@ int ft_vprintf(char const *format, va_list ap) while (*format != 0) { if (*format == '%') - { - if (*++format == '%') - ret += ft_vprintf_percent(format, ap); - else if (*format == 'c') - ret += ft_vprintf_char(format, ap); - else if (*format == 's') - ret += ft_vprintf_string(format, ap); - else if (*format == 'p') - ret += ft_vprintf_pointer(format, ap); - else if (*format == 'd' || *format == 'i' || *format == 'u') - ret += ft_vprintf_decimal(format, ap); - else if (*format == 'x' || *format == 'X') - ret += ft_vprintf_hexadecimal(format, ap); - } + ret += ft_vprintf_percent(++format, ap); + else if (*format == '\\') + ret += ft_vprintf_escape(++format, ap); else ret += ft_vprintf_other(format, ap); format++; diff --git a/libftprintf/ft_vprintf_char.c b/libftprintf/ft_vprintf_char.c index 84e74fc..bb097cc 100644 --- a/libftprintf/ft_vprintf_char.c +++ b/libftprintf/ft_vprintf_char.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:54:48 by gbaconni #+# #+# */ -/* Updated: 2022/04/16 00:24:01 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/17 20:56:56 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ diff --git a/libftprintf/ft_vprintf_escape.c b/libftprintf/ft_vprintf_escape.c new file mode 100644 index 0000000..1551e0d --- /dev/null +++ b/libftprintf/ft_vprintf_escape.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_escape.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/17 22:35:03 by gbaconni #+# #+# */ +/* Updated: 2022/04/17 23:08:35 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_escape(const char *format, va_list ap) +{ + int ret; + + (void) ap; + if (*format == '0') + ret = ft_putchar('\0'); + if (*format == 'a') + ret = ft_putchar('\a'); + else if (*format == 'b') + ret = ft_putchar('\b'); + else if (*format == 't') + ret = ft_putchar('\t'); + else if (*format == 'n') + ret = ft_putchar('\n'); + else if (*format == 'v') + ret = ft_putchar('\v'); + else if (*format == 'f') + ret = ft_putchar('\f'); + else if (*format == 'r') + ret = ft_putchar('\r'); + else if (*format == 'e') + ret = ft_putchar('\033'); + return (ret); +} diff --git a/libftprintf/ft_vprintf_other.c b/libftprintf/ft_vprintf_other.c index 6f33181..d87ebab 100644 --- a/libftprintf/ft_vprintf_other.c +++ b/libftprintf/ft_vprintf_other.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 12:58:03 by gbaconni #+# #+# */ -/* Updated: 2022/04/16 01:28:02 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/17 22:39:45 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ diff --git a/libftprintf/ft_vprintf_percent.c b/libftprintf/ft_vprintf_percent.c index 5e9ea50..b395a04 100644 --- a/libftprintf/ft_vprintf_percent.c +++ b/libftprintf/ft_vprintf_percent.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:55:58 by gbaconni #+# #+# */ -/* Updated: 2022/04/16 00:32:06 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/17 22:40:01 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -16,8 +16,17 @@ int ft_vprintf_percent(const char *format, va_list ap) { int ret; - (void) format; - (void) ap; - ret = ft_putchar('%'); + if (*format == '%') + ret = ft_vprintf_other(format, ap); + else if (*format == 'c') + ret = ft_vprintf_char(format, ap); + else if (*format == 's') + ret = ft_vprintf_string(format, ap); + else if (*format == 'p') + ret = ft_vprintf_pointer(format, ap); + else if (*format == 'd' || *format == 'i' || *format == 'u') + ret = ft_vprintf_decimal(format, ap); + else if (*format == 'x' || *format == 'X') + ret = ft_vprintf_hexadecimal(format, ap); return (ret); } diff --git a/libftprintf/libft/ft_strlen.c b/libftprintf/libft/ft_strlen.c index 5e5c51c..6acb5c0 100644 --- a/libftprintf/libft/ft_strlen.c +++ b/libftprintf/libft/ft_strlen.c @@ -6,7 +6,7 @@ /* By: gbaconni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/11 14:35:03 by gbaconni #+# #+# */ -/* Updated: 2021/10/24 13:11:12 by gbaconni ### ########.fr */ +/* Updated: 2022/04/17 23:47:44 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ diff --git a/libftprintf/libftprintf.h b/libftprintf/libftprintf.h index 1c463e4..545bc5c 100644 --- a/libftprintf/libftprintf.h +++ b/libftprintf/libftprintf.h @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/18 15:26:06 by gbaconni #+# #+# */ -/* Updated: 2022/04/16 00:45:36 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/18 00:26:16 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ int ft_vprintf_pointer(const char *format, va_list ap); int ft_vprintf_decimal(const char *format, va_list ap); int ft_vprintf_hexadecimal(const char *format, va_list ap); int ft_vprintf_percent(const char *format, va_list ap); +int ft_vprintf_escape(const char *format, va_list ap); int ft_vprintf_other(const char *format, va_list ap); /* Bonus */ diff --git a/main.c b/main.c index 71a2e99..7a19b84 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: baco +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/13 06:58:46 by gbaconni #+# #+# */ -/* Updated: 2022/04/17 18:51:22 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/18 00:50:01 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -94,6 +94,65 @@ int ft_sprintf(char *str, const char *format, ...) } } +size_t ft_unescape_len(const char *str) +{ + size_t len; + + len = 0; + while (*str != '\0') + { + if (*str == '\\') + str++; + len++; + str++; + } + return (len); +} + +char ft_unescape_char(const char c) +{ + if (c == 'a') + return ('\a'); + else if (c == 'b') + return ('\b'); + else if (c == 't') + return ('\t'); + else if (c == 'n') + return ('\n'); + else if (c == 'v') + return ('\v'); + else if (c == 'f') + return ('\f'); + else if (c == 'r') + return ('\r'); + else if (c == 'e') + return ('\033'); + else + return (c); +} + +char *ft_unescape(const char *str) +{ + char *s; + void *ptr; + + s = (char *) ft_calloc(ft_unescape_len(str), sizeof(char)); + if (s == NULL) + return (NULL); + ptr = s; + while (*str != '\0') + { + if (*str == '\\') + *s++ = ft_unescape_char(*++str); + else + *s++ = *str; + str++; + } + *s = '\0'; + s = ptr; + return (s); +} + int main(int argc, char *argv[]) { int ft_ret; @@ -101,6 +160,7 @@ int main(int argc, char *argv[]) char out[256]; char ft_out[256]; char *format; + char *f; int d; char c; char *s; @@ -116,18 +176,19 @@ int main(int argc, char *argv[]) (void)s; if (argc > 1) { - format = argv[1]; + f = argv[1]; + format = ft_unescape(f); if (format[0] == '%') { if (format[1] == 'c') { c = argv[2][0]; ret = sprintf(out, format, c); - printf("%d = printf(\"%s\", '%c')\n%s\n", ret, format, c, out); + printf("%d = printf(\"%s\", '%c')\n%s\n", ret, f, c, out); ft_begin(fd); ft_ret = ft_printf(format, c); ft_end(fd, ft_out); - printf("%d = ft_printf(\"%s\", '%c')\n%s\n\n", ft_ret, format, c, ft_out); + printf("%d = ft_printf(\"%s\", '%c')\n%s\n\n", ft_ret, f, c, ft_out); assert(ret == ft_ret); assert(strcmp(out, ft_out) == 0); } @@ -135,11 +196,11 @@ int main(int argc, char *argv[]) { s = argv[2]; ret = sprintf(out, format, s); - printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, format, s, out); + printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, f, s, out); ft_begin(fd); ft_ret = ft_printf(format, s); ft_end(fd, ft_out); - printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, format, s, ft_out); + printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, f, s, ft_out); assert(ret == ft_ret); assert(strcmp(out, ft_out) == 0); } @@ -152,11 +213,11 @@ int main(int argc, char *argv[]) ptr = NULL; } ret = sprintf(out, format, ptr); - printf("%d = printf(\"%s\", %p)\n%s\n", ret, format, ptr, out); + printf("%d = printf(\"%s\", %p)\n%s\n", ret, f, ptr, out); ft_begin(fd); ft_ret = ft_printf(format, ptr); ft_end(fd, ft_out); - printf("%d = ft_printf(\"%s\", %p)\n%s\n\n", ft_ret, format, ptr, ft_out); + printf("%d = ft_printf(\"%s\", %p)\n%s\n\n", ft_ret, f, ptr, ft_out); assert(ret == ft_ret); assert(strcmp(out, ft_out) == 0); } @@ -164,11 +225,11 @@ int main(int argc, char *argv[]) { d = atoi(argv[2]); ret = sprintf(out, format, d); - printf("%d = printf(\"%s\", %d)\n%s\n", ret, format, d, out); + printf("%d = printf(\"%s\", %d)\n%s\n", ret, f, d, out); ft_begin(fd); ft_ret = ft_printf(format, d); ft_end(fd, ft_out); - printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, format, d, ft_out); + printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, f, d, ft_out); assert(ret == ft_ret); assert(strcmp(out, ft_out) == 0); } @@ -176,11 +237,11 @@ int main(int argc, char *argv[]) { d = atoi(argv[2]); ret = sprintf(out, format, d); - printf("%d = printf(\"%s\", %d)\n%s\n", ret, format, d, out); + printf("%d = printf(\"%s\", %d)\n%s\n", ret, f, d, out); ft_begin(fd); ft_ret = ft_printf(format, d); ft_end(fd, ft_out); - printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, format, d, ft_out); + printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, f, d, ft_out); assert(ret == ft_ret); assert(strcmp(out, ft_out) == 0); } @@ -189,14 +250,15 @@ int main(int argc, char *argv[]) { s = argv[2]; ret = sprintf(out, format, s); - printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, format, s, out); + printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, f, s, out); ft_begin(fd); ft_ret = ft_printf(format, s); ft_end(fd, ft_out); - printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, format, s, ft_out); + printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, f, s, ft_out); assert(ret == ft_ret); assert(strcmp(out, ft_out) == 0); } + free(format); } return (0); }