Move dispatcher to ft_vprintf_percent.c

This commit is contained in:
gbaconni
2022-04-18 00:52:18 +02:00
parent 0269a1dc70
commit 0cd1d2ef1b
10 changed files with 141 additions and 39 deletions

90
main.c
View File

@@ -6,7 +6,7 @@
/* By: baco <baco@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/13 06:58:46 by gbaconni #+# #+# */
/* Updated: 2022/04/17 18:51:22 by gbaconni ### lausanne.ch */
/* Updated: 2022/04/18 00:50:01 by gbaconni ### lausanne.ch */
/* */
/* ************************************************************************** */
@@ -94,6 +94,65 @@ int ft_sprintf(char *str, const char *format, ...)
}
}
size_t ft_unescape_len(const char *str)
{
size_t len;
len = 0;
while (*str != '\0')
{
if (*str == '\\')
str++;
len++;
str++;
}
return (len);
}
char ft_unescape_char(const char c)
{
if (c == 'a')
return ('\a');
else if (c == 'b')
return ('\b');
else if (c == 't')
return ('\t');
else if (c == 'n')
return ('\n');
else if (c == 'v')
return ('\v');
else if (c == 'f')
return ('\f');
else if (c == 'r')
return ('\r');
else if (c == 'e')
return ('\033');
else
return (c);
}
char *ft_unescape(const char *str)
{
char *s;
void *ptr;
s = (char *) ft_calloc(ft_unescape_len(str), sizeof(char));
if (s == NULL)
return (NULL);
ptr = s;
while (*str != '\0')
{
if (*str == '\\')
*s++ = ft_unescape_char(*++str);
else
*s++ = *str;
str++;
}
*s = '\0';
s = ptr;
return (s);
}
int main(int argc, char *argv[])
{
int ft_ret;
@@ -101,6 +160,7 @@ int main(int argc, char *argv[])
char out[256];
char ft_out[256];
char *format;
char *f;
int d;
char c;
char *s;
@@ -116,18 +176,19 @@ int main(int argc, char *argv[])
(void)s;
if (argc > 1)
{
format = argv[1];
f = argv[1];
format = ft_unescape(f);
if (format[0] == '%')
{
if (format[1] == 'c')
{
c = argv[2][0];
ret = sprintf(out, format, c);
printf("%d = printf(\"%s\", '%c')\n%s\n", ret, format, c, out);
printf("%d = printf(\"%s\", '%c')\n%s\n", ret, f, c, out);
ft_begin(fd);
ft_ret = ft_printf(format, c);
ft_end(fd, ft_out);
printf("%d = ft_printf(\"%s\", '%c')\n%s\n\n", ft_ret, format, c, ft_out);
printf("%d = ft_printf(\"%s\", '%c')\n%s\n\n", ft_ret, f, c, ft_out);
assert(ret == ft_ret);
assert(strcmp(out, ft_out) == 0);
}
@@ -135,11 +196,11 @@ int main(int argc, char *argv[])
{
s = argv[2];
ret = sprintf(out, format, s);
printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, format, s, out);
printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, f, s, out);
ft_begin(fd);
ft_ret = ft_printf(format, s);
ft_end(fd, ft_out);
printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, format, s, ft_out);
printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, f, s, ft_out);
assert(ret == ft_ret);
assert(strcmp(out, ft_out) == 0);
}
@@ -152,11 +213,11 @@ int main(int argc, char *argv[])
ptr = NULL;
}
ret = sprintf(out, format, ptr);
printf("%d = printf(\"%s\", %p)\n%s\n", ret, format, ptr, out);
printf("%d = printf(\"%s\", %p)\n%s\n", ret, f, ptr, out);
ft_begin(fd);
ft_ret = ft_printf(format, ptr);
ft_end(fd, ft_out);
printf("%d = ft_printf(\"%s\", %p)\n%s\n\n", ft_ret, format, ptr, ft_out);
printf("%d = ft_printf(\"%s\", %p)\n%s\n\n", ft_ret, f, ptr, ft_out);
assert(ret == ft_ret);
assert(strcmp(out, ft_out) == 0);
}
@@ -164,11 +225,11 @@ int main(int argc, char *argv[])
{
d = atoi(argv[2]);
ret = sprintf(out, format, d);
printf("%d = printf(\"%s\", %d)\n%s\n", ret, format, d, out);
printf("%d = printf(\"%s\", %d)\n%s\n", ret, f, d, out);
ft_begin(fd);
ft_ret = ft_printf(format, d);
ft_end(fd, ft_out);
printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, format, d, ft_out);
printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, f, d, ft_out);
assert(ret == ft_ret);
assert(strcmp(out, ft_out) == 0);
}
@@ -176,11 +237,11 @@ int main(int argc, char *argv[])
{
d = atoi(argv[2]);
ret = sprintf(out, format, d);
printf("%d = printf(\"%s\", %d)\n%s\n", ret, format, d, out);
printf("%d = printf(\"%s\", %d)\n%s\n", ret, f, d, out);
ft_begin(fd);
ft_ret = ft_printf(format, d);
ft_end(fd, ft_out);
printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, format, d, ft_out);
printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, f, d, ft_out);
assert(ret == ft_ret);
assert(strcmp(out, ft_out) == 0);
}
@@ -189,14 +250,15 @@ int main(int argc, char *argv[])
{
s = argv[2];
ret = sprintf(out, format, s);
printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, format, s, out);
printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, f, s, out);
ft_begin(fd);
ft_ret = ft_printf(format, s);
ft_end(fd, ft_out);
printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, format, s, ft_out);
printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, f, s, ft_out);
assert(ret == ft_ret);
assert(strcmp(out, ft_out) == 0);
}
free(format);
}
return (0);
}