-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
104 lines (97 loc) · 1.89 KB
/
_printf.c
File metadata and controls
104 lines (97 loc) · 1.89 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
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
#include "main.h"
#define BUFFER_SIZE 1024
/**
* widthflag - handles width flag.
* @f: format string
* @w: pointer to width flag value.
* @per: percision flag.
* @neg: '-' flag.
* @args: arguments list.
* Return: length of printed string.
*
*/
int widthflag(const char *f, int *w, int *per, int *neg, va_list args)
{
int i = 0, width = 0, flag_plus, x = 0;
*per = 0, *neg = 0;
if (f[i] == '.')
{
*per = 1;
x = 1;
}
else if (f[i] == '0')
{
*per = 2;
x = 1;
}
else if (f[i] == '-')
{
*neg = 1;
}
flag_plus = x + *neg;
for (i = flag_plus; f[i]; i++)
{
if (f[i] >= '0' && f[i] <= '9')
{
width *= 10;
width += f[i] - '0';
}
else if (f[i] == '*')
{
i++;
width = va_arg(args, int);
break;
}
else
break;
}
*w = width;
return (i);
}
/**
* _printf - prints a formated text to stdout.
* @format: format to be followed.
* Return: length of printed text.
*/
int _printf(const char *format, ...)
{
va_list args;
char flag[40] = "0000000000000000000000000000000000000000";
int flg_indx = 0, nochar = 0, skip = 0, go_to, weight = 0, per, neg;
if (!format)
return (-1);
va_start(args, format);
while (*format)
{
MAINLOOP:
if (*format == '%')
{
FLAGLOOP:
if (*(++format) == '\0')
return (-1);
setvariables(&skip, &go_to);
format += widthflag(format, &weight, &per, &neg, args);
skip = caseselector(args, *format, &flg_indx, &nochar, flag,
&weight, &per, &neg);
go_to = flagselector(format, &flg_indx, flag, &nochar, args, &skip);
if (go_to == 1)
goto FLAGLOOP;
else if (go_to == 2 || go_to == 3)
{
format++;
if (go_to == 3)
format++;
goto MAINLOOP;
}
else if (go_to == 4)
break;
(*format == '%') ? (nochar += _putchar(*(format))) :
((!skip)) ? (nochar += _putchar(*(--format))) : (nochar *= 1);
}
else
nochar += _putchar(*format);
format++;
}
va_end(args);
return (nochar);
}