<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   putwstr.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/05/23 13:33:14 by dengstra          #+#    #+#             */
/*   Updated: 2017/06/03 18:32:55 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_printf.h"

char		*ft_putwchar(wchar_t c, t_id *id)
{
	char	*result;

	if (c <= 127 || (id->specifier == 'c' && id->e_length == none))
		result = one_byte(c);
	else if (c <= 2047)
		result = two_byte(c);
	else if (c <= 65535)
		result = three_byte(c);
	else
		result = four_byte(c);
	return (result);
}

size_t		ft_putwstr(wchar_t *str, t_id *id)
{
	size_t	len;
	char	*result;
	char	*tmp;

	len = 0;
	if (!str)
		return (ft_putwstr(L"(null)", id));
	while (str[len++])
		;
	result = ft_strnew(len * 4);
	if (!result)
		exit(-1);
	while (*str)
	{
		tmp = ft_putwchar(*str++, id);
		ft_strcat(result, tmp);
		free(tmp);
	}
	len = ft_printstr(result, id);
	free(result);
	return (len);
}

int			ft_putbinary(size_t num)
{
	int len;

	len = 0;
	if (!num)
		return (0);
	len += ft_putbinary(num >> 8);
	if (num >> 8)
		len += write(1, " ", 1);
	len += ft_printf("%c%c%c%c%c%c%c%c",
			(num & 0b10000000) ? '1' : '0',
			(num & 0b01000000) ? '1' : '0',
			(num & 0b00100000) ? '1' : '0',
			(num & 0b00010000) ? '1' : '0',
			(num & 0b00001000) ? '1' : '0',
			(num & 0b00000100) ? '1' : '0',
			(num & 0b00000010) ? '1' : '0',
			(num & 0b00000001) ? '1' : '0');
	return (len);
}

int			ft_putzerochar(t_id *id)
{
	size_t width;

	width = 0;
	if (id->width)
		width = id->width - 1;
	if (id->minus)
	{
		ft_putchar(0);
		while (width--)
			ft_putchar(id->zero);
	}
	else
	{
		while (width--)
			ft_putchar(id->zero);
		ft_putchar(0);
	}
	return ((id->width) ? id->width : 1);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   btree_delone.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: douglas <<EMAIL>>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/07/09 00:57:48 by douglas           #+#    #+#             */
/*   Updated: 2017/07/09 02:06:57 by douglas          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

static t_btree	*find_leftmost_node(t_btree *root)
{
	while (root->left)
		root = root->left;
	return (root);
}

t_btree 		*btree_delone(t_btree *root, void *item, int (*cmp)(void *, void *),
					void (*free_node)(void *))
{
	t_btree *tmp;

	if (!root)
		return (NULL);
	if (cmp(root->item, item) > 0)
		root->right = btree_delone(root->right, item, cmp, free_node);
	else if (cmp(root->item, item) < 0)
		root->left = btree_delone(root->left, item, cmp, free_node);
	else
	{
		if (root->left == NULL)
		{
			tmp = root->right;
			free_node(root);
			return (tmp);
		}
		else if (root->right == NULL)
		{
			tmp = root->left;
			free_node(root);
			return (tmp);
		}
		tmp = find_leftmost_node(root->right);
		root->item = tmp->item;
		root->right = btree_delone(root, item, cmp, free_node);
	}
	return (root);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   printnbr.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/05/24 11:48:35 by douglas           #+#    #+#             */
