From 2d6500136bd78fbc2545610f6f08718c728a9028 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 14 Nov 2019 10:26:41 +0100 Subject: Fixed little memory leak in handle_precision --- .gitignore | 4 +- Makefile | 7 ++-- convert.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ libft | 2 +- main.c | 77 +++++++++++++++++++------------------- printer.c | 115 --------------------------------------------------------- test | Bin 23144 -> 0 bytes 7 files changed, 168 insertions(+), 159 deletions(-) create mode 100644 convert.c delete mode 100644 printer.c delete mode 100755 test diff --git a/.gitignore b/.gitignore index 3115c5e..680d62b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.o -a.out *ghc +a.out ft_printf -*.a test +*.a *.dSYM diff --git a/Makefile b/Makefile index 68acda3..4097b0c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/28 17:41:14 by cacharle #+# #+# # -# Updated: 2019/11/13 09:27:01 by cacharle ### ########.fr # +# Updated: 2019/11/14 10:23:31 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -19,10 +19,10 @@ INCFLAGS = -I$(LIBFT_ROOT) RM = rm -f LIB = ar rcs -MAKE = make -j4 +MAKE = make NAME = libftprintf.a -SRC = ft_printf.c utils.c printer.c parse.c list.c extract.c \ +SRC = ft_printf.c utils.c convert.c parse.c list.c extract.c \ convert_int.c convert_uint.c convert_char.c convert_str.c \ convert_ptr.c convert_hex.c convert_percent.c convert_written.c \ convert_none.c length_modifier.c @@ -49,6 +49,7 @@ fclean: libft_fclean clean re: fclean all +test: CCFLAGS += -g test: all $(CC) $(CCFLAGS) $(LDFLAGS) -L./libft -lft $(INCFLAGS) -o test main.c diff --git a/convert.c b/convert.c new file mode 100644 index 0000000..a3a5ecf --- /dev/null +++ b/convert.c @@ -0,0 +1,122 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* printer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */ +/* Updated: 2019/11/14 10:22:04 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "libft.h" +#include "header.h" + +char *convert(t_pformat *pformat, va_list ap) +{ + char *str; + + if (pformat == NULL) + return (NULL); + if (pformat->flags & FLAG_WIDTH_WILDCARD) + { + if (pformat->flags & FLAG_WIDTH_OVERWRITE) + (void)va_arg(ap, int); + else + pformat->width = va_arg(ap, int); + if (pformat->width < 0) + { + pformat->flags |= FLAG_MINUS; + pformat->width *= -1; + } + } + if (pformat->flags & FLAG_PRECISION_WILDCARD) + pformat->precision = va_arg(ap, int); + if ((str = convert_specifier(ap, pformat)) == NULL) + return (NULL); + return (str); +} + +char *convert_specifier(va_list ap, t_pformat *pformat) +{ + if (pformat->specifier == 'c') + return (convert_char(ap, pformat)); + if (pformat->specifier == 's') + return (convert_str(ap, pformat)); + if (pformat->specifier == 'p') + return (convert_ptr(ap, pformat)); + if (pformat->specifier == 'd' || pformat->specifier == 'i') + return (convert_int(ap, pformat)); + if (pformat->specifier == 'u') + return (convert_uint(ap, pformat)); + if (pformat->specifier == 'x') + return (convert_hex(ap, pformat)); + if (pformat->specifier == 'X') + return (convert_hex(ap, pformat)); + if (pformat->specifier == '%') + return (convert_percent(ap, pformat)); + if (pformat->specifier == 'n') + return (convert_written(ap, pformat)); + else + return (convert_none(ap, pformat)); + return (NULL); +} + +char *handle_width(t_pformat *pformat, char *str) +{ + char *tmp; + int len; + int i; + + if ((len = ft_strlen(str)) >= pformat->width) + return (str); + if ((tmp = (char*)malloc(sizeof(char) * (pformat->width + 1))) == NULL) + return (NULL); + if (pformat->flags & FLAG_MINUS) + { + i = len; + ft_strcpy(tmp, str); + while (i < pformat->width) + tmp[i++] = ' '; + tmp[i] = 0; + } + else + { + i = 0; + while (i <= pformat->width - len) + tmp[i++] = pformat->flags & FLAG_ZERO ? '0' : ' '; + ft_strcpy(tmp + i - 1, str); + } + free(str); + return (tmp); +} + +char *handle_precision(t_pformat *pformat, char *str) +{ + int len; + char *tmp; + + if (pformat == NULL || str == NULL) + return (NULL); + if (ft_strchr("diuxX", pformat->specifier) && pformat->precision >= 0) + pformat->flags &= ~FLAG_ZERO; + len = ft_strlen(str); + if (pformat->precision == 0 && str[0] == '0') + { + free(str); + return (ft_strdup("")); + } + if (!ft_strchr("diuxXp", pformat->specifier) || len >= pformat->precision) + return (str); + if ((tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1))) == NULL) + return (NULL); + ft_strcpy(tmp + pformat->precision - len, str); + while (pformat->precision-- > len) + tmp[pformat->precision - len] = '0'; + free(str); + return (tmp); +} diff --git a/libft b/libft index 5bf4999..d8e5d37 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 5bf49994b911abc2b8ff3bdb525cdc23c71f2102 +Subproject commit d8e5d376244c6cf92e52666634cddaaf4e492aad diff --git a/main.c b/main.c index e6e0031..a6b61e5 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/28 04:25:09 by cacharle #+# #+# */ -/* Updated: 2019/11/13 09:27:58 by cacharle ### ########.fr */ +/* Updated: 2019/11/14 09:23:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,50 +38,51 @@ int main() /* ft_printf("[%05]\n"); */ /* printf("[%05]\n"); */ - int test = 42; - ft_printf("bonjour\n"); - ft_printf("char: %c\n", 'r'); - ft_printf("string: %s\n", "bonjour"); - ft_printf("pointer: %p\n", &test); - ft_printf("int: %d or %i\n", 45, 54); - ft_printf("uint: %u\n", 1 << 31); - ft_printf("hex lower: %x\n", 0xabcf012); - ft_printf("hex upper: %X\n", 0xabcf012); - ft_printf("percent: %%\n"); - ft_printf("multiple stuff: %d %u %d %x %d\n", 1, -2, 3, 0xa, 5); - - ft_printf("precision |%.9d|\n", 43); - printf("precision |%.9d|\n", 43); - ft_printf("string precision |%.9s|\n", "jesuisbonjourbonjour"); - printf("string precision |%.9s|\n", "jesuisbonjourbonjour"); - ft_printf("min width |%9d|\n", 43); - printf("min width |%9d|\n", 43); - ft_printf("zero padding |%09d|\n", 43); - printf("zero padding |%09d|\n", 43); - ft_printf("left adjusted |%-9d|\n", 43); - printf("left adjusted |%-9d|\n", 43); - ft_printf("string padding |%9s|\n", "bon"); - printf("string padding |%9s|\n", "bon"); - - ft_printf("width wildcard |%*d|\n", 5, 43); - printf("width wildcard |%*d|\n", 5, 43); - ft_printf("precision wildcard |%.*d|\n", 5, 43); - printf("precision wildcard |%.*d|\n", 5, 43); - ft_printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); - printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); - ft_printf("left adjusted |%*d|\n", -5, 43); - printf("left adjusted |%*d|\n", -5, 43); + /* int test = 42; */ + /* ft_printf("bonjour\n"); */ + /* ft_printf("char: %c\n", 'r'); */ + /* ft_printf("string: %s\n", "bonjour"); */ + /* ft_printf("pointer: %p\n", &test); */ + /* ft_printf("int: %d or %i\n", 45, 54); */ + /* ft_printf("uint: %u\n", 1 << 31); */ + /* ft_printf("hex lower: %x\n", 0xabcf012); */ + /* ft_printf("hex upper: %X\n", 0xabcf012); */ + /* ft_printf("percent: %%\n"); */ + /* ft_printf("multiple stuff: %d %u %d %x %d\n", 1, -2, 3, 0xa, 5); */ + /* */ + /* ft_printf("precision |%.9d|\n", 43); */ + /* printf("precision |%.9d|\n", 43); */ + /* ft_printf("string precision |%.9s|\n", "jesuisbonjourbonjour"); */ + /* printf("string precision |%.9s|\n", "jesuisbonjourbonjour"); */ + /* ft_printf("min width |%9d|\n", 43); */ + /* printf("min width |%9d|\n", 43); */ + /* ft_printf("zero padding |%09d|\n", 43); */ + /* printf("zero padding |%09d|\n", 43); */ + /* ft_printf("left adjusted |%-9d|\n", 43); */ + /* printf("left adjusted |%-9d|\n", 43); */ + /* ft_printf("string padding |%9s|\n", "bon"); */ + /* printf("string padding |%9s|\n", "bon"); */ + /* */ + /* ft_printf("width wildcard |%*d|\n", 5, 43); */ + /* printf("width wildcard |%*d|\n", 5, 43); */ + /* ft_printf("precision wildcard |%.*d|\n", 5, 43); */ + /* printf("precision wildcard |%.*d|\n", 5, 43); */ + /* ft_printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); */ + /* printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); */ + /* ft_printf("left adjusted |%*d|\n", -5, 43); */ + /* printf("left adjusted |%*d|\n", -5, 43); */ /* ft_printf("overwrite |%*3d|\n", 5, 43); */ /* printf("overwrite |%*3d|\n", 5, 43); */ /* ft_printf("overwrite neg |%*-1d|\n", 0, 43); */ /* printf("overwrite neg |%*-1d|\n", 0, 43); */ - ft_printf("pointer field width |%15p|\n", &test); - printf("pointer field width |%15p|\n", &test); + /* ft_printf("pointer field width |%15p|\n", &test); */ + /* printf("pointer field width |%15p|\n", &test); */ - ft_printf("bonjour%n", &test); - printf("%d\n", test); + /* ft_printf("bonjour%n", &test); */ + /* printf("%d\n", test); */ + ft_printf("%.0p, %.p", 0, 0); /* ft_printf("%f\n", 0.14159); */ /* printf("%f\n", 0.14159); */ diff --git a/printer.c b/printer.c deleted file mode 100644 index 0eab18b..0000000 --- a/printer.c +++ /dev/null @@ -1,115 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* printer.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */ -/* Updated: 2019/11/13 09:28:35 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include "libft.h" -#include "header.h" - -char *convert(t_pformat *pformat, va_list ap) -{ - char *str; - - if (pformat->flags & FLAG_WIDTH_WILDCARD) - { - if (pformat->flags & FLAG_WIDTH_OVERWRITE) - (void)va_arg(ap, int); - else - pformat->width = va_arg(ap, int); - if (pformat->width < 0) - { - pformat->flags |= FLAG_MINUS; - pformat->width *= -1; - } - } - if (pformat->flags & FLAG_PRECISION_WILDCARD) - pformat->precision = va_arg(ap, int); - if ((str = convert_specifier(ap, pformat)) == NULL) - return (NULL); - return (str); -} - -char *convert_specifier(va_list ap, t_pformat *pformat) -{ - if (pformat->specifier == 'c') - return (convert_char(ap, pformat)); - if (pformat->specifier == 's') - return (convert_str(ap, pformat)); - if (pformat->specifier == 'p') - return (convert_ptr(ap, pformat)); - if (pformat->specifier == 'd' || pformat->specifier == 'i') - return (convert_int(ap, pformat)); - if (pformat->specifier == 'u') - return (convert_uint(ap, pformat)); - if (pformat->specifier == 'x') - return (convert_hex(ap, pformat)); - if (pformat->specifier == 'X') - return (convert_hex(ap, pformat)); - if (pformat->specifier == '%') - return (convert_percent(ap, pformat)); - if (pformat->specifier == 'n') - return (convert_written(ap, pformat)); - else - return (convert_none(ap, pformat)); - return (NULL); -} - -char *handle_width(t_pformat *pformat, char *str) -{ - char *tmp; - int len; - int i; - - if ((len = ft_strlen(str)) >= pformat->width) - return (str); - if ((tmp = (char*)malloc(sizeof(char) * (pformat->width + 1))) == NULL) - return (NULL); - if (pformat->flags & FLAG_MINUS) - { - i = len; - ft_strcpy(tmp, str); - while (i < pformat->width) - tmp[i++] = ' '; - tmp[i] = 0; - } - else - { - i = 0; - while (i <= pformat->width - len) - tmp[i++] = pformat->flags & FLAG_ZERO ? '0' : ' '; - ft_strcpy(tmp + i - 1, str); - } - free(str); - return (tmp); -} - -char *handle_precision(t_pformat *pformat, char *str) -{ - int len; - char *tmp; - - if (ft_strchr("diuxX", pformat->specifier) && pformat->precision >= 0) - pformat->flags &= ~FLAG_ZERO; - len = ft_strlen(str); - if (pformat->precision == 0 && str[0] == '0') - return (ft_strdup("")); - if (!ft_strchr("diuxXp", pformat->specifier) || len >= pformat->precision) - return (str); - if ((tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1))) == NULL) - return (NULL); - ft_strcpy(tmp + pformat->precision - len, str); - while (pformat->precision-- > len) - tmp[pformat->precision - len] = '0'; - free(str); - return (tmp); -} diff --git a/test b/test deleted file mode 100755 index 473cbb5..0000000 Binary files a/test and /dev/null differ -- cgit