This commit is contained in:
Jeremy FLEURY
2019-07-17 11:22:24 +02:00
commit 748d10f4f3
174 changed files with 8953 additions and 0 deletions

81
libft/printf/Makefile Normal file
View File

@@ -0,0 +1,81 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/12/14 16:59:10 by igarbuz #+# #+# #
# Updated: 2019/07/17 11:14:14 by jfleury ### ########.fr #
# #
# **************************************************************************** #
NAME = libftprintf.a
SRC = buf_cnt.c \
ft_argcast.c \
ft_arglen_f.c \
ft_arglen_oct.c \
ft_arglen.c \
ft_buf_print_db.c \
ft_buf_print_ldb.c \
ft_error.c \
ft_exp_dec.c \
ft_init_param.c \
ft_nd_div.c \
ft_nd_long_div.c \
ft_nd_long_mul.c \
ft_nd_long_round.c \
ft_nd_mul.c \
ft_nd_round.c \
ft_null.c \
ft_parse.c \
ft_printf_db.c \
ft_printf_ldb.c \
ft_printf.c \
ft_unicode.c \
libft_printf.c \
pow10.c \
print_buf_bit_float.c \
print_buf_num.c \
print_buf_prefix.c \
print_buf_unicode.c \
print_buf.c \
print_cs_pad.c \
print_num_pad.c
HEAD = ft_printf.h
OBJ = $(SRC:.c=.o)
CC = gcc
CFLAGS = -Wall -Werror -Wextra -O3
CPPFLAGS = -I .
all: $(NAME)
$(NAME): $(OBJ)
ar rcs $(NAME) $(OBJ) *.o
ranlib $(NAME)
%.o : %.c $(HEAD)
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
norm:
norminette $(SRC) $(HEAD) | grep -B1 "Error*"
clean:
rm -f $(OBJ)
fclean: clean
rm -f $(NAME)
debug: CFLAGS += -g
debug: re
re: fclean all
.PHONY: all clean fclean re norm debug
.SILENT: $(OBJ) $(NAME) all clean fclean re

13
libft/printf/buf_cnt.c Normal file
View File

@@ -0,0 +1,13 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* buf_cnt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:03:33 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:03:39 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
int g_bcn = 0;

66
libft/printf/ft_argcast.c Normal file
View File

@@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_argcast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:03:50 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:03:56 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static intmax_t ft_arg_cast_c(va_list valist)
{
return (va_arg(valist, int));
}
static intmax_t ft_arg_cast_s(va_list valist)
{
return (va_arg(valist, intptr_t));
}
static intmax_t ft_arg_cast_i(va_list valist, int p)
{
if (p >> 8 & 1)
return (va_arg(valist, long long int));
else if (p >> 7 & 1)
return (va_arg(valist, long int));
else if (p >> 6 & 1)
return ((short int)va_arg(valist, int));
else if (p >> 5 & 1)
return ((signed char)va_arg(valist, int));
else
return (va_arg(valist, int));
}
static intmax_t ft_arg_cast_u(va_list valist, int p)
{
if (p >> 8 & 1)
return (va_arg(valist, unsigned long long int));
else if (p >> 7 & 1)
return (va_arg(valist, unsigned long int));
else if (p >> 6 & 1)
return ((unsigned short int)va_arg(valist, int));
else if (p >> 5 & 1)
return ((unsigned char)va_arg(valist, int));
else
return (va_arg(valist, unsigned int));
}
intmax_t ft_arg_cast(va_list valist, int p, const char c)
{
if (c == 'c')
return (ft_arg_cast_c(valist));
else if (c == 's')
return (ft_arg_cast_s(valist));
else if (c == 'd' || c == 'i')
return (ft_arg_cast_i(valist, p));
else if (c == 'u' || c == 'o' || c == 'x' || c == 'X')
return (ft_arg_cast_u(valist, p));
else if (c == 'p')
return (ft_arg_cast_u(valist, 0x100));
return (0);
}

74
libft/printf/ft_arglen.c Normal file
View File

@@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arglen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:04:12 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:04:16 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_unbrlen(uintmax_t n)
{
int i;
i = 1;
while (n && n > 9)
{
n = n / 10;
i++;
}
return (i);
}
static int ft_snbrlen(intmax_t n)
{
if (n < 0)
return (ft_unbrlen(-n));
return (ft_unbrlen(n));
}
static int ft_hexlen(uintmax_t x)
{
int i;
i = 1;
while (x > 15)
{
x = (x >> 4);
i++;
}
return (i);
}
/*
** For string length, if an argument is an integer Zero, "(null)" is printed
** that corresponds to below returned precision of 6;
*/
int ft_arg_len(intmax_t arg, char c, t_param *prm)
{
if (c == 'c' || c == '%')
return (1);
else if (!arg && !prm->prc && c != 'o')
return (0);
else if (c == 'd' || c == 'i')
return (ft_snbrlen((intmax_t)arg));
else if (c == 'u')
return (ft_unbrlen((uintmax_t)arg));
else if (c == 'o')
return (ft_arg_len_oct(arg, prm));
else if (c == 'x' || c == 'X')
return (ft_hexlen((uintmax_t)arg));
else if (c == 'p')
return (ft_hexlen((uintmax_t)arg));
else if (c == 's' && prm->p >> 7 & 1)
return (ft_strlen_unicode((wchar_t *)arg));
else if (c == 's')
return (ft_strlen_printf((char *)arg));
return (0);
}

View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arglen_f.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:04:25 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:04:30 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_fract_len(const char *f)
{
const char *t;
t = f;
while (*t && *t != '.')
t++;
f = t + 1;
while (*t)
t++;
return (t - f);
}
int ft_arg_db_len(const char *s, int p, double *arg)
{
int len;
uintmax_t *ptr;
ptr = (uintmax_t *)arg;
len = ft_strlen_printf(s);
if ((p & 1) || (p >> 3 & 1) || (*ptr >> 63))
len += 1;
return (len);
}
int ft_arg_long_db_len(const char *s, int p, long double *arg)
{
int len;
uintmax_t *ptr;
ptr = (uintmax_t *)arg;
len = ft_strlen_printf(s);
if ((p & 1) || (p >> 3 & 1) || (ptr[1] >> 15) & 1)
len += 1;
return (len);
}
int ft_arg_flen(intmax_t arg, char c, int p, int len)
{
if ((c == 'i' || c == 'd')
&& (arg < 0 || (arg >= 0 && (p & 1 || p >> 3 & 1))))
return (len + 1);
if ((c == 'x' || c == 'X') && (p >> 2 & 1) && arg)
return (len + 2);
else if (c == 'p')
return (len + 2);
return (len);
}

