-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
47 lines (46 loc) · 1.16 KB
/
parser.c
File metadata and controls
47 lines (46 loc) · 1.16 KB
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
#include "main.h"
/**
* parser - Parse the given string and print a formatted string
* @format: String to be parsed
* @fun_list: List of all the possible parse functions
* @arg_list: List of all arguments passed
* Return: Total count of characters printed
*/
int parser(const char *format, asoc_fun fun_list[], va_list arg_list)
{
int chars_printed, i, j, val;
chars_printed = 0;
for (i = 0; format[i] != '\0'; i++)/*Iterate through the given string*/
{
if (format[i] == '%') /*check for format specifiers*/
{ /*Iterate over struct to find the right fun*/
for (j = 0; fun_list[j].fmt != NULL; j++)
{
if (format[i + 1] == fun_list[j].fmt[0])
{
val = fun_list[j].fptr(arg_list);
if (val == -1)
return (-1);
chars_printed += val;
break;
}
}
if (fun_list[j].fmt == NULL && format[i + 1] != ' ')
{
if (format[i + 1] != '\0')
{ _write(format[i]);
_write(format[i + 1]);
chars_printed = chars_printed + 2;
}
else
return (-1);
}
i = i + 1;/*update i to skip format specifier*/
}
else
{ _write(format[i]);/*call the write function*/
chars_printed++;
}
}
return (chars_printed);
}