aboutsummaryrefslogtreecommitdiff
path: root/ft_printf.c
blob: 5bfd2e8a6212660fa8a3d6b9633502fe93c31dec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_printf.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/10/29 00:15:28 by cacharle          #+#    #+#             */
/*   Updated: 2019/10/29 05:17:55 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

int	ft_printf(const char *format, ...)
{
	va_list ap;
	t_flist	*format_list;
	char	*str;
	int		print_len;
	int		i;

	str = ft_strdup(format);
	if ((format_list = parse(str)) == NULL)
		return (-1);
	free(str);
	va_start(ap, format);
	print_len = 0;
	i = -1;
	while (format[++i])
	{
		if (format[i] != '%')
		{
			write(STDOUT_FILENO, format + i, 1);
			continue ;
		}
		str = convert(format_list->content, ap);
		if (str == NULL)
		{
			list_destroy(&format_list);
			return (-1);
		}
		ft_putstr(str);
		print_len += ft_strlen(str);
		free(str);
		i += format_list->content->len;
		list_pop_front(&format_list);
	}
	list_destroy(&format_list);
	va_end(ap);
	return (print_len + i);
}