-
Notifications
You must be signed in to change notification settings - Fork 0
/
_getmp.c
137 lines (117 loc) · 2.35 KB
/
_getmp.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "main.h"
#include <stdarg.h>
/**
* get_flags - Calculates active flags
* @format: Formatted string in which to print the arguments
* @i: take a parameter.
* Return: Flags:
*/
int get_flags(const char *format, int *i)
{
/* - + 0 # ' ' */
/* 1 2 4 8 16 */
int j, curr_i;
int flag;
const char flags_chr[] = {'-', '+', '0', '#', ' ', '\0'};
const int flags_int[] = {1, 2, 4, 8, 16, 0};
for (curr_i = *i + 1; format[curr_i]; curr_i++)
{
for (j = 0; flags_chr[j]; j++)
if (format[curr_i] == flags_chr[j])
{
flag = flags_int[j];
break;
}
if (!flags_chr[j])
{
flag = 0;
break;
}
}
*i = curr_i - 1;
return (flag);
}
/**
* get_precision - Calculates the precision for printing
* @format: Formatted string in which to print the arguments
* @i: List of arguments to be printed.
* @list: list of arguments.
*
* Return: Precision.
*/
int get_precision(const char *format, int *i, va_list list)
{
int curr_i = *i + 1;
int precision = -1;
if (format[curr_i] != '.')
return (precision);
precision = 0;
for (curr_i += 1; format[curr_i]; curr_i++)
{
if (is_digit(format[curr_i]))
{
precision = (precision * 10) + (format[curr_i] - '0');
}
else if (format[curr_i] == '*')
{
curr_i++;
precision = va_arg(list, int);
break;
}
else
break;
}
*i = curr_i - 1;
return (precision);
}
/**
* get_size - Calculates the size to cast the argument
* @format: Formatted string in which to print the arguments
* @i: List of arguments to be printed.
*
* Return: Precision.
*/
int get_size(const char *format, int *i)
{
int curr_i = *i + 1;
int size = 0;
if (format[curr_i] == 'l')
size = 2;
else if (format[curr_i] == 'h')
size = 1;
if (size == 0)
*i = curr_i - 1;
else
*i = curr_i;
return (size);
}
/**
* get_width - Calculates the width for printing
* @format: Formatted string in which to print the arguments.
* @i: List of arguments to be printed.
* @list: list of arguments.
*
* Return: width.
*/
int get_width(const char *format, int *i, va_list list)
{
int curr_i;
int width = 0;
for (curr_i = *i + 1; format[curr_i] != '\0'; curr_i++)
{
if (is_digit(format[curr_i]))
{
width = (width * 10) + (format[curr_i] - '0');
}
else if (format[curr_i] == '*')
{
curr_i++;
width = va_arg(list, int);
break;
}
else
break;
}
*i = curr_i - 1;
return (width);
}