View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arglen_oct.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 19:52:21 by igarbuz #+# #+# */
/* Updated: 2019/01/29 19:52:25 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_octlen(uintmax_t o)
{
int i;
i = 1;
while (o > 7)
{
o = (o >> 3);
i++;
}
return (i);
}
int ft_arg_len_oct(intmax_t arg, t_param *prm)
{
if (arg && prm->prc)
return (ft_octlen((uintmax_t)arg) + (prm->p >> 2 & 1));
else if (!arg && !prm->prc && !(prm->p >> 2 & 1))
return (0);
return (ft_octlen((uintmax_t)arg));
}

View File

@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_buf_print_db.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:37:10 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:37:15 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_sprint(int up, char **p, uint32_t u, int r)
{
while (up >= r)
{
**p = (u / g_pow10[up]) % 10 + 48U;
(*p)++;
up--;
}
}
static void ft_print_hi(t_nd *nd, char **p)
{
ft_sprint(ft_exp_dec(nd->n[nd->hi]), p, nd->n[nd->hi & 127], 0);
while (--(nd->hi) >= 0)
ft_sprint(8, p, nd->n[nd->hi & 127], 0);
}
static void ft_print_lo(t_nd *nd, char **p, int r)
{
int rloc;
rloc = r % 9;
while (nd->hi > nd->lo || (!rloc && nd->hi >= nd->lo))
{
ft_sprint(8, p, nd->n[nd->hi & 127], 0);
nd->hi--;
}
if (rloc && -nd->lo * 9 >= r)
ft_sprint(8, p, nd->n[nd->hi & 127], 9 - rloc);
}
char *ft_buf_print_db(t_nd *nd, int r, char *tbl)
{
char *p;
p = tbl;
if (nd->hi && !nd->n[nd->hi])
nd->hi--;
ft_print_hi(nd, &p);
*p++ = '.';
ft_print_lo(nd, &p, r);
*p = '\0';
return (tbl);
}

View File

@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_buf_print_ldb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:08:18 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:08:21 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_sprint(int up, char **p, uint32_t u, int r)
{
while (up >= r)
{
**p = (u / g_pow10[up]) % 10 + 48U;
(*p)++;
up--;
}
}
static void ft_print_hi(t_nd_long *nd, char **p)
{
ft_sprint(ft_exp_dec(nd->n[nd->hi]), p, nd->n[nd->hi & 2047], 0);
while (--(nd->hi) >= 0)
ft_sprint(8, p, nd->n[nd->hi & 2047], 0);
}
static void ft_print_lo(t_nd_long *nd, char **p, int r)
{
int rloc;
rloc = r % 9;
while (nd->hi > nd->lo || (!rloc && nd->hi >= nd->lo))
{
ft_sprint(8, p, nd->n[nd->hi & 2047], 0);
nd->hi--;
}
if (rloc && -nd->lo * 9 >= r)
ft_sprint(8, p, nd->n[nd->hi & 2047], 9 - rloc);
}
char *ft_buf_print_ldb(t_nd_long *nd, int r, char *tbl)
{
char *p;
p = tbl;
while (nd->hi && !nd->n[nd->hi])
nd->hi--;
ft_print_hi(nd, &p);
*p++ = '.';
ft_print_lo(nd, &p, r);
*p = '\0';
return (tbl);
}

25
libft/printf/ft_error.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pf_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:04:44 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:04:48 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void pf_error(int er)
{
if (er < 0)
{
if (er == -1)
;
exit(EXIT_FAILURE);
}
else
write(1, "usage : pf_error(int er), er < 0\n", 33);
}

27
libft/printf/ft_exp_dec.c Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_exp_dec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:04:55 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:04:59 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
int ft_exp_dec(unsigned int value)
{
int exponent;
exponent = 0;
if (value >= 10000 && (value /= 10000))
exponent += 4;
if (value >= 1000 && (value /= 1000))
exponent += 3;
if (value >= 100 && (value /= 100))
exponent += 2;
if (value >= 10 && (value /= 10))
exponent += 1;
return (exponent);
}

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_init_param.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:05:18 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:05:20 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_init_param(t_param *prm)
{
prm->p = 0;
prm->prc = -1;
prm->wdt = -1;
}
void ft_init_nd(t_nd *nd)
{
nd->hi = 0;
nd->lo = 0;
}

78
libft/printf/ft_nd_div.c Normal file
View File

@@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nd_div.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:15:58 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:16:02 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
** carry is a (value / 2^9) * 10^10 / 2^9, where
** value is divided by 512 and multiplied by 1953125, whick is the maximal
** decimal 9-digit decimal times 512;
*/
static void ft_nd_div2p_hi(t_nd *nd, uint32_t *carry, uint32_t *i)
{
uint32_t val;
while (1)
{
val = nd->n[*i];
nd->n[*i] = (val >> 9) + *carry;
*carry = (val & 0x1ff) * 1953125;
if (*i == (nd->lo & 127))
break ;
*i = (*i - 1) & 127;
}
}
static void ft_nd_div2p_last(t_nd *nd, uint32_t *carry,
uint32_t *i, uint32_t p)
{
uint32_t val;
uint32_t mask;
uint32_t mul;
mask = (1U << p) - 1;
mul = 1000000000 >> p;
while (1)
{
val = nd->n[*i];
nd->n[*i] = (val >> p) + *carry;
*carry = (val & mask) * mul;
if (*i == (nd->lo & 127))
break ;
*i = (*i - 1) & 127;
}
}
void ft_nd_div2p(t_nd *nd, uint32_t p)
{
uint32_t carry;
uint32_t i;
while (p >= 9)
{
carry = 0;
i = nd->hi & 127;
ft_nd_div2p_hi(nd, &carry, &i);
if (carry)
nd->n[--(nd->lo) & 127] = carry;
p -= 9;
}
if (p)
{
carry = 0;
i = nd->hi & 127;
ft_nd_div2p_last(nd, &carry, &i, p);
if (carry)
nd->n[--(nd->lo) & 127] = carry;
}
}

View File

