forked from jbocane6/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printmod.c
179 lines (164 loc) · 4.85 KB
/
_printmod.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
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
170
171
172
173
174
175
176
177
178
179
#include "holberton.h"
#include <limits.h>
/**
* pbuffer - Function that prints and delete chars in buffer.
* @printbuffer: string to print
* Return: void
*/
void pbuffer(char *printbuffer)
{
unsigned int i, j;
/* Calculate printbuffer length*/
for (i = 0; printbuffer[i] != '\0'; i++)
;
write(1, printbuffer, i); /* print all characters */
/* Clean printbuffer after printing*/
for (j = 0; j < i; j++)
printbuffer[j] = '\0';
}
/**
* p_intmin - Prints integers whe is equal to INT_MIN.
* @i: integer received to print
* @counter: counts chars printed
*/
void p_intmin(int i, int *counter)
{
int j, k, size, mod;
char *str;
/* Assign value of INT_MIN reduced in the last digit and positive */
k = (INT_MIN % 10) * -1;
/* Take the variable and reduce it the last digit and becomeing it positive */
i /= -10;
size = i;/* Asigned i to size to get the amount of digits */
/* Calculate the amount of digits of the shorten int */
for (j = 0; size > 0; j++)
size /= 10;
/* The number was reduced one digit and is positive, add 2 spaces to size */
size = j + 2;
str = malloc((size) * sizeof(char));/* Asigning memory to the new array */
if (str == NULL)
return;
for (j = 0; j < size - 1; j++)
{
mod = i % 10;
/* Store the module of the división in the place it belongs */
str[size - 2 - j] = mod + '0';
i /= 10;/* Reduce i in one digit */
}
str[0] = '-';/* When finished, add minus as the first char */
/* In the last position adds the digit removed at the begin */
str[j] = k + '0';
/* Increasing the count of chars in the same amount of digits */
*counter += size;
write(1, str, size);/* Write the number converted to string */
free(str);/* Free memory */
}
/**
* p_int - Prints integers.
* @list: list that contains value
* to print
* @counter: counts chars printed
*/
void p_int(va_list list, int *counter)
{
int size = 0;
int maxmin = va_arg(list, int);
char *str;
/* If the number is smaller or equal call the function */
if (maxmin <= INT_MIN)
{
p_intmin(maxmin, counter);/* Send the number and the counter */
return;
}
/* Call a function that converts number to string */
str = _itoa(maxmin, &size);
/* Increasing the count of chars in the same amount of size */
*counter += size;
write(1, str, size);/* Write the string received */
free(str);/* Free memory */
}
/**
* _itoa - converts a number into a string
* @number: number to convert
* @size: pointer to size of new array
* Return: string
*/
char *_itoa(long number, int *size)
{
long j = 0, k = 0;
char *str;
if (number <= 0)
{
/* If the number is less than 0 asigned a value to remind to add a - */
if (number < 0)
k = 1;
number *= -1;/* Become positive the number */
/* Inicialize the variable to 1 for a slot additional for the - or 0 */
j = 1;
}
/* know the size, asigned to the original variable through the pointer */
*size = number;
for (; *size > 0; j++)
*size /= 10;/* Calculate the size by counting the digits */
/* know the size, asigned to the original variable through the pointer */
*size = j;
str = malloc(sizeof(char) * j);/* Asigning memory to the string */
if (str == NULL)
return (NULL);
for (; j > 0; j--)
{
/* take the last digit and assign it to the righ position in string */
str[j - 1] = (number % 10) + '0';
number /= 10;/* Reduce number in one digit */
}
if (k == 1)
str[j] = '-';/* If the original number was negative, add ' as first char */
return (str);/* Return the new string */
}
/**
* _printmod - picks a function to print a format input
* @list: argument list
* @inpt: character to compare
* *@str: pointer to a printbuffer
* @count: counts chars printed
* *@j: loop counter for printbuffer position
* Return: Pointer to str
*/
char *_printmod(va_list list, char inpt, char *str, int *count, int *j)
{
int k = 0;
data_t datas[] = {{"c", p_char}, {"s", p_string}, {"d", p_int}, {"i", p_int},
{"r", p_reverse}, {"R", p_rot13}, {"b", p_binary}, {"o", p_octal},
{"x", p_hexalow}, {"X", p_hexaup}, {NULL, NULL}};
while (k < 10) /*Search for coincidence into the structure*/
{
/* If the caracter is found in the array of structures */
if (datas[k].type[0] == inpt)
{
pbuffer(str);/* Call the function to print all the chars added before */
/* Return j to its initial position to reallocate the next chars */
(*j) = 0;
datas[k].data_proto(list, count);/* Send args to the function that points */
break;
}
k++;
}
if (k >= 10) /* Case character is not a modifier, so add inpt[i+1] to str */
{
if (inpt == '%')
{
str[*j] = inpt;
*count += 1;/* As there was 1 char added, we add 1 to count */
(*j)++;/* As there was 1 char added, we add 1 to position */
}
else
{
str[*j] = '%';/* Add first the char % and then the char next */
(*j)++;
str[*j] = inpt;
*count += 2; /* As there were 2 chars added, we add 2 to count */
(*j)++;
}
}
return (str); /* Return buffer */
}