aboutsummaryrefslogtreecommitdiff
path: root/ft_printf.c
blob: c4df9b7bdce8abcd59a3ecc56c9c186870522250 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_printf.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/10/29 00:15:28 by cacharle          #+#    #+#             */
/*   Updated: 2019/11/13 08:56:49 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "header.h"

int			ft_printf(const char *format, ...)
{
	t_printf_status	status;

	if (format == NULL)
		return (STATUS_ERROR);
	if (!parse(format, &status.flist))
		return (STATUS_ERROR);
	va_start(status.ap, format);
	status.format = format;
	status.out = NULL;
	status.out_size = 0;
	while (*status.format)
	{
		if (*status.format == '%'
			&& (status.format =
				add_conversion(&status, status.flist->content)) == NULL)
			return (destroy_status_error(&status));
		else if ((status.format = add_between(&status)) == NULL)
			return (destroy_status_error(&status));
	}
	va_end(status.ap);
	list_destroy(&status.flist);
	write(STDOUT_FILENO, status.out, status.out_size);
	free(status.out);
	return (status.out_size);
}

const char	*add_conversion(t_printf_status *status, t_pformat *pformat)
{
	char	*conversion_str;

	conversion_str = convert(pformat, status->ap);
	if (pformat->specifier == 'n')
	{
		if (pformat->written != NULL)
			*pformat->written = status->out_size;
		status->format += pformat->fmt_len;
		list_pop_front(&status->flist);
		return (status->format + 1);
	}
	if (conversion_str == NULL)
		return (NULL);
	if (pformat->specifier != 'c')
		pformat->size = ft_strlen(conversion_str);
	if ((status->out = ft_memjoin_free(status->out, status->out_size,
									conversion_str, pformat->size)) == NULL)
		return (NULL);
	status->out_size += pformat->size;
	free(conversion_str);
	status->format += pformat->fmt_len;
	list_pop_front(&status->flist);
	return (status->format + 1);
}

const char	*add_between(t_printf_status *status)
{
	int i;

	i = 0;
	while (status->format[i] && status->format[i] != '%')
		i++;
	if ((status->out = ft_memjoin_free(status->out, status->out_size,
									(void*)status->format, i)) == NULL)
		return (NULL);
	status->out_size += i;
	return (status->format + i);
}

int			destroy_status_error(t_printf_status *status)
{
	if (status == NULL)
		return (STATUS_ERROR);
	va_end(status->ap);
	list_destroy(&status->flist);
	free(status->out);
	return (STATUS_ERROR);
}