@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nd_long_div.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:21:44 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:21:48 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_nd_div2p_hi(t_nd_long *nd, uint32_t *carry, uint32_t *i)
{
uint32_t val;
while (1)
{
val = nd->n[*i];
nd->n[*i] = (val >> 9) + *carry;
*carry = (val & 0x1ff) * 1953125;
if (*i == (nd->lo & 2047))
break ;
*i = (*i - 1) & 2047;
}
}
static void ft_nd_div2p_last(t_nd_long *nd, uint32_t *carry,
uint32_t *i, uint32_t p)
{
uint32_t val;
uint32_t mask;
uint32_t mul;
mask = (1U << p) - 1;
mul = 1000000000 >> p;
while (1)
{
val = nd->n[*i];
nd->n[*i] = (val >> p) + *carry;
*carry = (val & mask) * mul;
if (*i == (nd->lo & 2047))
break ;
*i = (*i - 1) & 2047;
}
}
void ft_nd_long_div2p(t_nd_long *nd, uint32_t p)
{
uint32_t carry;
uint32_t i;
while (p >= 9)
{
carry = 0;
i = nd->hi & 2047;
ft_nd_div2p_hi(nd, &carry, &i);
if (carry)
nd->n[--(nd->lo) & 2047] = carry;
p -= 9;
}
if (p)
{
carry = 0;
i = nd->hi & 2047;
ft_nd_div2p_last(nd, &carry, &i, p);
if (carry)
nd->n[--(nd->lo) & 2047] = carry;
}
}

View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nd_long_mul.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:23:07 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:23:09 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_nd_mul2p_main(t_nd_long *nd, uint32_t *carry, uint32_t *i)
{
uint64_t val;
while (++(*i) <= (uint32_t)nd->hi)
{
val = ((uint64_t)nd->n[*i] << 29) | *carry;
*carry = (uint32_t)(val / 1000000000);
nd->n[*i] = (uint32_t)val - *carry * 1000000000;
}
}
static void ft_nd_mul2p_last(t_nd_long *nd, uint32_t *carry,
uint32_t *i, uint32_t p)
{
uint64_t val;
while (++(*i) <= (uint32_t)nd->hi)
{
val = ((uint64_t)nd->n[*i] << p) | *carry;
*carry = (uint32_t)(val / 1000000000);
nd->n[*i] = (uint32_t)val - *carry * 1000000000;
}
}
void ft_nd_long_mul2p(t_nd_long *nd, uint32_t p, uint32_t carry)
{
uint32_t i;
while (p >= 29)
{
i = -1;
ft_nd_mul2p_main(nd, &carry, &i);
if (carry)
{
nd->n[++(nd->hi)] = carry;
carry = 0;
}
p -= 29;
}
if (p)
{
i = -1;
ft_nd_mul2p_last(nd, &carry, &i, p);
if (carry)
nd->n[++(nd->hi)] = carry;
}
}

View File

@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nd_long_round.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:25:42 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:25:45 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_round_up(uint32_t *nd, int ndlo)
{
while (nd[ndlo & 2047] >= 1000000000)
{
ndlo++;
nd[ndlo & 2047] += 1;
}
}
static void ft_round_ndlo(int r, int rloc, int *ndlo)
{
if (!r)
*ndlo = 0;
else if (!rloc && -*ndlo * 9 >= r)
*ndlo = -r / 9;
else if (rloc && -*ndlo * 9 >= r)
*ndlo = -r / 9 - 1;
}
static int ft_round_last(int ndlo, int r, uint32_t *nd)
{
int rloc;
int tlo;
tlo = ndlo;
rloc = (r % 9);
ft_round_ndlo(r, rloc, &ndlo);
if (((r / 9) + 1) == -tlo && !(nd[tlo & 2047] % g_pow10[8 - rloc]))
{
if (rloc && (nd[ndlo & 2047] / g_pow10[9 - rloc] % 10) & 1
&& (nd[ndlo & 2047] / g_pow10[8 - rloc]) % 10 == 5)
nd[ndlo & 2047] += g_pow10[9 - rloc];
else if (!rloc && (tlo <= ndlo - 1) && (nd[ndlo & 2047] % 10) & 1
&& nd[(ndlo - 1) & 2047] / g_pow10[8] == 5)
nd[ndlo & 2047] += 1;
}
else
{
if (rloc && (nd[ndlo & 2047] / g_pow10[8 - rloc]) % 10 >= 5)
nd[ndlo & 2047] += g_pow10[9 - rloc];
else if (!rloc && (tlo <= ndlo - 1)
&& nd[(ndlo - 1) & 2047] / g_pow10[8] >= 5)
nd[ndlo & 2047] += 1;
}
return (ndlo);
}
void ft_long_round(t_nd_long *nd, int *r)
{
if (*r > -nd->lo * 9)
*r = -nd->lo * 9;
nd->lo = ft_round_last(nd->lo, *r, nd->n);
ft_round_up(nd->n, nd->lo);
}

62
libft/printf/ft_nd_mul.c Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nd_mul.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:11:14 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:11:17 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_nd_mul2p_main(t_nd *nd, uint32_t *carry, uint32_t *i)
{
uint64_t val;
while (++(*i) <= (uint32_t)nd->hi)
{
val = ((uint64_t)nd->n[*i] << 29) | *carry;
*carry = (uint32_t)(val / 1000000000);
nd->n[*i] = (uint32_t)val - *carry * 1000000000;
}
}
static void ft_nd_mul2p_last(t_nd *nd, uint32_t *carry,
uint32_t *i, uint32_t p)
{
uint64_t val;
while (++(*i) <= (uint32_t)nd->hi)
{
val = ((uint64_t)nd->n[*i] << p) | *carry;
*carry = (uint32_t)(val / 1000000000);
nd->n[*i] = (uint32_t)val - *carry * 1000000000;
}
}
void ft_nd_mul2p(t_nd *nd, uint32_t p, uint32_t carry)
{
uint32_t i;
while (p >= 29)
{
i = -1;
ft_nd_mul2p_main(nd, &carry, &i);
if (carry)
{
nd->n[++(nd->hi)] = carry;
carry = 0;
}
p -= 29;
}
if (p)
{
i = -1;
ft_nd_mul2p_last(nd, &carry, &i, p);
if (carry)
nd->n[++(nd->hi)] = carry;
}
}

View File

