diff options
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | convert_char.c | 30 | ||||
| -rw-r--r-- | convert_hex.c (renamed from convert_hex_up.c) | 9 | ||||
| -rw-r--r-- | convert_int.c | 8 | ||||
| -rw-r--r-- | ft_printf.c | 49 | ||||
| -rw-r--r-- | header.h | 13 | ||||
| -rw-r--r-- | main.c | 5 | ||||
| -rw-r--r-- | parse.c | 10 | ||||
| -rw-r--r-- | printer.c | 37 | ||||
| -rwxr-xr-x | test | bin | 22988 -> 27460 bytes | |||
| -rw-r--r-- | utils.c | 15 |
11 files changed, 121 insertions, 59 deletions
@@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/28 17:41:14 by cacharle #+# #+# # -# Updated: 2019/10/29 23:00:05 by cacharle ### ########.fr # +# Updated: 2019/10/30 15:58:32 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -24,7 +24,7 @@ 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_low.c convert_hex_up.c convert_percent.c + convert_ptr.c convert_hex.c convert_percent.c OBJ = $(SRC:.c=.o) INCLUDE = header.h diff --git a/convert_char.c b/convert_char.c index ee30a2c..d6c3422 100644 --- a/convert_char.c +++ b/convert_char.c @@ -1,4 +1,5 @@ #include <stdarg.h> +#include <stdlib.h> #include "header.h" char *convert_char(va_list ap, t_pformat *pformat) @@ -8,6 +9,33 @@ char *convert_char(va_list ap, t_pformat *pformat) return (NULL); str[0] = va_arg(ap, int); str[1] = '\0'; - str = handle_padding(pformat, str); + + /* str = handle_padding(pformat, str); */ + pformat->size = 1;//ft_strlen(str); + /* return (str); */ + char *tmp; + int i; + if (1 >= pformat->min_width) + return (str); + if ((tmp = (char*)malloc(sizeof(char) * (pformat->min_width + 1))) == NULL) + return (NULL); + if (pformat->flags & FLAG_LEFT_ADJUSTED) + { + i = 1; + ft_memcpy(tmp, str, 2); + while (i < pformat->min_width) + tmp[i++] = ' '; + tmp[i] = 0; + } + else + { + i = 0; + while (i <= pformat->min_width - 1) + tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' '; + ft_memcpy(tmp + i - 1, str, 2); + } + free(str); + pformat->size = pformat->min_width; + return (tmp); return (str); } diff --git a/convert_hex_up.c b/convert_hex.c index eba1226..457b249 100644 --- a/convert_hex_up.c +++ b/convert_hex.c @@ -2,7 +2,7 @@ #include <stdlib.h> #include "header.h" -char *convert_hex_up(va_list ap, t_pformat *pformat) +char *convert_hex(va_list ap, t_pformat *pformat) { long long unsigned int n; @@ -17,7 +17,10 @@ char *convert_hex_up(va_list ap, t_pformat *pformat) else n = va_arg(ap, unsigned int); - char *str = ITOA_HEX_UP(n); + char *str; + str = ITOA_HEX_LOW(n); + if (pformat->type == 'X') + ft_strtoupper(str); str = handle_precision(pformat, str); if (pformat->flags & FLAG_ZERO_PADDING) { @@ -27,7 +30,7 @@ char *convert_hex_up(va_list ap, t_pformat *pformat) } if (pformat->flags & FLAG_ALTERNATE && n != 0) { - char *tmp = ft_strjoin("0X", str); + char *tmp = ft_strjoin(pformat->type == 'X' ? "0X" : "0x", str); free(str); str = tmp; } diff --git a/convert_int.c b/convert_int.c index 8bc0b1a..ebc360a 100644 --- a/convert_int.c +++ b/convert_int.c @@ -26,14 +26,18 @@ char *convert_int(va_list ap, t_pformat *pformat) str = handle_precision(pformat, str); if (pformat->flags & FLAG_ZERO_PADDING) { - if (is_neg || pformat->flags & FLAG_SIGNED || pformat->flags & FLAG_SPACE) + if (is_neg || pformat->flags & (FLAG_SIGNED | FLAG_SPACE)) pformat->min_width--; /* pformat->min_width--; */ str = handle_padding(pformat, str); } + /* str[ft_strlen(str)] = 0; */ if (is_neg) + { tmp = ft_strjoin("-", str); - else if (pformat->flags & FLAG_SIGNED || pformat->flags & FLAG_SPACE) + /* printf("%s\n", str); */ + } + else if (pformat->flags & (FLAG_SIGNED | FLAG_SPACE)) tmp = ft_strjoin(pformat->flags & FLAG_SPACE ? " " : "+", str); else tmp = ft_strdup(str); diff --git a/ft_printf.c b/ft_printf.c index c51cc22..cf5e020 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:15:28 by cacharle #+# #+# */ -/* Updated: 2019/10/30 02:01:25 by cacharle ### ########.fr */ +/* Updated: 2019/10/30 17:10:32 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int ft_printf(const char *format, ...) { va_list ap; - t_flist *format_list; + t_flist *flist; char *str; int print_len; int i; @@ -27,7 +27,7 @@ int ft_printf(const char *format, ...) if (format == NULL) return (-1); str = ft_strdup(format); - if (!parse(str, &format_list)) + if (!parse(str, &flist)) return (-1); free(str); va_start(ap, format); @@ -40,19 +40,46 @@ int ft_printf(const char *format, ...) write(STDOUT_FILENO, format + i, 1); continue ; } - str = convert(format_list->content, ap); + str = convert(flist->content, ap); if (str == NULL) { - list_destroy(&format_list); + list_destroy(&flist); return (-1); } - ft_putstr(str); - print_len += ft_strlen(str); - free(str); - i += format_list->content->len; - list_pop_front(&format_list); + if (flist->content->type == 'c') + { + write(1, str, flist->content->size); + /* printf("\n%d\n", flist->content->size); */ + print_len++; + free(str); + } + else + { + ft_putstr(str); + print_len += ft_strlen(str); + free(str); + } + i += flist->content->fmt_len; + list_pop_front(&flist); } - list_destroy(&format_list); + list_destroy(&flist); va_end(ap); return (print_len + i); } + +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,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:06:46 by cacharle #+# #+# */ -/* Updated: 2019/10/30 04:14:35 by cacharle ### ########.fr */ +/* Updated: 2019/10/30 16:59:55 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,8 +51,8 @@ # define FLAG_LONG_LONG (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")) +#define ITOA_HEX_UP(x) (ft_itoa_unsigned_base(x, "0123456789ABCDEF")) +#define ITOA_DEC(x) (ft_itoa_base(x, "0123456789")) #include <stdio.h> @@ -65,7 +65,8 @@ typedef struct int min_width; t_flags flags; char type; - int len; + int fmt_len; + int size; } t_pformat; typedef struct s_flist @@ -110,6 +111,7 @@ char *add_hex_prefix(char *str); 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); +char *ft_strtoupper(char *str); /* ** extract.c @@ -140,8 +142,7 @@ char *convert_str(va_list ap, t_pformat *pformat); char *convert_ptr(va_list ap, t_pformat *pformat); char *convert_int(va_list ap, t_pformat *pformat); char *convert_uint(va_list ap, t_pformat *pformat); -char *convert_hex_low(va_list ap, t_pformat *pformat); -char *convert_hex_up(va_list ap, t_pformat *pformat); +char *convert_hex(va_list ap, t_pformat *pformat); char *convert_percent(va_list ap, t_pformat *pformat); #endif @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/28 04:25:09 by cacharle #+# #+# */ -/* Updated: 2019/10/30 03:15:22 by cacharle ### ########.fr */ +/* Updated: 2019/10/30 17:35:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,7 @@ int main() { int test; - printf("%d\n", ft_printf("%", 4)); - printf("%d\n", printf("aa%bb", 4)); + ft_printf("%.37ld", -22337203685477l); /* ft_printf("char: %c\n", 'r'); */ /* ft_printf("string: %s\n", "bonjour"); */ /* ft_printf("pointer: %p\n", &test); */ @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:11:33 by cacharle #+# #+# */ -/* Updated: 2019/10/30 04:13:56 by cacharle ### ########.fr */ +/* Updated: 2019/10/30 17:02:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ int parse(char *format, t_flist **flist) if ((tmp = list_new(parsed)) == NULL) return ((int)list_destroy(flist)); list_push_front(flist, tmp); - format += (*flist)->content->len; + format += (*flist)->content->fmt_len; } *flist = list_reverse(*flist); return (1); @@ -58,9 +58,9 @@ t_pformat *parse_reduced(char *fmt) pformat->precision = -1; pformat->min_width = -1; pformat->flags = 0; - pformat->len = ft_strlen(fmt); - pformat->type = fmt[pformat->len - 1]; - fmt[pformat->len - 1] = '\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); fmt = extract_precision(pformat, fmt); @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */ -/* Updated: 2019/10/30 04:13:18 by cacharle ### ########.fr */ +/* Updated: 2019/10/30 18:04:06 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,9 +52,9 @@ char *convert_type(va_list ap, t_pformat *pformat) if (pformat->type == 'u') return (convert_uint(ap, pformat)); if (pformat->type == 'x') - return (convert_hex_low(ap, pformat)); + return (convert_hex(ap, pformat)); if (pformat->type == 'X') - return (convert_hex_up(ap, pformat)); + return (convert_hex(ap, pformat)); if (pformat->type == '%') return (convert_percent(ap, pformat)); return (NULL); @@ -116,26 +116,13 @@ char *handle_precision(t_pformat *pformat, char *str) len = ft_strlen(str); if (pformat->precision == 0 && str[0] == '0') return (ft_strdup("")); - else if (IN_STR("diuxXp", pformat->type) && len < pformat->precision) - { - if ((tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1))) == NULL) - return (NULL); - if (IN_STR("+-", str[0])) - { - tmp[0] = str[0]; - len--; - ft_strcpy(tmp + pformat->precision - len + 1, str + 1); - while (pformat->precision-- > len) - tmp[pformat->precision - len + 1] = '0'; - ft_strcpy(str, tmp); - free(tmp); - return (str); - } - ft_strcpy(tmp + pformat->precision - len, str); - while (pformat->precision-- > len) - tmp[pformat->precision - len] = '0'; - ft_strcpy(str, tmp); - free(tmp); - } - return (str); + if (!IN_STR("diuxXp", pformat->type) || 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); } Binary files differ@@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:12:40 by cacharle #+# #+# */ -/* Updated: 2019/10/30 04:06:52 by cacharle ### ########.fr */ +/* Updated: 2019/10/30 18:04:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,7 @@ char *ft_itoa_base(long long int n, char *base) radix = ft_strlen(base); len = nbrlen_radix(n, radix); + /* printf("%d\n", len); */ if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL) return (NULL); str[len] = '\0'; @@ -69,9 +70,11 @@ char *ft_itoa_base(long long int n, char *base) str[0] = '-'; while (--len >= (n < 0 ? 1 : 0)) { + /* printf("u > %ld\n", u_nbr); */ str[len] = base[u_nbr % radix]; u_nbr /= radix; } + /* printf("%s\n", str); */ return (str); } @@ -108,3 +111,13 @@ char *ft_itoa_unsigned_base(long long unsigned int n, char *base) } return (str); } + +char *ft_strtoupper(char *str) +{ + int i; + + i = -1; + while (str[++i]) + str[i] = ft_toupper(str[i]); + return (str); +} |
