tgrekov-fdf
HIVE fdf May 2024
Loading...
Searching...
No Matches
ft_printf.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_printf.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2023/10/31 23:03:16 by tgrekov #+# #+# */
9/* Updated: 2024/04/26 10:28:29 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include "../../../libft.h"
21#include "utils/utils.h"
22#include <stdarg.h>
23
24int handle_sequence(const char **format, va_list args, int *fd, int total);
25
36static void do_segment(const char **format, va_list args, int *fd, int *total)
37{
38 char *seq_start;
39 int print_len;
40
41 seq_start = ft_strchr(*format, '%');
42 if (!seq_start)
43 print_len = ft_strlen(*format);
44 else
45 print_len = seq_start - *format;
46 if (print_len && seq_start != *format
47 && !wrap_err(write(*fd, *format, print_len), total))
48 return ;
49 *format += print_len;
50 if (seq_start)
51 {
52 (*format)++;
53 wrap_err(handle_sequence(format, args, fd, *total), total);
54 }
55}
56
252int ft_printf(const char *format, ...)
253{
254 va_list args;
255 int total;
256 int fd;
257
258 if (!format)
259 return (-1);
260 fd = STDOUT_FILENO;
261 va_start(args, format);
262 total = 0;
263 while (total > -1 && *format)
264 do_segment(&format, args, &fd, &total);
265 va_end(args);
266 return (total);
267}
int wrap_err(int n, int *total)
Wrapper for handling functions that return a positive integer on success and -1 on failure....
Definition wrap_err.c:29
int ft_printf(const char *format,...)
Prints and formats a variable set of arguments.
Definition ft_printf.c:252
int handle_sequence(const char **format, va_list args, int *fd, int total)
Prepares and executes a sequence.
static void do_segment(const char **format, va_list args, int *fd, int *total)
Process a section of the format string, whether it contains text to print and / or format specifiers.
Definition ft_printf.c:36
char * ft_strchr(const char *s, int c)
Finds first occurence of character c in string s.
Definition ft_strchr.c:30
size_t ft_strlen(const char *str)
Get length of str.
Definition ft_strlen.c:28