/*   Updated: 2017/06/07 12:11:20 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_printf.h"

static	char	*precision(char *nbr, t_id *id)
{
	size_t	p_len;
	size_t	nbr_len;
	char	*result;

	if (!nbr)
		exit(-1);
	if (id->precision == 0 && *nbr == '0')
		return (ft_strnew(0));
	p_len = id->precision;
	nbr_len = ft_strlen(nbr);
	if (p_len > nbr_len)
	{
		if (!(result = ft_strnew(p_len)))
			exit(-1);
		ft_memset(result, '0', p_len);
		ft_memmove(result + p_len - nbr_len, nbr, nbr_len);
	}
	else
		result = ft_strdup(nbr);
	return (result);
}

static char		*get_sign(int minus, t_id *id)
{
	char specifier;

	specifier = id->specifier;
	if (specifier == 'x' || specifier == 'p')
		return ("0x");
	else if (specifier == 'X')
		return ("0X");
	else if (specifier == 'o' || specifier == 'O')
		return ("0");
	else if (minus)
		return ("-");
	else if (id->plus == '+')
		return ("+");
	else
		return ("");
}

static	int		sign_adder(t_id *id, int minus, char *nbr, int len)
{
	char *joined;

	if ((!ft_only_char(nbr, '0') || *nbr != '0'
						|| id->plus || id->specifier == 'p' || minus)
						&& (!ft_strchr("oOxX", id->specifier)
						|| (id->hash &&
						(*nbr || ft_strchr("oO", id->specifier)))))
	{
		if ((id->plus || id->hash || minus) && id->zero == '0')
		{
			len += ft_putstr(get_sign(minus, id));
			len += ft_putstrfree(padding(nbr, id, len));
		}
		else
		{
			joined = ft_strjoin(get_sign(minus, id), nbr);
			len += ft_putstrfree(padding(joined, id, len));
			free(joined);
		}
	}
	else
		len += ft_putstrfree(padding(nbr, id, len));
	return (len);
}

int				ft_print_nbr(char *nbr, t_id *id)
{
	int			len;
	int			minus;
	char		*prec;

	if (!nbr)
		exit(-1);
	if ((minus = (*nbr == '-') ? 1 : 0))
		nbr++;
	prec = NULL;
	if (id->has_precision)
	{
		if (!(prec = precision(nbr, id)))
			exit(-1);
		nbr = prec;
		id->zero = ' ';
	}
	len = 0;
	if (id->space && ft_strchr("di", id->specifier) && !minus && !id->plus)
		len = write(1, " ", 1);
	len = sign_adder(id, minus, nbr, len);
	free(prec);
	return (len);
}

int				ft_base(size_t n, char c, t_id *id)
{
	char	*base;
	char	*tmp;
	int		len;

	if (c == 'x' || c == 'p')
		base = BASE_16;
	else if (c == 'X')
		base = BASE_16_U;
	else
		base = BASE_8;
	tmp = ft_itoa_base(n, base);
	len = ft_print_nbr(tmp, id);
	if (tmp)
		free(tmp);
	return (len);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   padding.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/05/06 18:45:13 by dengstra          #+#    #+#             */
/*   Updated: 2017/06/03 18:34:45 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_printf.h"

char	*padding(char *str, t_id *id, size_t sign_len)
{
	size_t	width;
	size_t	str_len;
	void	*result;

	str_len = ft_strlen(str) + sign_len;
	if (id->specifier == 's' && id->has_precision)
		str_len = (id->precision < str_len) ? id->precision : str_len;
	width = (id->width > str_len) ? id->width : str_len;
	result = ft_strnew(width);
	if (!result)
		exit(-1);
	ft_memset(result, id->zero, width);
	if (!id->minus)
		ft_memmove(result + width - str_len, str, ft_strlen(str) + 1);
	else
		ft_memmove(result, str, str_len);
	return (result);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_printf.h                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/05/23 13:20:35 by dengstra          #+#    #+#             */