@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nd_round.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:07:19 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:07:22 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_round_up(uint32_t *nd, int ndlo)
{
while (nd[ndlo & 127] >= 1000000000)
{
ndlo++;
nd[ndlo & 127] += 1;
}
}
static void ft_round_ndlo(int r, int rloc, int *ndlo)
{
if (!r)
*ndlo = 0;
else if (!rloc && -*ndlo * 9 >= r)
*ndlo = -r / 9;
else if (rloc && -*ndlo * 9 >= r)
*ndlo = -r / 9 - 1;
}
static int ft_round_last(int ndlo, int r, uint32_t *nd)
{
int rloc;
int tlo;
tlo = ndlo;
rloc = (r % 9);
ft_round_ndlo(r, rloc, &ndlo);
if (((r / 9) + 1) == -tlo && !(nd[tlo & 127] % g_pow10[8 - rloc]))
{
if (rloc && (nd[ndlo & 127] / g_pow10[9 - rloc] % 10) & 1
&& (nd[ndlo & 127] / g_pow10[8 - rloc]) % 10 == 5)
nd[ndlo & 127] += g_pow10[9 - rloc];
else if (!rloc && (tlo <= ndlo - 1) && (nd[ndlo & 127] % 10) & 1
&& nd[(ndlo - 1) & 127] / g_pow10[8] == 5)
nd[ndlo & 127] += 1;
}
else
{
if (rloc && (nd[ndlo & 127] / g_pow10[8 - rloc]) % 10 >= 5)
nd[ndlo & 127] += g_pow10[9 - rloc];
else if (!rloc && (tlo <= ndlo - 1)
&& nd[(ndlo - 1) & 127] / g_pow10[8] >= 5)
nd[ndlo & 127] += 1;
}
return (ndlo);
}
void ft_round(t_nd *nd, int *r)
{
if (*r > -nd->lo * 9)
*r = -nd->lo * 9;
nd->lo = ft_round_last(nd->lo, *r, nd->n);
ft_round_up(nd->n, nd->lo);
}

16
libft/printf/ft_null.c Normal file
View File

