main ameliore avec comparaison output nbr

This commit is contained in:
hugodu69
2020-03-07 14:29:42 +01:00
parent a94b17240b
commit c27d8d8327
3 changed files with 125 additions and 102 deletions

View File

@@ -51,7 +51,7 @@ char *ft_convert(va_list ap, char *type, char **s);
char *precision_int(char *print, int precision); char *precision_int(char *print, int precision);
char *ft_precision(char *s, char *print, char *type); char *ft_precision(char *s, char *print, char *type);
char *width_flags(char *print, char *tmp, char *s, int width, int zero); char *width_flags(char *print, char *s, int width, int zero);
char *ft_width(char *s, char *print, int *size); char *ft_width(char *s, char *print, int *size);
char *ft_flag_transform(char *s, char *print, char *type, int *size); char *ft_flag_transform(char *s, char *print, char *type, int *size);

133
main.c
View File

@@ -10,15 +10,19 @@
// then the result of ft_printf, // then the result of ft_printf,
// and finally redirect the output into a file to compare // and finally redirect the output into a file to compare
#define PRINT(string, args...) \ #define PRINT(string, args...) \
\
outf = open("outf.txt", O_WRONLY | O_TRUNC); \ outf = open("outf.txt", O_WRONLY | O_TRUNC); \
outft = open("outft.txt", O_WRONLY | O_TRUNC); \ outft = open("outft.txt", O_WRONLY | O_TRUNC); \
\ \
dup2(outf, 1); \ dup2(outf, 1); \
printf(string "\n", ##args); \ pout = printf(string, ##args); \
fflush(stdout); \
printf("\n"); \
fflush(stdout); \ fflush(stdout); \
\ \
dup2(outft, 1); \ dup2(outft, 1); \
if ((ft_printf(string, ##args)) == -1) printf("\033[91mERROR\033[0m"); \ ftpout = ft_printf(string, ##args); \
if (ftpout == -1) printf("\033[91mERROR\033[0m"); \
fflush(stdout); \ fflush(stdout); \
printf("\n"); \ printf("\n"); \
fflush(stdout); \ fflush(stdout); \
@@ -33,15 +37,22 @@
fflush(stdout); \ fflush(stdout); \
printf("%*s", (int)(40 - ft_strlen(#string) - ft_strlen(#args)), ": "); \ printf("%*s", (int)(40 - ft_strlen(#string) - ft_strlen(#args)), ": "); \
fflush(stdout); \ fflush(stdout); \
printf("'" string "'\n", ##args); \ printf("'"); \
fflush(stdout); \ fflush(stdout); \
printf("%s", ft_compare(outf, outft, &error)); \ printf(string, ##args); \
fflush(stdout); \
printf("' [%i]\n", pout); \
fflush(stdout); \
printf("%s", ft_compare(outf, outft, &error, (ftpout == pout))); \
fflush(stdout); \ fflush(stdout); \
printf("%38s", ": '"); \ printf("%38s", ": '"); \
fflush(stdout); \ fflush(stdout); \
if ((ft_printf(string, ##args)) == -1) printf("\033[91mERROR\033[0m"); \ if ((ft_printf(string, ##args)) == -1) printf("\033[91mERROR\033[0m"); \
fflush(stdout); \ fflush(stdout); \
printf("'\n\n"); \ if (ftpout != pout) \
printf("' \033[91m[%i]\033[0m\n\n", ftpout); \
else \
printf("' [%i]\n\n", ftpout); \
fflush(stdout); \ fflush(stdout); \
\ \
close(outf); \ close(outf); \
@@ -54,13 +65,18 @@
** and it read them and compare them ** and it read them and compare them
*/ */
char *ft_compare(int fd1, int fd2, int *error) char *ft_compare(int fd1, int fd2, int *error, int output)
{ {
int ret1 = 1; int ret1 = 1;
int ret2 = 1; int ret2 = 1;
char *line = NULL; char *line = NULL;
char *tmp = NULL; char *tmp = NULL;
if (!output)
{
(*error)++;
return ("\033[91mHO HO..\033[0m");
}
while (ret1 > 0 && ret2 > 0) while (ret1 > 0 && ret2 > 0)
{ {
ret1 = get_next_line(fd1, &line); ret1 = get_next_line(fd1, &line);
@@ -142,12 +158,20 @@ int main(int ac, char **av)
{ {
setlocale(LC_ALL, "en_US.UTF-8"); setlocale(LC_ALL, "en_US.UTF-8");
// those two ints will contain the fd for files that stores output
int outf; int outf;
int outft; int outft;
// this int save the fd of the standard output
int save = dup(1);
// those two command create the two files to store the output
// 0644 are the permissions for the file
open("outf.txt", O_CREAT, 0644); open("outf.txt", O_CREAT, 0644);
open("outft.txt", O_CREAT, 0644); open("outft.txt", O_CREAT, 0644);
// error is used to count the total nbr of error
static int error = 0; static int error = 0;
int save = dup(1); // those two ints saves the return value of printf and ft_printf
int pout;
int ftpout;
/* ////////////////////////////////////////////////////////////////// */ /* ////////////////////////////////////////////////////////////////// */
@@ -160,7 +184,6 @@ int main(int ac, char **av)
printf("call ./ft_printf with arguments to launch tests :\n"); printf("call ./ft_printf with arguments to launch tests :\n");
printf("./ft_printf 'man' \n"); printf("./ft_printf 'man' \n");
printf("........... 'test' \n"); printf("........... 'test' \n");
printf("........... 'special' \n");
printf("........... 'all' \n"); printf("........... 'all' \n");
printf("........... ... 'noflag' \n"); printf("........... ... 'noflag' \n");
printf("........... ... '0' \n"); printf("........... ... '0' \n");
@@ -233,55 +256,51 @@ int main(int ac, char **av)
if (ac == 2 && !strcmp(av[1], "test")) if (ac == 2 && !strcmp(av[1], "test"))
{ {
PRINT("sdf"); // PRINT("sdf");
PRINT("%i", 23); // PRINT("%i", 23);
long int k = -23; // long int k = -23;
PRINT("%li", k); // PRINT("%li", k);
PRINT("%i", -32); // PRINT("%i", -32);
PRINT("%li", 9223372036854775807); // PRINT("%li", 9223372036854775807);
PRINT("%c", 'f'); // PRINT("%c", 'f');
PRINT("%s", "sdffhk"); // PRINT("%s", "sdffhk");
PRINT("%u", 1221879); // PRINT("%u", 1221879);
PRINT("%x", 3287); // PRINT("%x", 3287);
PRINT("%lX", 9223372036854775807); // PRINT("%lX", 9223372036854775807);
PRINT("%p", "dfgdf"); // PRINT("%p", "dfgdf");
PRINT("%.i", 121); // PRINT("%.i", 121);
PRINT("%.2i", 122); // PRINT("%.2i", 122);
PRINT("%.25i", 123); // PRINT("%.25i", 123);
PRINT("%0.6i", 124); // PRINT("%0.6i", 124);
//PRINT("%-032.6i", 125); // '0' and '-' not compatible // //PRINT("%-032.6i", 125); // '0' and '-' not compatible
//PRINT("%0-032.6i", 126); // '0' and '-' not compatible // //PRINT("%0-032.6i", 126); // '0' and '-' not compatible
//PRINT("%0-0.6i", 127); // '0' and '-' not compatible // //PRINT("%0-0.6i", 127); // '0' and '-' not compatible
PRINT("%s", "string"); // PRINT("%s", "string");
PRINT("%.7s", "strong"); // PRINT("%.7s", "strong");
PRINT("%.2s", "strung"); // PRINT("%.2s", "strung");
PRINT("%.0s", "strang"); // PRINT("%.0s", "strang");
PRINT("%.s", "streng"); // PRINT("%.s", "streng");
PRINT("%.7i", -123456); // PRINT("%.7i", -123456);
PRINT("%2i", -128); // PRINT("%2i", -128);
PRINT("%0i", -129); // PRINT("%0i", -129);
PRINT("%10i", -130); // PRINT("%10i", -130);
PRINT("%*i", 0,-131); // PRINT("%*i", 0,-131);
//PRINT("%0s", "stryng"); // '0' not compatible with string // //PRINT("%0s", "stryng"); // '0' not compatible with string
PRINT("%10s", "strxng"); // PRINT("%10s", "strxng");
//PRINT("%010s", "strzng"); // '0' not compatible with string // //PRINT("%010s", "strzng"); // '0' not compatible with string
PRINT("%s" "__TEST__", "strzng"); // PRINT("%s" "__TEST__", "strzng");
char *s = "hess"; // char *s = "hess";
PRINT("%s", s) // PRINT("%s", s)
} // PRINT("%16c", (char)0)
// PRINT("%-16c", (char)0)
/* ////////////////////////////////////////////////////////////////// */ // PRINT("%16c", 'a')
/* TESTS FROM THE PRINT UNIT TEST */ // PRINT("%-16c", 'a')
/* ////////////////////////////////////////////////////////////////// */ // PRINT("%16c", (char)7)
// PRINT("!%37c!", (char)0)
if (ac == 2 && !strcmp(av[1], "special")) PRINT("%.0i", 0);
{ PRINT("%.0i", 000);
PRINT("%16c", (char)0) PRINT("%.0X", 0);
PRINT("%-16c", (char)0) PRINT("%.i", 0);
PRINT("%16c", 'a')
PRINT("%-16c", 'a')
PRINT("%16c", (char)7)
PRINT("!%37c!", (char)0)
} }
/* ////////////////////////////////////////////////////////////////// */ /* ////////////////////////////////////////////////////////////////// */

View File

@@ -91,14 +91,17 @@ char *ft_precision(char *s, char *print, char *type)
** -else, put extra width as ' ' to left of 'print' ** -else, put extra width as ' ' to left of 'print'
*/ */
char *width_flags(char *print, char *tmp, char *s, int width, int zero) char *width_flags(char *print, char *s, int width, int zero)
{ {
char *tmp;
char c; char c;
int i; int i;
int j; int j;
i = 0; i = 0;
j = 0; j = 0;
if(!(tmp = ft_strnew(width)))
return (NULL);
if (ft_strchr(s, '-')) if (ft_strchr(s, '-'))
{ {
if (print[j] == '\0') if (print[j] == '\0')
@@ -126,11 +129,17 @@ char *width_flags(char *print, char *tmp, char *s, int width, int zero)
** -if there is a minimal width field, calculate it and add it to print ** -if there is a minimal width field, calculate it and add it to print
** according to the flags '-' and '0' if present ** according to the flags '-' and '0' if present
** -in details : ** -in details :
** 0 if print[0] value 0, as it happens for type c with (char)0, save it for
** later in 'zero'
** 1 loop through s, the string starting by '%' and ending by a converter, ** 1 loop through s, the string starting by '%' and ending by a converter,
** until it has passed all the flags that are not a potentiel width field ** until it has passed all the flags that are not a potentiel width field
** 2 then if it's the end of s, there is no width and print isn't changed, ** 2 then if it's the end of s, there is no width and print isn't changed,
** otherwise the int 'width' take the value returned by atoi ** otherwise the int 'size' take the value returned by atoi
** 3 if print[0] value 0, as it happens for type c with (char)0, ** in case print isn't changed, 'size' is the length of 'print'
** 3 then if the size of the width shield is bigger than the size of print
** (plus zero in case print is a (char)0), call 'width_flags' that will
** create a new char* to contain the string to print after transformation
** 4 otherwise 'size' is the length of print + zero
*/ */
char *ft_width(char *s, char *print, int *size) char *ft_width(char *s, char *print, int *size)
@@ -140,26 +149,21 @@ char *ft_width(char *s, char *print, int *size)
tmp = s; tmp = s;
zero = 0; zero = 0;
if (print[0] == '\0')
zero = 1;
while (*tmp != '\0' && ft_strchr("%#- +'0.", *tmp)) while (*tmp != '\0' && ft_strchr("%#- +'0.", *tmp))
tmp++; tmp++;
if (*tmp == '\0') if (*tmp == '\0')
{ {
*size = ft_strlen(print); *size = ft_strlen(print) + zero;
return (print); return (print);
} }
*size = ft_atoi(tmp); *size = ft_atoi(tmp);
tmp[0] = '\0'; tmp[0] = '\0';
if (print[0] == '\0')
zero = 1;
if (*size > ft_strlen(print) + zero) if (*size > ft_strlen(print) + zero)
{ print = width_flags(print, s, *size, zero);
if (!(tmp = (char *)malloc(sizeof(char) * (*size + 1))))
return (NULL);
tmp[*size] = '\0';
print = width_flags(print, tmp, s, *size, zero);
}
else else
*size = ft_strlen(print); *size = ft_strlen(print) + zero;
return (print); return (print);
} }