bug dans le width

This commit is contained in:
Hugo LAMY
2020-02-24 18:39:26 +01:00
parent f470ad061c
commit 0070661d1d
3 changed files with 101 additions and 24 deletions

BIN
ft_printf

Binary file not shown.

View File

@@ -84,18 +84,81 @@ char *ft_precision(char *s, char *print, char *type)
/*
**
** if i = flag_w(s) | -width is caculated
** if flag_-(&s) | -if flag '-', rm '-' and width from s
** print = ft_rpadd() | -put extra width as ' ' to right,
** else if flag_0(&s) | -if flag '0', rm '0' and width from s
** print = ft_lpadd() | -put extra width as '0' to left
** else | -if just width
** print = ft_lpadd() | put extra width as ' ' to left
*/
char *ft_width(char *s, char *print, char *type)
char *ft_width(char *s, char *print)
{
int i;
char *tmp;
int i; ft_putstr("[");ft_putstr(s);ft_putstr("|");
ft_putstr("[");ft_putstr(s);ft_putstr("|");
while (*s != '\0' && ft_strchr("%#- +'0.", *s))
s++;
i = ft_atoi(s);
(void)type;
ft_putnbr(i);ft_putstr("]");
tmp = s;
while (*tmp != '\0' && ft_strchr("%#- +'0.", *tmp))
tmp++;
i = ft_atoi(tmp); ft_putnbr(i);ft_putstr("|");
tmp[0] = '\0'; ft_putstr(s);ft_putstr("]");
if (i > ft_strlen(print))
{
if (!(tmp = (char *)malloc(sizeof(char) * (i + 1))))
return (NULL);
tmp[i] = '\0';
if (ft_strchr(s, '-'))
{
while (*print)
{
*tmp = *print;
tmp++;
print++;
i--;
}
while (i)
{
*tmp = ' ';
tmp++;
i--;
}
}
else if (ft_strchr(s, '0'))
{
i -= ft_strlen(print);
while (i)
{
*tmp = '0';
tmp++;
i--;
}
while (*print)
{
*tmp = *print;
tmp++;
print++;
}
}
else
{
i -= ft_strlen(print);
while (i)
{
*tmp = ' ';
tmp++;
i--;
}
while (*print)
{
*tmp = *print;
tmp++;
print++;
}
}
// ft_putstr(tmp);
print = tmp;
}
return (print);
}
@@ -129,9 +192,9 @@ char *ft_width(char *s, char *print, char *type)
*/
char *ft_flag_transform(char *s, char *print, char *type)
{
{ ft_putstr("[");ft_putstr(s);ft_putstr("]");
print = ft_precision(s, print, type);
print = ft_width(s, print, type);
print = ft_width(s, print);
// if ((i = flag_w(s)))
// {
// if (flag_-(&s))

42
main.c
View File

@@ -135,21 +135,35 @@ int main(int ac, char **av)
ft_printf("'%.s'\n\n", "string");
printf("'%.7i'\n", -123456);
ft_printf("'%.7i'\n\n", -123456);
printf("'%2i'\n", -123);
ft_printf("'%2i'\n\n", -123);
printf("'%0i'\n", -123);
ft_printf("'%0i'\n\n", -123);
printf("'%10i'\n", -123);
ft_printf("'%10i'\n\n", -123);
printf("'%*i'\n", 0,-123);
ft_printf("'%*i'\n\n", 0,-123);
printf("'%0s'\n", "string");
ft_printf("'%0s'\n\n", "string");
printf("'%10s'\n", "string");
ft_printf("'%10s'\n\n", "string");
printf("'%010s'\n", "string");
ft_printf("'%010s'\n\n", "string");
// #include <fcntl.h>
// ft_printf("Hi file\n");
// int out = open("out.txt", O_WRONLY);
// int save = dup(1);
// dup2(out, 1);
// ft_printf("Ho fole\n");
// dup2(save, 1);
// ft_printf("Ha fale\n");
// dup2(out, 1);
// ft_printf("Hu fule\n");
// dup2(save, 1);
// ft_printf("He fele\n");
// close(save);
// close(out);
// #include <fcntl.h>
// ft_printf("Hi file\n");
// int out = open("out.txt", O_WRONLY);
// int save = dup(1);
// dup2(out, 1);
// ft_printf("Ho fole\n");
// dup2(save, 1);
// ft_printf("Ha fale\n");
// dup2(out, 1);
// ft_printf("Hu fule\n");
// dup2(save, 1);
// ft_printf("He fele\n");
// close(save);
// close(out);
}