@@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_null.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 22:17:39 by igarbuz #+# #+# */
/* Updated: 2019/01/24 22:17:43 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
const char *ft_null(void)
{
return ("(null)");
}

91
libft/printf/ft_parse.c Normal file
View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 23:54:23 by igarbuz #+# #+# */
/* Updated: 2019/01/24 23:54:27 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_istype(char c)
{
return (c == '%' || c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i'
|| c == 'o' || c == 'u' || c == 'x' || c == 'X' || c == 'f' || c == 'B');
}
static void ft_parse_prefix(const char **s, t_param *prm)
{
if (**s == '+')
prm->p = prm->p | 1;
else if (**s == '-')
prm->p = prm->p | 1 << 1;
else if (**s == '#')
prm->p = prm->p | 1 << 2;
else if (**s == ' ')
prm->p = prm->p | 1 << 3;
else if (**s == '0')
prm->p = prm->p | 1 << 4;
}
static void ft_parse_type(const char **s, t_param *prm)
{
if (**s == 'h' && *(*s + 1) == 'h')
{
prm->p = prm->p | 1 << 5;
(*s)++;
}
else if (**s == 'h')
prm->p = prm->p | 1 << 6;
else if (**s == 'l' && *(*s + 1) == 'l')
{
prm->p = prm->p | 1 << 8;
(*s)++;
}
else if (**s == 'l')
prm->p = prm->p | 1 << 7;
else if (**s == 'L')
prm->p = prm->p | 1 << 9;
}
static void ft_parse_wdpr(const char **s, t_param *prm)
{
if (ft_isdigit_printf(**s))
{
prm->wdt = ft_atoi_printf(*s);
while (ft_isdigit_printf(*(*s + 1)))
(*s)++;
}
else if (**s == '.')
{
if (ft_isdigit_printf(*(*s + 1)))
prm->prc = ft_atoi_printf(*s + 1);
else
prm->prc = 0;
while (ft_isdigit_printf(*(*s + 1)))
(*s)++;
}
}
int ft_parse(const char **s, t_param *prm)
{
while (**s)
{
(*s)++;
if (**s == '+' || **s == '-' || **s == '#' || **s == ' ' || **s == '0')
ft_parse_prefix(s, prm);
else if (**s == 'h' || **s == 'l' || **s == 'L')
ft_parse_type(s, prm);
else if (**s == '.' || ft_isdigit_printf(**s))
ft_parse_wdpr(s, prm);
else if (ft_istype(**s))
return (1);
else
return (0);
}
return (0);
}

77
libft/printf/ft_printf.c Normal file
View File

@@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:05:37 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:05:40 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_print(const char c, char *b, t_param *prm, va_list valist)
{
intmax_t arg;
double db;
long double ldb;
if (c != '%' && c != 'f')
arg = ft_arg_cast(valist, prm->p, c);
else if (c == 'f' && (prm->p >> 9 & 1))
ldb = va_arg(valist, long double);
else if (c == 'f')
db = va_arg(valist, double);
if (c == 'c' || c == 's' || c == '%')
ft_print_c_pad(c, b, prm, arg);
else if ((c == 'd' || c == 'i' || c == 'u' || c == 'o' || c == 'x'
|| c == 'X' || c == 'p'))
ft_print_i_pad(c, b, prm, arg);
else if (c == 'f' && (prm->p >> 9 & 1))
ft_print_long_db_pad(b, prm, &ldb);
else if (c == 'f')
ft_print_db_pad(b, prm, &db);
else if (c == 'B')
ft_print_bit_float(b, prm, valist);
}
static void ft_read_input(va_list valist, char *buf,
t_param *prm, const char *format)
{
while (*format)
{
ft_init_param(prm);
ft_printbuf_c(buf, &format, '%');
if (!*format)
break ;
if (*format == '%' && ft_parse(&format, prm))
ft_print(*format, buf, prm, valist);
else if (*format)
{
ft_print_c_pad('c', buf, prm, *format);
format++;
if (*format)
ft_printbuf_c(buf, &format, '%');
}
if (*format && (*format != '%' || *(format - 1) == '%'))
format++;
}
}
int ft_printf(const char *format, ...)
{
va_list valist;
t_param prm;
char buf[BUF_SIZE];
g_bcn = 0;
if (format == 0 || *format == '\0')
return (0);
va_start(valist, format);
ft_read_input(valist, buf, &prm, format);
va_end(valist);
write(1, buf, g_bcn);
return (g_bcn);
}

258
libft/printf/ft_printf.h Normal file
View File

@@ -0,0 +1,258 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:05:59 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:06:04 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdlib.h>
# include <unistd.h>
# include <stdarg.h>
# include <wchar.h>
/*
** BUF_M is muffer mask; must be a divisible PAGESIZE minus 1;
** exception for BUF_M 0x1, BUF_SIZE must be 0x1;
** BUF_S is a buffer size - must be a divisible PAGESIZE;
** PAGESIZE default 2^12;
** default 0x7ffff is equivalent to 524287 bytes; Examples of values :
** BUF_M : 0x7ffff, 0x3ffff, 0x1ffff, 0x0ffff, 0x7fff, 0x3fff, 0x1fff, 0x0fff;
** BUF_S : 0x80000, 0x40000, 0x20000, 0x10000, 0x8000, 0x4000, 0x2000, 0x1000
*/
# define BUF_M 0x0fff
# define BUF_SIZE 0x1000
/*
** do not modify
*/
# define F_ROUND 6
# define DB_MAX_L 1076
# define DBL_MAX_L 16447
typedef struct s_param
{
int p;
int prc;
int wdt;
} t_param;
typedef union u_value
{
double n;
struct
{
uint32_t lo;
uint32_t hi;
} u32;
} t_value;
typedef union u_value64
{
long double n;
struct
{
uint64_t lo;
uint64_t hi;
} u64;
} t_value64;
typedef struct s_nd
{
uint32_t n[128];
int32_t lo;
int32_t hi;
} t_nd;
typedef struct s_nd_long
{
uint32_t n[2048];
int32_t lo;
int32_t hi;
} t_nd_long;
/*
** buf_cnt.c
*/
extern int g_bcn;
extern unsigned int g_pow10[];
/*
** ft_str_null.c
*/
const char *ft_null(void);
/*
** ft_printf.c
*/
int ft_printf(const char *format, ...);
/*
** print_num_pad.c
*/
void ft_print_long_db_pad(char *b, t_param *prm,
long double *arg);
void ft_print_db_pad(char *b, t_param *prm, double *arg);
void ft_print_i_pad(char c, char *b, t_param *prm, intmax_t arg);
/*
** print_cs_pad.c
*/
void ft_print_c_pad(char c, char *b, t_param *prm, intmax_t arg);
/*
** ft_printf_db.c
*/
char *ft_printf_db(double *n, int r, char *tbl);
/*
** ft_printf_ldb.c
*/
char *ft_printf_long_db(long double *n, int r, char *tbl);
/*
** ft_parse.c
*/
int ft_parse(const char **s, t_param *prm);
/*
** print_buf_prefix.c
*/
void ft_printbuf_long_fprefix(char *b, int p, long double *arg);
void ft_printbuf_fprefix(char *b, int p, double *arg);
void ft_printbuf_xprefix(char *b, char c,
t_param *prm, intmax_t *arg);
void ft_printbuf_sprefix(char *b, int p, intmax_t *arg);
/*
** libft_printf.c
*/
int ft_strlen_printf(const char *s);
int ft_strlen_unicode(const wchar_t *s);
int ft_atoi_printf(const char *str);
int ft_isdigit_printf(char c);
/*
** print_buf.c
*/
void ft_flush(char *dst);
void ft_printbuf_char(char *dst, char src);
void ft_printbuf_pad(char *dst, char c, int len);
void ft_printbuf_c(char *dst, const char **src, char c);
void ft_printbuf_prc(char *dst, char **src, int prc);
/*
** print_buf_unicode.c
*/
void ft_printbuf_wchar_t(char *dst, wchar_t src, int size);
void ft_printbuf_unicode_prc(char *dst, wchar_t **src, int prc);
/*
** print_buf_num.c
*/
void ft_printbuf_snum(char *dst, intmax_t n);
void ft_printbuf_unum(char *dst, uintmax_t n);
void ft_printbuf_oct(char *dst, uintmax_t o);
void ft_printbuf_hex(char *dst, uintmax_t x, char c);
/*
** ft_arglen.c
*/
int ft_arg_len(intmax_t arg, char c, t_param *prm);
/*
** ft_arglen_oct.c
*/
int ft_arg_len_oct(intmax_t arg, t_param *prm);
/*
** ft_arglen_f.c
*/
int ft_fract_len(const char *f);
int ft_arg_db_len(const char *s, int p, double *arg);
int ft_arg_long_db_len(const char *s, int p, long double *arg);
int ft_arg_flen(intmax_t arg, char c, int p, int len);
/*
** ft_argcast.c
*/
intmax_t ft_arg_cast(va_list valist, int p, const char c);
/*
** ft_init_param.c
*/
void ft_init_param(t_param *prm);
void ft_init_nd(t_nd *nd);
/*
** ft_exp_dec.c
*/
int ft_exp_dec(unsigned int value);
/*
** pf_error.c
*/
void pf_error(int er);
/*
** nd_round.c
*/
void ft_round(t_nd *nd, int *r);
/*
** ft_buf_print_db.c
*/
char *ft_buf_print_db(t_nd *nd, int r, char *tbl);
/*
** ft_buf_print_ldb.c
*/
char *ft_buf_print_ldb(t_nd_long *nd, int r, char *tbl);
/*
** ft_nd_div.c
*/
void ft_nd_div2p(t_nd *nd, uint32_t p);
/*
** ft_nd_mul.c
*/
void ft_nd_mul2p(t_nd *nd, uint32_t p, uint32_t carry);
/*
** ft_nd_long_div.c
*/
void ft_nd_long_div2p(t_nd_long *nd, uint32_t p);
/*
** ft_nd_long_mul.c
*/
void ft_nd_long_mul2p(t_nd_long *nd, uint32_t p, uint32_t carry);
/*
** ft_nd_long_round.c
*/
void ft_long_round(t_nd_long *nd, int *r);
/*
** ft_unicode.c
*/
int ft_wcsize(wchar_t wc);
wchar_t ft_utf8_encode(unsigned int wchar);
/*
** print_buf_bit_float.c
*/
void ft_print_buf_bit_float(char *b, float f);
void ft_print_buf_bit_double(char *b, double db);
void ft_print_buf_bit_long_double(char *b, long double ldb);
void ft_print_bit_float(char *b, t_param *prm, va_list valist);
#endif

View File

