From fb950d2d523259d55b4ca66daee2ca5aad8cf452 Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Sun, 21 Oct 2018 22:51:26 +0000 Subject: [PATCH 1/5] Added skeleton printf.c --- printf.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 printf.c diff --git a/printf.c b/printf.c new file mode 100644 index 0000000..3d36e4d --- /dev/null +++ b/printf.c @@ -0,0 +1,22 @@ +#include /* NULL macro */ +#include "holberton.h" /* _putchar */ + +/** + * _printf - prints to stdout according to a format string + * @format: constant string containing zero or more directives + * Return: int number of characters printed (excluding terminating null-byte) + */ +int _printf(const char *format, ...) +{ + int count = 0; + + /* begin filler code */ + if (format != NULL) + { + _putchar(*format); + count++; + } + /* end filler code */ + + return (count); +} From 956adecc6b135348349e4fc12f077f74bd29bf32 Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Mon, 22 Oct 2018 00:03:29 +0000 Subject: [PATCH 2/5] Included stdarg.h, implemented printer struct & print_char. --- printf.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/printf.c b/printf.c index 3d36e4d..95434a4 100644 --- a/printf.c +++ b/printf.c @@ -1,6 +1,33 @@ +#include /* va_list, va_start, va_arg, va_end macros */ #include /* NULL macro */ #include "holberton.h" /* _putchar */ + + +/** + * struct printer - format printer struct + * @spec: the format specifier + * @fn: the function that handles spec + */ +typedef struct printer +{ + char *spec; + void (*fn) (va_list); +} print_t; + + + +/** + * print_char - prints a char parameter from a va_list + * @ap: va_list from calling function + */ +void print_char(va_list ap) +{ + _putchar(va_arg(ap, int)); +} + + + /** * _printf - prints to stdout according to a format string * @format: constant string containing zero or more directives From 03ab8bb66a9b9b7cfa0752145e705557a4db6f4f Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Mon, 22 Oct 2018 00:27:00 +0000 Subject: [PATCH 3/5] Added printer struct to holberton.h --- holberton.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/holberton.h b/holberton.h index 22d9c78..7cef464 100644 --- a/holberton.h +++ b/holberton.h @@ -1,6 +1,18 @@ #ifndef __HOLBERTON_H__ #define __HOLBERTON_H__ +/** + * struct printer - format printer struct + * @spec: the format specifier + * @fn: the function that handles spec + */ +typedef struct printer +{ + char *spec; + void (*fn) (va_list); +} print_t; + + int _putchar(char c); #endif From 1a45fa95d00eb49b2fff80f486b3a33ccd49d896 Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Mon, 22 Oct 2018 16:28:11 +0000 Subject: [PATCH 4/5] Handles c, s and % with our latest changes. Includes small test in main function. Meant to close #6 and close #8. --- printf.c | 97 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 22 deletions(-) diff --git a/printf.c b/printf.c index 95434a4..a4117c2 100644 --- a/printf.c +++ b/printf.c @@ -1,31 +1,33 @@ +#include /* for testing only */ #include /* va_list, va_start, va_arg, va_end macros */ #include /* NULL macro */ #include "holberton.h" /* _putchar */ - - /** - * struct printer - format printer struct - * @spec: the format specifier - * @fn: the function that handles spec + * print_char - prints a char parameter from a va_list + * @ap: va_list from calling function + * Return: integer count of characters printed */ -typedef struct printer +int print_char(va_list ap) { - char *spec; - void (*fn) (va_list); -} print_t; - - + return (_putchar(va_arg(ap, int))); +} /** - * print_char - prints a char parameter from a va_list + * print_string - prints a string parameter from a va_list * @ap: va_list from calling function + * Return: integer count of characters printed */ -void print_char(va_list ap) +int print_string(va_list ap) { - _putchar(va_arg(ap, int)); -} + char *str = va_arg(ap, char *); + int count = 0; + + while (str[count] != '\0') + count += _putchar(str[count]); + return (count); +} /** @@ -35,15 +37,66 @@ void print_char(va_list ap) */ int _printf(const char *format, ...) { - int count = 0; + int i, j, count = 0; + va_list ap; + print_t funcs[] = { + {"c", print_char}, + {"s", print_string}, + {NULL, NULL} + }; - /* begin filler code */ - if (format != NULL) + va_start(ap, format); + for (i = 0; format != NULL && format[i] != '\0'; i++) { - _putchar(*format); - count++; + if (format[i] != '%') + { + count += _putchar(format[i]); + continue; + } + switch (format[++i]) + { + case '%': + count += _putchar('%'); + 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; + } + } + break; + default: + exit(-1); + } } - /* end filler code */ - + va_end(ap); return (count); } + +/* TODO: REMOVE BEFORE PUSHING TO MASTER! */ +/** + * main - tests _printf against stdio::printf + * Return: 0 on SUCCESS + */ +int main(void) +{ + int a, b; + + a = printf("a\n"); + b = _printf("a\n"); + printf("(%d, %d)\n", a, b); + + a = printf("%c\n", 'A'); + b = _printf("%c\n", 'A'); + printf("(%d, %d)\n", a, b); + + a = printf("%s\n", "Holberton"); + b = _printf("%s\n", "Holberton"); + printf("(%d, %d)\n", a, b); + + return (0); +} From 471922f7be799b687c95135093e55bd9af287ff3 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Mon, 22 Oct 2018 17:26:42 +0000 Subject: [PATCH 5/5] changes --- holberton.h | 6 +++++ printf.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/holberton.h b/holberton.h index 22d9c78..cc85782 100644 --- a/holberton.h +++ b/holberton.h @@ -3,4 +3,10 @@ int _putchar(char c); +typedef struct func +{ + char *spec; + int (*fn)(va_list); +} printf; + #endif diff --git a/printf.c b/printf.c index 3d36e4d..007620a 100644 --- a/printf.c +++ b/printf.c @@ -1,4 +1,6 @@ #include /* NULL macro */ +#include +#include /* for variadic */ #include "holberton.h" /* _putchar */ /** @@ -6,17 +8,71 @@ * @format: constant string containing zero or more directives * Return: int number of characters printed (excluding terminating null-byte) */ + +int print_char(va_list valist) +{ + return _putchar(va_arg(valist, int)); +} + +int print_string(va_list valist) +{ + char *str = va_arg(valist, char *); + int count = 0; + + while (str[count] != '\0') + count += _putchar(str[count]); + + return (count); +} + int _printf(const char *format, ...) { int count = 0; + int index_1; + int index_2 = 0; + va_list valist; - /* begin filler code */ - if (format != NULL) + printf ops[] = { + {"c", print_char}, + {NULL, NULL} + }; + + va_start(valist, format); + + for (index_1 = 0; format[index_1] != '\0'; index_1++) { - _putchar(*format); - count++; - } - /* end filler code */ + index_2 = 0; + + while (ops[index_2].spec != NULL) /* move through array */ + { + if (format[index_1] == '%') + { + if (format[++index_1] == '%') + { + count += _putchar('%'); + break; + } + } + + else if (format[index_1] == ops[index_2].spec[0]) + { + count += ops[index_2].fn(valist); + break; + } + else + exit (-1); + + count += _putchar(format[index_1]); + index_2++; + } + } + va_end(valist); return (count); } + +int main() +{ + _printf("a\n"); + return (0); +}