-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetfunction.c
More file actions
132 lines (120 loc) · 2.46 KB
/
Copy pathgetfunction.c
File metadata and controls
132 lines (120 loc) · 2.46 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
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
#include "holberton.h"
/**
* getfunction- selects the correct function to perform the operation
* @format: Operator given
*
* Return: a pointer to the function that corresponds to the operator given
*/
int (*getfunction(const char format))(va_list, options)
{
int i_struct = 0;
op_t print[] = {
{"c", print_char},
{"i", print_integer},
{"d", print_integer},
{"f", print_float},
{"s", print_string},
{"x", print_hexa_lower_case},
{"X", print_hexa_upper_case},
{"o", print_octal},
{"u", print_unsigned_integer},
{"r", print_reverse_string},
{"R", print_rot13},
{NULL, NULL}
};
while (print[i_struct].op != NULL)
{
if (format == *(print[i_struct].op))
{
return (print[i_struct].f);
}
i_struct++;
}
return (NULL);
}
/**
* str_length - calculate length
* @str: strint to calculate length
* Return: length of array
*/
unsigned int str_length(char *str)
{
unsigned int iterator = 0;
while (str[iterator])
iterator++;
return (iterator);
}
/**
* print_integer - print integer
* @valist: valist
* @opt: option to print
* Return: count of bytes printed
*/
int print_integer(va_list valist, options opt)
{
char *str;
char flag;
int bytes = 0;
str = number_to_string(va_arg(valist, int), 10);
if (str == NULL)
{
return (_putchar("(null)", str_length("(null)")));
}
if (opt.signp == 1)
{
flag = '+';
write(1,&flag,1);
bytes++;
}
bytes += _putchar(str, str_length(str));
free(str);
return (bytes);
}
/**
* print_string - print string
* @valist: valist
* @opt: option to print
* Return: count of bytes printed
*/
int print_string(va_list valist, options opt)
{
char *str = va_arg(valist, char*);
(void) opt;
if (str == NULL)
{
return (_putchar("(null)", str_length("(null)")));
}
return (_putchar(str, str_length(str)));
}
/**
* print_float - print float
* @valist: valist
* @opt: option to print
* Return: count of bytes printed
*/
int print_float(va_list valist, options opt)
{
char *str = va_arg(valist, char*);
(void) opt;
return (_putchar(str, str_length(str)));
}
/**
* print_unsigned_integer - print unsigned integer
* @valist: valist
* @opt: option to print
* Return: count of bytes printed
*/
int print_unsigned_integer(va_list valist, options opt)
{
char *str;
int bytes = 0;
(void) opt;
str = unsigned_number_to_string(va_arg(valist, unsigned int), 10);
if (str == NULL)
{
return (_putchar("(null)", str_length("(null)")));
}
bytes = _putchar(str, str_length(str));
free(str);
return (bytes);
}