@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_db.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:06:20 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:06:22 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static char *ft_naninf_db(t_value *t)
{
if (((t->u32.hi & 0x000fffff) | t->u32.lo) != 0)
return ("nan");
else
return ("inf");
}
static void ft_fract_to_nd(t_value *t, t_nd *nd, int32_t *e)
{
if (t->u32.lo)
{
*e -= 32;
nd->n[0] = (nd->n[0] << 3) | (t->u32.lo >> 29);
ft_nd_mul2p(nd, 29, t->u32.lo & 0x1fffffff);
}
}
static void ft_implicit_one(int32_t *e, t_nd *nd)
{
if (*e == 0)
(*e)++;
else
nd->n[0] |= 0x100000;
}
char *ft_printf_db(double *n, int r, char *tbl)
{
t_value t;
int32_t e;
t_nd nd;
t.n = *n;
ft_init_nd(&nd);
if ((t.u32.hi << 1) >= 0xffe00000)
return (ft_naninf_db(&t));
else
{
e = (t.u32.hi >> 20) & 0x7ff;
nd.n[0] = t.u32.hi & 0xfffff;
ft_implicit_one(&e, &nd);
e -= 1043;
ft_fract_to_nd(&t, &nd, &e);
if (e >= 0)
ft_nd_mul2p(&nd, (uint32_t)e, 0);
else
ft_nd_div2p(&nd, (uint32_t)-e);
if (n && nd.lo)
ft_round(&nd, &r);
return (ft_buf_print_db(&nd, r, tbl));
}
return (0);
}

View File

@@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_ldb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:06:44 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:06:47 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static char *ft_naninf_ldb(t_value64 *t)
{
if (t->u64.lo != 0)
return ("nan");
else
return ("inf");
}
static void ft_fract_to_nd(t_value64 *t, t_nd_long *nd, int32_t *e)
{
if (t->u64.lo & 0x3FFFFFFFFFFFFFF)
{
*e -= 58;
ft_nd_long_mul2p(nd, 29, (t->u64.lo >> 29) & 0x1FFFFFFF);
ft_nd_long_mul2p(nd, 29, t->u64.lo & 0x1FFFFFFF);
}
}
char *ft_printf_long_db(long double *n, int r, char *tbl)
{
t_nd_long nd;
t_value64 t;
int32_t e;
t.n = *n;
if ((t.u64.hi << 49) >= 0xfffe000000000000)
return (ft_naninf_ldb(&t));
else
{
e = (t.u64.hi & 0x7fff);
nd.n[0] = (t.u64.lo >> 58) & 0x3f;
e -= 16388;
ft_fract_to_nd(&t, &nd, &e);
if (e >= 0)
ft_nd_long_mul2p(&nd, (uint32_t)e, 0);
else if (1)
ft_nd_long_div2p(&nd, (uint32_t)-e);
if (*n && nd.lo)
ft_long_round(&nd, &r);
return (ft_buf_print_ldb(&nd, r, tbl));
}
}

70
libft/printf/ft_unicode.c Normal file
View File

