ecriture de la macro pour faire les tests, pas la partie detournement de stdout dans fichier
This commit is contained in:
1
Makefile
1
Makefile
@@ -18,6 +18,7 @@ SRCS = ft_printf.c \
|
||||
ft_next_word.c \
|
||||
aside.c \
|
||||
ft_convert.c \
|
||||
ft_width_n_precision.c \
|
||||
main.c
|
||||
|
||||
ODIR = ./builds
|
||||
|
||||
172
ft_printf.c
172
ft_printf.c
@@ -1,167 +1,6 @@
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
/*
|
||||
** -function that modify the string 'print' according to the precision flag :
|
||||
** if length(s) < precision, add x '0' bfr nbr, but after '-' if negative
|
||||
*/
|
||||
|
||||
char *precision_int(char *print, int precision)
|
||||
{
|
||||
int i;
|
||||
char *tmp;
|
||||
|
||||
i = ft_strlen(print);
|
||||
if (print[0] == '-')
|
||||
precision++;
|
||||
if (precision > i)
|
||||
{
|
||||
if (!(tmp = (char *)malloc(sizeof(char) * (precision + 1))))
|
||||
return (NULL);
|
||||
tmp[precision] = '\0';
|
||||
if (print[0] == '-')
|
||||
tmp[0] = '-';
|
||||
while (i)
|
||||
tmp[--precision] = print[--i];
|
||||
if (print[0] == '-')
|
||||
precision++;
|
||||
while (precision)
|
||||
tmp[--precision] = '0';
|
||||
if (print[0] == '-')
|
||||
tmp[0] = '-';
|
||||
free(print);
|
||||
print = tmp;
|
||||
}
|
||||
return (print);
|
||||
}
|
||||
|
||||
/*
|
||||
** -it first verify if there is a precision point, and if so, it execute a
|
||||
** serie of action listed below, otherwise return print as it is
|
||||
** ACTIONS :
|
||||
** -look for a '.'
|
||||
** -if followed by numbers, extract an int version of those numbers
|
||||
** -if the '.' is alone, gives value '0' to the int
|
||||
** -then removes the '.' and the numbers from the %string
|
||||
** -if flag '0' is present in %string, removes it (actually turn each occurence
|
||||
** in a '.')
|
||||
** -and transform 'print' according to the precision :
|
||||
** -1 if type is s: if length(s) > precision, removes end of 'print' to print
|
||||
** only x chars, with x = precision
|
||||
** -2 if type is "diouxX": call fonction 'precision_int' that return :
|
||||
** if length(s) < precision, add x '0' bfr nbr, but after '-' if negative
|
||||
** -3 if type is "aAeEfF": not covered
|
||||
** -4 si type is "gG": not covered
|
||||
** -5 else: error
|
||||
*/
|
||||
|
||||
char *ft_precision(char *s, char *print, char *type)
|
||||
{
|
||||
char *tmp;
|
||||
int precision;
|
||||
int i;
|
||||
|
||||
if ((tmp = ft_strchr(s, '.')))
|
||||
{
|
||||
precision = ft_atoi(tmp + 1);
|
||||
*tmp = '\0';
|
||||
while (ft_strchr("#- +'0", *(++s)))
|
||||
if (*s == '0')
|
||||
*s = '.';
|
||||
i = 0;
|
||||
if (ft_strchr(type, 's'))
|
||||
{
|
||||
while (i < precision && print[i])
|
||||
i++;
|
||||
if (print[i])
|
||||
print[i] = '\0';
|
||||
}
|
||||
if (ft_strchrset(type, "diouxX"))
|
||||
print = precision_int(print, precision);
|
||||
}
|
||||
return (print);
|
||||
}
|
||||
|
||||
/*
|
||||
**
|
||||
** 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 *tmp;
|
||||
int i; ft_putstr("[");ft_putstr(s);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);
|
||||
}
|
||||
|
||||
/*
|
||||
** FT_FLAG_TRANSFORM :
|
||||
** if i = flag_p(&s) | -precision is calculated before width,
|
||||
@@ -192,18 +31,9 @@ char *ft_width(char *s, char *print)
|
||||
*/
|
||||
|
||||
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);
|
||||
// if ((i = flag_w(s)))
|
||||
// {
|
||||
// if (flag_-(&s))
|
||||
// print = ft_rpadd(i, print);
|
||||
// else if (flag_0(&s))
|
||||
// print = ft_lpadd(i, print, '0');
|
||||
// else
|
||||
// print = ft_lpadd(i, print, ' ');
|
||||
// }
|
||||
|
||||
// if (flag_+(s)) //
|
||||
// else if (flag_space(s)) //
|
||||
|
||||
17
ft_printf.h
17
ft_printf.h
@@ -7,14 +7,29 @@
|
||||
|
||||
# include <stdio.h>
|
||||
|
||||
// ft_printf.c
|
||||
char *ft_flag_transform(char *s, char *print, char *type);
|
||||
int ft_printf(char *string, ...);
|
||||
|
||||
// ft_next_word.c
|
||||
int width_precision(char *s);
|
||||
int word_length(char *s);
|
||||
char *next_word(char **s);
|
||||
char *ft_convert(va_list ap, char *type);
|
||||
|
||||
// aside.c
|
||||
char *specifier(char *s);
|
||||
int ft_put_word(char *s);
|
||||
int ft_expand_star(int nbr, char **string);
|
||||
|
||||
// ft_convert.c
|
||||
char *conv_i(char c, long int i);
|
||||
char *conv_u(char c, unsigned long int i);
|
||||
char *ft_convert(va_list ap, char *type);
|
||||
|
||||
// ft_width_n_precision.c
|
||||
char *precision_int(char *print, int precision);
|
||||
char *ft_precision(char *s, char *print, char *type);
|
||||
char *width_flags(char *print, char *tmp, char *s, int width);
|
||||
char *ft_width(char *s, char *print);
|
||||
|
||||
#endif
|
||||
|
||||
76
main.c
76
main.c
@@ -86,6 +86,12 @@ int main(int ac, char **av)
|
||||
|
||||
if (ac == 2 && !strcmp(av[1], "test"))
|
||||
{
|
||||
|
||||
//#define PRINT(string, args...) printf("(%s, %s)\n", #string, #args); \
|
||||
// printf("'" string "'\n", args); \
|
||||
// ft_printf("'" string "'\n", args);
|
||||
// PRINT("%*i", 5, 123)
|
||||
|
||||
printf("sdf\n");
|
||||
ft_printf("sdf\n\n");
|
||||
printf("'%i'\n", 23);
|
||||
@@ -109,46 +115,48 @@ int main(int ac, char **av)
|
||||
ft_printf("'%lX'\n\n", 9223372036854775807);
|
||||
printf("'%p'\n", "dfgdf");
|
||||
ft_printf("'%p'\n\n", "dfgdf");
|
||||
printf("'%.i'\n", 123);
|
||||
ft_printf("'%.i'\n\n", 123);
|
||||
printf("'%.2i'\n", 123);
|
||||
ft_printf("'%.2i'\n\n", 123);
|
||||
printf("'%.i'\n", 121);
|
||||
ft_printf("'%.i'\n\n", 121);
|
||||
printf("'%.2i'\n", 122);
|
||||
ft_printf("'%.2i'\n\n", 122);
|
||||
printf("'%.25i'\n", 123);
|
||||
ft_printf("'%.25i'\n\n", 123);
|
||||
printf("'%0.6i'\n", 123);
|
||||
ft_printf("'%0.6i'\n\n", 123);
|
||||
printf("'%- 032.6i'\n", 123);
|
||||
ft_printf("'%- 032.6i'\n\n", 123);
|
||||
printf("'%0-0 32.6i'\n", 123);
|
||||
ft_printf("'%0-0 32.6i'\n\n", 123);
|
||||
printf("'%0-0.6i'\n", 123);
|
||||
ft_printf("'%0-0.6i'\n\n", 123);
|
||||
printf("'%0.6i'\n", 124);
|
||||
ft_printf("'%0.6i'\n\n", 124);
|
||||
printf("'%-032.6i'\n", 125);
|
||||
ft_printf("'%-032.6i'\n\n", 125);
|
||||
printf("'%0-032.6i'\n", 126);
|
||||
ft_printf("'%0-032.6i'\n\n", 126);
|
||||
printf("'%0-0.6i'\n", 127);
|
||||
ft_printf("'%0-0.6i'\n\n", 127);
|
||||
printf("'%s'\n", "string");
|
||||
ft_printf("'%s'\n\n", "string");
|
||||
printf("'%.7s'\n", "string");
|
||||
ft_printf("'%.7s'\n\n", "string");
|
||||
printf("'%.2s'\n", "string");
|
||||
ft_printf("'%.2s'\n\n", "string");
|
||||
printf("'%.0s'\n", "string");
|
||||
ft_printf("'%.0s'\n\n", "string");
|
||||
printf("'%.s'\n", "string");
|
||||
ft_printf("'%.s'\n\n", "string");
|
||||
printf("'%.7s'\n", "strong");
|
||||
ft_printf("'%.7s'\n\n", "strong");
|
||||
printf("'%.2s'\n", "strung");
|
||||
ft_printf("'%.2s'\n\n", "strung");
|
||||
printf("'%.0s'\n", "strang");
|
||||
ft_printf("'%.0s'\n\n", "strang");
|
||||
printf("'%.s'\n", "streng");
|
||||
ft_printf("'%.s'\n\n", "streng");
|
||||
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");
|
||||
printf("'%2i'\n", -128);
|
||||
ft_printf("'%2i'\n\n", -128);
|
||||
printf("'%0i'\n", -129);
|
||||
ft_printf("'%0i'\n\n", -129);
|
||||
printf("'%10i'\n", -130);
|
||||
ft_printf("'%10i'\n\n", -130);
|
||||
printf("'%*i'\n", 0,-131);
|
||||
ft_printf("'%*i'\n\n", 0,-131);
|
||||
printf("'%0s'\n", "stryng");
|
||||
ft_printf("'%0s'\n\n", "stryng");
|
||||
printf("'%10s'\n", "strxng");
|
||||
ft_printf("'%10s'\n\n", "strxng");
|
||||
printf("'%010s'\n", "strzng");
|
||||
ft_printf("'%010s'\n\n", "strzng");
|
||||
printf("'%010s'" "__TEST__\n", "strzng");
|
||||
ft_printf("'%010s'" "__TEST__\n\n", "strzng");
|
||||
|
||||
// #include <fcntl.h>
|
||||
// ft_printf("Hi file\n");
|
||||
|
||||
153
srcs/ft_width_n_precision.c
Normal file
153
srcs/ft_width_n_precision.c
Normal file
@@ -0,0 +1,153 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_width_n_precision.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/02/25 14:48:55 by hulamy #+# #+# */
|
||||
/* Updated: 2020/02/25 15:12:32 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
/*
|
||||
** -function that modify the string 'print' according to the precision flag :
|
||||
** if length(s) < precision, add x '0' bfr nbr, but after '-' if negative
|
||||
*/
|
||||
|
||||
char *precision_int(char *print, int precision)
|
||||
{
|
||||
int i;
|
||||
char *tmp;
|
||||
|
||||
i = ft_strlen(print);
|
||||
if (print[0] == '-')
|
||||
precision++;
|
||||
if (precision > i)
|
||||
{
|
||||
if (!(tmp = (char *)malloc(sizeof(char) * (precision + 1))))
|
||||
return (NULL);
|
||||
tmp[precision] = '\0';
|
||||
if (print[0] == '-')
|
||||
tmp[0] = '-';
|
||||
while (i)
|
||||
tmp[--precision] = print[--i];
|
||||
if (print[0] == '-')
|
||||
precision++;
|
||||
while (precision)
|
||||
tmp[--precision] = '0';
|
||||
if (print[0] == '-')
|
||||
tmp[0] = '-';
|
||||
free(print);
|
||||
print = tmp;
|
||||
}
|
||||
return (print);
|
||||
}
|
||||
|
||||
/*
|
||||
** -it first verify if there is a precision point, and if so, it execute a
|
||||
** serie of action listed below, otherwise return print as it is
|
||||
** ACTIONS :
|
||||
** -look for a '.'
|
||||
** -if followed by numbers, extract an int version of those numbers
|
||||
** -if the '.' is alone, gives value '0' to the int
|
||||
** -then removes the '.' and the numbers from the %string
|
||||
** -if flag '0' is present in %string, removes it (actually turn each occurence
|
||||
** in a '.')
|
||||
** -and transform 'print' according to the precision :
|
||||
** -1 if type is s: if length(s) > precision, removes end of 'print' to print
|
||||
** only x chars, with x = precision
|
||||
** -2 if type is "diouxX": call fonction 'precision_int' that return :
|
||||
** if length(s) < precision, add x '0' bfr nbr, but after '-' if negative
|
||||
** -3 if type is "aAeEfF": not covered
|
||||
** -4 si type is "gG": not covered
|
||||
** -5 else: error
|
||||
*/
|
||||
|
||||
char *ft_precision(char *s, char *print, char *type)
|
||||
{
|
||||
char *tmp;
|
||||
int precision;
|
||||
int i;
|
||||
|
||||
if ((tmp = ft_strchr(s, '.')))
|
||||
{
|
||||
precision = ft_atoi(tmp + 1);
|
||||
*tmp = '\0';
|
||||
while (ft_strchr("#- +'0", *(++s)))
|
||||
if (*s == '0')
|
||||
*s = '.';
|
||||
i = 0;
|
||||
if (ft_strchr(type, 's'))
|
||||
{
|
||||
while (i < precision && print[i])
|
||||
i++;
|
||||
if (print[i])
|
||||
print[i] = '\0';
|
||||
}
|
||||
if (ft_strchrset(type, "diouxX"))
|
||||
print = precision_int(print, precision);
|
||||
}
|
||||
return (print);
|
||||
}
|
||||
|
||||
/*
|
||||
** -if flag '-' is present, put extra width as ' ' to right of 'print'
|
||||
** -if flag '0' is present, put extra width as '0' to left of 'print'
|
||||
** -else, put extra width as ' ' to left of 'print'
|
||||
*/
|
||||
|
||||
char *width_flags(char *print, char *tmp, char *s, int width)
|
||||
{
|
||||
char c;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
if (ft_strchr(s, '-'))
|
||||
{
|
||||
while (print[j])
|
||||
tmp[i++] = print[j++];
|
||||
while (i < width)
|
||||
tmp[i++] = ' ';
|
||||
}
|
||||
else
|
||||
{
|
||||
c = (ft_strchr(s, '0')) ? '0' : ' ' ;
|
||||
while (i < (width - ft_strlen(print)))
|
||||
tmp[i++] = c;
|
||||
while (print[j])
|
||||
tmp[i++] = print[j++];
|
||||
}
|
||||
free(print);
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
** -if there is a minimal width field, calculate it and add it to print
|
||||
** according to the flags '-' and '0' if present
|
||||
*/
|
||||
|
||||
char *ft_width(char *s, char *print)
|
||||
{
|
||||
char *tmp;
|
||||
int width;
|
||||
|
||||
tmp = s;
|
||||
while (*tmp != '\0' && ft_strchr("%#- +'0.", *tmp))
|
||||
tmp++;
|
||||
width = ft_atoi(tmp);
|
||||
tmp[0] = '\0';
|
||||
if (width > ft_strlen(print))
|
||||
{
|
||||
if (!(tmp = (char *)malloc(sizeof(char) * (width + 1))))
|
||||
return (NULL);
|
||||
tmp[width] = '\0';
|
||||
print = width_flags(print, tmp, s, width);
|
||||
}
|
||||
return (print);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user