-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
169 lines (153 loc) · 3.07 KB
/
_printf.c
File metadata and controls
169 lines (153 loc) · 3.07 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "main.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
/**
* _printf - custom printf function
* @format: format string
* @...: subsequent arguments
*
* Description - a custom printf function
* Return: count of characters printed to stdout
*/
int _printf(const char *format, ...)
{
int i;
int count;
char cursor;
char spec;
va_list values;
_struct res_struct;
count = 0;
i = 0;
va_start(values, format);
if (format == NULL)
return (-1);
while (format[i] != '\0')
{
cursor = format[i];
if (cursor != '%')
{
_putchar(cursor);
cursor++;
count++;
/*printf("Non-scount: %d\n", count);*/
}
else
{
spec = format[i + 1];
res_struct = specify(format, i + 1, spec, values);
/*if (res_struct.count == -1)*/
/*return (-1);*/
count += res_struct.count;
i++;
if (res_struct.flag_true != 0)
i++;
}
i++;
}
va_end(values);
return (count);
}
/**
* specify - handles the specifiers c, s, %, d, and i
* @spec: format specifier
* @values: list of arguments
* @str: format string
* @spec_loc: index of format specifier
*
* Definition - handles format specifiers c, s, %, d, and i
* Return: structure count of printed characters and flag_true
*/
_struct specify(const char *str, int spec_loc, char spec, va_list values)
{
char *value_str;
int j;
int sub_count = 0;
int flag = 0;
struct _struct _specify = {-1, 0};
if (spec == 's')
{
value_str = va_arg(values, char *);
if (value_str == NULL)
{
print_null();
return (_specify);
}
j = 0;
while (value_str[j] != '\0')
{
_putchar(value_str[j]);
sub_count++;
j++;
}
}
else if (str[spec_loc] == 'l')
{
flag = longfunction(spec_loc, str, values);
if (flag != 0)
sub_count = flag;
}
else if (str[spec_loc] == 'h')
{
flag = shortfunction(spec_loc, str, values);
sub_count = flag;
}
else if (spec != 's' && spec != 'l' && spec != 'h')
sub_count = specify_sub(spec, values);
_specify.count = sub_count;
_specify.flag_true = flag;
return (_specify);
}
/**
* specify_sub - completes specify function
* @spec: format specifier
* @values: list of arguments
* Description - completes function specify
* Return: count of printed character
*/
int specify_sub(char spec, va_list values)
{
int sub_count = 0;
if (spec == 'c')
{
_putchar(va_arg(values, int));
sub_count = 1;
}
if (spec == 'b')
sub_count = _malloc(values, 2);
if (spec == 'd' || spec == 'i')
sub_count = _malloc2(values, 10);
if (spec == 'u')
sub_count = _malloc(values, 10);
if (spec == 'o')
sub_count = _malloc(values, 8);
if (spec == 'x')
sub_count = _malloc3(values, 16, 'a');
if (spec == 'X')
sub_count = _malloc3(values, 16, 'A');
if (spec == 'r')
sub_count = rev_string(values);
if (spec == 'R')
sub_count = rot13(values);
if (spec == '%')
{
_putchar('%');
sub_count = 1;
}
return (sub_count);
}
/**
* print_null - prints (null)
* Definition - runs when argument to s is null
* Return: void
*/
void print_null(void)
{
_putchar(40);
_putchar('n');
_putchar('u');
_putchar('l');
_putchar('l');
_putchar(41);
}