Skip to content

Commit

Permalink
printf
Browse files Browse the repository at this point in the history
- fix '+', ' '
- added padded
  • Loading branch information
Pixailz committed Nov 30, 2023
1 parent 0c61321 commit c1a1daf
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 121 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
## TODO

1. improve ft_printf
1. add a buffer
1. add format and other usefull stuff ("%02x", "%#02x")
1. flags, implement `-`
1. move padding outside of integer to integrate with other format type
1. error
1. implement singleton, utils like perror etc ..
1. doc
Expand Down
3 changes: 2 additions & 1 deletion inc/libft_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ void ft_printf_fmt_hex(t_fmt_conf conf, va_list args);
void ft_printf_fmt_hexa(t_fmt_conf conf, va_list args);

// print/ft_printf/fmt/integer.c
void ft_printf_fmt_integer_sign(t_fmt_conf conf);
void ft_printf_fmt_integer_sign(t_fmt_conf *conf);
void ft_printf_fmt_integer_padding(t_fmt_conf *conf, t_size str_len);
void ft_printf_fmt_integer(t_fmt_conf conf, va_list args);

// print/ft_printf/fmt/string.c
Expand Down
57 changes: 37 additions & 20 deletions src/print/ft_printf/fmt/integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,60 @@
/* By: brda-sil <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/28 19:20:11 by brda-sil #+# #+# */
/* Updated: 2023/11/29 13:22:16 by brda-sil ### ########.fr */
/* Updated: 2023/11/30 03:31:52 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft_print.h"

void ft_printf_fmt_integer_sign(t_fmt_conf conf)
void ft_printf_fmt_integer_sign(t_fmt_conf *conf)
{
if (conf.flags & FT_PRINTF_FLAG_PLUS)
ft_strncpy(conf.cur_fmt, "+", 1);
else if (conf.flags & FT_PRINTF_FLAG_SPACE)
ft_strncpy(conf.cur_fmt, " ", 1);
if (conf->flags & FT_PRINTF_FLAG_PLUS)
ft_strncpy(conf->cur_fmt, "+", 1);
else if (conf->flags & FT_PRINTF_FLAG_SPACE)
ft_strncpy(conf->cur_fmt, " ", 1);
}

void ft_printf_fmt_integer_padding(t_fmt_conf *conf, t_size str_len)
{
int pad_char;
char sign;
int i;

if (conf->flags & FT_PRINTF_FLAG_ZERO)
pad_char = '0';
else
pad_char = ' ';
sign = conf->cur_fmt[0];
i = 0;
str_len += sign != 0;
while (str_len < conf->width)
{
conf->cur_fmt[i] = pad_char;
str_len++;
i++;
}
conf->cur_fmt[ft_strlen(conf->cur_fmt)] = sign;
}

void ft_printf_fmt_integer(t_fmt_conf conf, va_list args)
{
char *buff;
int i;
char *i_str;
t_size i_str_len;
t_size str_len;

buff = ft_printf_buff_get();
i = va_arg(args, int);
i_str = ft_itoa_base(i, "0123456789");
i_str_len = ft_strlen(i_str);
str_len = ft_strlen(i_str);
if (i > 0)
ft_printf_fmt_integer_sign(conf);
if (conf.flags & FT_PRINTF_FLAG_ZERO)
i = '0';
else
i = ' ';
while (i_str_len < conf.width)
{
buff[(*conf.i_buff)++ + ft_strlen(conf.cur_fmt)] = i;
i_str_len++;
}
ft_strncpy(conf.cur_fmt + ft_strlen(conf.cur_fmt), i_str, i_str_len);
ft_printf_fmt_integer_sign(&conf);
if (conf.width)
ft_printf_fmt_integer_padding(&conf, str_len);
ft_strncpy(conf.cur_fmt + ft_strlen(conf.cur_fmt), i_str, str_len);
str_len = ft_strlen(conf.cur_fmt);
ft_strncpy(buff + *conf.i_buff, conf.cur_fmt, str_len);
free(i_str);
*conf.i_buff += i_str_len;
*conf.i_buff += str_len;
}
219 changes: 121 additions & 98 deletions test/src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/07 03:09:08 by brda-sil #+# #+# */
/* Updated: 2023/11/29 01:05:31 by brda-sil ### ########.fr */
/* Updated: 2023/11/30 03:36:38 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,151 +17,174 @@
# define CI_TEST FALSE
#endif

void test_printf_base(void)
int test_printf_base(void)
{
t_size libc;
t_size mine;

libc = printf("LIBC: {Hello World!}\n");
mine = ft_printf_2("MINE: {Hello World!}\n");
if (libc == mine)
printf("PASS\n");
printf("PASS [string]\n");
else
printf("FAIL\n");
}

void test_printf_pourcentage(void)
{
t_size libc;
t_size mine;

{
printf("FAIL [string]\n");
return (1);
}
libc = printf("LIBC: {%%}\n");
mine = ft_printf_2("MINE: {%%}\n");
if (libc == mine)
printf("PASS\n");
printf("PASS [pourcentage]\n");
else
printf("FAIL\n");
{
printf("FAIL [pourcentage]\n");
return (1);
}
return (0);
}

void test_printf_char(char c)
int test_print_result(t_size libc, t_size mine, char *fmt)
{
t_size libc;
t_size mine;
int retv;

libc = printf("LIBC: {%c}\n", c);
mine = ft_printf_2("MINE: {%c}\n", c);
if (libc == mine)
printf("PASS\n");
{
printf("PASS");
retv = 0;
}
else
printf("FAIL\n");
{
printf("FAIL");
retv = 1;
}
printf(" [%s]\n", fmt);
return (retv);
}

void test_printf_string(char *s)
int test_printf_char(char *fmt, char c)
{
t_size libc;
t_size mine;

libc = printf("LIBC: {%s}\n", s);
mine = ft_printf_2("MINE: {%s}\n", s);
if (libc == mine)
printf("PASS\n");
else
printf("FAIL\n");
char buff[0x100];

ft_bzero(buff, 0x100);
ft_strcpy(buff, "LIBC: {");
ft_strcpy(buff + 7, fmt);
ft_strcpy(buff + 7 + ft_strlen(fmt), "}\n");
libc = printf(buff, c);
ft_strncpy(buff, "MINE: {", 7);
mine = ft_printf_2(buff, c);
return (test_print_result(libc, mine, fmt));
}

void test_printf_address(void *ptr)
int test_printf_string(char *fmt, char *s)
{
t_size libc;
t_size mine;

libc = printf("LIBC: {%p}\n", ptr);
mine = ft_printf_2("MINE: {%p}\n", ptr);
if (libc == mine)
printf("PASS\n");
else
printf("FAIL\n");
char buff[0x100];

ft_bzero(buff, 0x100);
ft_strcpy(buff, "LIBC: {");
ft_strcpy(buff + 7, fmt);
ft_strcpy(buff + 7 + ft_strlen(fmt), "}\n");
libc = printf(buff, s);
ft_strncpy(buff, "MINE: {", 7);
mine = ft_printf_2(buff, s);
return (test_print_result(libc, mine, fmt));
}

void test_printf_integer(int i)
int test_printf_address(char *fmt, void *ptr)
{
t_size libc;
t_size mine;

libc = printf("LIBC: {%i}\n", i);
mine = ft_printf_2("MINE: {%i}\n", i);
if (libc == mine)
printf("PASS\n");
else
printf("FAIL\n");
libc = printf("LIBC: {%d}\n", i);
mine = ft_printf_2("MINE: {%d}\n", i);
if (libc == mine)
printf("PASS\n");
else
printf("FAIL\n");
char buff[0x100];

ft_bzero(buff, 0x100);
ft_strcpy(buff, "LIBC: {");
ft_strcpy(buff + 7, fmt);
ft_strcpy(buff + 7 + ft_strlen(fmt), "}\n");
libc = printf(buff, ptr);
ft_strncpy(buff, "MINE: {", 7);
mine = ft_printf_2(buff, ptr);
return (test_print_result(libc, mine, fmt));
}

void test_printf_unsigned(unsigned int i)
int test_printf_integer(char *fmt, int i)
{
t_size libc;
t_size mine;

libc = printf("LIBC: {%u}\n", i);
mine = ft_printf_2("MINE: {%u}\n", i);
if (libc == mine)
printf("PASS\n");
else
printf("FAIL\n");
char buff[0x100];

ft_bzero(buff, 0x100);
ft_strcpy(buff, "LIBC: {");
ft_strcpy(buff + 7, fmt);
ft_strcpy(buff + 7 + ft_strlen(fmt), "}\n");
libc = printf(buff, i);
ft_strncpy(buff, "MINE: {", 7);
mine = ft_printf_2(buff, i);
return (test_print_result(libc, mine, fmt));
}

void test_printf_hexa(int i)
int test_printf_unsigned(char *fmt, unsigned int i)
{
t_size libc;
t_size mine;
char buff[0x100];

ft_bzero(buff, 0x100);
ft_strcpy(buff, "LIBC: {");
ft_strcpy(buff + 7, fmt);
ft_strcpy(buff + 7 + ft_strlen(fmt), "}\n");
libc = printf(buff, i);
ft_strncpy(buff, "MINE: {", 7);
mine = ft_printf_2(buff, i);
return (test_print_result(libc, mine, fmt));
}

libc = printf("LIBC: {%x}\n", i);
mine = ft_printf_2("MINE: {%x}\n", i);
if (libc == mine)
printf("PASS\n");
else
printf("FAIL\n");
libc = printf("LIBC: {%X}\n", i);
mine = ft_printf_2("MINE: {%X}\n", i);
if (libc == mine)
printf("PASS\n");
else
printf("FAIL\n");
int ci_base(void)
{
int retv;

retv = 0;
retv |= test_printf_base();
retv |= test_printf_char("%c", 'A');
retv |= test_printf_string("%s", "This is a string.");
retv |= test_printf_address("%p", &retv);
retv |= test_printf_address("%p", 0);
retv |= test_printf_integer("%i", -1234);
retv |= test_printf_integer("%d", -1234);
retv |= test_printf_integer("%x", 0xABCD);
retv |= test_printf_integer("%X", 0xABCD);
retv |= test_printf_unsigned("%u", 1234);
return (retv);
}

int ci_flags(void)
{
int retv;

retv = 0;
retv |= test_printf_integer("%+d", 1000);
retv |= test_printf_integer("% d", 1000);
retv |= test_printf_integer("% 6d", 1000);
retv |= test_printf_integer("%+18d", 1000);
retv |= test_printf_integer("%+-18d", 1000);
// retv |= test_printf_integer("%#x", 0x1234);
// retv |= test_printf_integer("%#X", 0x1234);
// retv |= test_printf_integer("%04d", 12);
// retv |= test_printf_integer("%04d", -12);
// retv |= test_printf_integer("%01.3d", 0);
return (retv);
}

int ci_test(void)
{
char dummy_var;

dummy_var = 1;
// test_printf_base();
// test_printf_pourcentage();
// test_printf_char('A');
// test_printf_string("This is a string.");
// test_printf_address(&dummy_var);
// test_printf_address(0);
// test_printf_integer(-1234);
// test_printf_unsigned(1234);
// test_printf_hexa(0xABCD);
// printf("test: %+d\n", 1000);
// ft_printf_2("test: %+d\n", 1000);
// printf("test: % d\n", 1000);
// ft_printf_2("test: % d\n", 1000);
// printf("test: %#x\n", 0x1234);
// ft_printf_2("test: %#x\n", 0x1234);
// printf("test: %#X\n", 0x1234);
// ft_printf_2("test: %#X\n", 0x1234);
printf("%03d\n", -1);
ft_printf_2("%03d\n", -1);
// printf("%03d\n", 12);
// ft_printf_2("%03d\n", 12);
// printf("%01.3d\n", 0);
// ft_printf_2("%01.3d\n", 0);
return (0);
int retv;

retv = 0;
retv |= ci_base();
retv |= ci_flags();
return (retv);
}

int interactive(void)
Expand Down

0 comments on commit c1a1daf

Please sign in to comment.