/*
** EPITECH PROJECT, 2018
** task01
** File description:
** I do task
*/
#include "lib/my_stdio.h"

static void vfprintf_get_sign(printf_t *print)
{
    intmax_t value;

    value = (intmax_t)print->value;
    if (value < 0){
        print->value = (uintmax_t)(-value);
        print->str_width++;
        print->sign = '-';
        return;
    }
    if (print->mode_flags & PRINT_MODE_PLUS){
        print->str_width++;
        print->sign = '+';
        return;
    }
    if (print->mode_flags & PRINT_MODE_SPACE){
        print->str_width++;
        print->sign = ' ';
    }
}

static void vfprintf_get_size_unsigned(printf_t *print)
{
    uintmax_t value;

    if (print->type & PRINT_TYPE_CHAR)
        value = MY_UCHAR_MAX;
    if (print->type & PRINT_TYPE_SINT)
        value = MY_USHORT_MAX;
    if (print->type & PRINT_TYPE_LONG)
        value = MY_ULONG_MAX;
    if (print->type & PRINT_TYPE_PTRD)
        value = MY_PTRDIFF_MAX;
    if (print->type & PRINT_TYPE_LLONG)
        value = MY_ULLONG_MAX;
    if (!print->type)
        value = MY_UINT_MAX;
    while (value){
        value /= print->base;
        print->nbr_width++;
        print->str_width++;
    }
}

static void vfprintf_get_size_signed(printf_t *print)
{
    intmax_t value;

    value = (intmax_t)print->value;
    if (!value){
        print->nbr_width = 1;
        print->str_width++;
        return;
    }
    while (value){
        value /= print->base;
        print->nbr_width++;
        print->str_width++;
    }
}

void vfprintf_get_size(printf_t *print, char type)
{
    vfprintf_get_sign(print);
    if (type == 'x' || type == 'X' || type == 'p' || type == 'o'
    || type == 'b' || type == 'u'){
        vfprintf_get_size_unsigned(print);
        print->str_width += 2;
    }
    else
        vfprintf_get_size_signed(print);
    if (print->str_width < print->precision)
        print->str_width = print->precision;
}
