fprintf
This commit is contained in:
@@ -79,6 +79,7 @@ int ft_put_ui(unsigned int n);
|
||||
void ft_error(char *e);
|
||||
|
||||
int ft_printf(const char *fstr, ...);
|
||||
int ft_fprintf(int fd, const char *str, ...);
|
||||
int ft_debug(const char *fstr, ...);
|
||||
|
||||
char *get_next_line(int fd);
|
||||
|
||||
89
lib/libft/srcs/fprintf/ft_flags.c
Normal file
89
lib/libft/srcs/fprintf/ft_flags.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_flags.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/29 12:38:28 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/04 13:45:40 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
t_flags init_flags(t_flags flags, int fd)
|
||||
{
|
||||
flags.spec = 0;
|
||||
flags.width = 0;
|
||||
flags.precision = -1;
|
||||
flags.left = 0;
|
||||
flags.zero = 0;
|
||||
flags.hash = 0;
|
||||
flags.plus = 0;
|
||||
flags.space = 0;
|
||||
flags.fd = fd;
|
||||
return (flags);
|
||||
}
|
||||
|
||||
t_flags flag_left(t_flags flags)
|
||||
{
|
||||
flags.left = 1;
|
||||
flags.zero = 0;
|
||||
return (flags);
|
||||
}
|
||||
|
||||
int flag_precision(const char *str, int i, t_flags *flags)
|
||||
{
|
||||
int j;
|
||||
int res;
|
||||
|
||||
res = 0;
|
||||
j = i + 1;
|
||||
while (str[j] >= '0' && str[j] <= '9')
|
||||
{
|
||||
res = (res * 10) + str[j] - '0';
|
||||
j++;
|
||||
}
|
||||
flags->precision = res;
|
||||
return (j);
|
||||
}
|
||||
|
||||
int flag_width(const char *str, int i, t_flags *flags)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = 0;
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
res = (res * 10) + str[i] - '0';
|
||||
i++;
|
||||
}
|
||||
flags->width = res;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_print_width(t_flags flags, int size, int zero, int *check)
|
||||
{
|
||||
int count;
|
||||
int tmp;
|
||||
int width;
|
||||
|
||||
width = flags.width;
|
||||
count = 0;
|
||||
while (width - size > 0)
|
||||
{
|
||||
if (zero == 1)
|
||||
tmp = ft_print_c('0', flags);
|
||||
else
|
||||
tmp = ft_print_c(' ', flags);
|
||||
if (tmp == -1)
|
||||
{
|
||||
*check = -1;
|
||||
return (-1);
|
||||
}
|
||||
count += tmp;
|
||||
width--;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
38
lib/libft/srcs/fprintf/ft_flags_utils.c
Normal file
38
lib/libft/srcs/fprintf/ft_flags_utils.c
Normal file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_flags_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/29 12:31:24 by lderidde #+# #+# */
|
||||
/* Updated: 2024/10/31 12:51:29 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
int ft_isflag(char c)
|
||||
{
|
||||
if (is_flag(c) || is_spec(c) || (c >= '0' && c <= '9'))
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
int is_flag(char c)
|
||||
{
|
||||
if (c == '-' || c == '0' || c == '.' || c == '#' || c == '+' || c == ' ')
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
int is_spec(char c)
|
||||
{
|
||||
if (c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i' || c == 'u'
|
||||
|| c == 'x' || c == 'X' || c == '%')
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
107
lib/libft/srcs/fprintf/ft_fprintf.c
Normal file
107
lib/libft/srcs/fprintf/ft_fprintf.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_fprintf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/28 09:53:00 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/04 13:45:38 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
static int ft_print_arg(char c, va_list args, t_flags flags, int *check)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
if (c == 'c')
|
||||
count = ft_print_char(va_arg(args, int), flags);
|
||||
else if (c == 's')
|
||||
count = ft_print_s(va_arg(args, char *), flags);
|
||||
else if (c == 'p')
|
||||
count = ft_print_ptr((unsigned long)va_arg(args, void *), flags);
|
||||
else if (c == 'd' || c == 'i')
|
||||
count = ft_print_int(va_arg(args, int), flags);
|
||||
else if (c == 'u')
|
||||
count = ft_print_us(va_arg(args, unsigned int), flags);
|
||||
else if (c == 'x')
|
||||
count = ft_print_hex((unsigned int)va_arg(args, int), 0, flags);
|
||||
else if (c == 'X')
|
||||
count = ft_print_hex((unsigned int)va_arg(args, int), 1, flags);
|
||||
else if (c == '%')
|
||||
count = ft_print_char('%', flags);
|
||||
if (count == -1)
|
||||
*check = -1;
|
||||
return (count);
|
||||
}
|
||||
|
||||
static int parse_flag(const char *str, int i, t_flags *flags)
|
||||
{
|
||||
while (str[++i] && ft_isflag(str[i]))
|
||||
{
|
||||
if (str[i] == '-')
|
||||
*flags = flag_left(*flags);
|
||||
if (str[i] == '0' && flags->left == 0)
|
||||
flags->zero = 1;
|
||||
if (str[i] == '.')
|
||||
i = flag_precision(str, i, flags);
|
||||
if (str[i] == '#')
|
||||
flags->hash = 1;
|
||||
if (str[i] == '+')
|
||||
flags->plus = 1;
|
||||
if (str[i] == ' ')
|
||||
flags->space = 1;
|
||||
if (str[i] > '0' && str[i] <= '9')
|
||||
i = flag_width(str, i, flags) - 1;
|
||||
if (is_spec(str[i]))
|
||||
{
|
||||
flags->spec = str[i];
|
||||
break ;
|
||||
}
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static int parse_print(int fd, const char *str, va_list args)
|
||||
{
|
||||
int count;
|
||||
int i;
|
||||
int tmp;
|
||||
t_flags flags;
|
||||
|
||||
i = -1;
|
||||
count = 0;
|
||||
tmp = 0;
|
||||
while (str[++i])
|
||||
{
|
||||
flags = init_flags(flags, fd);
|
||||
if (str[i] == '%' && str[i + 1] != '\0')
|
||||
{
|
||||
tmp = parse_flag(str, i, &flags);
|
||||
if (flags.spec != 0)
|
||||
i = tmp;
|
||||
if (str[i] && is_spec(str[i]) && flags.spec != 0)
|
||||
count += ft_print_arg(str[i], args, flags, &tmp);
|
||||
}
|
||||
else if (str[i] != '%')
|
||||
count += print_c((unsigned char)str[i], &tmp, flags);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_fprintf(int fd, const char *str, ...)
|
||||
{
|
||||
va_list args;
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
va_start(args, str);
|
||||
count = parse_print(fd, str, args);
|
||||
va_end(args);
|
||||
return (count);
|
||||
}
|
||||
72
lib/libft/srcs/fprintf/ft_fprintf.h
Normal file
72
lib/libft/srcs/fprintf/ft_fprintf.h
Normal file
@@ -0,0 +1,72 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_fprintf.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/28 09:52:54 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/05 08:37:23 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_PRINTF_H
|
||||
# define FT_PRINTF_H
|
||||
|
||||
# include "../../libft.h"
|
||||
# include <stdlib.h>
|
||||
# include <stdarg.h>
|
||||
# include <unistd.h>
|
||||
|
||||
typedef struct s_flags
|
||||
{
|
||||
int spec;
|
||||
int width;
|
||||
int precision;
|
||||
int left;
|
||||
int zero;
|
||||
int hash;
|
||||
int plus;
|
||||
int space;
|
||||
int fd;
|
||||
} t_flags;
|
||||
|
||||
// PRINTF
|
||||
|
||||
// PRINT ARGS
|
||||
int ft_print_char(unsigned char c, t_flags flags);
|
||||
int ft_print_c(unsigned char c, t_flags flags);
|
||||
int print_c(unsigned char c, int *check, t_flags flags);
|
||||
int ft_print_hex(unsigned int num, int uppercase, t_flags flags);
|
||||
int ft_print_int(int num, t_flags flags);
|
||||
int ft_print_us(unsigned int num, t_flags flags);
|
||||
int ft_print_s(char *str, t_flags flags);
|
||||
int ft_print_ptr(unsigned long add, t_flags flags);
|
||||
|
||||
// UTILS
|
||||
int hex_len(unsigned long num);
|
||||
int num_len(long n);
|
||||
int is_arg(char c);
|
||||
int get_size(int num, int len, t_flags flags);
|
||||
int get_ussize(unsigned int num, int len, t_flags flags);
|
||||
int get_hexsize(unsigned int nbr, int len, t_flags flags);
|
||||
size_t ft_strlen(const char *str);
|
||||
char *build_strn(char *str, long nbr, int len, t_flags flags);
|
||||
char *build_strusn(char *str, unsigned int nbr, int len);
|
||||
char *build_hexn(unsigned int nbr, char *hash, t_flags flags, char *base);
|
||||
void hex_hash(char *s, char *hash, int *i);
|
||||
void check_minus(char *str);
|
||||
|
||||
//FLAGS
|
||||
t_flags init_flags(t_flags flags, int fd);
|
||||
t_flags flag_left(t_flags flags);
|
||||
int flag_precision(const char *str, int i, t_flags *flags);
|
||||
int flag_width(const char *str, int i, t_flags *flags);
|
||||
int ft_print_width(t_flags flags, int size, int zero, int *check);
|
||||
|
||||
//FlAG UTILS
|
||||
int ft_isflag(char c);
|
||||
int is_flag(char c);
|
||||
int is_spec(char c);
|
||||
|
||||
#endif
|
||||
67
lib/libft/srcs/fprintf/ft_print_char.c
Normal file
67
lib/libft/srcs/fprintf/ft_print_char.c
Normal file
@@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_char.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/28 10:28:26 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/04 13:35:23 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
int print_c(unsigned char c, int *check, t_flags flags)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
tmp = write (flags.fd, &c, 1);
|
||||
if (tmp < 1)
|
||||
{
|
||||
*check = -1;
|
||||
return (-1);
|
||||
}
|
||||
else
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_print_c(unsigned char c, t_flags flags)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
tmp = write (flags.fd, &c, 1);
|
||||
if (tmp < 1)
|
||||
return (-1);
|
||||
else
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_print_char(unsigned char c, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
int check;
|
||||
|
||||
check = 0;
|
||||
count = 0;
|
||||
if (flags.left == 1)
|
||||
check = ft_print_c(c, flags);
|
||||
if (check == -1)
|
||||
return (-1);
|
||||
count += check;
|
||||
if (flags.zero == 1)
|
||||
count += ft_print_width(flags, 1, 1, &check);
|
||||
else if (flags.zero == 0)
|
||||
count += ft_print_width(flags, 1, 0, &check);
|
||||
if (flags.left == 0 && check != -1)
|
||||
{
|
||||
check = ft_print_c(c, flags);
|
||||
if (check == -1)
|
||||
return (-1);
|
||||
else
|
||||
count += check;
|
||||
}
|
||||
if (check == -1)
|
||||
return (-1);
|
||||
return (count);
|
||||
}
|
||||
74
lib/libft/srcs/fprintf/ft_print_hex.c
Normal file
74
lib/libft/srcs/fprintf/ft_print_hex.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_hex.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/28 11:15:38 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/05 08:32:57 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
void hex_hash(char *s, char *hash, int *i)
|
||||
{
|
||||
s[0] = hash[0];
|
||||
s[1] = hash[1];
|
||||
*i = 2;
|
||||
return ;
|
||||
}
|
||||
|
||||
static int display_hexnum(char *str, int *check, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
int len;
|
||||
|
||||
len = ft_strlen(str);
|
||||
count = 0;
|
||||
count = write(flags.fd, str, len);
|
||||
if (count < len)
|
||||
*check = -1;
|
||||
return (count);
|
||||
}
|
||||
|
||||
static int pnbr_base(unsigned long num, char *base, char *hash, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
char *strn;
|
||||
int check;
|
||||
int len;
|
||||
|
||||
count = 0;
|
||||
check = 0;
|
||||
len = hex_len(num);
|
||||
len = get_hexsize(num, len, flags);
|
||||
strn = build_hexn(num, hash, flags, base);
|
||||
if (!strn)
|
||||
return (-1);
|
||||
if (flags.left == 1)
|
||||
count = display_hexnum(strn, &check, flags);
|
||||
count += ft_print_width(flags, len, 0, &check);
|
||||
if (flags.left == 0)
|
||||
count += display_hexnum(strn, &check, flags);
|
||||
free(strn);
|
||||
if (check == -1)
|
||||
return (-1);
|
||||
else
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_print_hex(unsigned int num, int uppercase, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
unsigned long nbr;
|
||||
|
||||
nbr = num;
|
||||
count = 0;
|
||||
if (uppercase)
|
||||
count = pnbr_base(nbr, "0123456789ABCDEF", "0X", flags);
|
||||
else
|
||||
count = pnbr_base(nbr, "0123456789abcdef", "0x", flags);
|
||||
return (count);
|
||||
}
|
||||
106
lib/libft/srcs/fprintf/ft_print_hex_utils.c
Normal file
106
lib/libft/srcs/fprintf/ft_print_hex_utils.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_hex_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/30 15:32:29 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/05 08:33:09 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
static int get_hexsizer(int len, t_flags flags)
|
||||
{
|
||||
if (flags.precision <= len && flags.zero == 1)
|
||||
return (flags.width);
|
||||
if (flags.precision <= len && flags.zero == 0)
|
||||
return (len);
|
||||
if (flags.precision >= len)
|
||||
{
|
||||
if (flags.hash == 1)
|
||||
return (flags.precision + 2);
|
||||
return (flags.precision);
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
static int get_hexsizel(int len, t_flags flags)
|
||||
{
|
||||
if (flags.precision <= len)
|
||||
return (len);
|
||||
if (flags.precision >= len)
|
||||
{
|
||||
if (flags.hash == 1)
|
||||
return (flags.precision + 2);
|
||||
return (flags.precision);
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
int get_hexsize(unsigned int nbr, int len, t_flags flags)
|
||||
{
|
||||
if (flags.precision == 0 && nbr == 0)
|
||||
return (0);
|
||||
if (flags.precision > len)
|
||||
len = flags.precision;
|
||||
if (flags.hash == 1 && nbr != 0)
|
||||
len += 2;
|
||||
if (flags.precision != -1 && len >= flags.precision)
|
||||
return (len);
|
||||
if (len > flags.width && len >= flags.precision)
|
||||
return (len);
|
||||
else
|
||||
{
|
||||
if (flags.left == 1)
|
||||
len = get_hexsizel(len, flags);
|
||||
if (flags.left == 0)
|
||||
len = get_hexsizer(len, flags);
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
static void puthexnbr_str(char *str, unsigned long nbr, int *i, char *base)
|
||||
{
|
||||
if (nbr > 15)
|
||||
{
|
||||
puthexnbr_str(str, nbr / 16, i, base);
|
||||
puthexnbr_str(str, nbr % 16, i, base);
|
||||
}
|
||||
else
|
||||
{
|
||||
str[*i] = base[nbr];
|
||||
*i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
char *build_hexn(unsigned int nbr, char *hash, t_flags flags, char *base)
|
||||
{
|
||||
int i;
|
||||
int len;
|
||||
int tmp;
|
||||
char *strn;
|
||||
|
||||
tmp = hex_len(nbr);
|
||||
len = get_hexsize(nbr, tmp, flags);
|
||||
strn = malloc(len + 1);
|
||||
if (!strn)
|
||||
return (NULL);
|
||||
strn[len] = '\0';
|
||||
if (len == 0)
|
||||
return (strn);
|
||||
i = 0;
|
||||
if (flags.hash == 1 && nbr != 0)
|
||||
hex_hash(strn, hash, &i);
|
||||
if (flags.hash == 1 && flags.precision < tmp && nbr != 0)
|
||||
tmp += 2;
|
||||
if (flags.precision == -1 && flags.zero == 1)
|
||||
flags.precision = flags.width;
|
||||
while (flags.precision-- > tmp)
|
||||
strn[i++] = '0';
|
||||
puthexnbr_str(strn, nbr, &i, base);
|
||||
strn[i] = '\0';
|
||||
return (strn);
|
||||
}
|
||||
75
lib/libft/srcs/fprintf/ft_print_int.c
Normal file
75
lib/libft/srcs/fprintf/ft_print_int.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_int.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/28 11:04:33 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/04 13:26:44 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
void check_minus(char *str)
|
||||
{
|
||||
if (str[0] == '-')
|
||||
str[0] = 'a';
|
||||
}
|
||||
|
||||
static char *ft_itoa_num(int num, t_flags flags)
|
||||
{
|
||||
int len;
|
||||
char *str;
|
||||
long nbr;
|
||||
|
||||
nbr = num;
|
||||
len = num_len(nbr);
|
||||
len = get_size(num, len, flags);
|
||||
str = malloc(len + 1);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[len] = '\0';
|
||||
str = build_strn(str, nbr, len, flags);
|
||||
return (str);
|
||||
}
|
||||
|
||||
static int display_int(char *str, int *check, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
int len;
|
||||
|
||||
len = ft_strlen(str);
|
||||
count = 0;
|
||||
count = write(flags.fd, str, len);
|
||||
if (count < len)
|
||||
{
|
||||
*check = -1;
|
||||
return (-1);
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_print_int(int num, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
char *strn;
|
||||
int check;
|
||||
|
||||
check = 0;
|
||||
count = 0;
|
||||
strn = ft_itoa_num(num, flags);
|
||||
if (!strn)
|
||||
return (-1);
|
||||
if (flags.left == 1)
|
||||
count = display_int(strn, &check, flags);
|
||||
count += ft_print_width(flags, ft_strlen(strn), 0, &check);
|
||||
if (flags.left == 0)
|
||||
count += display_int(strn, &check, flags);
|
||||
free(strn);
|
||||
if (check == -1)
|
||||
return (-1);
|
||||
else
|
||||
return (count);
|
||||
}
|
||||
118
lib/libft/srcs/fprintf/ft_print_int_utils.c
Normal file
118
lib/libft/srcs/fprintf/ft_print_int_utils.c
Normal file
@@ -0,0 +1,118 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_int_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/30 12:01:33 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/04 13:33:48 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
static int get_sizer(int num, int len, t_flags flags)
|
||||
{
|
||||
int nbr;
|
||||
|
||||
nbr = num;
|
||||
if (flags.precision < len && flags.zero == 1)
|
||||
return (flags.width);
|
||||
if (flags.precision < len && flags.zero == 0)
|
||||
return (len);
|
||||
if (flags.precision >= len)
|
||||
{
|
||||
if (((flags.space == 1 || flags.plus == 1) && nbr >= 0) || nbr < 0)
|
||||
return (flags.precision + 1);
|
||||
else
|
||||
return (flags.precision);
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
static int get_sizel(int num, int len, t_flags flags)
|
||||
{
|
||||
int nbr;
|
||||
|
||||
nbr = num;
|
||||
if (flags.precision < len)
|
||||
return (len);
|
||||
if (flags.precision >= len)
|
||||
{
|
||||
if (((flags.space == 1 || flags.plus == 1) && nbr >= 0) || nbr < 0)
|
||||
return (flags.precision + 1);
|
||||
else
|
||||
return (flags.precision);
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
int get_size(int num, int len, t_flags flags)
|
||||
{
|
||||
if (flags.precision == 0 && num == 0)
|
||||
{
|
||||
if (flags.plus == 1 || flags.space == 1)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
if ((flags.space == 1 || flags.plus == 1) && num >= 0)
|
||||
len += 1;
|
||||
if (flags.precision != -1 && len > flags.precision)
|
||||
return (len);
|
||||
if (len > flags.width && len > flags.precision)
|
||||
return (len);
|
||||
if (num < 0 && flags.precision == len)
|
||||
return (len + 1);
|
||||
else
|
||||
{
|
||||
if (flags.left == 1)
|
||||
len = get_sizel(num, len, flags);
|
||||
if (flags.left == 0)
|
||||
len = get_sizer(num, len, flags);
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
static void putnbr_str(char *str, long nbr, int *i, int len)
|
||||
{
|
||||
if (nbr > 9)
|
||||
{
|
||||
putnbr_str(str, nbr % 10, i, len);
|
||||
putnbr_str(str, nbr / 10, i, len);
|
||||
}
|
||||
else
|
||||
{
|
||||
str[len - 1 - *i] = nbr + '0';
|
||||
*i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
char *build_strn(char *str, long nbr, int len, t_flags flags)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
check_minus(str);
|
||||
if (nbr < 0)
|
||||
{
|
||||
str[0] = '-';
|
||||
nbr *= -1;
|
||||
}
|
||||
if (len - i == 0)
|
||||
return (str);
|
||||
putnbr_str(str, nbr, &i, len);
|
||||
if (len - i == 0 && (flags.space == 0 && flags.plus == 0))
|
||||
return (str);
|
||||
while (len - i - 1 > 0)
|
||||
str[len - i++ - 1] = '0';
|
||||
if (len - i > 0 && str[len - i -1] == '-')
|
||||
return (str);
|
||||
if (nbr >= 0 && flags.plus == 1)
|
||||
str[0] = '+';
|
||||
else if (nbr >= 0 && flags.space == 1)
|
||||
str[0] = ' ';
|
||||
else
|
||||
str[0] = '0';
|
||||
return (str);
|
||||
}
|
||||
81
lib/libft/srcs/fprintf/ft_print_p.c
Normal file
81
lib/libft/srcs/fprintf/ft_print_p.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_p.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/28 12:10:55 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/05 08:38:38 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
static void print_p(unsigned long add, int *count, int *check, t_flags flags)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
if (add >= 16)
|
||||
{
|
||||
print_p(add / 16, *(&count), *(&check), flags);
|
||||
print_p(add % 16, *(&count), *(&check), flags);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (add < 10)
|
||||
tmp = ft_print_c(add + '0', flags);
|
||||
else
|
||||
tmp = ft_print_c((add - 10) + 'a', flags);
|
||||
if (tmp == -1)
|
||||
{
|
||||
*check = -1;
|
||||
return ;
|
||||
}
|
||||
*count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int ft_print_p(unsigned long add, int *check2, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
int check;
|
||||
|
||||
check = 0;
|
||||
count = 0;
|
||||
count += ft_fprintf(flags.fd, "0x");
|
||||
if (count == -1)
|
||||
return (-1);
|
||||
print_p(add, &count, &check, flags);
|
||||
if (check == -1)
|
||||
{
|
||||
*check2 = -1;
|
||||
return (-1);
|
||||
}
|
||||
else
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_print_ptr(unsigned long add, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
int check;
|
||||
|
||||
check = 0;
|
||||
count = 0;
|
||||
if (flags.left == 1)
|
||||
count += ft_print_p(add, &check, flags);
|
||||
if (flags.width != 0)
|
||||
{
|
||||
if (add == 0)
|
||||
count += ft_print_width(flags, 3, 0, &check);
|
||||
else
|
||||
count += ft_print_width(flags, hex_len(add) + 2, 0, &check);
|
||||
}
|
||||
if (flags.left == 0)
|
||||
count += ft_print_p(add, &check, flags);
|
||||
if (check == -1)
|
||||
return (-1);
|
||||
count += check;
|
||||
return (count);
|
||||
}
|
||||
64
lib/libft/srcs/fprintf/ft_print_str.c
Normal file
64
lib/libft/srcs/fprintf/ft_print_str.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_str.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/28 11:00:06 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/04 08:42:43 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
int print_string(char *str, t_flags flags, int *check)
|
||||
{
|
||||
int count;
|
||||
int i;
|
||||
int tmp;
|
||||
|
||||
tmp = 0;
|
||||
i = 0;
|
||||
count = 0;
|
||||
if (flags.precision == -1)
|
||||
flags.precision = ft_strlen(str);
|
||||
while (str[i] && i < flags.precision)
|
||||
{
|
||||
tmp = ft_print_c(str[i], flags);
|
||||
if (tmp == -1)
|
||||
{
|
||||
*check = -1;
|
||||
return (-1);
|
||||
}
|
||||
count += tmp;
|
||||
i++;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_print_s(char *str, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
int check;
|
||||
int len;
|
||||
|
||||
check = 0;
|
||||
count = 0;
|
||||
if (!str)
|
||||
str = "(null)";
|
||||
len = ft_strlen(str);
|
||||
if (flags.precision > 0 && flags.precision > len)
|
||||
flags.precision = len;
|
||||
if (flags.left == 1)
|
||||
count += print_string(str, flags, &check);
|
||||
if (flags.precision >= 0)
|
||||
count += ft_print_width(flags, flags.precision, 0, &check);
|
||||
else
|
||||
count += ft_print_width(flags, len, 0, &check);
|
||||
if (flags.left == 0)
|
||||
count += print_string(str, flags, &check);
|
||||
if (check == -1)
|
||||
return (-1);
|
||||
return (count);
|
||||
}
|
||||
69
lib/libft/srcs/fprintf/ft_print_us.c
Normal file
69
lib/libft/srcs/fprintf/ft_print_us.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_us.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/30 10:38:55 by lderidde #+# #+# */
|
||||
/* Updated: 2024/10/31 15:06:23 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
static char *ft_itoa_usnum(unsigned int num, t_flags flags)
|
||||
{
|
||||
int len;
|
||||
char *str;
|
||||
long nbr;
|
||||
|
||||
nbr = num;
|
||||
len = num_len(nbr);
|
||||
len = get_ussize(num, len, flags);
|
||||
str = malloc(len + 1);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[len] = 0;
|
||||
str = build_strusn(str, num, len);
|
||||
return (str);
|
||||
}
|
||||
|
||||
static int display_usint(char *str, int *check, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
int len;
|
||||
|
||||
len = ft_strlen(str);
|
||||
count = 0;
|
||||
count = write(flags.fd, str, len);
|
||||
if (count < len)
|
||||
{
|
||||
*check = -1;
|
||||
return (-1);
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_print_us(unsigned int num, t_flags flags)
|
||||
{
|
||||
int count;
|
||||
char *strn;
|
||||
int check;
|
||||
|
||||
check = 0;
|
||||
count = 0;
|
||||
strn = ft_itoa_usnum(num, flags);
|
||||
if (!strn)
|
||||
return (-1);
|
||||
if (flags.left == 1)
|
||||
count = display_usint(strn, &check, flags);
|
||||
count += ft_print_width(flags, ft_strlen(strn), 0, &check);
|
||||
if (flags.left == 0)
|
||||
count += display_usint(strn, &check, flags);
|
||||
free(strn);
|
||||
if (check == -1)
|
||||
return (-1);
|
||||
else
|
||||
return (count);
|
||||
}
|
||||
81
lib/libft/srcs/fprintf/ft_print_us_utils.c
Normal file
81
lib/libft/srcs/fprintf/ft_print_us_utils.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_us_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/30 14:14:47 by lderidde #+# #+# */
|
||||
/* Updated: 2024/10/31 15:03:30 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
static int get_ussizer(int len, t_flags flags)
|
||||
{
|
||||
if (flags.precision < len && flags.zero == 1)
|
||||
return (flags.width);
|
||||
if (flags.precision < len && flags.zero == 0)
|
||||
return (len);
|
||||
if (flags.precision > len)
|
||||
return (flags.precision);
|
||||
return (len);
|
||||
}
|
||||
|
||||
static int get_ussizel(int len, t_flags flags)
|
||||
{
|
||||
if (flags.precision < len)
|
||||
return (len);
|
||||
if (flags.precision > len)
|
||||
return (flags.precision);
|
||||
return (len);
|
||||
}
|
||||
|
||||
int get_ussize(unsigned int num, int len, t_flags flags)
|
||||
{
|
||||
if (flags.precision == 0 && num == 0)
|
||||
return (0);
|
||||
if (flags.precision != -1 && len > flags.precision)
|
||||
return (len);
|
||||
if (len > flags.width && len > flags.precision)
|
||||
return (len);
|
||||
else
|
||||
{
|
||||
if (flags.left == 1)
|
||||
len = get_ussizel(len, flags);
|
||||
if (flags.left == 0)
|
||||
len = get_ussizer(len, flags);
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
static void putusnbr_str(char *str, unsigned int nbr, int *i, int len)
|
||||
{
|
||||
if (nbr > 9)
|
||||
{
|
||||
putusnbr_str(str, nbr % 10, i, len);
|
||||
putusnbr_str(str, nbr / 10, i, len);
|
||||
}
|
||||
else
|
||||
{
|
||||
str[len - 1 - *i] = nbr + '0';
|
||||
*i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
char *build_strusn(char *str, unsigned int nbr, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (len - i == 0)
|
||||
return (str);
|
||||
putusnbr_str(str, nbr, &i, len);
|
||||
if (len - i == 0)
|
||||
return (str);
|
||||
while (len - i - 1 > 0)
|
||||
str[len - i++ - 1] = '0';
|
||||
str[0] = '0';
|
||||
return (str);
|
||||
}
|
||||
46
lib/libft/srcs/fprintf/ft_printf_utils.c
Normal file
46
lib/libft/srcs/fprintf/ft_printf_utils.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/28 10:42:48 by lderidde #+# #+# */
|
||||
/* Updated: 2024/11/04 09:23:14 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_fprintf.h"
|
||||
|
||||
int num_len(long n)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (n < 0)
|
||||
{
|
||||
n *= -1;
|
||||
i++;
|
||||
}
|
||||
while (n > 9)
|
||||
{
|
||||
n /= 10;
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int hex_len(unsigned long num)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (num > 15)
|
||||
{
|
||||
num /= 16;
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
Reference in New Issue
Block a user