/*   Updated: 2017/06/08 17:35:40 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef FT_PRINTF_H
# define FT_PRINTF_H

# include <unistd.h>
# include <stdarg.h>
# include <stdlib.h>
# include "../libft.h"

# define BASE_16_U	"0123456789ABCDEF"
# define BASE_16	"0123456789abcdef"
# define BASE_10	"0123456789"
# define BASE_8		"01234567"

typedef struct		s_id
{
	char			hash;
	char			zero;
	char			plus;
	char			minus;
	char			space;
	size_t			width;
	size_t			precision;
	int				has_precision;
	char			specifier;
	struct s_id		*next;
	enum {
		none,
		h,
		hh,
		l,
		ll,
		j,
		z
	}				e_length;
}					t_id;

t_id				*ft_get_flags(char *format);
int					ft_printf(const char *format, ...);
int					ft_printer(char *format, t_id *id, va_list ap);
int					ft_base(size_t n, char c, t_id *id);
char				ft_is_specifier(char c);
int					ft_print_nbr(char *nbr, t_id *id);
int					ft_printstr(char *str, t_id *id);
size_t				ft_putwstr(wchar_t *str, t_id *id);
char				*ft_putwchar(wchar_t c, t_id *id);
int					ft_putbinary(size_t c);
size_t				length_converter(va_list ap, int len);
size_t				ulength_converter(va_list ap, int len);
char				*padding(char *str, t_id *id, size_t sign_len);
int					ft_putzerochar(t_id *id);
void				input_checker(t_id *id);
char				*one_byte(int c);
char				*two_byte(int c);
char				*three_byte(int c);
char				*four_byte(int c);

#endif
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   get_flags.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/05/05 22:27:28 by douglas           #+#    #+#             */
/*   Updated: 2017/06/03 18:34:11 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_printf.h"

static char	*get_length(char *format, t_id **id)
{
	if (*format == 'l' && *(format + 1) == 'l')
	{
		(*id)->e_length = ll;
		return (format + 2);
	}
	if (*format == 'h' && *(format + 1) == 'h')
	{
		(*id)->e_length = hh;
		return (format + 2);
	}
	(*format == 'l') ? (*id)->e_length = l : 0;
	(*format == 'h') ? (*id)->e_length = h : 0;
	(*format == 'j') ? (*id)->e_length = j : 0;
	(*format == 'z') ? (*id)->e_length = z : 0;
	if ((*id)->e_length)
		return (++format);
	return (format);
}

static char	*get_flags(t_id **id, char *format)
{
	while (ft_strchr("#0- +", *format))
	{
		if (*format == '#')
			(*id)->hash = *format;
		if (*format == '0' && !(*id)->minus)
			(*id)->zero = *format;
		if (*format == '-')
		{
			(*id)->minus = *format;
			(*id)->zero = ' ';
		}
		if (*format == '+')
			(*id)->plus = *format;
		if (*format == ' ')
			(*id)->space = ' ';
		format++;
	}
	return (format);
}

static t_id	*ft_id(char *format)
{
	t_id	*id;

	if (!(id = (t_id*)ft_memalloc(sizeof(t_id))))
		exit(-1);
	id->zero = ' ';
	id->specifier = 0;
	format = get_flags(&id, format);
	if (*format >= '1' && *format <= '9')
	{
		id->width = ft_atoi(format);
		while (ft_isdigit(*format))
			format++;
	}
	if (*format == '.')
	{
		id->precision = ft_atoi(++format);
		id->has_precision = 1;
		while (ft_isdigit(*format))
			format++;
	}
	format = get_length(format, &id);
	id->specifier = *format;
	if (id->specifier == 'p')
		id->hash = '#';
	return (id);
}

static void	add_to_lst(t_id **lst, t_id *id)
{
	t_id	*tmp;

	if (*lst == NULL)
	{
		*lst = id;
		return ;
	}
	tmp = *lst;
	while (tmp->next)
		tmp = tmp->next;
	tmp->next = id;
}

t_id		*ft_get_flags(char *format)
{
	t_id	*lst;

	lst = NULL;
	while (*format)
	{
		if (*format == '%')
			add_to_lst(&lst, ft_id(++format));
		format++;
	}
	return (lst);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_strtrim.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/04/18 11:14:59 by dengstra          #+#    #+#             */
/*   Updated: 2017/04/18 11:19:58 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

static unsigned int	is_white(char c)
{
	return (c == ' ' || c == '\n' || c == '\t');
}

char				*ft_strtrim(char const *s)
{
	unsigned int	needs_trim;
	unsigned int	len;
	char			*trimmed;

	if (!s)
		return (NULL);
	while (*s && is_white(*s))
		s++;
	if (!*s)
		return (ft_strnew(0));
	len = ft_strlen(s);
	needs_trim = 0;
	while (is_white(s[--len]))
		needs_trim = 1;
	if (needs_trim == 0)
		return (ft_strdup(s));
	trimmed = (char*)malloc(len + 2);
	if (!trimmed)
		return (NULL);
	trimmed[++len] = '\0';
	while (len--)
		trimmed[len] = s[len];
	return (trimmed);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   printer.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/05/05 22:27:44 by douglas           #+#    #+#             */
