-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferization.c
More file actions
143 lines (129 loc) · 3.47 KB
/
bufferization.c
File metadata and controls
143 lines (129 loc) · 3.47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bufferization.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlesieur <dlesieur@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/30 19:32:46 by dlesieur #+# #+# */
/* Updated: 2025/07/02 14:02:16 by dlesieur ### ########.fr */
/* */
/* ************************************************************************** */
/**
Instead of calling spagetti code ft ft_printf, let's recode logs function
that give more send to a debug maintainability in both client and server.
*/
# include "auto_parser.h"
void buffer_append_char(t_buffer buffer, int *pos, char c)
{
if (*pos < 1023)
buffer[(*pos)++] = c;
}
void buffer_append_str(t_buffer buffer, int *pos, const char *str)
{
while (*str && *pos < 1023)
buffer[(*pos)++] = *str++;
}
void buffer_append_str_formatted(t_buffer buffer, int *pos,
const char *str, t_format_spec *spec)
{
int len;
int padding;
int i;
int max_chars;
len = 0;
if (str)
len = strlen(str);
max_chars = len;
if (spec->has_precision && spec->precision < len)
max_chars = spec->precision;
padding = 0;
if (spec->width > max_chars)
padding = spec->width - max_chars;
if (!spec->left_align && padding > 0)
{
i = -1;
while (++i < padding && *pos < 1023)
buffer[(*pos)++] = ' ';
}
i = -1;
while (++i < max_chars && *pos < 1023)
buffer[(*pos)++] = str[i];
if (spec->left_align && padding > 0)
{
i = -1;
while (++i < padding && *pos < 1023)
buffer[(*pos)++] = ' ';
}
}
static void reverse_string(char *str, int len)
{
int start;
int end;
char temp;
start = 0;
end = len - 1;
while (start < end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
static int convert_to_string(long num, char *temp, int base, int uppercase)
{
int temp_pos;
char *digits;
if (uppercase)
digits = "0123456789ABCDEF";
else
digits = "0123456789abcdef";
temp_pos = 0;
if (num == 0)
temp[temp_pos++] = '0';
else
{
while (num > 0)
{
temp[temp_pos++] = digits[num % base];
num /= base;
}
}
return (temp_pos);
}
void buffer_append_int_formatted(t_buffer buffer, int *pos,
long num, t_format_spec *spec, int base)
{
char temp[64];
int temp_pos;
int is_negative;
int uppercase;
is_negative = 0;
uppercase = (spec->specifier == 'X');
if (num < 0 && base == 10)
{
is_negative = 1;
num = -num;
}
temp_pos = convert_to_string(num, temp, base, uppercase);
if (is_negative)
temp[temp_pos++] = '-';
else if (spec->show_sign && base == 10)
temp[temp_pos++] = '+';
else if (spec->space_prefix && base == 10)
temp[temp_pos++] = ' ';
reverse_string(temp, temp_pos);
temp[temp_pos] = '\0';
buffer_append_str(buffer, pos, temp);
}
void put_level_buffered(t_buffer buffer, int *pos, t_log_level level)
{
t_parser_tables *tables;
tables = get_parser_tables();
if (level >= 0 && level < 5 && tables->level_strings[level])
buffer_append_str(buffer, pos, tables->level_strings[level]);
else
buffer_append_str(buffer, pos, "[UNKNOWN] ");
}