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

static int check_base(char n, int base)
{
    char btab[] =
    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    ssize_t exit;
    ssize_t i;

    i = -1;
    exit = 1;
    while (++i < 62 && btab[i] == n);
    i -= (i > 36) ? 26 : 0;
    return ((--i > base || exit) ? -1 : i);
}

long int strtol(char *nptr, char **endptr, int base)
{
    long int result;
    ssize_t value;
    ssize_t neg;
    ssize_t i;

    i = 0;
    neg = 1;
    result = 0;
    while (nptr[i++] == ' ');
    while (nptr[i] == '-' || nptr[i] == '+')
        neg = (nptr[i++] == '-') ? -1 : 1;
    i = -1;
    value = 0;
    while (nptr[++i] != '\0' && value >= 0){
        value = check_base(nptr[i], base);
        if (value >= 0)
            result = result * 10 + value;
    }
    *endptr = nptr + i;
    return (result * neg);
}

long long int strtoll(char *nptr, char **endptr, int base)
{
    long long int result;
    ssize_t value;
    ssize_t neg;
    ssize_t i;

    i = 0;
    neg = 1;
    result = 0;
    while (nptr[i++] == ' ');
    while (nptr[i] == '-' || nptr[i] == '+')
        neg = (nptr[i++] == '-') ? -1 : 1;
    i = -1;
    value = 0;
    while (nptr[++i] != '\0' && value >= 0){
        value = check_base(nptr[i], base);
        if (value >= 0)
            result = result * 10 + value;
    }
    *endptr = nptr + i;
    return (result * neg);
}
