Browse Source

updated libft

master
nicolas-arnaud 2 years ago
parent
commit
42806641e5
  1. 2
      Makefile
  2. 0
      ft_print_chars.c
  3. 0
      ft_print_hexs.c
  4. 0
      ft_print_nbrs.c
  5. 108
      ft_print_opts.c
  6. 0
      ft_print_ptrs.c
  7. 85
      ft_printf.c
  8. 22
      ft_printf.h
  9. 2
      libft
  10. 8
      main.c
  11. 87
      opts.c
  12. 57
      utils.c

2
Makefile

@ -1,6 +1,6 @@
NAME = libftprintf.a NAME = libftprintf.a
SRCS = ft_printf.c ft_putchars.c ft_putnbrs.c ft_putptr.c ft_putx.c utils.c opts.c SRCS = ft_print_chars.c ft_print_hexs.c ft_print_nbrs.c ft_print_opts.c ft_print_ptrs.c ft_printf.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
RM = rm -rf RM = rm -rf

0
ft_putchars.c → ft_print_chars.c

0
ft_putx.c → ft_print_hexs.c

0
ft_putnbrs.c → ft_print_nbrs.c

108
ft_print_opts.c

@ -0,0 +1,108 @@
#include "ft_printf.h"
char *zero_fill(char *nbr, t_opts *opts)
{
int len;
char *fill;
fill = NULL;
if (opts->zero)
len = opts->width - ft_strlen(nbr);
else if (opts->dot)
len = opts->precision - ft_strlen(nbr);
else
return (NULL);
if (len < 0 || (opts->zero && len == 0))
return (NULL);
if ((nbr[0] == '-' || nbr[0] == '+' || nbr[0] == ' ') && opts->dot)
fill = ft_strnew(len + 1, '0');
else
fill = ft_strnew(len, '0');
if (nbr[0] == '-' || nbr[0] == '+' || nbr[0] == ' ')
{
fill[0] = nbr[0];
nbr[0] = '0';
}
return (fill);
}
char *int_opts_transform(int n, char *nbr, t_opts *opts)
{
if (opts->plus && n >= 0)
nbr = ft_strjoin("+", nbr);
else if (opts->space && n >= 0)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot && n == 0
&& opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(zero_fill(nbr, opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(zero_fill(nbr, opts), nbr);
return (nbr);
}
char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts)
{
if (opts->plus)
nbr = ft_strjoin("+", nbr);
else if (opts->space)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot && n == 0
&& opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(zero_fill(nbr, opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(zero_fill(nbr, opts), nbr);
return (nbr);
}
char *ptr_opts_transform(char *nbr, t_opts *opts)
{
if (opts->plus)
nbr = ft_strjoin("+", nbr);
else if (opts->space)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot
&& opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(zero_fill(nbr, opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(zero_fill(nbr, opts), nbr);
return (nbr);
}
char *str_opts_transform(char *str, t_opts *opts)
{
int len;
char *tmp;
char *sub;
tmp = NULL;
sub = NULL;
if (opts->dot)
sub = ft_substr(str, 0, opts->precision);
if (sub)
str = sub;
len = opts->width - (int)ft_strlen(str);
tmp = ft_strnew(len, ' ');
if (!opts->minus && len > 0)
str = ft_strjoin(tmp, str);
else if (opts->minus && len > 0)
str = ft_strjoin(str, tmp);
else
str = ft_strdup(str);
if (tmp)
free(tmp);
if (sub)
free(sub);
return (str);
}

0
ft_putptr.c → ft_print_ptrs.c

85
ft_printf.c

@ -12,50 +12,61 @@
#include "ft_printf.h" #include "ft_printf.h"
#include "./libft/libft.h" void get_digit(t_opts *opts, int n)
{
if (opts->dot)
opts->precision = n;
else
opts->width = n;
}
int handle_options(va_list args, const char *str, t_opts *opts) const char *handle_options(va_list args, const char *str, t_opts *opts)
{ {
while (*str && !ft_strchr("cspdiuxX%", *str)) { if (*str == '-')
if (*str == '-') opts->minus = 1; opts->minus = 1;
else if (*str == '0') opts->zero = 1; else if (*str == '0')
else if (*str == '#') opts->hash = 1; opts->zero = 1;
else if (*str == ' ') opts->space = 1; else if (*str == '#')
else if (*str == '+') opts->plus = 1; opts->hash = 1;
else if (*str == '.') opts->dot = 1; else if (*str == ' ')
opts->space = 1;
else if (*str == '+')
opts->plus = 1;
else if (*str == '.')
opts->dot = 1;
else if (ft_isdigit(*str)) else if (ft_isdigit(*str))
{ {
if (opts->dot) opts->precision = ft_atoi(str); get_digit(opts, ft_atoi(str));
else opts->width = ft_atoi(str); while (ft_isdigit(*str) && str++)
while (ft_isdigit(*str)) {
str++;
opts->len++; opts->len++;
return (str);
} }
continue; else if (*str == '*')
} else if (*str == '*') get_digit(opts, va_arg(args, int));
{ else
if (opts->dot) opts->precision = va_arg(args, int); return (str);
else opts->width = va_arg(args, int);
} else return (0);
str++;
opts->len++; opts->len++;
} return (++str);
return (1);
} }
int handle_functions(va_list args, const char *str, t_opts *opts) int handle_functions(va_list args, const char *str, t_opts *opts)
{ {
size_t n = 0; size_t n;
size_t ret = 0;
const char *g_types = {"cspdiuxX%"}; const char *g_types = {"cspdiuxX%"};
static int (*g_printf_fcts[9])(va_list arg, const char *str, t_opts *opts) = { static int (*g_printf_fcts[9])(va_list arg, const char *str,
va_print_char, va_print_str, va_print_ptr, va_print_nbr, va_print_nbr, t_opts *opts) = {va_print_char, va_print_str, va_print_ptr,
va_print_unsign, va_print_x, va_print_x_cap, va_print_perc}; va_print_nbr, va_print_nbr, va_print_unsign, va_print_x,
while (g_types[n] && g_types[n] != *str) va_print_x_cap, va_print_perc};
n++;
if (g_types[n]) { n = 0;
if (opts->minus) opts->zero = 0; while (g_types[n] && g_types[n] != *str && ++n)
if (opts->precision > 0) opts->zero = 0; ;
if (!g_types[n])
return (0);
if (opts->minus)
opts->zero = 0;
if (opts->precision > 0)
opts->zero = 0;
if (opts->width < 0) if (opts->width < 0)
{ {
opts->minus = 1; opts->minus = 1;
@ -63,9 +74,7 @@ int 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;
ret += (g_printf_fcts[n])(args, str, opts); return ((g_printf_fcts[n])(args, str, opts));
}
return ret;
} }
int ft_printf(const char *str, ...) int ft_printf(const char *str, ...)
@ -81,12 +90,10 @@ int ft_printf(const char *str, ...)
if (*str == '%' && str++) if (*str == '%' && str++)
{ {
opts = (t_opts){0}; opts = (t_opts){0};
if (handle_options(args, str, &opts)) while (*str && !ft_strchr("cspdiuxX%", *str))
{ str = handle_options(args, str, &opts);
str += opts.len;
i += handle_functions(args, str, &opts); i += handle_functions(args, str, &opts);
} }
}
else if (++i) else if (++i)
ft_putchar_fd(*str, 1); ft_putchar_fd(*str, 1);
str++; str++;

22
ft_printf.h

@ -10,8 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FT_PRINTF_H #pragma once
# define FT_PRINTF_H
# define MEM_ADD_SIZE 6 # define MEM_ADD_SIZE 6
@ -23,23 +22,18 @@ typedef struct s_opts {
int len; int len;
int width; int width;
int precision; int precision;
int zero; int zero : 1;
int minus; int minus : 1;
int dot; int dot : 1;
int hash; int hash : 1;
int space; int space : 1;
int plus; int plus : 1;
} t_opts; } t_opts;
char *int_opts_transform(int n, char *nbr, t_opts *opts); char *int_opts_transform(int n, char *nbr, t_opts *opts);
char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts); char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts);
char *ptr_opts_transform(char *ptr, t_opts *opts); char *ptr_opts_transform(char *ptr, t_opts *opts);
char *str_opts_transform(char *str, t_opts *opts); char *str_opts_transform(char *str, t_opts *opts);
char *ft_append(char *s1, char *s2);
char *ft_zero_fill(char *nbr, char c, t_opts *opts);
char *ft_strnew(int n, char c);
int ft_putnstr(char *str, int n);
int ft_print_char(int ch, t_opts *opts); int ft_print_char(int ch, t_opts *opts);
int ft_print_str(char *str, t_opts *opts); int ft_print_str(char *str, t_opts *opts);
@ -52,5 +46,3 @@ int va_print_x(va_list va_uint, const char *str, t_opts *opts);
int va_print_x_cap(va_list va_uint, const char *str, t_opts *opts); int va_print_x_cap(va_list va_uint, const char *str, t_opts *opts);
int va_print_perc(va_list va, const char *str, t_opts *opts); int va_print_perc(va_list va, const char *str, t_opts *opts);
int ft_printf(const char *str, ...); int ft_printf(const char *str, ...);
#endif

2
libft

@ -1 +1 @@
Subproject commit 840a26935181c35cd8416853f1aad9a0bf99621f Subproject commit 91542393af2673f7cba570fc4b868294810f1abc

8
main.c

@ -1,8 +0,0 @@
#include "ft_printf.h"
int main(void)
{
ft_printf("%.4s\n", "Hello world!\n");
return 0;
}

87
opts.c

@ -1,87 +0,0 @@
#include "ft_printf.h"
char *int_opts_transform(int n, char *nbr, t_opts *opts)
{
if (opts->plus && n>=0)
nbr = ft_strjoin("+", nbr);
else if (opts->space && n>=0)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot && n == 0 &&
opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
return (nbr);
}
char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts)
{
if (opts->plus)
nbr = ft_strjoin("+", nbr);
else if (opts->space)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot && n == 0 &&
opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
return (nbr);
}
char *ptr_opts_transform(char *nbr, t_opts *opts)
{
if (opts->plus)
nbr = ft_strjoin("+", nbr);
else if (opts->space)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot &&
opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
return (nbr);
}
char *str_opts_transform(char *str, t_opts *opts)
{
int len;
char *tmp = NULL;
char *sub = NULL;
if (opts->dot)
sub = ft_substr(str, 0, opts->precision);
if (sub)
str = sub;
len = opts->width - (int)ft_strlen(str);
tmp = ft_strnew(len, ' ');
if (!opts->minus && len > 0)
str = ft_strjoin(tmp, str);
else if (opts->minus && len > 0)
str = ft_strjoin(str, tmp);
else
str = ft_strdup(str);
if (tmp)
free(tmp);
if (sub)
free(sub);
return (str);
}

57
utils.c

@ -1,57 +0,0 @@
#include "ft_printf.h"
char *ft_strnew(int n, char c)
{
char *str = NULL;
if (n < 0)
return (NULL);
str = malloc(n + 1);
if (!str)
return (NULL);
str[n] = '\0';
while (--n >= 0)
str[n] = c;
return (str);
}
char *ft_zero_fill(char *nbr, char c, t_opts *opts)
{
int len;
char *fill = NULL;
if (opts->zero)
len = opts->width - ft_strlen(nbr);
else if (opts->dot)
len = opts->precision - ft_strlen(nbr);
else
return (NULL);
if (len < 0 || (opts->zero && len == 0))
return (NULL);
if ((nbr[0] == '-' || nbr[0] == '+' || nbr[0] == ' ') && opts->dot)
fill = ft_strnew(len + 1, c);
else
fill = ft_strnew(len , c);
if (nbr[0] == '-' || nbr[0] == '+' || nbr[0] == ' ')
{
fill[0] = nbr[0];
nbr[0] = '0';
}
return (fill);
}
char *ft_append(char *s1, char *s2) {
char *ret = NULL;
if (!s1)
return (s2);
if (!s2)
return (s1);
ret = ft_strjoin(s1, s2);
free(s1);
free(s2);
return (ret);
}
Loading…
Cancel
Save