/*   Updated: 2017/06/03 18:27:51 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_printf.h"

static int		function_caller(char c, size_t num, t_id *id, va_list ap)
{
	int		len;
	char	*tmp;

	len = 0;
	tmp = NULL;
	len += (ft_strchr("uU", c)) ?
			ft_print_nbr((tmp = ft_itoa_base(num, BASE_10)), id) : 0;
	len += (ft_strchr("idD", c)) ? ft_print_nbr((tmp = ft_itoa(num)), id) : 0;
	len += (c == '%') ? ft_printstr("%", id) : 0;
	len += (num && ft_strchr("cC", c)) ?
			ft_printstr((tmp = ft_putwchar(num, id)), id) : 0;
	len += (!num && ft_strchr("cC", c)) ? ft_putzerochar(id) : 0;
	len += (c == 's' && id->e_length != l) ?
			ft_printstr(va_arg(ap, char*), id) : 0;
	len += (ft_strchr("xXoO", c)) ? ft_base(num, c, id) : 0;
	len += (c == 'p') ? ft_base((long)va_arg(ap, void*), c, id) : 0;
	len += (c == 'b') ? ft_putbinary(va_arg(ap, size_t)) : 0;
	len += (c == 'S' || (c == 's' && id->e_length == l))
			? ft_putwstr(va_arg(ap, wchar_t*), id) : 0;
	if (tmp)
		free(tmp);
	return (len);
}

static size_t	len_converter(char c, va_list ap, t_id *id)
{
	if (ft_strchr("cdDi", c))
		return (length_converter(ap, id->e_length));
	else if (ft_strchr("xXoOuUC", c))
		return (ulength_converter(ap, id->e_length));
	return (0);
}

int				ft_printer(char *format, t_id *id, va_list ap)
{
	char	c;
	int		len;
	t_id	*tmp;

	len = 0;
	if (!id)
		return (ft_putstr(format));
	while (*format)
	{
		if (*format != '%' && (len += ft_putchar(*format++)))
			continue;
		c = id->specifier;
		(ft_strchr("DOU", c)) ? id->e_length = l : 0;
		if (ft_is_specifier(c))
			len += function_caller(c, len_converter(c, ap, id), id, ap);
		(c == '%') ? format++ : 0;
		while (*format && *format++ != c)
			;
		tmp = id;
		id = id->next;
		free(tmp);
	}
	free(id);
	return (len);
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_strsplit.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/04/18 11:14:50 by dengstra          #+#    #+#             */
/*   Updated: 2017/04/18 12:35:05 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

static unsigned int		count_words(char const *s, char c)
{
	unsigned int i;
	unsigned int words;

	if (!*s)
		return (0);
	i = 0;
	words = 0;
	while (s[i + 1])
	{
		if (s[i] != c && s[i + 1] == c)
			words++;
		i++;
	}
	if (s[i] != c)
		words++;
	return (words);
}

static const char		*skip_c(char const *s, char c)
{
	while (*s == c)
		s++;
	return (s);
}

static unsigned int		word_len(char const *s, char c)
{
	unsigned int len;

	len = 0;
	while (*s && *s != c)
	{
		len++;
		s++;
	}
	return (len);
}

static char				**spliter(const char *s, char c, char **split,
		unsigned int words)
{
	unsigned int	i;
	unsigned int	len;

	i = 0;
	while (words--)
	{
		s = skip_c(s, c);
		len = word_len(s, c);
		split[i] = ft_strnew(len);
		if (!split[i])
			return (NULL);
		ft_memmove(split[i], s, len);
		split[i][len] = '\0';
		s += len;
		i++;
	}
	split[i] = NULL;
	return (split);
}

char					**ft_strsplit(char const *s, char c)
{
	unsigned int	words;
	char			**split;

	if (!s)
		return (NULL);
	s = skip_c(s, c);
	words = count_words(s, c);
	split = (char**)malloc(sizeof(*split) * (words + 1));
	if (!split)
		return (NULL);
	return (spliter(s, c, split, words));
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   length_converter.c                                 :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/05/24 11:35:29 by douglas           #+#    #+#             */
/*   Updated: 2017/06/03 18:29:00 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_printf.h"

size_t		length_converter(va_list ap, int len)
{
	if (len == h)
		return (short)(va_arg(ap, int));
	if (len == hh)
		return (char)(va_arg(ap, int));
	if (len == l)
		return (va_arg(ap, long));
	if (len == ll)
		return (va_arg(ap, long long));
	if (len == j)
		return (va_arg(ap, uintmax_t));
	if (len == z)
		return (va_arg(ap, size_t));
	return (va_arg(ap, int));
}

size_t		ulength_converter(va_list ap, int len)
{
	if (len == h)
		return (unsigned short)(va_arg(ap, unsigned int));
	if (len == hh)
		return (unsigned char)(va_arg(ap, unsigned int));
	if (len == l)
		return (va_arg(ap, unsigned long));
	if (len == ll)
		return (va_arg(ap, unsigned long long));
	if (len == j)
		return (va_arg(ap, uintmax_t));
	if (len == z)
		return (va_arg(ap, size_t));
	return (va_arg(ap, unsigned int));
}
<file_sep>/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   get_next_line.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dengstra <<EMAIL>>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/05/02 11:42:19 by dengstra          #+#    #+#             */
/*   Updated: 2017/06/02 14:11:07 by dengstra         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "get_next_line.h"

static int		ft_enqueue(t_queue *q)
{
	t_node *node;

	if (!(node = (t_node*)malloc(sizeof(t_node))))
		return (-1);
	node->next = NULL;
	if (!q->head)
	{
		q->tail = node;
		q->head = node;
	}
	else
	{
		q->tail->next = node;
		q->tail = node;
	}
	return (1);
}

static int		read_and_enqueue(int fd, t_queue *q)
{
	char	*buffer;
	int		i;
	int		read_return;
	int		new_line;

	new_line = 0;
	read_return = 1;
	while (!new_line && read_return > 0)
	{
		if (!(buffer = ft_strnew(BUFF_SIZE)))
			return (-1);
		read_return = read(fd, buffer, BUFF_SIZE);
		i = 0;
		while (read_return > 0 && buffer[i])
		{
			if (-1 == ft_enqueue(q))
				return (-1);
			q->tail->value = buffer[i++];
			if (buffer[i] == '\n')
				new_line = 1;
		}
		free(buffer);
	}
	return (read_return);
}

static char		dequeue(t_queue *q)
{
	t_node	*tmp;
	char	c;

	if (!q->head)
		return ('\0');
	c = q->head->value;
	tmp = q->head;
	q->head = q->head->next;
	free(tmp);
	return (c);
}

static int		add_line_and_dequeue(t_queue *q, char **line)
{
	int		len;
	int		ii;
	t_node	*i_node;

	len = 0;
	i_node = q->head;
	while (i_node && i_node->value != '\n')
	{
		i_node = i_node->next;
		len++;
	}
	if (!(line[0] = ft_strnew(len)))
		return (-1);
	ii = 0;
	while (len--)
		line[0][ii++] = dequeue(q);
	return (1);
}

int				get_next_line(const int fd, char **line)
{
	static t_queue	*start_q;
	t_queue			*current_q;
	int				read_return;

	current_q = start_q;
	while (current_q && current_q->fd != fd)
		current_q = current_q->next;
	if (!current_q)
	{
		if (!(current_q = (t_queue*)ft_memalloc(sizeof(t_queue))))
			return (-1);
		current_q->fd = fd;
		current_q->head = NULL;
		current_q->next = start_q;
		start_q = current_q;
	}
	if (0 > (read_return = read_and_enqueue(fd, current_q)))
		return (-1);
	if (-1 == add_line_and_dequeue(current_q, line))
		return (-1);
	if (!dequeue(current_q) && !line[0][0])
		return (0);
	return (1);
}
