orton_runner/lib/my_stdio/vfprintf_print_arg.c

88 lines
2.1 KiB
C

/*
** EPITECH PROJECT, 2018
** task01
** File description:
** I do task
*/
#include <unistd.h>
#include "lib/my_stdio.h"
static const char base[2][16] = {
"0123456789abcdef",
"0123456789ABCDEF"
};
static void vfprintf_set_output(printf_t *print, char str[], int maj)
{
int locate;
int size;
locate = (maj) ? 1 : 0;
size = print->str_width;
while (++print->cursor < print->nbr_width){
str[size - 1 - print->cursor] =
base[locate][print->value % print->base];
print->value /= print->base;
print->precision--;
}
}
static void vfprintf_add_sign(printf_t *print, char str[])
{
int size;
size = print->str_width;
if (print->mode_flags & PRINT_MODE_DIEZ){
str[size - 1 - print->cursor++] = '0';
print->precision--;
}
if (print->precision > 0)
while (print->precision > 0){
str[size - 1 - print->cursor++] = '0';
print->precision--;
}
if (print->mode_flags & PRINT_MODE_HEXA){
if (print->mode_flags & PRINT_MODE_MAJ)
str[size - 1 - print->cursor++] = 'X';
else
str[size - 1 - print->cursor++] = 'x';
str[size - 1 - print->cursor++] = '0';
}
if (print->sign != '\0')
str[size - 1 - print->cursor++] = print->sign;
}
static void vfprintf_reverse_padding(char str[], int size)
{
char tmp[size + 1];
int len;
int i;
i = 0;
len = -1;
tmp[size] = '\0';
while (str[i++] == ' ');
while (str[i++])
tmp[++len] = str[i];
while (++len < size)
tmp[len] = ' ';
i = -1;
while (++i < size)
str[i] = tmp[i];
}
void vfprintf_print_arg(printf_t *print, int size)
{
char str[size + 1];
str[size] = '\0';
print->cursor = -1;
vfprintf_set_output(print, str, print->mode_flags & PRINT_MODE_MAJ);
vfprintf_add_sign(print, str);
while (print->cursor < size)
str[size - 1 - print->cursor++] = ' ';
if (print->mode_flags & PRINT_MODE_MINUS)
vfprintf_reverse_padding(str, print->str_width);
write(print->stream, str, sizeof(str) - 1);
}