/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* helper.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/17 07:53:07 by cacharle #+# #+# */ /* Updated: 2019/07/18 10:38:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "include.h" void ft_putchar(char c) { write(1, &c, 1); } void ft_putnbr(int nb) { unsigned int p_nb; p_nb = nb; if (nb < 0) { write(1, "-", 1); p_nb = -nb; } if (p_nb > 9) ft_putnbr(p_nb / 10); ft_putchar(p_nb % 10 + '0'); } int pow10(int exponent) { int accumulator; accumulator = 1; while (exponent > 0) { accumulator *= 10; exponent--; } return (accumulator); } int ft_atoi(char *str) { int is_negative; int nb; int i; int j; while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\v' || *str == '\f' || *str == '\r') str++; is_negative = 0; while (*str == '-' || *str == '+') { if (*str == '-') is_negative = !is_negative; str++; } nb = 0; i = 0; while (str[i] >= '0' && str[i] <= '9') i++; j = 0; while (str[j] >= '0' && str[j] <= '9') nb += pow10(--i) * (str[j++] - '0'); if (is_negative) nb = -nb; return (nb); }