nicolas-arnaud 2 years ago
parent
commit
5aa3574849
  1. 22
      includes/ft_printf.h
  2. 53
      srcs/ft_print_chars.c
  3. 14
      srcs/ft_print_hexs.c
  4. 19
      srcs/ft_print_nbrs.c
  5. 21
      srcs/ft_print_opts.c
  6. 11
      srcs/ft_print_ptrs.c
  7. 48
      srcs/ft_print_strings.c
  8. 96
      srcs/ft_printf.c
  9. 1
      tester

22
includes/ft_printf.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 08:09:45 by narnaud #+# #+# */ /* Created: 2021/10/27 08:09:45 by narnaud #+# #+# */
/* Updated: 2021/12/21 09:39:34 by narnaud ### ########.fr */ /* Updated: 2023/04/13 13:53:00 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,16 +43,16 @@ char *str_opts_transform(char *str, t_opts *opts);
void ft_print_char(int ch, t_opts *opts); void ft_print_char(int ch, t_opts *opts);
void ft_print_str(char *str, t_opts *opts); void ft_print_str(char *str, t_opts *opts);
void va_print_char(va_list va_ch, const char *str, t_opts *opts); void va_print_char(va_list va_ch, t_opts *opts);
void va_print_str(va_list va_str, const char *str, t_opts *opts); void va_print_str(va_list va_str, t_opts *opts);
void va_print_ptr(va_list va_ptr, const char *str, t_opts *opts); void va_print_ptr(va_list va_ptr, t_opts *opts);
void va_print_nbr(va_list va_int, const char *str, t_opts *opts); void va_print_nbr(va_list va_int, t_opts *opts);
void va_print_unsign(va_list va_uint, const char *str, t_opts *opts); void va_print_unsign(va_list va_uint, t_opts *opts);
void va_print_x(va_list va_uint, const char *str, t_opts *opts); void va_print_x(va_list va_uint, t_opts *opts);
void va_print_x_cap(va_list va_uint, const char *str, t_opts *opts); void va_print_x_cap(va_list va_uint, t_opts *opts);
void va_print_perc(va_list va, const char *str, t_opts *opts); void va_print_perc(va_list va, t_opts *opts);
void ft_format(const char *str, va_list args); void ft_format( va_list args);
int ft_printf(const char *str, ...); int ft_printf(const char *str, ...);
int ft_fprintf(FILE *file, const char *format, ...); int ft_fprintf(FILE *file, const char *format, ...);

53
srcs/ft_print_chars.c

@ -1,49 +1,48 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_putchars.c :+: :+: :+: */ /* ft_print_chars.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 09:17:03 by narnaud #+# #+# */ /* Created: 2021/10/27 09:17:03 by narnaud #+# #+# */
/* Updated: 2021/11/17 09:57:53 by narnaud ### ########.fr */ /* Updated: 2023/04/13 12:14:39 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "ft_printf.h"
void ft_print_char(int ch, t_opts *opts) void ft_print_char(int ch, t_opts *opts)
{ {
char c; char c;
char *ret; char *ret;
int len; int len;
c = (char)ch;
if (opts->width < 1)
len = 1;
else
len = opts->width;
ret = ft_calloc(len + 1, sizeof(char));
c = (char)ch;
if (opts->width < 1)
len = 1;
else
len = opts->width;
ret = ft_calloc(len + 1, sizeof(char));
if (opts->minus) if (opts->minus)
{ {
ret[0] = c; ret[0] = c;
while (opts->width-- > 1) while (opts->width-- > 1)
ret[opts->width] = ' '; ret[opts->width] = ' ';
} }
else else
{ {
ret[len - 1] = c; ret[len - 1] = c;
while (opts->width-- > 1) while (opts->width-- > 1)
ret[opts->width - 1] = ' '; ret[opts->width - 1] = ' ';
} }
add_string(ret, len);
add_string(ret, len);
} }
void ft_print_str(char *str, t_opts *opts) void ft_print_str(char *str, t_opts *opts)
{ {
char *ret; char *ret;
if (!str) if (!str)
{ {
if (!opts->dot || opts->precision >= 6) if (!opts->dot || opts->precision >= 6)
@ -51,26 +50,22 @@ void ft_print_str(char *str, t_opts *opts)
else else
str = ""; str = "";
} }
ret = str_opts_transform(str, opts); ret = str_opts_transform(str, opts);
add_string(ret, ft_strlen(ret));
add_string(ret, ft_strlen(ret));
} }
void va_print_char(va_list va_ch, const char *str, t_opts *opts) void va_print_char(va_list va_ch, t_opts *opts)
{ {
(void)*str;
ft_print_char(va_arg(va_ch, int), opts); ft_print_char(va_arg(va_ch, int), opts);
} }
void va_print_str(va_list va_str, const char *str, t_opts *opts) void va_print_str(va_list va_str, t_opts *opts)
{ {
(void)*str;
ft_print_str(va_arg(va_str, char *), opts); ft_print_str(va_arg(va_str, char *), opts);
} }
void va_print_perc(va_list va, const char *str, t_opts *opts) void va_print_perc(va_list va, t_opts *opts)
{ {
(void)*str;
(void)va; (void)va;
ft_print_char('%', opts); ft_print_char('%', opts);
} }

14
srcs/ft_print_hexs.c

@ -1,18 +1,18 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_print_x.c :+: :+: :+: */ /* ft_print_hexs.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 09:54:24 by narnaud #+# #+# */ /* Created: 2021/10/27 09:54:24 by narnaud #+# #+# */
/* Updated: 2021/11/17 10:24:24 by narnaud ### ########.fr */ /* Updated: 2023/04/13 12:13:36 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "ft_printf.h"
void ft_print_x(unsigned int n, t_opts *opts) void ft_print_x(unsigned int n, t_opts *opts)
{ {
char *raw; char *raw;
char *nbr; char *nbr;
@ -32,7 +32,7 @@ void ft_print_x(unsigned int n, t_opts *opts)
free(nbr); free(nbr);
} }
void ft_print_x_cap(unsigned int n, t_opts *opts) void ft_print_x_cap(unsigned int n, t_opts *opts)
{ {
char *raw; char *raw;
char *tmp; char *tmp;
@ -52,14 +52,12 @@ void ft_print_x_cap(unsigned int n, t_opts *opts)
free(nbr); free(nbr);
} }
void va_print_x(va_list va_uint, const char *str, t_opts *opts) void va_print_x(va_list va_uint, t_opts *opts)
{ {
(void)*str;
ft_print_x(va_arg(va_uint, unsigned int), opts); ft_print_x(va_arg(va_uint, unsigned int), opts);
} }
void va_print_x_cap(va_list va_uint, const char *str, t_opts *opts) void va_print_x_cap(va_list va_uint, t_opts *opts)
{ {
(void)*str;
ft_print_x_cap(va_arg(va_uint, unsigned int), opts); ft_print_x_cap(va_arg(va_uint, unsigned int), opts);
} }

19
srcs/ft_print_nbrs.c

@ -6,18 +6,17 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 09:34:01 by narnaud #+# #+# */ /* Created: 2021/10/27 09:34:01 by narnaud #+# #+# */
/* Updated: 2021/12/21 10:06:28 by narnaud ### ########.fr */ /* Updated: 2023/04/13 12:13:19 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "ft_printf.h"
void ft_print_nbr(int n, const char *str, t_opts *opts) void ft_print_nbr(int n, t_opts *opts)
{ {
char *nbr; char *nbr;
char *raw; char *raw;
(void)*str;
raw = ft_itoa(n); raw = ft_itoa(n);
nbr = int_opts_transform(n, raw, opts); nbr = int_opts_transform(n, raw, opts);
opts->dot = 0; opts->dot = 0;
@ -26,25 +25,25 @@ void ft_print_nbr(int n, const char *str, t_opts *opts)
free(nbr); free(nbr);
} }
void ft_print_unsign(unsigned int n, const char *str, t_opts *opts) void ft_print_unsign(unsigned int n, t_opts *opts)
{ {
char *raw; char *raw;
char *nbr; char *nbr;
(void)*str;
raw = ft_utoa(n); raw = ft_utoa(n);
nbr = uint_opts_transform(n, raw, opts); nbr = uint_opts_transform(n, raw, opts);
opts->dot = 0; opts->dot = 0;
ft_print_str(nbr, opts); ft_print_str(nbr, opts);
free(raw); free(raw);
free(nbr); free(nbr);
} }
void va_print_nbr(va_list va, const char *str, t_opts *opts) void va_print_nbr(va_list va, t_opts *opts)
{ {
ft_print_nbr(va_arg(va, int), str, opts); ft_print_nbr(va_arg(va, int), opts);
} }
void va_print_unsign(va_list va, const char *str, t_opts *opts)
void va_print_unsign(va_list va, t_opts *opts)
{ {
ft_print_unsign(va_arg(va, unsigned int), str, opts); ft_print_unsign(va_arg(va, unsigned int), opts);
} }

21
srcs/ft_print_opts.c

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_opts.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 08:05:56 by narnaud #+# #+# */
/* Updated: 2023/04/11 08:05:58 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h" #include "ft_printf.h"
char *zero_fill(char *nbr, t_opts *opts) char *zero_fill(char *nbr, t_opts *opts)
@ -32,8 +44,7 @@ char *int_opts_transform(int n, char *nbr, t_opts *opts)
nbr = ft_strjoin("+", nbr); nbr = ft_strjoin("+", nbr);
else if (opts->space && n >= 0) else if (opts->space && n >= 0)
nbr = ft_strjoin(" ", nbr); nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot && n == 0 else if (opts->precision == 0 && opts->dot && n == 0 && opts->len)
&& opts->len)
nbr = ft_strdup(""); nbr = ft_strdup("");
else else
nbr = ft_strdup(nbr); nbr = ft_strdup(nbr);
@ -50,8 +61,7 @@ char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts)
nbr = ft_strjoin("+", nbr); nbr = ft_strjoin("+", nbr);
else if (opts->space) else if (opts->space)
nbr = ft_strjoin(" ", nbr); nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot && n == 0 else if (opts->precision == 0 && opts->dot && n == 0 && opts->len)
&& opts->len)
nbr = ft_strdup(""); nbr = ft_strdup("");
else else
nbr = ft_strdup(nbr); nbr = ft_strdup(nbr);
@ -68,8 +78,7 @@ char *ptr_opts_transform(char *nbr, t_opts *opts)
nbr = ft_strjoin("+", nbr); nbr = ft_strjoin("+", nbr);
else if (opts->space) else if (opts->space)
nbr = ft_strjoin(" ", nbr); nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot else if (opts->precision == 0 && opts->dot && opts->len)
&& opts->len)
nbr = ft_strdup(""); nbr = ft_strdup("");
else else
nbr = ft_strdup(nbr); nbr = ft_strdup(nbr);

11
srcs/ft_print_ptrs.c

@ -1,24 +1,24 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_print_ptr.c :+: :+: :+: */ /* ft_print_ptrs.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 10:13:07 by narnaud #+# #+# */ /* Created: 2021/10/27 10:13:07 by narnaud #+# #+# */
/* Updated: 2021/12/21 09:50:41 by narnaud ### ########.fr */ /* Updated: 2023/04/13 12:12:31 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "ft_printf.h"
void ft_print_ptr(void *ptr, t_opts *opts) void ft_print_ptr(void *ptr, t_opts *opts)
{ {
char *raw; char *raw;
char *nbr; char *nbr;
if (ptr == NULL) if (ptr == NULL)
raw = ft_strdup("(nil)"); raw = ft_strdup("0x0");
else else
{ {
raw = ft_itox((unsigned long long int)ptr, "0123456789abcdef"); raw = ft_itox((unsigned long long int)ptr, "0123456789abcdef");
@ -31,8 +31,7 @@ void ft_print_ptr(void *ptr, t_opts *opts)
free(nbr); free(nbr);
} }
void va_print_ptr(va_list va_ptr, const char *str, t_opts *opts) void va_print_ptr(va_list va_ptr, t_opts *opts)
{ {
(void)*str;
ft_print_ptr(va_arg(va_ptr, void *), opts); ft_print_ptr(va_arg(va_ptr, void *), opts);
} }

48
srcs/ft_print_strings.c

@ -6,42 +6,46 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */ /* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */
/* Updated: 2021/11/17 09:40:16 by narnaud ### ########.fr */ /* Updated: 2023/04/11 08:07:11 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "ft_printf.h"
static t_dlist *list = NULL; static t_dlist *g_list = NULL;
void add_string(char *str, size_t size) void add_string(char *str, size_t size)
{ {
list = ft_dlst_add(list, str); g_list = ft_dlst_add(g_list, str);
list->size = size; g_list->size = size;
} }
t_dlist *get_last(void) t_dlist *get_last(void)
{ {
return list; return (g_list);
} }
t_dlist *get_first(void) t_dlist *get_first(void)
{ {
t_dlist *tmp = list; t_dlist *tmp;
while (tmp->previous)
tmp = tmp->previous; tmp = g_list;
return tmp; while (tmp->previous)
tmp = tmp->previous;
return (tmp);
} }
void clean_list(void) void clean_list(void)
{ {
t_dlist *tmp = list; t_dlist *tmp;
while (list)
{ tmp = g_list;
tmp = list->previous; while (g_list)
free(list->content); {
free(list); tmp = g_list->previous;
list = tmp; free(g_list->content);
} free(g_list);
list = NULL; g_list = tmp;
}
g_list = NULL;
} }

96
srcs/ft_printf.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */ /* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */
/* Updated: 2021/11/17 09:40:16 by narnaud ### ########.fr */ /* Updated: 2023/04/13 12:20:28 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,14 +49,14 @@ const char *handle_options(va_list args, const char *str, t_opts *opts)
return (++str); return (++str);
} }
void handle_functions(va_list args, const char *str, t_opts *opts) void handle_functions(va_list args, const char *str, t_opts *opts)
{ {
size_t n; size_t n;
const char *g_types = {"cspdiuxX%"}; const char *g_types = {"cspdiuxX%"};
static void(*g_printf_fcts[9])(va_list arg, const char *str, static void (*g_printf_fcts[9])(va_list arg, t_opts *opts)
t_opts *opts) = {va_print_char, va_print_str, va_print_ptr, = {va_print_char, va_print_str, va_print_ptr,
va_print_nbr, va_print_nbr, va_print_unsign, va_print_x, va_print_nbr, va_print_nbr, va_print_unsign, va_print_x, va_print_x_cap,
va_print_x_cap, va_print_perc}; va_print_perc};
n = 0; n = 0;
while (g_types[n] && g_types[n] != *str && ++n) while (g_types[n] && g_types[n] != *str && ++n)
@ -74,10 +74,10 @@ void handle_functions(va_list args, const char *str, t_opts *opts)
} }
if (opts->precision < 0) if (opts->precision < 0)
opts->dot = 0; opts->dot = 0;
(g_printf_fcts[n])(args, str, opts); (g_printf_fcts[n])(args, opts);
} }
void ft_format(const char *format, va_list args) void ft_format(const char *format, va_list args)
{ {
t_opts opts; t_opts opts;
@ -88,74 +88,72 @@ void ft_format(const char *format, va_list args)
opts = (t_opts){0}; opts = (t_opts){0};
while (*format && !ft_strchr("cspdiuxX%", *format)) while (*format && !ft_strchr("cspdiuxX%", *format))
format = handle_options(args, format, &opts); format = handle_options(args, format, &opts);
handle_functions(args, format, &opts); handle_functions(args, format, &opts);
} }
else else
add_string(ft_strnew(1, *format), 1); add_string(ft_strnew(1, *format), 1);
format++; format++;
} }
} }
int ft_printf(const char *format, ...) int ft_printf(const char *format, ...)
{ {
int ret; int ret;
t_dlist *strings; t_dlist *strings;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
ft_format(format, args); ft_format(format, args);
ret = 0; ret = 0;
strings = get_first(); strings = get_first();
while (strings) while (strings)
{ {
ret += strings->size; ret += strings->size;
ft_putnstr_fd(strings->size, strings->content, 1); ft_putnstr_fd(strings->size, strings->content, 1);
strings = strings->next; strings = strings->next;
} }
clean_list(); clean_list();
va_end(args); va_end(args);
return (ret); return (ret);
} }
int ft_fprintf(FILE *file, const char *format, ...) int ft_fprintf(FILE *file, const char *format, ...)
{ {
int ret; int ret;
t_dlist *strings; t_dlist *strings;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
ft_format(format, args); ft_format(format, args);
ret = 0; ret = 0;
strings = get_first(); strings = get_first();
while (strings) while (strings)
{ {
ret += strings->size; ret += strings->size;
fwrite(strings->content, sizeof(char), strings->size, file); fwrite(strings->content, sizeof(char), strings->size, file);
strings = strings->next; strings = strings->next;
} }
clean_list(); clean_list();
va_end(args); va_end(args);
return (ret); return (ret);
} }
int ft_sprintf(char *str, const char *format, ...) int ft_sprintf(char *str, const char *format, ...)
{ {
int ret; int ret;
t_dlist *strings; t_dlist *strings;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
ft_format(format, args); ft_format(format, args);
ret = 0; ret = 0;
strings = get_first(); strings = get_first();
while (strings) while (strings)
{ {
ret += strings->size; ret += strings->size;
ft_strlcat(str, strings->content, ret); ft_strlcat(str, strings->content, ret);
strings = strings->next; strings = strings->next;
} }
clean_list(); clean_list();
va_end(args); va_end(args);
return (ret); return (ret);
} }

1
tester

@ -1 +0,0 @@
Subproject commit a053a3500c9124a5c64c95cd8ff9e78a783932c4
Loading…
Cancel
Save