| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_printf.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2016/02/11 00:22:06 by bchanot #+# #+# */
- /* Updated: 2018/10/14 03:40:26 by bchanot ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "libftprintf.h"
- #include <unistd.h>
- static int (*g_func[])(va_list, t_inf) = {
- pf_percent, pf_s, pf_s_up, pf_p,
- pf_di, pf_d_up, pf_di, pf_o,
- pf_o_up, pf_u, pf_u_up, pf_x,
- pf_x_up, pf_c, pf_c_up, pf_b};
- int pf_print_part(const char *format, char *s, int fd)
- {
- int len;
- len = 0;
- while (format + len != s)
- len++;
- write(fd, format, len);
- return (len);
- }
- static int ft_format(char **s, va_list ap, t_inf inf)
- {
- int len;
- len = 0;
- if (inf.type == -1)
- return (-2);
- else if (inf.type >= 0)
- len = g_func[inf.type](ap, inf);
- if (len < 0)
- return (-1);
- *s -= 1;
- return (len);
- }
- static int pf_printf(const char **format, va_list ap, char **s, int tmp[2])
- {
- int chk;
- t_inf inf;
- int len;
- len = tmp[0];
- len += pf_print_part(*format, *s, tmp[1]);
- *s += 1;
- inf = pf_take_inf(s, ap, tmp[1]);
- if ((chk = ft_format(s, ap, inf)) < 0)
- {
- if (chk == -1)
- return (-1);
- if (pf_wrong_conv(s, format, inf, &len))
- return (len);
- }
- else
- {
- *format = *s + 1;
- len += chk;
- }
- return (len);
- }
- int ft_printf(int fd, const char *format, ...)
- {
- va_list ap;
- int len[2];
- char *s;
- int col;
- len[0] = 0;
- len[1] = fd;
- col = 0;
- s = (char *)format;
- va_start(ap, format);
- while (*s)
- {
- if (*s == '{')
- col = pf_take_color(&s, &format, &len[0], len[1]);
- if (*s == '%')
- if ((len[0] = pf_printf(&format, ap, &s, len) == -1))
- return (-1);
- s++;
- }
- va_end(ap);
- len[0] += pf_putstr(format, fd);
- if (col == 1)
- ft_putstr_fd(EOC, fd);
- return (len[0]);
- }
|