aboutsummaryrefslogtreecommitdiff
path: root/ft_printf.c
blob: da9a14ac21a84d0092105f4272c12081c10a8333 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_printf.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/10/29 00:15:28 by cacharle          #+#    #+#             */
/*   Updated: 2019/11/06 00:01:32 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "header.h"

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

	if (format == NULL)
		return (-1);
	str = ft_strdup(format);
	if (!parse(str, &flist))
		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);
			print_len++;
			continue ;
		}
		str = convert(flist->content, ap);
		if (str == NULL && flist->content->specifier == 'n')
		{
			if (flist->content->written != NULL)
				*flist->content->written = print_len;
			i += flist->content->fmt_len;
			list_pop_front(&flist);
			continue;
		}
		if (str == NULL)
		{
			list_destroy(&flist);
			return (-1);
		}
		if (flist->content->specifier == 'c')
		{
			write(1, str, flist->content->size);
			print_len++;
			free(str);
		}
		else
		{
			ft_putstr(str);
			print_len += ft_strlen(str);
			free(str);
		}
		i += flist->content->fmt_len;
		list_pop_front(&flist);
	}
	list_destroy(&flist);
	va_end(ap);
	return (print_len);
}