if ftprintf send error it write it into the file and on stdout

This commit is contained in:
Hugo LAMY
2020-02-26 19:30:26 +01:00
parent 4692903f8e
commit 9c627da9cb
7 changed files with 102 additions and 91 deletions

BIN
ft_printf

Binary file not shown.

174
main.c
View File

@@ -18,7 +18,9 @@
fflush(stdout); \ fflush(stdout); \
\ \
dup2(outft, 1); \ dup2(outft, 1); \
ft_printf(string "\n", ##args); \ if ((ft_printf(string, ##args)) == -1) printf("\033[91mERROR\033[0m"); \
fflush(stdout); \
printf("\n"); \
fflush(stdout); \ fflush(stdout); \
\ \
close(outf); \ close(outf); \
@@ -37,7 +39,7 @@
fflush(stdout); \ fflush(stdout); \
printf("%38s", ": '"); \ printf("%38s", ": '"); \
fflush(stdout); \ fflush(stdout); \
ft_printf(string, ##args); \ if ((ft_printf(string, ##args)) == -1) printf("\033[91mERROR\033[0m"); \
fflush(stdout); \ fflush(stdout); \
printf("'\n\n"); \ printf("'\n\n"); \
fflush(stdout); \ fflush(stdout); \
@@ -153,39 +155,39 @@ int main(int ac, char **av)
printf("USAGE:\n"); printf("USAGE:\n");
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(" 'all' \n"); printf("........... 'all' \n");
printf(" 'noflag' \n"); printf("........... ... 'noflag' \n");
printf(" '0' \n"); printf("........... ... '0' \n");
printf(" '-' \n"); printf("........... ... '-' \n");
printf(" 'width' \n"); printf("........... ... 'width' \n");
printf(" 'precision' \n"); printf("........... ... 'precision' \n");
printf(" '*' \n"); printf("........... ... '*' \n");
printf(" 'd' \n"); printf("........... ... 'd' \n");
printf(" 'i' \n"); printf("........... ... 'i' \n");
printf(" 'u' \n"); printf("........... ... 'u' \n");
printf(" 'x' \n"); printf("........... ... 'x' \n");
printf(" 'X' \n"); printf("........... ... 'X' \n");
printf(" 'c' \n"); printf("........... ... 'c' \n");
printf(" 's' \n"); printf("........... ... 's' \n");
printf(" 'p' \n"); printf("........... ... 'p' \n");
printf(" '%%' \n"); printf("........... ... '%%' \n");
printf(" 'repetition'\n"); printf("........... ... 'repetition'\n");
printf(" 'bonus' \n"); printf("........... 'bonus' \n");
printf(" '#' \n"); printf("........... ..... '#' \n");
printf(" ''' \n"); printf("........... ..... ''' \n");
printf(" ' ' \n"); printf("........... ..... ' ' \n");
printf(" '+' \n"); printf("........... ..... '+' \n");
printf(" 'e' \n"); printf("........... ..... 'e' \n");
printf(" 'f' \n"); printf("........... ..... 'f' \n");
printf(" 'g' \n"); printf("........... ..... 'g' \n");
printf(" 'n' \n"); printf("........... ..... 'n' \n");
printf(" 'h' \n"); printf("........... ..... 'h' \n");
printf(" 'hh' \n"); printf("........... ..... 'hh' \n");
printf(" 'l' \n"); printf("........... ..... 'l' \n");
printf(" 'll' \n"); printf("........... ..... 'll' \n");
printf(" 'repetition'\n"); printf("........... ..... 'repetition'\n");
printf(" 'error' \n"); printf("........... 'error' \n");
} }
if (ac == 2 && !strcmp(av[1], "man")) if (ac == 2 && !strcmp(av[1], "man"))
@@ -242,9 +244,9 @@ int main(int ac, char **av)
// PRINT("%.2i", 122); // PRINT("%.2i", 122);
// PRINT("%.25i", 123); // PRINT("%.25i", 123);
// PRINT("%0.6i", 124); // PRINT("%0.6i", 124);
// // PRINT("%-032.6i", 125); // //PRINT("%-032.6i", 125); // '0' and '-' not compatible
// // PRINT("%0-032.6i", 126); // //PRINT("%0-032.6i", 126); // '0' and '-' not compatible
// // PRINT("%0-0.6i", 127); // //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");
@@ -255,17 +257,11 @@ int main(int ac, char **av)
// PRINT("%0i", -129); // PRINT("%0i", -129);
// PRINT("%10i", -130); // PRINT("%10i", -130);
// PRINT("%*i", 0,-131); // PRINT("%*i", 0,-131);
// // PRINT("%0s", "stryng"); // //PRINT("%0s", "stryng"); // '0' not compatible with string
// PRINT("%10s", "strxng"); // PRINT("%10s", "strxng");
// // PRINT("%010s", "strzng"); // //PRINT("%010s", "strzng"); // '0' not compatible with string
// // PRINT("%010s" "__TEST__", "strzng"); // PRINT("%s" "__TEST__", "strzng");
PRINT("%%"); PRINT("%.0X", 0);
PRINT("%.10%");
PRINT("%10%");
PRINT("%-10%");
PRINT("%010%");
PRINT("%-010%");
} }
/* ////////////////////////////////////////////////////////////////// */ /* ////////////////////////////////////////////////////////////////// */
@@ -376,6 +372,7 @@ int main(int ac, char **av)
PRINT("%-9.7X", 8645); PRINT("%-9.7X", 8645);
PRINT("%7.9X", 8645); PRINT("%7.9X", 8645);
PRINT("%.0i", 0); PRINT("%.0i", 0);
PRINT("%.0i", 000);
PRINT("%.0X", 0); PRINT("%.0X", 0);
PRINT("%.i", 0); PRINT("%.i", 0);
PRINT("%i", 0); PRINT("%i", 0);
@@ -473,43 +470,18 @@ int main(int ac, char **av)
printf("----------------------------------------------------------------\n\n"); printf("----------------------------------------------------------------\n\n");
PRINT("%i", 33333); PRINT("%i", 33333);
PRINT("%.0f", 33333.0);
PRINT("%08i", 33333); PRINT("%08i", 33333);
PRINT("%-8i", 33333); PRINT("%-8i", 33333);
printf("\nrepetition of flag '0' --------------------------------------------\n"); printf("\nrepetition of flag '0' -----------------------------------------\n");
printf("'0' and '-' not compatible ----------------------------------------\n\n"); printf("'0' and '-' not compatible -------------------------------------\n\n");
PRINT("%0000i", 33333); PRINT("%0000i", 33333);
PRINT("%0#.0f", 33333.0);
PRINT("%#0.0f", 33333.0);
PRINT("%0#0.0f", 33333.0);
PRINT("%0+8i", 33333);
PRINT("%+08i", 33333);
PRINT("%0+08i", 33333);
PRINT("%0 i", 33333);
PRINT("% 0i", 33333);
PRINT("%0 0i", 33333);
PRINT("%0'i", 33333);
PRINT("%'0i", 33333);
PRINT("%0'0i", 33333);
printf("\nrepetition of flag '-' --------------------------------------------\n"); printf("\nrepetition of flag '-' -----------------------------------------\n");
printf("'0' and '-' not compatible ----------------------------------------\n\n"); printf("'0' and '-' not compatible -------------------------------------\n\n");
PRINT("%----i", 33333); PRINT("%----i", 33333);
PRINT("%-#.0f", 33333.0);
PRINT("%#-.0f", 33333.0);
PRINT("%-#-.0f", 33333.0);
PRINT("%-+8i", 33333);
PRINT("%+-8i", 33333);
PRINT("%-+-8i", 33333);
PRINT("%- i", 33333);
PRINT("% -i", 33333);
PRINT("%- -i", 33333);
PRINT("%-'i", 33333);
PRINT("%'-i", 33333);
PRINT("%-'-i", 33333);
} }
} }
@@ -618,7 +590,7 @@ int main(int ac, char **av)
if (ac == 2 || !strcmp(av[2], "repetition")) if (ac == 2 || !strcmp(av[2], "repetition"))
{ {
printf("\n----------------------------------------------------------------\n"); printf("\n----------------------------------------------------------------\n");
printf("tests repetitions of flags [#' +]\n"); printf("tests repetitions of flags [0-#' +]\n");
printf("----------------------------------------------------------------\n\n"); printf("----------------------------------------------------------------\n\n");
PRINT("%i", 33333); PRINT("%i", 33333);
@@ -630,7 +602,41 @@ int main(int ac, char **av)
PRINT("% i", 33333); PRINT("% i", 33333);
PRINT("%'i", 33333); PRINT("%'i", 33333);
printf("\nrepetition of flag '#' --------------------------------------------\n"); printf("\nrepetition of flag '0' -----------------------------------------\n");
printf("'0' and '-' not compatible -------------------------------------\n\n");
PRINT("%0000i", 33333);
PRINT("%0#.0f", 33333.0);
PRINT("%#0.0f", 33333.0);
PRINT("%0#0.0f", 33333.0);
PRINT("%0+8i", 33333);
PRINT("%+08i", 33333);
PRINT("%0+08i", 33333);
PRINT("%0 i", 33333);
PRINT("% 0i", 33333);
PRINT("%0 0i", 33333);
PRINT("%0'i", 33333);
PRINT("%'0i", 33333);
PRINT("%0'0i", 33333);
printf("\nrepetition of flag '-' -----------------------------------------\n");
printf("'0' and '-' not compatible -------------------------------------\n\n");
PRINT("%----i", 33333);
PRINT("%-#.0f", 33333.0);
PRINT("%#-.0f", 33333.0);
PRINT("%-#-.0f", 33333.0);
PRINT("%-+8i", 33333);
PRINT("%+-8i", 33333);
PRINT("%-+-8i", 33333);
PRINT("%- i", 33333);
PRINT("% -i", 33333);
PRINT("%- -i", 33333);
PRINT("%-'i", 33333);
PRINT("%'-i", 33333);
PRINT("%-'-i", 33333);
printf("\nrepetition of flag '#' -----------------------------------------\n");
PRINT("%####f", 33333.0); PRINT("%####f", 33333.0);
PRINT("%#+.0f", 33333.0); PRINT("%#+.0f", 33333.0);
@@ -649,7 +655,7 @@ int main(int ac, char **av)
PRINT("% #f", 33333.0); PRINT("% #f", 33333.0);
PRINT("%# #f", 33333.0); PRINT("%# #f", 33333.0);
printf("\nrepetition of flag ''' --------------------------------------------\n\n"); printf("\nrepetition of flag ''' -----------------------------------------\n\n");
PRINT("%''''i", 33333); PRINT("%''''i", 33333);
PRINT("%'#.0f", 33333.0); PRINT("%'#.0f", 33333.0);
@@ -668,8 +674,8 @@ int main(int ac, char **av)
PRINT("% 'i", 33333); PRINT("% 'i", 33333);
PRINT("%' 'i", 33333); PRINT("%' 'i", 33333);
printf("\nrepetition of flag ' ' --------------------------------------------\n"); printf("\nrepetition of flag ' ' -----------------------------------------\n");
printf("'+' and ' ' not compatible ----------------------------------------\n\n"); printf("'+' and ' ' not compatible -------------------------------------\n\n");
PRINT("% i", 33333); PRINT("% i", 33333);
PRINT("% #.0f", 33333.0); PRINT("% #.0f", 33333.0);
@@ -685,8 +691,8 @@ int main(int ac, char **av)
PRINT("%' i", 33333); PRINT("%' i", 33333);
PRINT("% ' i", 33333); PRINT("% ' i", 33333);
printf("\nrepetition of flag '+' --------------------------------------------\n"); printf("\nrepetition of flag '+' -----------------------------------------\n");
printf("'+' and ' ' not compatible ----------------------------------------\n\n"); printf("'+' and ' ' not compatible -------------------------------------\n\n");
PRINT("%++++i", 33333); PRINT("%++++i", 33333);
PRINT("%+#.0f", 33333.0); PRINT("%+#.0f", 33333.0);

View File

@@ -1 +1 @@
33,333

View File

@@ -1 +1 @@
33333 ERROR

View File

@@ -72,6 +72,8 @@ char *ft_convert(va_list ap, char *type)
return (conv_u(s[0], va_arg(ap, unsigned int))); return (conv_u(s[0], va_arg(ap, unsigned int)));
if ((s = ft_strchrset(type, "s"))) if ((s = ft_strchrset(type, "s")))
return (conv_u(s[0], va_arg(ap, unsigned long int))); return (conv_u(s[0], va_arg(ap, unsigned long int)));
if (ft_strchr(type, '%'))
return(ft_strdup("%"));
if (ft_strchrset(type, "efgn")) if (ft_strchrset(type, "efgn"))
return (NULL); return (NULL);
return (NULL); return (NULL);

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 13:58:30 by hulamy #+# #+# */ /* Created: 2020/02/10 13:58:30 by hulamy #+# #+# */
/* Updated: 2020/02/24 14:33:47 by hulamy ### ########.fr */ /* Updated: 2020/02/26 18:24:04 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/25 14:48:55 by hulamy #+# #+# */ /* Created: 2020/02/25 14:48:55 by hulamy #+# #+# */
/* Updated: 2020/02/25 15:12:32 by hulamy ### ########.fr */ /* Updated: 2020/02/26 19:24:18 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -57,12 +57,13 @@ char *precision_int(char *print, int precision)
** -if flag '0' is present in %string, removes it (actually turn each occurence ** -if flag '0' is present in %string, removes it (actually turn each occurence
** in a '.') ** in a '.')
** -and transform 'print' according to the precision : ** -and transform 'print' according to the precision :
** -0 if .precision == 0 and print == "0": return (print[0] = '\0') (print nothing)
** -1 if type is s: if length(s) > precision, removes end of 'print' to print ** -1 if type is s: if length(s) > precision, removes end of 'print' to print
** only x chars, with x = precision ** only x chars, with x = precision
** -2 if type is "diouxX": call fonction 'precision_int' that return : ** -2 if type is "diouxX": call fonction 'precision_int' that return :
** if length(s) < precision, add x '0' bfr nbr, but after '-' if negative ** if length(s) < precision, add x '0' bfr nbr, but after '-' if negative
** -3 if type is "aAeEfF": not covered ** -3 if type is "aAeEfF": not covered
** -4 si type is "gG": not covered ** -4 if type is "gG": not covered
** -5 else: error ** -5 else: error
*/ */
@@ -80,14 +81,16 @@ char *ft_precision(char *s, char *print, char *type)
if (*s == '0') if (*s == '0')
*s = '.'; *s = '.';
i = 0; i = 0;
if (ft_strchr(type, 's')) if (precision == 0 && !ft_strcmp(print, "0"))
print[0] = '\0';
else if (ft_strchr(type, 's'))
{ {
while (i < precision && print[i]) while (i < precision && print[i])
i++; i++;
if (print[i]) if (print[i])
print[i] = '\0'; print[i] = '\0';
} }
if (ft_strchrset(type, "diouxX")) else if (ft_strchrset(type, "diouxX"))
print = precision_int(print, precision); print = precision_int(print, precision);
} }
return (print); return (print);