Skip to content

Commit

Permalink
pronto
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerio Antunes de souza junior committed Mar 25, 2021
1 parent 6ae61ee commit 7f50419
Show file tree
Hide file tree
Showing 17 changed files with 963 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
NAME = libftprintf.a

SRCS = ft_flag_parse.c \
ft_printf.c ft_flags.c ft_parse_types.c \
ft_is_type_percent.c ft_is_type_di.c ft_is_type_x.c \
ft_type_c.c ft_type_p.c ft_type_s.c \
ft_type_u.c ft_itoa_ull.c \
ft_itoa.c ft_printf_utils.c ft_printf_utils2.c\

OBJS = $(SRCS:.c=.o)

CC = gcc

FLAGS = -c -I -Wall -Wetra -Werror

$(NAME): $(OBJS)
ar rc $(NAME) $(OBJS)
ranlib $(NAME)

all: $(NAME)

bonus: $(NAME)

clean:
rm -f $(OBJS)

fclean: clean
rm -f $(NAME)

re: fclean all

.PHONY: clean fclean all re
39 changes: 39 additions & 0 deletions ft_flag_parse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_flag_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vantunes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/22 15:33:45 by vantunes #+# #+# */
/* Updated: 2021/03/22 15:33:50 by vantunes ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

int ft_flag_parse(t_flags *flags, va_list args, const char *str, int pos)
{
while (str[pos])
{
if (str[pos] == '0' && flags->minus == 0 && flags->width == 0)
flags->zero = 1;
if (str[pos] == '+')
flags->plus = 1;
if (str[pos] == '.')
pos = ft_isdot(flags, args, str, pos);
if (str[pos] == '-')
flags->minus = 1;
if (str[pos] == '*')
flags = ft_isstar(flags, args);
if (ft_isdigit(str[pos]))
flags = ft_digitflag(flags, str[pos]);
if (ft_strchr(ALLCARACTERES, str[pos]))
{
flags->type = str[pos];
break ;
}
pos++;
}
return (pos);
}
71 changes: 71 additions & 0 deletions ft_flags.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_flags.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vantunes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/22 15:38:12 by vantunes #+# #+# */
/* Updated: 2021/03/22 15:38:16 by vantunes ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

int ft_isdot(t_flags *flags, va_list args, const char *str, int pos)
{
pos++;
if (str[pos] == '*')
{
flags->dot = va_arg(args, int);
pos++;
}
else
{
flags->dot = 0;
while (ft_isdigit(str[pos]))
flags->dot = (flags->dot * 10) + (str[pos++] - 48);
}
return (pos);
}

t_flags *ft_isminus(t_flags *flags)
{
flags->minus = 1;
flags->zero = 0;
return (flags);
}

t_flags ft_init_flags(void)
{
t_flags flags;

flags.dot = -1;
flags.minus = 0;
flags.star = 0;
flags.width = 0;
flags.zero = 0;
flags.type = 0;
flags.plus = 0;
return (flags);
}

t_flags *ft_isstar(t_flags *flags, va_list args)
{
flags->star = 1;
flags->width = va_arg(args, int);
if (flags->width < 0)
{
flags->width *= -1;
flags->minus = 1;
}
return (flags);
}

t_flags *ft_digitflag(t_flags *flags, char c)
{
if (flags->star == 1)
flags->width = 0;
flags->width = (flags->width * 10) + (c - 48);
return (flags);
}
104 changes: 104 additions & 0 deletions ft_is_type_di.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_type_di.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vantunes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/22 15:34:01 by vantunes #+# #+# */
/* Updated: 2021/03/22 15:34:03 by vantunes ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

static int ft_isminuszero1(t_flags *flags, char *str, long int d_i)
{
int char_count;

char_count = 0;
if (d_i < 0)
{
char_count += ft_putchar('-');
flags->width--;
}
else if (d_i >= 0 && flags->plus == 1)
{
char_count += ft_putchar('+');
flags->width--;
}
char_count += ft_print_width(flags->width, ft_strlen(str), 1);
char_count += ft_print_prec(ft_strlen(str), str);
return (char_count);
}

static int ft_isminuszero(t_flags *flags, char *str, long int d_i)
{
int char_count;

char_count = 0;
if ((d_i < 0 && flags->dot >= 0) || (d_i >= 0 && flags->plus == 1))
flags->width--;
char_count += ft_print_width(flags->width, flags->dot, 0);
if (d_i < 0 && flags->dot >= 0)
char_count += ft_putchar('-');
else if (d_i >= 0 && flags->plus == 1)
{
char_count += ft_putchar('+');
flags->width--;
}
char_count += ft_print_width(flags->dot, ft_strlen(str), 1);
char_count += ft_print_prec(ft_strlen(str), str);
return (char_count);
}

static int ft_isminus(t_flags *flags, char *str, long int d_i)
{
int char_count;

char_count = 0;
if (d_i < 0)
{
char_count += ft_putchar('-');
flags->width--;
}
else if (d_i >= 0 && flags->plus == 1)
{
char_count += ft_putchar('+');
flags->width--;
}
if (flags->dot >= 0)
char_count += ft_print_width(flags->dot - 1, ft_strlen(str) - 1, 1);
char_count += ft_print_prec(ft_strlen(str), str);
char_count += ft_print_width(flags->width, flags->dot, 0);
return (char_count);
}

int ft_isint(t_flags *flags, long int d_i)
{
int char_count;
char *str;

char_count = 0;
if (flags->dot == 0 && d_i == 0 && flags->plus == 0)
{
char_count += ft_print_width(flags->width, 0, 0);
return (char_count);
}
if (d_i == 0 && flags->dot != 0)
str = ft_strdup("0");
else
str = ft_itoa(d_i, 10);
if (flags->dot >= 0 || flags->minus == 1)
flags->zero = 0;
if (flags->dot <= ft_strlen(str))
flags->dot = ft_strlen(str);
if (flags->minus == 1)
char_count += ft_isminus(flags, str, d_i);
if (flags->minus == 0 && flags->zero == 0)
char_count += ft_isminuszero(flags, str, d_i);
if (flags->minus == 0 && flags->zero == 1)
char_count += ft_isminuszero1(flags, str, d_i);
free(str);
return (char_count);
}
38 changes: 38 additions & 0 deletions ft_is_type_percent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_type_%.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vantunes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/21 14:12:11 by vantunes #+# #+# */
/* Updated: 2021/03/21 14:12:14 by vantunes ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

int ft_ispercent(t_flags *flags)
{
int char_count;

char_count = 0;
if (flags->minus == 1 || flags->dot >= 0)
flags->zero = 0;
if (flags->minus == 1)
{
char_count += ft_putchar('%');
char_count += ft_print_width(flags->width, 1, 0);
}
if (flags->minus == 0 && flags->zero == 0)
{
char_count += ft_print_width(flags->width, 1, 0);
char_count += ft_putchar('%');
}
if (flags->zero == 1)
{
char_count += ft_print_width(flags->width, 1, 1);
char_count += ft_putchar('%');
}
return (char_count);
}
82 changes: 82 additions & 0 deletions ft_is_type_x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_type_x.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vantunes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/22 15:34:25 by vantunes #+# #+# */
/* Updated: 2021/03/22 15:34:27 by vantunes ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

static int ft_isminus(t_flags *flags, char *str)
{
int char_count;

char_count = 0;
if (flags->dot > ft_strlen(str))
char_count += ft_print_width(flags->dot, ft_strlen(str), 1);
char_count += ft_print_prec(ft_strlen(str), str);
char_count += ft_print_width(flags->width, flags->dot, 0);
return (char_count);
}

static int ft_printhex(t_flags *flags, int lower, char *str)
{
int char_count;

char_count = 0;
if (lower == 1)
str = ft_tolower_str(str);
if ((flags->dot >= 0) || (flags->minus == 1))
flags->zero = 0;
if (flags->dot <= ft_strlen(str))
flags->dot = ft_strlen(str);
if (flags->minus == 1)
char_count += ft_isminus(flags, str);
if (flags->minus == 0 && flags->zero == 0)
{
char_count += ft_print_width(flags->width, flags->dot, 0);
if (flags->dot > ft_strlen(str))
char_count += ft_print_width(flags->dot, ft_strlen(str), 1);
char_count += ft_print_prec(ft_strlen(str), str);
}
if (flags->minus == 0 && flags->zero == 1)
{
char_count += ft_print_width(flags->width, ft_strlen(str), 1);
char_count += ft_print_prec(ft_strlen(str), str);
}
free(str);
return (char_count);
}

int ft_ishex(t_flags *flags, unsigned int hex, int lower)
{
char *str;
int char_count;

char_count = 0;
if (flags->dot == 0 && hex == 0)
return (char_count += ft_print_width(flags->width, 0, 0));
if (hex == 0 && flags->dot > 0 && flags->minus == 1)
{
char_count += ft_print_width(flags->dot, 0, 1);
char_count += ft_print_width(flags->width, flags->dot, 0);
return (char_count);
}
if (hex == 0 && flags->dot > 0 && flags->minus == 0)
{
char_count += ft_print_width(flags->width, flags->dot, 0);
char_count += ft_print_width(flags->dot, 0, 1);
return (char_count);
}
else
{
str = ft_itoa_ull(hex, 16);
char_count = ft_printhex(flags, lower, str);
}
return (char_count);
}
Loading

0 comments on commit 7f50419

Please sign in to comment.