From 70337eb9bc78abb3b0f56acc3e9abeea59b62089 Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Mon, 22 Oct 2018 23:31:18 +0000 Subject: [PATCH 01/10] Betty fix on holberton.h, fixes character edge cases, small changes to tests/small_tests.c --- holberton.h | 2 +- printf.c | 8 +++++++- tests/small_tests.c | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/holberton.h b/holberton.h index eafd1e6..41ee210 100644 --- a/holberton.h +++ b/holberton.h @@ -9,7 +9,7 @@ typedef struct printer { char *spec; - int (*fn) (va_list); + int (*fn)(va_list); } print_t; diff --git a/printf.c b/printf.c index fe4b8d3..2bffe3f 100644 --- a/printf.c +++ b/printf.c @@ -69,7 +69,13 @@ int _printf(const char *format, ...) } break; default: - exit(-1); + if (format[i] >= 7 && format[i] <= 13) + { + count += _putchar('%'); + count += _putchar(format[i]); + break; + } + return (-1); } } va_end(ap); diff --git a/tests/small_tests.c b/tests/small_tests.c index c0e1ff8..e516e7d 100644 --- a/tests/small_tests.c +++ b/tests/small_tests.c @@ -7,19 +7,21 @@ */ int main(void) { - int a, b; + int a = 0; + int b = 0; - a = printf("a\n"); - b = _printf("a\n"); + a = printf("stdio%"); + b = _printf("ours%"); +/* printf("%d", a);*/ printf("(%d, %d)\n", a, b); - a = printf("%c\n", 'A'); - b = _printf("%c\n", 'A'); +/* a = printf("stdio %c\n", '\0'); + b = _printf("ours %c\n", '\0'); printf("(%d, %d)\n", a, b); - a = printf("%s\n", "Holberton"); - b = _printf("%s\n", "Holberton"); + a = printf("stdio %s\n", "Holberton"); + b = _printf("ours %s\n", "Holberton"); printf("(%d, %d)\n", a, b); - +*/ return (0); } From 9db7cf6a19c13f9d8f3281702be4d64b390659b7 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Tue, 23 Oct 2018 00:16:38 +0000 Subject: [PATCH 02/10] removed print functions --- printf.c | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/printf.c b/printf.c index 2bffe3f..0c15175 100644 --- a/printf.c +++ b/printf.c @@ -2,33 +2,6 @@ #include /* NULL macro */ #include "holberton.h" /* _putchar */ -/** - * print_char - prints a char parameter from a va_list - * @ap: va_list from calling function - * Return: integer count of characters printed - */ -int print_char(va_list ap) -{ - return (_putchar(va_arg(ap, int))); -} - -/** - * print_string - prints a string parameter from a va_list - * @ap: va_list from calling function - * Return: integer count of characters printed - */ -int print_string(va_list ap) -{ - char *str = va_arg(ap, char *); - int count = 0; - - while (str[count] != '\0') - count += _putchar(str[count]); - - return (count); -} - - /** * _printf - prints to stdout according to a format string * @format: constant string containing zero or more directives @@ -59,14 +32,7 @@ int _printf(const char *format, ...) break; case 'c': case 's': - for (j = 0; funcs[j].spec != NULL; j++) - { - if (format[i] == funcs[j].spec[0]) - { - count += funcs[j].fn(ap); - break; - } - } + count += call_print_fn(format[i], funcs, ap); break; default: if (format[i] >= 7 && format[i] <= 13) @@ -81,4 +47,3 @@ int _printf(const char *format, ...) va_end(ap); return (count); } - From c394c6cb67b7feed22b8c50bcf2ca03c17c1d2ae Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Tue, 23 Oct 2018 00:17:36 +0000 Subject: [PATCH 03/10] moved print functions from printf.c --- print_funcs.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 print_funcs.c diff --git a/print_funcs.c b/print_funcs.c new file mode 100644 index 0000000..af6345e --- /dev/null +++ b/print_funcs.c @@ -0,0 +1,30 @@ +#include /* va_list, va_start, va_arg, va_end macros */ +#include /* NULL macro */ +#include "holberton.h" /* _putchar */ + +/** + * print_char - prints a char parameter from a va_list + * @ap: va_list from calling function + * Return: integer count of characters printed + */ +int print_char(va_list ap) +{ + return (_putchar(va_arg(ap, int))); +} + +/** + * print_string - prints a string parameter from a va_list + * @ap: va_list from calling function + * Return: integer count of characters printed + */ +int print_string(va_list ap) +{ + char *str = va_arg(ap, char *); + int count = 0; + + while (str[count] != '\0') + count += _putchar(str[count]); + + return (count); +} + From 19077661e5c5bf3501c0b48d7689bc670e04e12d Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Tue, 23 Oct 2018 00:18:14 +0000 Subject: [PATCH 04/10] implemented call print funcs --- get_print_functions.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 get_print_functions.c diff --git a/get_print_functions.c b/get_print_functions.c new file mode 100644 index 0000000..fe1e136 --- /dev/null +++ b/get_print_functions.c @@ -0,0 +1,26 @@ +#include +#include +#include "holberton.h" + +/** + * call_print_func - call appropriate print fn + * @format: format string character + * @funcs: arrays of structures and functions + * @va_list: object to be printed + * Return: number of characters printed + */ +int call_print_func(char ch, print_t funcs, va_list ap) +{ + int j; + int count = 0; + + for (j = 0; funcs[j].spec != NULL; j++) + { + if (ch == funcs[j].spec[0]) + { + count += funcs[j].fn(ap); + break; + } + } + return (count); +} From cbb0c498a841b6da636a946672f8b23e50d5224e Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Tue, 23 Oct 2018 00:26:43 +0000 Subject: [PATCH 05/10] Added print_char and print_string to holberton.h --- holberton.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/holberton.h b/holberton.h index 41ee210..cc7e610 100644 --- a/holberton.h +++ b/holberton.h @@ -15,5 +15,7 @@ typedef struct printer int _putchar(char c); int _printf(const char *format, ...); +int print_char(va_list ap); +int print_string(va_list ap); #endif From ff999506cd42e9f11dda81bbfa0c1c9d4eda4d85 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Tue, 23 Oct 2018 00:26:33 +0000 Subject: [PATCH 06/10] removing unused varaible j --- printf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/printf.c b/printf.c index 0c15175..1d47453 100644 --- a/printf.c +++ b/printf.c @@ -9,7 +9,7 @@ */ int _printf(const char *format, ...) { - int i, j, count = 0; + int i, count = 0; va_list ap; print_t funcs[] = { {"c", print_char}, From e3a547f878a85993cad4ce78c3b8c661c8167700 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Tue, 23 Oct 2018 00:32:28 +0000 Subject: [PATCH 07/10] updated headerfile --- holberton.h | 1 + 1 file changed, 1 insertion(+) diff --git a/holberton.h b/holberton.h index cc7e610..0a23dc2 100644 --- a/holberton.h +++ b/holberton.h @@ -17,5 +17,6 @@ int _putchar(char c); int _printf(const char *format, ...); int print_char(va_list ap); int print_string(va_list ap); +int call_print_fn(char ch, print_t funcs[], va_list ap); #endif From 52e742e722f5a393e5ff7d38def3a32d438c8917 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Tue, 23 Oct 2018 00:33:04 +0000 Subject: [PATCH 08/10] fixing function prototype --- get_print_functions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/get_print_functions.c b/get_print_functions.c index fe1e136..e4a3af6 100644 --- a/get_print_functions.c +++ b/get_print_functions.c @@ -9,7 +9,7 @@ * @va_list: object to be printed * Return: number of characters printed */ -int call_print_func(char ch, print_t funcs, va_list ap) +int call_print_fn(char ch, print_t funcs[], va_list ap) { int j; int count = 0; From c1219ddade4d783079b6a5aa710f42fc07e293bd Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Tue, 23 Oct 2018 00:52:57 +0000 Subject: [PATCH 09/10] fixing betty --- get_print_functions.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/get_print_functions.c b/get_print_functions.c index e4a3af6..4b9e3c2 100644 --- a/get_print_functions.c +++ b/get_print_functions.c @@ -3,10 +3,10 @@ #include "holberton.h" /** - * call_print_func - call appropriate print fn - * @format: format string character + * call_print_fn - call appropriate print fn + * @ch: format string character * @funcs: arrays of structures and functions - * @va_list: object to be printed + * @ap: object to be printed * Return: number of characters printed */ int call_print_fn(char ch, print_t funcs[], va_list ap) From 6618340dddca998118cbc0b77161b5cbfe9faa62 Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Tue, 23 Oct 2018 00:57:21 +0000 Subject: [PATCH 10/10] Handled NULL string parameter in va_list --- print_funcs.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/print_funcs.c b/print_funcs.c index af6345e..b64efec 100644 --- a/print_funcs.c +++ b/print_funcs.c @@ -22,6 +22,8 @@ int print_string(va_list ap) char *str = va_arg(ap, char *); int count = 0; + if (!str) + str = "(null)"; while (str[count] != '\0') count += _putchar(str[count]);