@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_unicode.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/30 01:52:28 by igarbuz #+# #+# */
/* Updated: 2019/01/30 01:52:31 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_wcsize(wchar_t wc)
{
if (wc <= 0x007F)
return (1);
if (wc <= 0x07FF)
return (2);
if (wc <= 0xFFFF)
return (3);
if (wc <= 0x10FFFF)
return (4);
return (0);
}
static int ft_wc_to_tbl(unsigned char *s, unsigned int wchar)
{
int len;
unsigned int rshift;
unsigned int mask[2];
int i;
if (!s || !(len = ft_wcsize(wchar)))
return (0);
rshift = (len - 1) * 6;
mask[0] = 0xFF;
mask[1] = 0xC0;
if (len > 2)
mask[1] += (len - 1) * 16;
i = 0;
while (i < len)
{
s[i++] = (wchar >> rshift & mask[0]) | mask[1];
mask[0] = 0x3F;
mask[1] = 0x80;
rshift -= 6;
}
return (len);
}
wchar_t ft_utf8_encode(unsigned int wchar)
{
int len;
unsigned char s[4];
unsigned int lshift;
if (!(len = ft_wc_to_tbl(s, wchar)))
return (0);
if (!(lshift = (len - 1) * 8))
return (wchar);
wchar = 0;
while (len--)
{
wchar |= s[len] << lshift;
lshift -= 8;
}
return (wchar);
}

View File

@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:40:42 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:40:44 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_strlen_unicode(const wchar_t *s)
{
const wchar_t *tmp;
if (!s)
return (6);
tmp = s;
while (*s)
s++;
return (s - tmp);
}
int ft_strlen_printf(const char *s)
{
size_t len;
unsigned int *x;
if (!s)
return (6);
len = 0;
while (1)
{
x = (unsigned int *)s;
if ((*x & 0xFF) == 0)
return (len);
if ((*x & 0xFF00) == 0)
return (len + 1);
if ((*x & 0xFF0000) == 0)
return (len + 2);
if ((*x & 0xFF000000) == 0)
return (len + 3);
s += 4;
len += 4;
}
}
int ft_atoi_printf(const char *str)
{
int d;
int integer;
d = 1;
integer = 0;
while ((*str >= 9 && *str <= 13) || *str == 32)
str++;
if (*str == '-' && (d = -1))
str++;
else if (*str == '+')
str++;
str--;
while (++str && (*str >= 48 && *str <= 57))
integer = integer * 10 + *str - 48;
if (d == -1)
return (integer * d);
return (integer);
}
int ft_isdigit_printf(char c)
{
return (c >= '0' && c <= '9');
}

23
libft/printf/pow10.c Normal file
View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pow10.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 19:58:41 by igarbuz #+# #+# */
/* Updated: 2019/01/29 19:58:45 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
unsigned int g_pow10[] = {
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
};

70
libft/printf/print_buf.c Normal file
View File

@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_buf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 21:34:43 by igarbuz #+# #+# */
/* Updated: 2019/01/29 21:34:47 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_flush(char *dst)
{
write(1, dst, BUF_M);
g_bcn++;
}
void ft_printbuf_char(char *dst, char src)
{
if ((g_bcn & BUF_M) < BUF_M)
dst[g_bcn & BUF_M] = src;
else if ((g_bcn & BUF_M) == BUF_M)
{
ft_flush(dst);
dst[g_bcn & BUF_M] = src;
}
g_bcn++;
}
void ft_printbuf_prc(char *dst, char **src, int prc)
{
if (!dst || !src)
pf_error(-1);
while (**src && prc)
{
if ((g_bcn & BUF_M) == BUF_M)
ft_flush(dst);
dst[g_bcn & BUF_M] = **src;
g_bcn++;
(*src)++;
prc--;
}
}
void ft_printbuf_c(char *dst, const char **src, char c)
{
while (**src && **src != c)
{
if ((g_bcn & BUF_M) == BUF_M)
ft_flush(dst);
dst[g_bcn & BUF_M] = **src;
g_bcn++;
(*src)++;
}
}
void ft_printbuf_pad(char *dst, char c, int len)
{
while (len > 0)
{
if ((g_bcn & BUF_M) == BUF_M)
ft_flush(dst);
dst[g_bcn & BUF_M] = c;
g_bcn++;
len--;
}
}

View File

@@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_bit_float.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/26 16:51:17 by igarbuz #+# #+# */
/* Updated: 2019/01/02 18:44:46 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_print_buf_bit_float(char *b, float f)
{
int size;
int sztmp;
unsigned char *ptr;
int j;
if (!f)
return ;
size = sizeof(float) - 1;
sztmp = size;
ptr = (unsigned char *)&f;
while (size >= 0)
{
j = 8;
while (--j >= 0)
{
if ((size == sztmp || size == (sztmp - 1)) && j == 6)
ft_printbuf_char(b, ' ');
ft_printbuf_char(b, (ptr[size] >> j & 1) + '0');
}
size--;
}
}
void ft_print_buf_bit_double(char *b, double db)
{
int size;
int sztmp;
unsigned char *ptr;
int j;
if (!db)
return ;
size = sizeof(double) - 1;
sztmp = size;
ptr = (unsigned char *)&db;
while (size >= 0)
{
j = 8;
while (--j >= 0)
{
if (size == sztmp && j == 6)
ft_printbuf_char(b, ' ');
else if (size == (sztmp - 1) && j == 3)
ft_printbuf_char(b, ' ');
ft_printbuf_char(b, (ptr[size] >> j & 1) + '0');
}
size--;
}
}
void ft_print_buf_bit_long_double(char *b, long double ldb)
{
int size;
int sztmp;
unsigned char *ptr;
int j;
if (!ldb)
return ;
size = sizeof(long double) - 7;
sztmp = size;
ptr = (unsigned char *)&ldb;
while (size >= 0)
{
j = 8;
while (--j >= 0)
{
if (size == sztmp && j == 6)
ft_printbuf_char(b, ' ');
else if (size == (sztmp - 2) && j == 7)
ft_printbuf_char(b, ' ');
ft_printbuf_char(b, (ptr[size] >> j & 1) + '0');
}
size--;
}
}
void ft_print_bit_float(char *b, t_param *prm, va_list valist)
{
double db;
long double ldb;
if (prm->p >> 9 & 1)
ldb = va_arg(valist, long double);
else
db = va_arg(valist, double);
if (prm->p >> 9 & 1)
ft_print_buf_bit_long_double(b, ldb);
else if ((prm->p >> 7 & 1) || (prm->p >> 8 & 1))
ft_print_buf_bit_double(b, db);
else
ft_print_buf_bit_float(b, (float)db);
}

View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft_printf_f.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/24 13:07:04 by igarbuz #+# #+# */
/* Updated: 2019/01/24 13:07:07 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_printbuf_unum(char *dst, uintmax_t n)
{
if (n / 10)
ft_printbuf_unum(dst, n / 10);
ft_printbuf_char(dst, n % 10 + '0');
}
void ft_printbuf_snum(char *dst, intmax_t n)
{
if (n < 0)
ft_printbuf_unum(dst, -n);
else
ft_printbuf_unum(dst, n);
}
void ft_printbuf_oct(char *dst, uintmax_t o)
{
if (o >> 3)
ft_printbuf_oct(dst, o >> 3);
ft_printbuf_char(dst, (o & 7) + '0');
}
void ft_printbuf_hex(char *dst, uintmax_t x, char c)
{
if (x >> 4)
ft_printbuf_hex(dst, x >> 4, c);
if ((x & 15) > 9)
ft_printbuf_char(dst, (x & 15) + c);
else
ft_printbuf_char(dst, (x & 15) + '0');
}

View File

@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_buf_prefix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 20:59:34 by igarbuz #+# #+# */
/* Updated: 2019/01/29 20:59:36 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_printbuf_sprefix(char *b, int p, intmax_t *arg)
{
if (*arg < 0)
ft_printbuf_char(b, '-');
else if (*arg >= 0 && (p & 1))
ft_printbuf_char(b, '+');
else if (*arg >= 0 && (p >> 3 & 1))
ft_printbuf_char(b, ' ');
}
/*
** c must be 55 (65 - 10) for HEX upper case;
** 87 (97 - 10) for hex lower case;
*/
void ft_printbuf_xprefix(char *b, char c, t_param *prm, intmax_t *arg)
{
if ((c == 'x' || c == 'X') && (prm->p >> 2 & 1) && *arg)
{
ft_printbuf_char(b, '0');
ft_printbuf_char(b, c);
}
else if (c == 'p')
{
ft_printbuf_char(b, '0');
ft_printbuf_char(b, 'x');
}
else if (c == 'o' && (prm->p >> 2 & 1) && (*arg || !prm->prc))
ft_printbuf_char(b, '0');
}
void ft_printbuf_fprefix(char *b, int p, double *arg)
{
uintmax_t *ptr;
ptr = (uintmax_t *)arg;
if (!(*ptr >> 63) && (p & 1))
ft_printbuf_char(b, '+');
else if (!(*ptr >> 63) && (p >> 3 & 1))
ft_printbuf_char(b, ' ');
else if (*ptr >> 63)
ft_printbuf_char(b, '-');
}
void ft_printbuf_long_fprefix(char *b, int p, long double *arg)
{
uintmax_t *ptr;
ptr = (uintmax_t *)arg;
if (!((ptr[1] >> 15) & 1) && (p & 1))
ft_printbuf_char(b, '+');
else if (!((ptr[1] >> 15) & 1) && (p >> 3 & 1))
ft_printbuf_char(b, ' ');
else if ((ptr[1] >> 15) & 1)
ft_printbuf_char(b, '-');
}

