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

int vfprintf_get_flags(printf_t *print, char const *format)
{
    int read_size;

    read_size = 0;
    while (format[++read_size] == '+' || format[read_size] == '#'
    || format[read_size] == ' ' || format[read_size] == '-'){
        if (format[read_size] == '#')
            print->mode_flags |= PRINT_MODE_DIEZ;
        if (format[read_size] == '+')
            print->mode_flags |= PRINT_MODE_PLUS;
        if (format[read_size] == '-')
            print->mode_flags |= PRINT_MODE_MINUS;
        if (format[read_size] == ' ')
            print->mode_flags |= PRINT_MODE_SPACE;
    }
    return (read_size);
}

int vfprintf_get_type(printf_t *print, char const *format)
{
    if (*format == 'l' && format[1] == 'l'){
        print->type |= PRINT_TYPE_LLONG;
        return (2);
    }
    if (*format == 'h' && format[1] == 'h'){
        print->type |= PRINT_TYPE_SINT;
        return (2);
    }
    print->type |= (*format == 'l') ? PRINT_TYPE_LONG : 0;
    print->type |= (*format == 'h') ? PRINT_TYPE_CHAR : 0;
    print->type |= (*format == 't') ? PRINT_TYPE_PTRD : 0;
    print->type |= (*format == 'j') ? PRINT_TYPE_INTM : 0;
    print->type |= (*format == 'z') ? PRINT_TYPE_SIZE : 0;
    print->type |= (*format == 'Z') ? PRINT_TYPE_SIZE : 0;
    print->type |= (*format == 'q') ? PRINT_TYPE_LLONG : 0;
    return ((print->type) ? 1 : 0);
}

int vfprintf_get_precision(printf_t *print, char const *format)
{
    int read_size;

    if (*format != '.')
        return (0);
    read_size = -1;
    while (format[++read_size] >= '0' && format[++read_size] <= '9'){
        print->precision *= 10;
        print->precision += format[read_size] - '0';
    }
    return (read_size);
}

void vfprintf_get_base(printf_t *print, char type)
{
    if (type == 'x' || type == 'X' || type == 'p')
        print->base = 16;
    if (type == 'b')
        print->base = 2;
    if (type == 'o')
        print->base = 8;
    if (type == 'X')
        print->mode_flags |= PRINT_MODE_MAJ;
}

int vfprintf_get_ouput_type(printf_t *print, char type)
{
    int len = -1;
    char buffer;
    char *str;

    if (type == 'c'){
        buffer = (char)va_arg(print->ap, int);
        write(1, &buffer, 1);
    }
    if (type == 's'){
        str = va_arg(print->ap, char*);
        while (str[++len] != '\0');
        write(1, str, len);
    }
    if (type == '%')
        write(1, "%", 1);
    if (type == '%' || type == 's' || type == 'c')
        return (-1);
    return ((!(type == 'o' || type == 'x' || type == 'X'
    || type == 'p' || type == 'b' || type == 'i'
    || type == 'd' || type == 'u')) ? 0 : 1);
}
