From 87bce91050b56915dcf5964f6f66d5f47299e7f3 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 29 Oct 2019 01:57:41 +0100 Subject: Reworking, binary flags, extract advance - Binary flags attribute instead of multiple bool - Extract doesnst strdup fmt each time, just advance the current pointer - In parsing push front then reverse - Moved some functions to libf --- .gitignore | 2 ++ Makefile | 17 ++++++++++-- extract.c | 85 ++++++++++++++++++++++++++++++------------------------------ ft_printf.c | 71 +++++++++++++++++++++++++++++++------------------- header.h | 84 ++++++++++++++++++++++++++++++++++------------------------- list.c | 37 +++++++++++++++----------- main.c | 63 ++++++++++++++++++++++++++++++++------------ parse.c | 50 +++++++++++++++++++---------------- printer.c | 69 +++++++++++++++++++++++++++++------------------- test | Bin 8472 -> 20308 bytes utils.c | 67 +++++++++-------------------------------------- 11 files changed, 300 insertions(+), 245 deletions(-) diff --git a/.gitignore b/.gitignore index 4bb32df..3115c5e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ a.out *ghc ft_printf *.a +test +*.dSYM diff --git a/Makefile b/Makefile index bc3e13c..43cbdfd 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,19 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/10/28 17:41:14 by cacharle #+# #+# # +# Updated: 2019/10/29 02:01:51 by cacharle ### ########.fr # +# # +# **************************************************************************** # + LIBFT_ROOT = ./libft CC = gcc -CCFLAGS = -Wall -Wextra -Werror +CCFLAGS = -Wall -Wextra -g #-Werror LDFLAGS = -L. -lftprintf INCFLAGS = -I$(LIBFT_ROOT) @@ -14,6 +26,7 @@ SRC = ft_printf.c utils.c printer.c parse.c list.c extract.c OBJ = $(SRC:.c=.o) INCLUDE = header.h + all: libft_all $(NAME) $(NAME): $(OBJ) @@ -34,7 +47,7 @@ fclean: libft_fclean clean re: fclean all test: all - $(CC) $(CCFLAGS) $(LDFLAGS) $(INCFLAGS) -o test main.c + $(CC) $(CCFLAGS) $(LDFLAGS) -L./libft -lft $(INCFLAGS) -o test main.c libft_all: diff --git a/extract.c b/extract.c index 7054c38..2f64b51 100644 --- a/extract.c +++ b/extract.c @@ -1,65 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* extract.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/29 00:10:36 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:11:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include "header.h" char *extract_standalone_flags(t_pformat *pformat, char *fmt) { - int i; - - i = 0; - while (IS_STANDALONE_FLAG(fmt[i])) + if (*fmt == 0) + return (fmt); + while (IS_STANDALONE_FLAG(*fmt)) { - if (!pformat->zero_padding) - pformat->zero_padding = fmt[i] == '0'; - if (!pformat->left_adjusted) - pformat->left_adjusted = fmt[i] == '-'; - i++; + if (*fmt == '0') + pformat->flags |= FLAG_ZERO_PADDING; + if (*fmt == '-') + pformat->flags |= FLAG_LEFT_ADJUSTED; + fmt++; } - return (fmt + i); + return (fmt); } char *extract_min_width(t_pformat *pformat, char *fmt) { - int i; - int tmp; - - i = 0; + if (*fmt == 0) + return (fmt); if (*fmt == '*') { - pformat->min_width.wildcard = TRUE; - i++; + pformat->flags |= FLAG_MIN_WIDTH_WILDCARD; + fmt++; } - else - { - if (ft_isdigit(fmt[i])) - { - tmp = ft_atoi(&fmt[i]); - while (ft_isdigit(fmt[i])) - i++; - pformat->min_width.value = tmp; - pformat->min_width.wildcard = FALSE; - } - } - return (fmt + i); + if (!ft_isdigit(*fmt)) + return (fmt); + pformat->min_width = ft_atoi(fmt); + while (*fmt && ft_isdigit(*fmt)) + fmt++; + if (pformat->flags & FLAG_MIN_WIDTH_WILDCARD) + pformat->flags |= FLAG_MIN_WIDTH_OVERWRITE; + return (fmt); } char *extract_precision(t_pformat *pformat, char *fmt) { - int i; - int tmp; - - if (*fmt != '.') + if (*fmt == 0 || *fmt != '.') return (fmt); - i = 1; - if (fmt[i] == '*') + fmt++; + if (*fmt == '*') { - pformat->precision.wildcard = TRUE; - i++; + pformat->flags |= FLAG_PRECISION_WILDCARD; + fmt++; } - else if (!ft_isdigit(fmt[i])) - pformat->precision.value = 0; - tmp = ft_atoi(&fmt[i]); - while (ft_isdigit(fmt[i])) - i++; - pformat->precision.value = tmp; - return (fmt + i); + pformat->precision = ft_atoi(fmt); + while (*fmt && ft_isdigit(*fmt)) + fmt++; + return (fmt); } diff --git a/ft_printf.c b/ft_printf.c index ce7f15c..5a57a23 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -1,39 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/29 00:15:28 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:16:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include #include +#include "libft.h" #include "header.h" -int ft_printf(const char *format, ...) +int ft_printf(const char *format, ...) { - va_list ap; + va_list ap; t_flist *format_list; char *str; - int print_len; - int i; + int print_len; + int i; - if ((format_list = parse(format)) == NULL) + str = ft_strdup(format); + if ((format_list = parse(str)) == NULL) return (-1); - va_start(ap, format); - print_len = 0; + free(str); + va_start(ap, format); + print_len = 0; i = -1; - while (format[++i]) - { - if (format[i] == '%') - { - if ((str = convert_to_str(format_list->content, ap)) == NULL) - { - list_destroy(&format_list); - return (-1); - } - ft_putstr(str); - print_len += ft_strlen(str); - free(str); - i += format_list->content->len; - list_pop_front(&format_list); - } - else - write(STDOUT_FILENO, format + i, 1); - } - va_end(ap); - return (print_len + i); + while (format[++i]) + { + if (format[i] != '%') + { + write(STDOUT_FILENO, format + i, 1); + continue ; + } + str = convert_to_str(format_list->content, ap); + if (str == NULL) + { + list_destroy(&format_list); + return (-1); + } + ft_putstr(str); + print_len += ft_strlen(str); + free(str); + i += format_list->content->len; + list_pop_front(&format_list); + } + list_destroy(&format_list); + va_end(ap); + return (print_len + i); } diff --git a/header.h b/header.h index 20d6370..359bff9 100644 --- a/header.h +++ b/header.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* header.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/29 00:06:46 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:10:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef HEADER_H # define HEADER_H @@ -24,76 +36,76 @@ # define CONVERSION_HEX_UPPER 'X' # define CONVERSION_PERCENT '%' -typedef char t_conversion; -typedef int t_bool; +# define FLAG_LEFT_ADJUSTED 0b00000001 +# define FLAG_ZERO_PADDING 0b00000010 +# define FLAG_MIN_WIDTH_WILDCARD 0b00000100 +# define FLAG_PRECISION_WILDCARD 0b00001000 +# define FLAG_MIN_WIDTH_OVERWRITE 0b00010000 -typedef struct -{ - int value; - t_bool wildcard; -} t_maybe_wildcard; +#include + +typedef char t_conversion; +typedef int t_bool; +typedef unsigned char t_flags; typedef struct { - t_bool left_adjusted; - t_bool zero_padding; - t_maybe_wildcard precision; - t_maybe_wildcard min_width; - t_conversion conversion; - int len; -} t_pformat; - -typedef struct s_flist + int precision; + int min_width; + t_flags flags; + t_conversion conversion; + int len; +} t_pformat; + +typedef struct s_flist { - struct s_flist *next; - t_pformat *content; -} t_flist; + struct s_flist *next; + t_pformat *content; +} t_flist; /* ** ft_printf.c */ -int ft_printf(const char *format, ...); +int ft_printf(const char *format, ...); /* ** parse.c */ - -t_flist *parse(const char *format); -char *isolate_conversion(const char *conversion_start); -t_pformat *parse_reduced(char *fmt); +t_flist *parse(char *format); +t_pformat *parse_reduced(char *fmt); /* ** printer.c */ -void handle_padding(t_pformat *pformat, char *str); -char *convert_to_str(t_pformat *pformat, va_list ap); -void handle_precision(t_pformat *pformat, char *str); +void handle_padding(t_pformat *pformat, char *str); +char *convert_to_str(t_pformat *pformat, va_list ap); +void handle_precision(t_pformat *pformat, char *str); /* ** utils.c */ -int strrchr_index(const char *s, char c); +int strrchr_index(const char *s, char c); /* ** extract.c */ -char *extract_standalone_flags(t_pformat *pformat, char *fmt); -char *extract_min_width(t_pformat *pformat, char *fmt); -char *extract_precision(t_pformat *pformat, char *fmt); +char *extract_standalone_flags(t_pformat *pformat, char *fmt); +char *extract_min_width(t_pformat *pformat, char *fmt); +char *extract_precision(t_pformat *pformat, char *fmt); /* ** list.c */ -t_flist *list_new(t_pformat *content); -void *list_destroy(t_flist **lst); -void list_push_front(t_flist **lst, t_flist *new); -void list_pop_front(t_flist **lst); -void list_reverse(t_flist **lst); +t_flist *list_new(t_pformat *content); +void *list_destroy(t_flist **lst); +void list_push_front(t_flist **lst, t_flist *new); +void list_pop_front(t_flist **lst); +t_flist *list_reverse(t_flist *lst); #endif diff --git a/list.c b/list.c index e6a9460..b87c69a 100644 --- a/list.c +++ b/list.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/29 00:14:50 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:14:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include "header.h" @@ -6,7 +18,7 @@ t_flist *list_new(t_pformat *content) t_flist *lst; if ((lst = (t_flist*)malloc(sizeof(t_flist))) == NULL) - return NULL; + return (NULL); lst->content = content; lst->next = NULL; return (lst); @@ -21,7 +33,6 @@ void *list_destroy(t_flist **lst) return (NULL); } - void list_push_front(t_flist **lst, t_flist *new) { if (lst == NULL || new == NULL) @@ -42,20 +53,16 @@ void list_pop_front(t_flist **lst) *lst = tmp; } -void list_reverse(t_flist **lst) +t_flist *list_reverse(t_flist *lst) { - t_flist *cursor; - t_flist *prev; t_flist *tmp; - prev = NULL; - cursor = *lst; - while (cursor != NULL) - { - tmp = cursor; - cursor->next = prev; - prev = cursor; - cursor = tmp->next; - } - *lst = prev; + if (lst == NULL) + return (NULL); + if (lst->next == NULL) + return (lst); + tmp = list_reverse(lst->next); + lst->next->next = lst; + lst->next = NULL; + return (tmp); } diff --git a/main.c b/main.c index 54944d5..2b04c77 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/28 04:25:09 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:04:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include "libft.h" #include "header.h" @@ -6,29 +18,46 @@ int main() { int test; - printf("bonjour"); - 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("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); - ft_printf("string precision |%.9s|\n", "jesuisbonjourbonjour"); - ft_printf("min width |%9d|\n", 43); - ft_printf("zero padding |%09d|\n", 43); - ft_printf("left adjusted |%-9d|\n", 43); - ft_printf("string padding |%9s|\n", "bon"); + 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("overwrite |%*3d|\n", 5, 43); */ - /* ft_printf("overwrite |%*-1d|\n", 0, 43); */ + /* ft_printf("pointer field width |%15p|\n", &test); */ + /* printf("pointer field width |%15p|\n", &test); */ + /* ft_printf("pointer precision |%.15p|\n", &test); */ + /* printf("pointer precision |%.15p|\n", &test); */ return 0; } diff --git a/parse.c b/parse.c index d486ae7..83a7d55 100644 --- a/parse.c +++ b/parse.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/29 00:11:33 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:11:50 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include "header.h" @@ -7,30 +19,25 @@ ** %(?:\d+\$)?[-]?(?:[0]|'.{1})?-?\d*(?:\.\d+)?[cdusxX] */ -#include -t_flist *parse(const char *format) +t_flist *parse(char *format) { - t_flist *format_list; - t_flist *tmp; - t_pformat *parsed; + t_flist *format_list; + t_flist *tmp; + t_pformat *parsed; format_list = NULL; while (*format) { - if (*format != '%') - { - format++; - continue; - } format++; - if ((parsed = parse_reduced(isolate_conversion(format))) == NULL) + if (format[-1] != '%') + continue; + if ((parsed = parse_reduced(format)) == NULL) return (list_destroy(&format_list)); if ((tmp = list_new(parsed)) == NULL) return (list_destroy(&format_list)); list_push_front(&format_list, tmp); } - list_reverse(&format_list); - return (format_list); + return (list_reverse(format_list)); } t_pformat *parse_reduced(char *fmt) @@ -45,17 +52,14 @@ t_pformat *parse_reduced(char *fmt) return (NULL); if ((pformat = (t_pformat*)malloc(sizeof(t_pformat))) == NULL) return (NULL); - pformat->left_adjusted = FALSE; - pformat->zero_padding = FALSE; - pformat->precision.value = -1; - pformat->precision.wildcard = FALSE; - pformat->min_width.value = -1; - pformat->min_width.wildcard = FALSE; + pformat->precision = -1; + pformat->min_width = -1; + pformat->flags = 0; pformat->len = ft_strlen(fmt); - pformat->conversion = strrchr_index(CONVERSIONS_STR, fmt[pformat->len - 1]); + pformat->conversion = fmt[pformat->len - 1]; fmt[pformat->len - 1] = '\0'; - fmt = extract_standalone_flags(pformat, fmt + i); - fmt = extract_min_width(pformat, fmt + i); - fmt = extract_precision(pformat, fmt + i); + fmt = extract_standalone_flags(pformat, fmt); + fmt = extract_min_width(pformat, fmt); + fmt = extract_precision(pformat, fmt); return (pformat); } diff --git a/printer.c b/printer.c index 8884c00..2bc0116 100644 --- a/printer.c +++ b/printer.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* printer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:13:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include #include @@ -9,21 +21,25 @@ char *convert_to_str(t_pformat *pformat, va_list ap) { - char *str; - t_conversion conversion = pformat->conversion; + char *str; + t_conversion conversion; + conversion = pformat->conversion; str = NULL; - if (pformat->min_width.wildcard) + if (pformat->flags & FLAG_MIN_WIDTH_WILDCARD) { - pformat->min_width.value = va_arg(ap, int); - if (pformat->min_width.value < 0) + if (pformat->flags & FLAG_MIN_WIDTH_OVERWRITE) + (void)va_arg(ap, int); + else + pformat->min_width = va_arg(ap, int); + if (pformat->min_width < 0) { - pformat->left_adjusted = TRUE; - pformat->min_width.value *= -1; + pformat->flags |= FLAG_LEFT_ADJUSTED; + pformat->min_width *= -1; } } - if (pformat->precision.wildcard) - pformat->precision.value = va_arg(ap, int); + if (pformat->flags & FLAG_PRECISION_WILDCARD) + pformat->precision = va_arg(ap, int); if (conversion == CONVERSION_CHAR) { if ((str = ft_strnew(2)) == NULL) @@ -66,16 +82,17 @@ void handle_padding(t_pformat *pformat, char *str) int len; int i; - if (pformat->min_width.value == -1) + if (pformat->min_width == -1) return; - if ((len = ft_strlen(str)) >= pformat->min_width.value) + if ((len = ft_strlen(str)) >= pformat->min_width) return; - tmp = (char*)malloc(sizeof(char) * (pformat->min_width.value + 1)); - if (!pformat->left_adjusted) + //protect this + tmp = (char*)malloc(sizeof(char) * (pformat->min_width + 1)); + if (!(pformat->flags & FLAG_LEFT_ADJUSTED)) { i = 0; - while (i <= pformat->min_width.value - len) - tmp[i++] = pformat->zero_padding ? '0' : ' '; + while (i <= pformat->min_width - len) + tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' '; ft_strcpy(tmp + i - 1, str); ft_strcpy(str, tmp); } @@ -83,39 +100,37 @@ void handle_padding(t_pformat *pformat, char *str) { ft_strcpy(tmp, str); i = len; - while (i < pformat->min_width.value) + while (i < pformat->min_width) tmp[i++] = ' '; ft_strcpy(str, tmp); } free(tmp); } -/* #include */ void handle_precision(t_pformat *pformat, char *str) { int len; char *tmp; t_conversion conv; - /* printf("\n%d\n", pformat->precision.value); */ - if (pformat->precision.value == -1) + if (pformat->precision == -1) return ; len = ft_strlen(str); conv = pformat->conversion; if (conv == CONVERSION_STR) - str[pformat->precision.value] = '\0'; + str[pformat->precision] = '\0'; else if (conv == CONVERSION_DECIMAL || conv == CONVERSION_INT || conv == CONVERSION_UINT || conv == CONVERSION_HEX_LOWER || conv == CONVERSION_HEX_UPPER) { - if (len >= pformat->precision.value) + if (len >= pformat->precision) return ; - tmp = (char*)malloc(sizeof(char) * (pformat->precision.value + 1)); - ft_strcpy(tmp + pformat->precision.value - len, str); - /* printf("\n>%s< %d %d\n", str, len, pformat->precision); */ - pformat->precision.value -= len; - while (pformat->precision.value-- > 0) - tmp[pformat->precision.value] = '0'; + // protect this + tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1)); + ft_strcpy(tmp + pformat->precision - len, str); + pformat->precision -= len; + while (pformat->precision-- > 0) + tmp[pformat->precision] = '0'; ft_strcpy(str, tmp); free(tmp); } diff --git a/test b/test index a37e028..36cc5a3 100755 Binary files a/test and b/test differ diff --git a/utils.c b/utils.c index 77c5f58..c018e5a 100644 --- a/utils.c +++ b/utils.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/29 00:12:40 by cacharle #+# #+# */ +/* Updated: 2019/10/29 00:12:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include "header.h" @@ -16,59 +28,4 @@ int strrchr_index(const char *s, char c) i--; } return (i); - -} - -int nbrlen_radix(long int nbr, int radix) -{ - int counter; - long unsigned int u_nbr; - - if (nbr == 0) - return (1); - counter = 0; - u_nbr = nbr; - if (nbr < 0) - { - counter++; - u_nbr = -nbr; - } - while (u_nbr > 0) - { - u_nbr /= radix; - counter++; - } - return (counter); -} - -char *ft_itoa_base(long int n, char *base) -{ - char *str; - int len; - int is_negative; - int radix; - long unsigned int u_nbr; - - - radix = ft_strlen(base); - len = nbrlen_radix(n, radix); - if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL) - return (NULL); - str[len] = '\0'; - is_negative = 0; - u_nbr = n; - if (n < 0) - { - is_negative = 1; - str[0] = '-'; - u_nbr = -n; - } - len--; - while (len >= (is_negative ? 1 : 0)) - { - str[len] = base[u_nbr % radix]; - u_nbr /= radix; - len--; - } - return (str); } -- cgit