View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_buf_unicode.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/30 01:50:29 by igarbuz #+# #+# */
/* Updated: 2019/01/30 01:50:34 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_printbuf_wchar_t(char *dst, wchar_t src, int size)
{
int shift;
if (!size)
return ;
if ((g_bcn & BUF_M) > BUF_M - size)
ft_flush(dst);
shift = 0;
while (--size >= 0)
{
dst[g_bcn & BUF_M] = src >> shift & 0xFF;
g_bcn++;
shift += 8;
}
}
void ft_printbuf_unicode_prc(char *dst, wchar_t **src, int prc)
{
while (**src && prc)
{
ft_printbuf_wchar_t(dst, ft_utf8_encode(**src), ft_wcsize(**src));
(*src)++;
prc--;
}
}

View File

@@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_cs_pad.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 21:09:48 by igarbuz #+# #+# */
/* Updated: 2019/01/29 21:09:50 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_print_c(char c, char *b, intmax_t arg, t_param *prm)
{
char *s;
if (c == 's')
{
if (!arg)
{
s = (char *)ft_null();
ft_printbuf_prc(b, &s, prm->prc);
}
else if (prm->p >> 7 & 1)
ft_printbuf_unicode_prc(b, (wchar_t **)&arg, prm->prc);
else
ft_printbuf_prc(b, (char **)&arg, prm->prc);
}
else if (c == 'c')
{
if (prm->p >> 7 & 1)
ft_printbuf_wchar_t(b, ft_utf8_encode(arg), ft_wcsize(arg));
else
ft_printbuf_char(b, (char)arg);
}
else if (c == '%')
ft_printbuf_char(b, '%');
}
void ft_print_c_pad(char c, char *b, t_param *prm, intmax_t arg)
{
int len;
len = ft_arg_len(arg, c, prm);
if (prm->prc != -1 && c == 's' && prm->prc < len)
len = prm->prc;
if (!(prm->p >> 1 & 1) && (prm->p >> 4 & 1))
ft_printbuf_pad(b, '0', prm->wdt - len);
else if (!(prm->p >> 1 & 1))
ft_printbuf_pad(b, ' ', prm->wdt - len);
ft_print_c(c, b, arg, prm);
if (prm->p >> 1 & 1)
ft_printbuf_pad(b, ' ', prm->wdt - len);
}

View File

@@ -0,0 +1,132 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_num_pad.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 21:05:47 by igarbuz #+# #+# */
/* Updated: 2019/01/29 21:05:50 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_float_default_prc(t_param *prm)
{
if (prm->prc == -1)
prm->prc = F_ROUND;
}
static void ft_print_i(char c, char *dst, intmax_t arg, t_param *prm)
{
if (!arg && !prm->prc)
return ;
else if (c == 'd' || c == 'i')
ft_printbuf_snum(dst, arg);
else if (c == 'u')
ft_printbuf_unum(dst, (uintmax_t)arg);
else if (c == 'o')
ft_printbuf_oct(dst, (uintmax_t)arg);
else if (c == 'x' || c == 'p')
ft_printbuf_hex(dst, (uintmax_t)arg, 87);
else if (c == 'X')
ft_printbuf_hex(dst, (uintmax_t)arg, 55);
}
/*
** if arg == 0 and a precision is specified : empty is preanted
** ft_arg_len() : real length rlen without format prefixes
** ft_arg_flen() : full length len
*/
void ft_print_i_pad(char c, char *b, t_param *prm, intmax_t arg)
{
int flen;
int rlen;
rlen = ft_arg_len(arg, c, prm);
flen = rlen;
if (prm->prc > rlen)
flen = prm->prc;
flen = ft_arg_flen(arg, c, prm->p, flen);
if (!(prm->p >> 1 & 1) && (!(prm->p >> 4 & 1) || prm->prc != -1))
ft_printbuf_pad(b, ' ', prm->wdt - flen);
if (c == 'i' || c == 'd')
ft_printbuf_sprefix(b, prm->p, &arg);
else if (c == 'x' || c == 'X' || c == 'o' || c == 'p')
ft_printbuf_xprefix(b, c, prm, &arg);
if (!(prm->p >> 1 & 1) && (prm->p >> 4 & 1) && prm->prc == -1)
ft_printbuf_pad(b, '0', prm->wdt - flen);
ft_printbuf_pad(b, '0', prm->prc - rlen);
ft_print_i(c, b, arg, prm);
if ((prm->p >> 1 & 1))
ft_printbuf_pad(b, ' ', prm->wdt - flen);
}
/*
** ft_printf_db_pad return a string of a max length nor more than decimal LSB
*/
void ft_print_db_pad(char *b, t_param *prm, double *arg)
{
const char *f;
int len;
int frc;
char tbl[DB_MAX_L];
ft_float_default_prc(prm);
if ((f = ft_printf_db(arg, prm->prc, tbl)))
{
len = ft_arg_db_len(f, prm->p, arg);
if ((frc = ft_fract_len(f)) >= 0 && prm->prc > frc)
len += (prm->prc - frc);
if (!(prm->p >> 1 & 1) && (!(prm->p >> 4 & 1)))
ft_printbuf_pad(b, ' ', prm->wdt - len);
ft_printbuf_fprefix(b, prm->p, arg);
if (!(prm->p >> 1 & 1) && (prm->p >> 4 & 1))
ft_printbuf_pad(b, '0', prm->wdt - len);
if (!frc && !(prm->p >> 2 & 1) && prm->prc == 0)
ft_printbuf_c(b, &f, '.');
else
ft_printbuf_c(b, &f, '\0');
if (frc != -1)
ft_printbuf_pad(b, '0', prm->prc - frc);
if ((prm->p >> 1 & 1))
ft_printbuf_pad(b, ' ', prm->wdt - len);
}
}
/*
** ft_printf_ldb return a string of a max length nor more than
** lond double LSB in decimal; Equivalent to ft_print_db_pad
*/
void ft_print_long_db_pad(char *b, t_param *prm, long double *arg)
{
const char *f;
int len;
int frc;
char tbl[DBL_MAX_L];
ft_float_default_prc(prm);
if ((f = ft_printf_long_db(arg, prm->prc, tbl)))
{
len = ft_arg_long_db_len(f, prm->p, arg);
if ((frc = ft_fract_len(f)) >= 0 && prm->prc > frc)
len += (prm->prc - frc);
if (!(prm->p >> 1 & 1) && (!(prm->p >> 4 & 1)))
ft_printbuf_pad(b, ' ', prm->wdt - len);
ft_printbuf_long_fprefix(b, prm->p, arg);
if (!(prm->p >> 1 & 1) && (prm->p >> 4 & 1))
ft_printbuf_pad(b, '0', prm->wdt - len);
if (!frc && !(prm->p >> 2 & 1) && prm->prc == 0)
ft_printbuf_c(b, &f, '.');
else
ft_printbuf_c(b, &f, '\0');
if (frc != -1)
ft_printbuf_pad(b, '0', prm->prc - frc);
if ((prm->p >> 1 & 1))
ft_printbuf_pad(b, ' ', prm->wdt - len);
}
}