diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-11-06 00:02:56 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-11-06 00:02:56 +0100 |
| commit | dafee6410a4ecd7400a83adf84ded3621f30a365 (patch) | |
| tree | 401c4169f4c2bf849d33a6a0201ab47d0f0f7782 | |
| parent | e6d8f543af4c1a45f30495b90a3912b1c75f2be7 (diff) | |
| download | ft_printf-dafee6410a4ecd7400a83adf84ded3621f30a365.tar.gz ft_printf-dafee6410a4ecd7400a83adf84ded3621f30a365.tar.bz2 ft_printf-dafee6410a4ecd7400a83adf84ded3621f30a365.zip | |
Refactored parsing to handle alone '%'
- renamed a few variables
- added join_free* to libft
- normed everything but ft_printf.c
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | convert_char.c | 23 | ||||
| -rw-r--r-- | convert_hex.c | 37 | ||||
| -rw-r--r-- | convert_int.c | 35 | ||||
| -rw-r--r-- | convert_none.c | 25 | ||||
| -rw-r--r-- | convert_percent.c | 6 | ||||
| -rw-r--r-- | convert_ptr.c | 28 | ||||
| -rw-r--r-- | convert_str.c | 6 | ||||
| -rw-r--r-- | convert_uint.c | 5 | ||||
| -rw-r--r-- | convert_written.c | 12 | ||||
| -rw-r--r-- | extract.c | 25 | ||||
| -rw-r--r-- | ft_printf.c | 29 | ||||
| -rw-r--r-- | header.h | 67 | ||||
| -rw-r--r-- | length_modifier.c | 39 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | list.c | 3 | ||||
| -rw-r--r-- | main.c | 20 | ||||
| -rw-r--r-- | parse.c | 37 | ||||
| -rw-r--r-- | printer.c | 58 | ||||
| -rwxr-xr-x | test | bin | 27868 -> 23228 bytes | |||
| -rw-r--r-- | utils.c | 20 |
21 files changed, 227 insertions, 255 deletions
@@ -6,14 +6,14 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/28 17:41:14 by cacharle #+# #+# # -# Updated: 2019/10/30 23:58:39 by cacharle ### ########.fr # +# Updated: 2019/11/06 00:00:19 by cacharle ### ########.fr # # # # **************************************************************************** # LIBFT_ROOT = ./libft CC = gcc -CCFLAGS = -Wall -Wextra -g #-Werror +CCFLAGS = -Wall -Wextra #-Werror LDFLAGS = -L. -lftprintf INCFLAGS = -I$(LIBFT_ROOT) @@ -24,7 +24,8 @@ MAKE = make -j4 NAME = libftprintf.a SRC = ft_printf.c utils.c printer.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_ptr.c convert_hex.c convert_percent.c convert_written.c \ + convert_none.c length_modifier.c OBJ = $(SRC:.c=.o) INCLUDE = header.h diff --git a/convert_char.c b/convert_char.c index c838974..8e51804 100644 --- a/convert_char.c +++ b/convert_char.c @@ -6,41 +6,38 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/30 23:22:29 by cacharle #+# #+# */ -/* Updated: 2019/11/03 20:13:44 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:44:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdarg.h> -#include <stdlib.h> -#include "libft.h" #include "header.h" -static char *handle_padding_char(t_pformat *pformat, char *str) +static char *handle_width_char(t_pformat *pformat, char *str) { char *tmp; int i; pformat->size = 1; - if (1 >= pformat->min_width) + if (1 >= pformat->width) return (str); - if ((tmp = (char*)malloc(sizeof(char) * (pformat->min_width + 1))) == NULL) + if ((tmp = (char*)malloc(sizeof(char) * (pformat->width + 1))) == NULL) return (NULL); - if (pformat->flags & FLAG_LEFT_ADJUSTED) + if (pformat->flags & FLAG_MINUS) { ft_memcpy(tmp, str, (i = 1) + 1); - while (i < pformat->min_width) + while (i < pformat->width) tmp[i++] = ' '; tmp[i] = 0; } else { i = 0; - while (i <= pformat->min_width - 1) - tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' '; + while (i <= pformat->width - 1) + tmp[i++] = pformat->flags & FLAG_ZERO ? '0' : ' '; ft_memcpy(tmp + i - 1, str, 2); } free(str); - pformat->size = pformat->min_width; + pformat->size = pformat->width; return (tmp); } @@ -52,5 +49,5 @@ char *convert_char(va_list ap, t_pformat *pformat) return (NULL); str[0] = va_arg(ap, int); str[1] = '\0'; - return (handle_padding_char(pformat, str)); + return (handle_width_char(pformat, str)); } diff --git a/convert_hex.c b/convert_hex.c index 9d3ee00..b59a4a1 100644 --- a/convert_hex.c +++ b/convert_hex.c @@ -6,48 +6,29 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/30 23:23:06 by cacharle #+# #+# */ -/* Updated: 2019/11/04 19:23:28 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:58:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdarg.h> -#include <stdlib.h> -#include "libft.h" #include "header.h" char *convert_hex(va_list ap, t_pformat *pformat) { char *str; - char *tmp; long long unsigned int n; - if (pformat->flags & FLAG_SHORT) - n = (unsigned short)va_arg(ap, int); - else if (pformat->flags & FLAG_SHORT_SHORT) - n = (unsigned char)va_arg(ap, int); - else if (pformat->flags & FLAG_LONG) - n = va_arg(ap, long unsigned int); - else if (pformat->flags & FLAG_LONG_LONG) - n = va_arg(ap, long long unsigned int); - else - n = va_arg(ap, unsigned int); - str = ITOA_HEX_LOW(n); - if (pformat->type == 'X') - ft_strtoupper(str); + n = length_modifier_unsigned_int(ap, pformat); + str = pformat->specifier == 'x' ? ITOA_HEX_LOW(n) : ITOA_HEX_UP(n); str = handle_precision(pformat, str); - if (pformat->flags & FLAG_ZERO_PADDING) + if (pformat->flags & FLAG_ZERO) { if (pformat->flags & FLAG_ALTERNATE && n != 0) - pformat->min_width -= 2; - str = handle_padding(pformat, str); + pformat->width -= 2; + str = handle_width(pformat, str); } if (pformat->flags & FLAG_ALTERNATE && n != 0) - { - tmp = ft_strjoin(pformat->type == 'X' ? "0X" : "0x", str); - free(str); - str = tmp; - } - if (!(pformat->flags & FLAG_ZERO_PADDING)) - str = handle_padding(pformat, str); + str = ft_strjoin_free_snd(pformat->specifier == 'X' ? "0X" : "0x", str); + if (!(pformat->flags & FLAG_ZERO)) + str = handle_width(pformat, str); return (str); } diff --git a/convert_int.c b/convert_int.c index 0f4d7d1..f995ced 100644 --- a/convert_int.c +++ b/convert_int.c @@ -6,52 +6,35 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/30 23:29:53 by cacharle #+# #+# */ -/* Updated: 2019/11/04 01:26:38 by cacharle ### ########.fr */ +/* Updated: 2019/11/06 00:00:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> -#include <stdarg.h> -#include "libft.h" #include "header.h" char *convert_int(va_list ap, t_pformat *pformat) { - char *tmp; int is_neg; long long int n; char *str; - if (pformat->flags & FLAG_SHORT) - n = (short)va_arg(ap, int); - else if (pformat->flags & FLAG_SHORT_SHORT) - n = (signed char)va_arg(ap, int); - else if (pformat->flags & FLAG_LONG) - n = va_arg(ap, long int); - else if (pformat->flags & FLAG_LONG_LONG) - n = va_arg(ap, long long int); - else - n = va_arg(ap, int); + n = length_modifier_int(ap, pformat); is_neg = n < 0; str = ITOA_DEC(n); if (is_neg) ft_strcpy(str, str + 1); str = handle_precision(pformat, str); - if (pformat->flags & FLAG_ZERO_PADDING) + if (pformat->flags & FLAG_ZERO) { if (is_neg || pformat->flags & (FLAG_SIGNED | FLAG_SPACE)) - pformat->min_width--; - str = handle_padding(pformat, str); + pformat->width--; + str = handle_width(pformat, str); } if (is_neg) - tmp = ft_strjoin("-", str); + str = ft_strjoin_free_snd("-", str); else if (pformat->flags & (FLAG_SIGNED | FLAG_SPACE)) - tmp = ft_strjoin(pformat->flags & FLAG_SPACE ? " " : "+", str); - else - tmp = ft_strdup(str); - free(str); - str = tmp; - if (!(pformat->flags & FLAG_ZERO_PADDING)) - str = handle_padding(pformat, str); + str = ft_strjoin_free_snd(pformat->flags & FLAG_SPACE ? " " : "+", str); + if (!(pformat->flags & FLAG_ZERO)) + str = handle_width(pformat, str); return (str); } diff --git a/convert_none.c b/convert_none.c new file mode 100644 index 0000000..769592c --- /dev/null +++ b/convert_none.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* convert_none.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/04 19:30:25 by cacharle #+# #+# */ +/* Updated: 2019/11/05 23:44:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "header.h" + +char *convert_none(va_list ap, t_pformat *pformat) +{ + char *str; + + (void)ap; + if ((str = ft_strdup("")) == NULL) + return (NULL); + str = handle_precision(pformat, str); + str = handle_width(pformat, str); + return (str); +} diff --git a/convert_percent.c b/convert_percent.c index 0833748..492fa42 100644 --- a/convert_percent.c +++ b/convert_percent.c @@ -6,12 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/30 23:23:27 by cacharle #+# #+# */ -/* Updated: 2019/10/30 23:23:56 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:44:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdarg.h> -#include "libft.h" #include "header.h" char *convert_percent(va_list ap, t_pformat *pformat) @@ -20,6 +18,6 @@ char *convert_percent(va_list ap, t_pformat *pformat) (void)ap; str = ft_strdup("%"); - str = handle_padding(pformat, str); + str = handle_width(pformat, str); return (str); } diff --git a/convert_ptr.c b/convert_ptr.c index 8c0797a..b07f3c6 100644 --- a/convert_ptr.c +++ b/convert_ptr.c @@ -6,12 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/30 23:24:08 by cacharle #+# #+# */ -/* Updated: 2019/11/04 01:41:49 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:43:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdarg.h> -#include <stdlib.h> #include "header.h" char *convert_ptr(va_list ap, t_pformat *pformat) @@ -20,22 +18,12 @@ char *convert_ptr(va_list ap, t_pformat *pformat) str = ITOA_HEX_LOW((long long unsigned int)va_arg(ap, void*)); str = handle_precision(pformat, str); - if (!(pformat->flags & FLAG_ZERO_PADDING)) - str = add_hex_prefix(str); - if (pformat->flags & FLAG_ZERO_PADDING) - pformat->min_width -= 2; - str = handle_padding(pformat, str); - if (pformat->flags & FLAG_ZERO_PADDING) - str = add_hex_prefix(str); + if (!(pformat->flags & FLAG_ZERO)) + str = ft_strjoin_free_snd("0x", str); + if (pformat->flags & FLAG_ZERO) + pformat->width -= 2; + str = handle_width(pformat, str); + if (pformat->flags & FLAG_ZERO) + str = ft_strjoin_free_snd("0x", str); return (str); } - -char *add_hex_prefix(char *str) -{ - char *tmp; - - if ((tmp = ft_strjoin("0x", str)) == NULL) - return (NULL); - free(str); - return (tmp); -} diff --git a/convert_str.c b/convert_str.c index e4cf70a..5bba1b1 100644 --- a/convert_str.c +++ b/convert_str.c @@ -6,12 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/30 23:22:25 by cacharle #+# #+# */ -/* Updated: 2019/11/04 01:33:05 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:43:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdarg.h> -#include "libft.h" #include "header.h" char *convert_str(va_list ap, t_pformat *pformat) @@ -22,6 +20,6 @@ char *convert_str(va_list ap, t_pformat *pformat) str = str == NULL ? ft_strdup("(null)") : ft_strdup(str); if (pformat->precision != -1 && pformat->precision < (int)ft_strlen(str)) str[pformat->precision] = '\0'; - str = handle_padding(pformat, str); + str = handle_width(pformat, str); return (str); } diff --git a/convert_uint.c b/convert_uint.c index ddf9773..92f5ed2 100644 --- a/convert_uint.c +++ b/convert_uint.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/30 23:25:40 by cacharle #+# #+# */ -/* Updated: 2019/11/04 19:20:27 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:44:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdarg.h> #include "header.h" char *convert_uint(va_list ap, t_pformat *pformat) @@ -30,6 +29,6 @@ char *convert_uint(va_list ap, t_pformat *pformat) n = va_arg(ap, unsigned int); str = ft_itoa_unsigned_base(n, "0123456789"); str = handle_precision(pformat, str); - str = handle_padding(pformat, str); + str = handle_width(pformat, str); return (str); } diff --git a/convert_written.c b/convert_written.c index 7564d35..06c5963 100644 --- a/convert_written.c +++ b/convert_written.c @@ -6,25 +6,23 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/30 23:38:28 by cacharle #+# #+# */ -/* Updated: 2019/11/04 00:21:16 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:59:24 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdarg.h> #include "header.h" char *convert_written(va_list ap, t_pformat *pformat) { - if (pformat->flags & FLAG_SHORT) - pformat->written = va_arg(ap, signed char*); + pformat->written = (long long int*)va_arg(ap, signed char*); if (pformat->flags & FLAG_SHORT_SHORT) - pformat->written = va_arg(ap, short*); + pformat->written = (long long int*)va_arg(ap, short*); if (pformat->flags & FLAG_LONG) - pformat->written = va_arg(ap, long int*); + pformat->written = (long long int*)va_arg(ap, long int*); if (pformat->flags & FLAG_LONG_LONG) pformat->written = va_arg(ap, long long int*); else - pformat->written = va_arg(ap, int*); + pformat->written = (long long int*)va_arg(ap, int*); return (NULL); } @@ -6,23 +6,22 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:10:36 by cacharle #+# #+# */ -/* Updated: 2019/11/04 01:32:44 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:43:33 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "header.h" -char *extract_standalone_flags(t_pformat *pformat, char *fmt) +char *extract_flags(t_pformat *pformat, char *fmt) { - if (*fmt == 0) + if (*fmt == '\0') return (fmt); - while (IS_STANDALONE_FLAG(*fmt)) + while (ft_strchr(FLAGS_STR, *fmt) != NULL) { if (*fmt == '0') - pformat->flags |= FLAG_ZERO_PADDING; + pformat->flags |= FLAG_ZERO; if (*fmt == '-') - pformat->flags |= FLAG_LEFT_ADJUSTED; + pformat->flags |= FLAG_MINUS; if (*fmt == '+') pformat->flags |= FLAG_SIGNED; if (*fmt == ' ') @@ -38,22 +37,22 @@ char *extract_standalone_flags(t_pformat *pformat, char *fmt) return (fmt); } -char *extract_min_width(t_pformat *pformat, char *fmt) +char *extract_width(t_pformat *pformat, char *fmt) { - if (*fmt == 0) + if (*fmt == '\0') return (fmt); if (*fmt == '*') { - pformat->flags |= FLAG_MIN_WIDTH_WILDCARD; + pformat->flags |= FLAG_WIDTH_WILDCARD; fmt++; } if (!ft_isdigit(*fmt)) return (fmt); - pformat->min_width = ft_atoi(fmt); + pformat->width = ft_atoi(fmt); while (*fmt && ft_isdigit(*fmt)) fmt++; - if (pformat->flags & FLAG_MIN_WIDTH_WILDCARD) - pformat->flags |= FLAG_MIN_WIDTH_OVERWRITE; + if (pformat->flags & FLAG_WIDTH_WILDCARD) + pformat->flags |= FLAG_WIDTH_OVERWRITE; return (fmt); } diff --git a/ft_printf.c b/ft_printf.c index 40bc89d..da9a14a 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,14 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:15:28 by cacharle #+# #+# */ -/* Updated: 2019/11/04 00:28:38 by cacharle ### ########.fr */ +/* Updated: 2019/11/06 00:01:32 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <unistd.h> -#include <stdlib.h> -#include <stdarg.h> -#include "libft.h" #include "header.h" int ft_printf(const char *format, ...) @@ -33,8 +29,6 @@ int ft_printf(const char *format, ...) va_start(ap, format); print_len = 0; i = -1; - /* int *a = NULL; */ - /* printf("%d", *a); */ while (format[++i]) { if (format[i] != '%') @@ -44,7 +38,7 @@ int ft_printf(const char *format, ...) continue ; } str = convert(flist->content, ap); - if (str == NULL && flist->content->type == 'n') + if (str == NULL && flist->content->specifier == 'n') { if (flist->content->written != NULL) *flist->content->written = print_len; @@ -57,7 +51,7 @@ int ft_printf(const char *format, ...) list_destroy(&flist); return (-1); } - if (flist->content->type == 'c') + if (flist->content->specifier == 'c') { write(1, str, flist->content->size); print_len++; @@ -76,20 +70,3 @@ int ft_printf(const char *format, ...) va_end(ap); return (print_len); } - -char *ft_strappend(char *dest, char *src) -{ - void *copy; - - if ((copy = (char*)malloc(sizeof(char) * (ft_strlen(dest) + 1))) == NULL) - return (NULL); - ft_strcpy(copy, dest); - free(dest); - dest = (char*)malloc(sizeof(char) * (ft_strlen(copy) + ft_strlen(src) + 1)); - if (dest == NULL) - return (NULL); - ft_strcpy(dest, copy); - free(copy); - ft_strcat(dest, src); - return (dest); -} @@ -6,58 +6,52 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:06:46 by cacharle #+# #+# */ -/* Updated: 2019/10/31 00:04:28 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:58:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef HEADER_H # define HEADER_H +# include <unistd.h> +# include <stdlib.h> # include <stdarg.h> # include "libft.h" -# define TRUE 1 -# define FALSE 0 +# define SPECIFIERS_STR "ncspdiuxX%" +# define FLAGS_STR "#0- +'" -# define HEX_SYMBOLS "0123456789abcdef" -# define HEX_MAJ_SYMBOLS "0123456789ABCDEF" +# define IS_STANDALONE_FLAG(c) (ft_strchr(FLAGS_STR, c) != NULL) -# define IN_STR(str, c) (ft_strchr(str, c) != NULL) -# define IS_STANDALONE_FLAG(c) (IN_STR("0-+ #'", c)) - -# define CONVERSIONS_STR "ncspdiuxX%" - -# define FLAG_LEFT_ADJUSTED (1 << 0) -# define FLAG_ZERO_PADDING (1 << 1) -# define FLAG_MIN_WIDTH_WILDCARD (1 << 2) -# define FLAG_PRECISION_WILDCARD (1 << 3) -# define FLAG_MIN_WIDTH_OVERWRITE (1 << 4) -# define FLAG_SIGNED (1 << 5) -# define FLAG_SPACE (1 << 6) -# define FLAG_ALTERNATE (1 << 7) -# define FLAG_SHORT_SHORT (1 << 8) -# define FLAG_SHORT (1 << 9) -# define FLAG_LONG (1 << 10) -# define FLAG_LONG_LONG (1 << 11) +# define FLAG_MINUS (1 << 0) +# define FLAG_ZERO (1 << 1) +# define FLAG_SIGNED (1 << 2) +# define FLAG_SPACE (1 << 3) +# define FLAG_ALTERNATE (1 << 4) +# define FLAG_SHORT (1 << 5) +# define FLAG_SHORT_SHORT (1 << 6) +# define FLAG_LONG (1 << 7) +# define FLAG_LONG_LONG (1 << 8) +# define FLAG_WIDTH_WILDCARD (1 << 9) +# define FLAG_PRECISION_WILDCARD (1 << 10) +# define FLAG_WIDTH_OVERWRITE (1 << 11) # define ITOA_HEX_LOW(x) (ft_itoa_unsigned_base(x, "0123456789abcdef")) # define ITOA_HEX_UP(x) (ft_itoa_unsigned_base(x, "0123456789ABCDEF")) # define ITOA_DEC(x) (ft_itoa_base(x, "0123456789")) -#include <stdio.h> - typedef int t_bool; typedef short t_flags; typedef struct { int precision; - int min_width; + int width; t_flags flags; - char type; + char specifier; int fmt_len; int size; - int *written; + long long int *written; } t_pformat; typedef struct s_flist @@ -84,16 +78,14 @@ t_pformat *parse_reduced(char *fmt); */ char *convert(t_pformat *pformat, va_list ap); -char *convert_type(va_list ap, t_pformat *pformat); -char *handle_padding(t_pformat *pformat, char *str); +char *convert_specifier(va_list ap, t_pformat *pformat); +char *handle_width(t_pformat *pformat, char *str); char *handle_precision(t_pformat *pformat, char *str); -char *add_hex_prefix(char *str); /* ** utils.c */ -int strrchr_index(const char *s, char c); char *ft_itoa_base(long long int n, char *base); char *ft_itoa_unsigned_base(long long unsigned int n, char *base); @@ -103,8 +95,8 @@ char *ft_strtoupper(char *str); ** extract.c */ -char *extract_standalone_flags(t_pformat *pformat, char *fmt); -char *extract_min_width(t_pformat *pformat, char *fmt); +char *extract_flags(t_pformat *pformat, char *fmt); +char *extract_width(t_pformat *pformat, char *fmt); char *extract_precision(t_pformat *pformat, char *fmt); char *extract_length_modifier(t_pformat *pformat, char *fmt); @@ -130,5 +122,14 @@ char *convert_uint(va_list ap, t_pformat *pformat); char *convert_hex(va_list ap, t_pformat *pformat); char *convert_percent(va_list ap, t_pformat *pformat); char *convert_written(va_list ap, t_pformat *pformat); +char *convert_none(va_list ap, t_pformat *pformat); + +/* +** length_modifier.c +*/ + +long long unsigned int length_modifier_unsigned_int( + va_list ap, t_pformat *pformat); +long long int length_modifier_int(va_list ap, t_pformat *pformat); #endif diff --git a/length_modifier.c b/length_modifier.c new file mode 100644 index 0000000..f39fde3 --- /dev/null +++ b/length_modifier.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* length_modifier.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/05 23:56:07 by cacharle #+# #+# */ +/* Updated: 2019/11/05 23:58:24 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "header.h" + +long long unsigned int length_modifier_unsigned_int(va_list ap, t_pformat *pformat) +{ + if (pformat->flags & FLAG_SHORT) + return ((unsigned short)va_arg(ap, int)); + else if (pformat->flags & FLAG_SHORT_SHORT) + return ((unsigned char)va_arg(ap, int)); + else if (pformat->flags & FLAG_LONG) + return (va_arg(ap, long unsigned int)); + else if (pformat->flags & FLAG_LONG_LONG) + return (va_arg(ap, long long unsigned int)); + return (va_arg(ap, unsigned int)); +} + +long long int length_modifier_int(va_list ap, t_pformat *pformat) +{ + if (pformat->flags & FLAG_SHORT) + return ((short)va_arg(ap, int)); + else if (pformat->flags & FLAG_SHORT_SHORT) + return ((signed char)va_arg(ap, int)); + else if (pformat->flags & FLAG_LONG) + return (va_arg(ap, long int)); + else if (pformat->flags & FLAG_LONG_LONG) + return (va_arg(ap, long long int)); + return (va_arg(ap, int)); +} diff --git a/libft b/libft -Subproject 70b928b3ee9a1de7769531edd82c0b43cf6d4d8 +Subproject 5bf49994b911abc2b8ff3bdb525cdc23c71f210 @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:14:50 by cacharle #+# #+# */ -/* Updated: 2019/10/29 00:14:51 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:45:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "header.h" t_flist *list_new(t_pformat *content) @@ -6,25 +6,29 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/28 04:25:09 by cacharle #+# #+# */ -/* Updated: 2019/11/04 04:45:36 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:45:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdio.h> -#include "libft.h" #include "header.h" int main() { int test = 42; + /* ft_printf("% 63.108d%14p%03.i%106.19u%*.31s% 84.i%-122.78i%*c%32c%035.d" ,541752262,(void*)9603300624406182979lu,-346423835,2885566767u,-54,"0BG\td~l%",-1237089009,1122784046,-158,-7,66,1034929054); */ + /* printf("% 63.108d%14p%03.i%106.19u%*.31s% 84.i%-122.78i%*c%32c%035.d" ,541752262,(void*)9603300624406182979lu,-346423835,2885566767u,-54,"0BG\td~l%",-1237089009,1122784046,-158,-7,66,1034929054); */ + /* printf("{%*3d}", 5, 0); */ + /* printf("{%*c}\n", -15, 0); */ + /* ft_printf("{%*c}\n", -15, 0); */ /* ft_printf("%-72.170%%183.84x%174.189x%0+0'191.159s%107.*i%0#0+ 12.162c%#.38s%+'# 197.161i%'+#138.189s%#-0+'45.16X%' #+.71X%#-058.*d%0 +#137.30x%+16.145u%'23.72u" ,800115413,3199362515,"l$l%H6",-84514175,10,"fF(@Ju",795959316,";",3064849260,1535631011,-706426049,3490703638,3859198544,2268674351); */ /* printf("%#26.49p%'*.96c%-+-+ 37.180%%0#141.85i% 146.152X%91.199x%58.77i%0+'+'.140p%#67.141i%- 151.136d%101.187%%#*.56X%'+ 116.169x%69.74X%#0 #7.176%" ,16318453712790348322lu,46,814074579,1778514580,2425607191,1566569696,9615815307080556729lu,935162517,-447393454,385385973,3774990313,589711275); */ /* ft_printf("%'+-+ 115.57d%#+0#115.33p%0--198.37x%77.42s%#+0+118.*X% 62.25i%0+#0#21.164i%+''157.91x%0 -0179.107c%++ 70.100i%126.119d%118.182p%175.10i%''+-28.c%#167.153X" ,-812555527,8065328576609463455lu,2102650417,"Pw\tOVtwS",2307405425,1290938377,-351008211,3819076396,-51,-101089464,267730313,5298290595841494834lu,-911149367,-70,485455641); */ /* printf("%#+0#115.33p\n", 54); */ /* ft_printf("%#+0#115.33p\n", 54); */ - ft_printf("%"); /* ft_printf("%"); */ + /* printf("%"); */ /* ft_printf("32 This is an int : %0d\n\n", 0); */ /* printf("32 This is an int : %0d\n\n", 0); */ @@ -40,6 +44,16 @@ int main() /* ft_printf("%hi\n", 32768); */ /* printf("%hi\n", 32768); */ /* ft_printf("%.37ld", -22337203685477l); */ + + + ft_printf("[%]\n"); + printf("[%]\n"); + ft_printf("[%5]\n"); + printf("[%5]\n"); + ft_printf("[%05]\n"); + printf("[%05]\n"); + + /* ft_printf("char: %c\n", 'r'); */ /* ft_printf("string: %s\n", "bonjour"); */ /* ft_printf("pointer: %p\n", &test); */ @@ -6,19 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:11:33 by cacharle #+# #+# */ -/* Updated: 2019/10/31 00:08:04 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:46:02 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "header.h" -#define STRRCHR_CONVERSIONS(c) (ft_strrchr(CONVERSIONS_STR, c)) - -/* -** %(?:\d+\$)?[-]?(?:[0]|'.{1})?-?\d*(?:\.\d+)?[cdusxX] -*/ - int parse(char *format, t_flist **flist) { t_flist *tmp; @@ -44,27 +37,25 @@ int parse(char *format, t_flist **flist) t_pformat *parse_reduced(char *fmt) { t_pformat *pformat; - int i; char *start; - i = 0; - while (strrchr_index(CONVERSIONS_STR, fmt[i]) == -1) - i++; - if ((start = ft_strndup(fmt, i + 1)) == NULL) - return (NULL); if ((pformat = (t_pformat*)malloc(sizeof(t_pformat))) == NULL) return (NULL); - fmt = start; pformat->precision = -1; - pformat->min_width = -1; + pformat->width = -1; pformat->flags = 0; - pformat->fmt_len = ft_strlen(fmt); - pformat->type = fmt[pformat->fmt_len - 1]; - fmt[pformat->fmt_len - 1] = '\0'; - fmt = extract_standalone_flags(pformat, fmt); - fmt = extract_min_width(pformat, fmt); + start = fmt; + fmt = extract_flags(pformat, fmt); + fmt = extract_width(pformat, fmt); fmt = extract_precision(pformat, fmt); - extract_length_modifier(pformat, fmt); - free(start); + fmt = extract_length_modifier(pformat, fmt); + pformat->fmt_len = fmt - start + 1; + if (*fmt == '\0' || ft_strchr(SPECIFIERS_STR, *fmt) == NULL) + { + pformat->fmt_len--; + pformat->specifier = '_'; + } + else + pformat->specifier = *ft_strchr(SPECIFIERS_STR, *fmt); return (pformat); } @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */ -/* Updated: 2019/11/04 01:31:49 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:40:12 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,71 +20,73 @@ char *convert(t_pformat *pformat, va_list ap) { char *str; - if (pformat->flags & FLAG_MIN_WIDTH_WILDCARD) + if (pformat->flags & FLAG_WIDTH_WILDCARD) { - if (pformat->flags & FLAG_MIN_WIDTH_OVERWRITE) + if (pformat->flags & FLAG_WIDTH_OVERWRITE) (void)va_arg(ap, int); else - pformat->min_width = va_arg(ap, int); - if (pformat->min_width < 0) + pformat->width = va_arg(ap, int); + if (pformat->width < 0) { - pformat->flags |= FLAG_LEFT_ADJUSTED; - pformat->min_width *= -1; + pformat->flags |= FLAG_MINUS; + pformat->width *= -1; } } if (pformat->flags & FLAG_PRECISION_WILDCARD) pformat->precision = va_arg(ap, int); - if ((str = convert_type(ap, pformat)) == NULL) + if ((str = convert_specifier(ap, pformat)) == NULL) return (NULL); return (str); } -char *convert_type(va_list ap, t_pformat *pformat) +char *convert_specifier(va_list ap, t_pformat *pformat) { - if (pformat->type == 'c') + if (pformat->specifier == 'c') return (convert_char(ap, pformat)); - if (pformat->type == 's') + else if (pformat->specifier == 's') return (convert_str(ap, pformat)); - if (pformat->type == 'p') + else if (pformat->specifier == 'p') return (convert_ptr(ap, pformat)); - if (pformat->type == 'd' || pformat->type == 'i') + else if (pformat->specifier == 'd' || pformat->specifier == 'i') return (convert_int(ap, pformat)); - if (pformat->type == 'u') + else if (pformat->specifier == 'u') return (convert_uint(ap, pformat)); - if (pformat->type == 'x') + else if (pformat->specifier == 'x') return (convert_hex(ap, pformat)); - if (pformat->type == 'X') + else if (pformat->specifier == 'X') return (convert_hex(ap, pformat)); - if (pformat->type == '%') + else if (pformat->specifier == '%') return (convert_percent(ap, pformat)); - if (pformat->type == 'n') + else if (pformat->specifier == 'n') return (convert_written(ap, pformat)); + else + return (convert_none(ap, pformat)); return (NULL); } -char *handle_padding(t_pformat *pformat, char *str) +char *handle_width(t_pformat *pformat, char *str) { char *tmp; int len; int i; - if ((len = ft_strlen(str)) >= pformat->min_width) + if ((len = ft_strlen(str)) >= pformat->width) return (str); - if ((tmp = (char*)malloc(sizeof(char) * (pformat->min_width + 1))) == NULL) + if ((tmp = (char*)malloc(sizeof(char) * (pformat->width + 1))) == NULL) return (NULL); - if (pformat->flags & FLAG_LEFT_ADJUSTED) + if (pformat->flags & FLAG_MINUS) { i = len; ft_strcpy(tmp, str); - while (i < pformat->min_width) + while (i < pformat->width) tmp[i++] = ' '; tmp[i] = 0; } else { i = 0; - while (i <= pformat->min_width - len) - tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' '; + while (i <= pformat->width - len) + tmp[i++] = pformat->flags & FLAG_ZERO ? '0' : ' '; ft_strcpy(tmp + i - 1, str); } free(str); @@ -96,12 +98,12 @@ char *handle_precision(t_pformat *pformat, char *str) int len; char *tmp; - if (ft_strchr("diuxX", pformat->type) && pformat->precision > 0) - pformat->flags &= ~FLAG_ZERO_PADDING; + 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 (!IN_STR("diuxXp", pformat->type) || len >= pformat->precision) + if (!ft_strchr("diuxXp", pformat->specifier) || len >= pformat->precision) return (str); if ((tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1))) == NULL) return (NULL); Binary files differ@@ -6,30 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:12:40 by cacharle #+# #+# */ -/* Updated: 2019/10/30 23:15:27 by cacharle ### ########.fr */ +/* Updated: 2019/11/05 23:46:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "header.h" -#define MIN_INT (1 << 31) -#define MAX_INT (~(1 << 31)) - -int strrchr_index(const char *s, char c) -{ - int i; - - i = ft_strlen((char*)s) - 1; - while (s[i] != c) - { - if (i == 0) - return (-1); - i--; - } - return (i); -} - static int nbrlen_radix(long long int nbr, int radix) { int counter; |
