/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/05 11:30:56 by ljiriste #+# #+# */
-/* Updated: 2023/12/09 15:49:45 by ljiriste ### ########.fr */
+/* Updated: 2024/03/05 09:16:23 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (len);
}
-int tp_print_and_free_members(t_to_print *tp, t_conv conv)
+static int tp_print_and_free_members(int fd, t_to_print *tp, t_conv conv)
{
int len;
len = to_print_len(tp);
- ft_putstr_fd(tp->left_pad, 1);
+ ft_putstr_fd(tp->left_pad, fd);
if (tp->sign != '\0')
- ft_putchar_fd(tp->sign, 1);
- ft_putstr_fd(tp->alt, 1);
- ft_putstr_fd(tp->zero_pad, 1);
+ ft_putchar_fd(tp->sign, fd);
+ ft_putstr_fd(tp->alt, fd);
+ ft_putstr_fd(tp->zero_pad, fd);
if (*(tp->main_part) == '\0' && conv.type == 'c')
{
- ft_putchar_fd('\0', 1);
+ ft_putchar_fd('\0', fd);
++len;
}
else
- ft_putstr_fd(tp->main_part, 1);
- ft_putstr_fd(tp->right_pad, 1);
+ ft_putstr_fd(tp->main_part, fd);
+ ft_putstr_fd(tp->right_pad, fd);
free(tp->left_pad);
free(tp->alt);
free(tp->zero_pad);
return (len);
}
-int handle_conversion(const char **format, va_list *args)
+int handle_conversion(int fd, const char **format, va_list *args)
{
t_conv conv;
char *temp;
free(temp);
if (!valid_toprint(to_print))
return (-1);
- return (tp_print_and_free_members(&to_print, conv));
+ return (tp_print_and_free_members(fd, &to_print, conv));
}
/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/17 09:14:21 by ljiriste #+# #+# */
-/* Updated: 2023/12/09 15:41:36 by ljiriste ### ########.fr */
+/* Updated: 2024/03/05 09:21:56 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdarg.h> // va_*
#include <unistd.h> // write
-int print_ordinary(const char **s)
+static int print_ordinary(int fd, const char **s)
{
int len;
len = 0;
while ((*s)[len] && (*s)[len] != '%')
++len;
- write(1, *s, len);
+ write(fd, *s, len);
*s += len;
return (len);
}
-int ft_printf(const char *format, ...)
+int ft_vdprintf(int fd, const char *format, va_list *args)
{
- va_list args;
- int res;
+ int res;
+ if (fd < 0 || write(fd, NULL, 0) == -1)
+ return (0);
res = 0;
- va_start(args, format);
while (*format)
{
- res += print_ordinary(&format);
+ res += print_ordinary(fd, &format);
if (!*format)
break ;
- res += handle_conversion(&format, &args);
+ res += handle_conversion(fd, &format, args);
}
+ return (res);
+}
+
+int ft_dprintf(int fd, const char *format, ...)
+{
+ va_list args;
+ int res;
+
+ va_start(args, format);
+ res = ft_vdprintf(fd, format, &args);
+ va_end(args);
+ return (res);
+}
+
+int ft_printf(const char *format, ...)
+{
+ va_list args;
+ int res;
+
+ va_start(args, format);
+ res = ft_vdprintf(STDOUT_FILENO, format, &args);
va_end(args);
return (res);
}
/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/05 12:00:16 by ljiriste #+# #+# */
-/* Updated: 2023/12/09 15:13:41 by ljiriste ### ########.fr */
+/* Updated: 2024/03/05 09:15:29 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
} t_to_print;
int ft_printf(const char *format, ...);
-int handle_conversion(const char **format, va_list *args);
+int ft_dprintf(int fd, const char *format, ...);
+int ft_vdprintf(int fd, const char *format, va_list *args);
+int handle_conversion(int fd, const char **format, va_list *args);
t_to_print formatting(char **str, t_conv conv);
t_conv parse_format(const char **format, va_list *args);
void create_padding(t_to_print *tp, t_conv conv);
/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 11:38:28 by ljiriste #+# #+# */
-/* Updated: 2023/12/09 15:13:41 by ljiriste ### ########.fr */
+/* Updated: 2024/03/05 09:30:28 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_IO_H
# define FT_IO_H
+# include <stdarg.h>
+
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
int ft_printf(const char *format, ...);
+int ft_dprintf(int fd, const char *format, ...);
+int ft_vdprintf(int fd, const char *format, va_list *args);
char *get_next_line(int fd);
#endif