-
Notifications
You must be signed in to change notification settings - Fork 0
/
_parse.c
executable file
·76 lines (72 loc) · 1.39 KB
/
_parse.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "main.h"
/**
* functionsList - contain list of functions to execte.
*
*
* Return: array of strct.
*/
struct_map *functionsList()
{
/*to redirect each exist char to it's own function*/
static struct_map type_list[] = {
{"c", p_char},
{"s", p_string},
{"d", p_int},
{"i", p_int},
{"%", p_persent},
{"b", p_binary},
{"u", p_unsigned_int},
{"o", p_octal},
{NULL, NULL}
};
return (type_list);
}
/**
* parse - a function that check format of printf function
* and send instruction to appropriate method
*
* @format: string parmaeter that hold format from printf
* @list : va_list parameters that hold parameters from printf
*
* Return: number of printed character.
*/
int parse(const char *format, va_list list)
{
struct_map *type_list = functionsList();
int h, d, num_char = 0, find_f;
for (h = 0; format[h] != '\0'; h++)
{
if (format[h] == '%')
{
for (d = 0; type_list[d].op != NULL; d++)
{
if (format[h + 1] == type_list[d].op[0])
{
find_f = type_list[d].f(list);
if (find_f == -1)
return (find_f);
num_char += find_f;
break;
}
}
if (type_list[d].op == NULL && format[h + 1] != ' ')
{
if (format[h + 1] != '\0')
{
_write(format[h]);
_write(format[h + 1]);
num_char += 2;
}
else
return (-1);
}
h++;
}
else
{
_write(format[h]);
num_char++;
}
}
return (num_char);
}