From caff0064c9f2129c72fe3761adb2e4279be9f7df Mon Sep 17 00:00:00 2001 From: JaiberS <758@holbertonschool.com> Date: Mon, 18 Mar 2019 19:03:16 +0000 Subject: [PATCH 1/8] Get options of the format for %d and %i --- getformat.c | 34 +++++++++++++++++++++++++++++----- holberton.h | 3 ++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/getformat.c b/getformat.c index 9f0a6b0..6b60071 100644 --- a/getformat.c +++ b/getformat.c @@ -13,14 +13,38 @@ options getformat(const char *c, int *pos) int i = *pos; options op; - for (; c[i]; i++, *pos++) + op.precision = 0; + op.signn = 0; + for (; c[i]; i++) { - if (c[i] == '%') + if (c[i + 1] >= '0' && c[i + 1] <= '9') { - *pos = *pos + 1; - op.type = c[i + 1]; - return (op); + op.precision = op.precision + c[i + 1] - 48; + continue; } + if (c[i + 1] == '-') + { + if (op.signn == 1) + { + printf("Error\n"); + exit(96); + } + op.signn = 1; + continue; + } + if (c[i + 1] == '+') + { + if (op.signp == 1) + { + printf("Error\n"); + exit(96); + } + op.signp = 1; + continue; + } + *pos = i + 1; + op.type = c[i + 1]; + return (op); } op.type = 0; return (op); diff --git a/holberton.h b/holberton.h index c01836f..6309257 100755 --- a/holberton.h +++ b/holberton.h @@ -19,7 +19,8 @@ typedef struct options char type; int precision; int length; - char sign; + int signn; + int signp; } options; /** From 190c64ab91fd3c8c956f709c77ee57c62c980101 Mon Sep 17 00:00:00 2001 From: JaiberS <758@holbertonschool.com> Date: Mon, 18 Mar 2019 20:01:23 +0000 Subject: [PATCH 2/8] added 0 and space format, added error on man page --- getformat.c | 22 +++++++++++----------- holberton.h | 2 ++ man_page | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100755 man_page diff --git a/getformat.c b/getformat.c index 6b60071..a7e13f9 100644 --- a/getformat.c +++ b/getformat.c @@ -17,31 +17,31 @@ options getformat(const char *c, int *pos) op.signn = 0; for (; c[i]; i++) { - if (c[i + 1] >= '0' && c[i + 1] <= '9') + if (c[i + 1] > '0' && c[i + 1] <= '9') { op.precision = op.precision + c[i + 1] - 48; continue; } if (c[i + 1] == '-') { - if (op.signn == 1) - { - printf("Error\n"); - exit(96); - } op.signn = 1; continue; } if (c[i + 1] == '+') { - if (op.signp == 1) - { - printf("Error\n"); - exit(96); - } op.signp = 1; continue; } + if (c[i + 1] == ' ') + { + op.spc = 1; + continue; + } + if (c[i + 1] == '0') + { + op.zeros = 1; + continue; + } *pos = i + 1; op.type = c[i + 1]; return (op); diff --git a/holberton.h b/holberton.h index 6309257..e96afba 100755 --- a/holberton.h +++ b/holberton.h @@ -21,6 +21,8 @@ typedef struct options int length; int signn; int signp; + int zeros; + int spc; } options; /** diff --git a/man_page b/man_page new file mode 100755 index 0000000..bc66945 --- /dev/null +++ b/man_page @@ -0,0 +1,38 @@ +.TH man 3 "19 March 2019" "1.0" "_printf man page" +.SH NAME +.B _printf +- formatted output conversion +.SH SYNOPSIS +int printf(const char *format, ...); +.SH DESCRIPTION +The function produce output according to a format as described below, the output is written to stdout, the standard output stream. +.SH FORMAT +.TP +%i + integer number +.TP +%f + float number +.TP +%c + character +.TP +%s + string +.SH SEE ALSO +.I printf(3) +.SH BUGS +.TP +Error 96 + double flag '+' or '-' +.TP +Error 97 + No proper character after '%' +.TP +Error 98 + No '%' +.TP +Error 99 + wrong format +.SH AUTHOR +Ferney Medina, Jaiber Ramirez \ No newline at end of file From 7da072f783d786a29a755ca5d865e004f0a41b53 Mon Sep 17 00:00:00 2001 From: JaiberS <758@holbertonschool.com> Date: Sat, 23 Mar 2019 22:09:17 +0000 Subject: [PATCH 3/8] [Feature] get more formats and handle + --- getformat.c | 11 ++++++++++- getfunction.c | 8 +++++++- holberton.h | 9 ++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/getformat.c b/getformat.c index a7e13f9..7635506 100644 --- a/getformat.c +++ b/getformat.c @@ -14,7 +14,6 @@ options getformat(const char *c, int *pos) options op; op.precision = 0; - op.signn = 0; for (; c[i]; i++) { if (c[i + 1] > '0' && c[i + 1] <= '9') @@ -42,6 +41,16 @@ options getformat(const char *c, int *pos) op.zeros = 1; continue; } + if (c[i + 1] == '#') + { + op.hash = 1; + continue; + } + if (c[i + 1] == '.') + { + op.point = 1; + continue; + } *pos = i + 1; op.type = c[i + 1]; return (op); diff --git a/getfunction.c b/getfunction.c index d33e847..c888ebb 100644 --- a/getfunction.c +++ b/getfunction.c @@ -54,9 +54,15 @@ unsigned int str_length(char *str) int print_integer(va_list valist, options opt) { char *str; - (void) opt; + char flag; str = number_to_string(va_arg(valist, int)); + if (opt.signp == 1) + { + flag = '+'; + write(1,&flag,1); + return (_putchar(str, str_length(str)) + 1); + } return (_putchar(str, str_length(str))); } diff --git a/holberton.h b/holberton.h index e96afba..03ed359 100755 --- a/holberton.h +++ b/holberton.h @@ -12,7 +12,12 @@ * @type: i-integers, f-float, s- string * @precision: Pricission of the print * @length: Length of the print - * @sign: Sing in printf + * @signn: Sing - in printf + * @signp: Sing + in printf + * @zeros: zero flag in printf + * @spc: space flag in printf + * @hash: # flag in printf + * @point: point flag in printf */ typedef struct options { @@ -23,6 +28,8 @@ typedef struct options int signp; int zeros; int spc; + int hash; + int point; } options; /** From a3744c641e4a75ed57abee159472d638e8687144 Mon Sep 17 00:00:00 2001 From: JaiberS <758@holbertonschool.com> Date: Sat, 23 Mar 2019 23:17:09 +0000 Subject: [PATCH 4/8] Handle + --- getfunction.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getfunction.c b/getfunction.c index 7ede87e..0f7635b 100644 --- a/getfunction.c +++ b/getfunction.c @@ -67,7 +67,7 @@ int print_integer(va_list valist, options opt) write(1,&flag,1); bytes++; } - bytes = _putchar(str, str_length(str)); + bytes += _putchar(str, str_length(str)); free(str); return (bytes); } From 9ccf27645f6aeb75430edd0037ae5ad6101dd435 Mon Sep 17 00:00:00 2001 From: JaiberS <758@holbertonschool.com> Date: Sat, 23 Mar 2019 23:42:48 +0000 Subject: [PATCH 5/8] [Fixes] Betty on Getformat --- getformat.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/getformat.c b/getformat.c index 4e7b9ca..c6c8ff2 100644 --- a/getformat.c +++ b/getformat.c @@ -18,40 +18,23 @@ options getformat(const char *c, int *pos) for (; c[i]; i++) { if (c[i + 1] > '0' && c[i + 1] <= '9') - { op.precision = op.precision + c[i + 1] - 48; - continue; - } if (c[i + 1] == '-') - { op.signn = 1; - continue; - } if (c[i + 1] == '+') - { op.signp = 1; - continue; - } if (c[i + 1] == ' ') - { op.spc = 1; - continue; - } if (c[i + 1] == '0') - { op.zeros = 1; - continue; - } if (c[i + 1] == '#') - { op.hash = 1; - continue; - } if (c[i + 1] == '.') - { op.point = 1; + if ((c[i + 1] > '0' && c[i + 1] <= '9') || c[i + 1] == '-' + || c[i + 1] == '+' || c[i + 1] == ' ' || c[i + 1] == '0' + || c[i + 1] == '#' || c[i + 1] == '.') continue; - } *pos = i + 1; op.type = c[i + 1]; return (op); From f8ada3818a33e1a75e159e17c62b60fcf3f9fc3a Mon Sep 17 00:00:00 2001 From: JaiberS <758@holbertonschool.com> Date: Sun, 24 Mar 2019 00:37:29 +0000 Subject: [PATCH 6/8] [Features] Handle + and space --- getfunction.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/getfunction.c b/getfunction.c index 0f7635b..d3e25bd 100644 --- a/getfunction.c +++ b/getfunction.c @@ -61,11 +61,13 @@ int print_integer(va_list valist, options opt) { return (_putchar("(null)", str_length("(null)"))); } - if (opt.signp == 1) + if(str[0] != '-' && (opt.signp == 1 || opt.spc == 1)) { - flag = '+'; - write(1,&flag,1); - bytes++; + if (opt.signp == 1) + flag = '+'; + if (opt.spc == 1) + flag = ' '; + bytes += write(1,&flag,1); } bytes += _putchar(str, str_length(str)); free(str); From 2b3a92116b622a54f709a099bd070cc804b54a7e Mon Sep 17 00:00:00 2001 From: JaiberS <758@holbertonschool.com> Date: Sun, 24 Mar 2019 00:43:06 +0000 Subject: [PATCH 7/8] [Feature] Added l and h flags --- getformat.c | 7 ++++++- holberton.h | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/getformat.c b/getformat.c index c6c8ff2..82b2b60 100644 --- a/getformat.c +++ b/getformat.c @@ -31,9 +31,14 @@ options getformat(const char *c, int *pos) op.hash = 1; if (c[i + 1] == '.') op.point = 1; + if (c[i + 1] == 'l') + op.longer = 1; + if (c[i + 1] == 'h') + op.sh = 1; if ((c[i + 1] > '0' && c[i + 1] <= '9') || c[i + 1] == '-' || c[i + 1] == '+' || c[i + 1] == ' ' || c[i + 1] == '0' - || c[i + 1] == '#' || c[i + 1] == '.') + || c[i + 1] == '#' || c[i + 1] == '.' || c[i + 1] == 'l' + || c[i + 1] == 'h') continue; *pos = i + 1; op.type = c[i + 1]; diff --git a/holberton.h b/holberton.h index b1e917e..eb4baaa 100755 --- a/holberton.h +++ b/holberton.h @@ -30,6 +30,8 @@ typedef struct options int spc; int hash; int point; + int longer; + int sh; } options; /** From 393a4ba25f8aa9719e340b89a422e7e3d27b7c89 Mon Sep 17 00:00:00 2001 From: JaiberS <758@holbertonschool.com> Date: Sun, 24 Mar 2019 02:04:31 +0000 Subject: [PATCH 8/8] [Feature] Handle 0 flag --- getfunction.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/getfunction.c b/getfunction.c index d3e25bd..e8dbbff 100644 --- a/getfunction.c +++ b/getfunction.c @@ -54,22 +54,33 @@ int print_integer(va_list valist, options opt) { char *str; char flag; - int bytes = 0; + int bytes = 0, compare, len, i = 0; str = number_to_string(va_arg(valist, int)); + len = str_length(str); if (str == NULL) - { return (_putchar("(null)", str_length("(null)"))); - } if(str[0] != '-' && (opt.signp == 1 || opt.spc == 1)) + { + if (opt.signp == 1) + flag = '+'; + if (opt.spc == 1) + flag = ' '; + opt.precision -= 1; + bytes += write(1,&flag,1); + } + if (len < opt.precision) { - if (opt.signp == 1) - flag = '+'; - if (opt.spc == 1) - flag = ' '; - bytes += write(1,&flag,1); + compare = opt.precision - len; + str = _realloc(str, len, len + compare); + if (opt.zeros == 1) + { + for (; i < opt.precision; i++) + str[i] = '0'; + } + len = str_length(str); } - bytes += _putchar(str, str_length(str)); + bytes += _putchar(str, len); free(str); return (bytes); }