diff --git a/getformat.c b/getformat.c index 523a3c1..82b2b60 100644 --- a/getformat.c +++ b/getformat.c @@ -4,6 +4,7 @@ * getformat - get the char after the % * @c: the string * @pos: position of format + * * Return: Option contain the char after % * 0 if fails */ @@ -13,14 +14,35 @@ options getformat(const char *c, int *pos) int i = *pos; options op; + op.precision = 0; for (; c[i]; i++) { - if (c[i] == '%') - { - *pos = i + 1; - op.type = c[i + 1]; - return (op); - } + if (c[i + 1] > '0' && c[i + 1] <= '9') + op.precision = op.precision + c[i + 1] - 48; + if (c[i + 1] == '-') + op.signn = 1; + if (c[i + 1] == '+') + op.signp = 1; + if (c[i + 1] == ' ') + op.spc = 1; + if (c[i + 1] == '0') + op.zeros = 1; + if (c[i + 1] == '#') + 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] == 'l' + || c[i + 1] == 'h') + continue; + *pos = i + 1; + op.type = c[i + 1]; + return (op); } op.type = 0; return (op); diff --git a/getfunction.c b/getfunction.c index 6b3a7c7..ca03a52 100644 --- a/getfunction.c +++ b/getfunction.c @@ -59,15 +59,36 @@ unsigned int str_length(char *str) int print_integer(va_list valist, options opt) { char *str; - int bytes = 0; - (void) opt; + char flag; + int bytes = 0, compare, len, i = 0; str = number_to_string(va_arg(valist, int), 10); + 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); } - bytes = _putchar(str, str_length(str)); + if (len < opt.precision) + { +/* compare = opt.precision - len;*/ +/* str = _realloc(str, len, len + compare);*/ + i = len; + if (opt.zeros == 1) + { + for (; i < opt.precision; i++) + bytes += write(1, "0", 1); +/* str[i] = '0';*/ + } +/* len = str_length(str);*/ + } + bytes += _putchar(str, len); free(str); return (bytes); } diff --git a/holberton.h b/holberton.h index fa122ee..1ce8724 100755 --- a/holberton.h +++ b/holberton.h @@ -12,14 +12,26 @@ * @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 { char type; int precision; int length; - char sign; + int signn; + int signp; + int zeros; + int spc; + int hash; + int point; + int longer; + int sh; } options; /** diff --git a/man_page b/man_page index b53515b..33bd0f9 100755 --- a/man_page +++ b/man_page @@ -18,6 +18,23 @@ The function produce output according to a format as described below, the output 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 +======= string - prints (null) on null string .SH Return value Returns the number of characters printed (excluding the null byte used to end output to strings) @@ -27,5 +44,6 @@ The format string is a character string, beginning and ending in its initial shi .I printf(3), printf(1) .SH BUGS Return -1 at print(%) only and print(NULL) +>>>>>>> 8a9a51ac646b8b548940cbac57ea3c5f19859d46 .SH AUTHOR Ferney Medina, Jaiber Ramirez \ No newline at end of file diff --git a/printhexa.c b/printhexa.c index ec62be0..4678fcb 100644 --- a/printhexa.c +++ b/printhexa.c @@ -16,7 +16,9 @@ int print_hexa_lower_case(va_list valist, options opt) { return (_putchar("(null)", str_length("(null)"))); } - bytes = _putchar(str, str_length(str)); + if (opt.hash == 1) + bytes += write(1, "0x", 2); + bytes += _putchar(str, str_length(str)); free(str); return (bytes); } @@ -37,7 +39,9 @@ int print_hexa_upper_case(va_list valist, options opt) { return (_putchar("(null)", str_length("(null)"))); } - bytes = _putchar(str, str_length(str)); + if (opt.hash == 1) + bytes += write(1, "0X", 2); + bytes += _putchar(str, str_length(str)); free(str); return (bytes); } @@ -58,7 +62,9 @@ int print_octal(va_list valist, options opt) { return (_putchar("(null)", str_length("(null)"))); } - bytes = _putchar(str, str_length(str)); + if (opt.hash == 1 && str[0] != '0') + bytes += write(1, "0", 1); + bytes += _putchar(str, str_length(str)); free(str); return (bytes); diff --git a/test/64-main.c b/test/64-main.c new file mode 100644 index 0000000..8b13395 --- /dev/null +++ b/test/64-main.c @@ -0,0 +1,29 @@ +#include +#include +#include "../holberton.h" + +/** + * main - Entry point + * + * Return: Always 0 + */ +int main(void) +{ + int r; + + r = _printf("%6d", 10); + printf("\n%i\n", r); + r = _printf("%06d", 10); + printf("\n%i\n", r); + r = _printf("% 6d", 10); + printf("\n%i\n", r); + r = _printf("%#X", 10); + printf("\n%i\n", r); + r = _printf("%#x", 10); + printf("\n%i\n", r); + r = _printf("%#o", 10); + printf("\n%i\n", r); + r = _printf("%#o", 0); + printf("\n%i\n", r); + return (0); +}