-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction4.c
52 lines (50 loc) · 1.28 KB
/
function4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "main.h"
/**
* parser - Receives the main string and all the necessary parameters to
* print a formated string.
* @format: A string containing all the desired characters.
* @f_list: A list of all the posible functions.
* @arg_list: A list containing all the argumentents passed to the program.
* Return: A total count of the characters printed.
*/
int parser(const char *format, conver_t f_list[], va_list arg_list)
{
int i, j, r_val, printed_chars;
printed_chars = 0;
for (i = 0; format[i] != '\0'; i++)/* Iterates through the main str*/
{
if (format[i] == '%') /*Checks for format specifiers*/
{
/*Iterates through struct to find the right func*/
for (j = 0; f_list[j].sym != NULL; j++)
{
if (format[i + 1] == f_list[j].sym[0])
{
r_val = f_list[j].f(arg_list);
if (r_val == -1)
return (-1);
printed_chars += r_val;
break;
}
}
if (f_list[j].sym == NULL && format[i + 1] != ' ')
{
if (format[i + 1] != '\0')
{
_putchar(format[i]);
_putchar(format[i + 1]);
printed_chars = printed_chars + 2;
}
else
return (-1);
}
i = i + 1; /*Updating i to skip format symbols*/
}
else
{
_putchar(format[i]); /*call the write function*/
printed_chars++;
}
}
return (printed_chars);
}