Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions getformat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand Down
29 changes: 25 additions & 4 deletions getfunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 14 additions & 2 deletions holberton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
18 changes: 18 additions & 0 deletions man_page
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
12 changes: 9 additions & 3 deletions printhexa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);

Expand Down
29 changes: 29 additions & 0 deletions test/64-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <limits.h>
#include <stdio.h>
#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);
}