-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
executable file
·111 lines (101 loc) · 2.06 KB
/
_printf.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "main.h"
/**
*_printf - printf
*@format: const char pointer
*Description: this functions implement some functions of printf
*Return: num of characteres printed
*/
int _printf(const char *format, ...)
{
const char *string;
int cont = 0;
va_list arg;
if (!format)
return (-1);
va_start(arg, format);
string = format;
cont = loop_format(arg, string);
va_end(arg);
return (cont);
}
/**
*loop_format - loop format
*@arg: va_list arg
*@string: pointer from format
*Description: This function make loop tp string pointer
*Return: num of characteres printed
*/
int loop_format(va_list arg, const char *string)
{
int i = 0, flag = 0, cont_fm = 0, cont = 0, check_per = 0;
while (i < _strlen((char *)string) && *string != '\0')
{
char aux = string[i];
if (aux == '%')
{
i++, flag++;
aux = string[i];
if (aux == '\0' && _strlen((char *)string) == 1)
return (-1);
if (aux == '\0')
return (cont);
if (aux == '%')
{
flag++;
} else
{
cont_fm = funct_mgr(aux, arg);
if (cont_fm >= 0 && cont_fm != -1)
{
i++;
aux = string[i];
if (aux == '%')
flag--;
cont = cont + cont_fm;
} else if (cont_fm == -1 && aux != '\n')
{
cont += _putchar('%');
}
}
}
check_per = check_percent(&flag, aux);
cont += check_per;
if (check_per == 0 && aux != '\0' && aux != '%')
cont += _putchar(aux), i++;
check_per = 0;
}
return (cont);
}
/**
* check_percent - call function manager
*@flag: value by reference
*@aux: character
*Description: This function print % pear
*Return: 1 if % is printed
*/
int check_percent(int *flag, char aux)
{
int tmp_flag;
int cont = 0;
tmp_flag = *flag;
if (tmp_flag == 2 && aux == '%')
{
_putchar('%');
tmp_flag = 0;
cont = 1;
}
return (cont);
}
/**
* call_funct_mgr - call function manager
*@aux: character parameter
*@arg: va_list arg
*Description: This function call function manager
*Return: num of characteres printed
*/
int call_funct_mgr(char aux, va_list arg)
{
int cont = 0;
cont = funct_mgr(aux, arg);
return (cont);
}