8 files changed, +77, -29
a378824add header and initial implementation of dispatcher for char and string. rename "parsers" to "parser". update makefile
author
mogiya <git@mogiya.dev> on 31-03-2026 02:24 UTC+1
committer
mogiya <git@mogiya.dev> on 31-03-2026 02:24 UTC+1
parent20c1f8c
Makefile
+6, -2
ft_printf.c
+11, -14
+6, -2
    @@ -13,7 +13,9 @@ INCLUDE_DIR			= include
1313 #        DIRECTORIES         #
1414 #============================#
1515 
16  -PARSERS_DIR			= parsers
  16+PARSER_DIR			= parser
  17+DISPATCHER_DIR		= dispatcher
  18+PRINTERS_DIR		= printers
1719 UTILS_DIR			= utils
1820 OBJ_DIR				= obj
1921 LIBFT_DIR			= libft
    @@ -23,8 +25,10 @@ LIBFT_DIR			= libft
2325 #============================#
2426 
2527 SRCS				= ft_printf.c \
26  -					  $(wildcard $(PARSERS_DIR)/*.c) \
  28+					  $(wildcard $(PARSER_DIR)/*.c) \
2729 					  $(wildcard $(UTILS_DIR)/*.c) \
  30+					  $(wildcard $(PRINTERS_DIR)/*.c) \
  31+					  $(wildcard $(DISPATCHER_DIR)/*.c) \
2832 
2933 OBJS				= $(SRCS:%.c=$(OBJ_DIR)/%.o)
3034 
    @@ -0,0 +1,24 @@
   1+/* ************************************************************************** */
   2+/*                                                                            */
   3+/*                                                        :::      ::::::::   */
   4+/*   dispatcher.c                                       :+:      :+:    :+:   */
   5+/*                                                    +:+ +:+         +:+     */
   6+/*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
   7+/*                                                +#+#+#+#+#+   +#+           */
   8+/*   Created: 2026/03/31 01:30:26 by mleitao           #+#    #+#             */
   9+/*   Updated: 2026/03/31 02:13:57 by mleitao          ###   ########.fr       */
  10+/*                                                                            */
  11+/* ************************************************************************** */
  12+
  13+#include <dispatcher.h>
  14+
  15+void	dispatch(t_data *d)
  16+{
  17+	char	spec;
  18+
  19+	spec = d->format.spec;
  20+	if (spec == 'c')
  21+		print_char(d);
  22+	if (spec == 's')
  23+		print_str(d);
  24+}
+11, -14
    @@ -6,7 +6,7 @@
 6 6 /*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
 7 7 /*                                                +#+#+#+#+#+   +#+           */
 8 8 /*   Created: 2026/03/30 01:31:08 by mleitao           #+#    #+#             */
 9  -/*   Updated: 2026/03/31 00:36:36 by mleitao          ###   ########.fr       */
   9+/*   Updated: 2026/03/31 02:09:50 by mleitao          ###   ########.fr       */
1010 /*                                                                            */
1111 /* ************************************************************************** */
1212 
    @@ -20,25 +20,22 @@ static void	init_data(t_data *d, const char *fmt)
2020 
2121 int	ft_printf(const char *fmt, ...)
2222 {
23  -	t_data		data;
24  -	int			count;
  23+	t_data		d;
2524 
26  -	count = 0;
27  -	init_data(&data, fmt);
28  -	va_start(data.args, fmt);
29  -	while (*data.fmt)
  25+	init_data(&d, fmt);
  26+	va_start(d.args, fmt);
  27+	while (*d.fmt)
3028 	{
31  -		if (*data.fmt == '%')
  29+		if (*d.fmt == '%')
3230 		{
33  -			if (parse_format(&data))
  31+			if (parse_format(&d))
3432 			{
35  -				//print_format(&data);
  33+				dispatch(&d);
3634 				continue ;
3735 			}
3836 		}
39  -		count += write(1, data.fmt++, 1);
  37+		d.chars_written += write(1, d.fmt++, 1);
4038 	}
41  -	count += data.chars_written;
42  -	va_end(data.args);
43  -	return (count);
  39+	va_end(d.args);
  40+	return (d.chars_written);
4441 }
    @@ -0,0 +1,20 @@
   1+/* ************************************************************************** */
   2+/*                                                                            */
   3+/*                                                        :::      ::::::::   */
   4+/*   dispatcher.h                                       :+:      :+:    :+:   */
   5+/*                                                    +:+ +:+         +:+     */
   6+/*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
   7+/*                                                +#+#+#+#+#+   +#+           */
   8+/*   Created: 2026/03/31 01:31:59 by mleitao           #+#    #+#             */
   9+/*   Updated: 2026/03/31 01:37:24 by mleitao          ###   ########.fr       */
  10+/*                                                                            */
  11+/* ************************************************************************** */
  12+
  13+#ifndef DISPATCHER_H
  14+# define DISPATCHER_H
  15+
  16+# include <printers.h>
  17+
  18+void	dispatch(t_data *d);
  19+
  20+#endif
    @@ -6,7 +6,7 @@
 6 6 /*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
 7 7 /*                                                +#+#+#+#+#+   +#+           */
 8 8 /*   Created: 2026/03/30 01:35:34 by mleitao           #+#    #+#             */
 9  -/*   Updated: 2026/03/30 23:41:12 by mleitao          ###   ########.fr       */
   9+/*   Updated: 2026/03/31 01:54:10 by mleitao          ###   ########.fr       */
1010 /*                                                                            */
1111 /* ************************************************************************** */
1212 
    @@ -16,7 +16,9 @@
1616 /****************** REMOVE ******************/
1717 # include <stdio.h>
1818 /********************************************/
19  -# include <parsers.h>
  19+# include <parser.h>
  20+# include <dispatcher.h>
  21+# include <printers.h>
2022 
2123 int			ft_printf(const char *fmt, ...);
2224 
    @@ -1,22 +1,24 @@
 1 1 /* ************************************************************************** */
 2 2 /*                                                                            */
 3 3 /*                                                        :::      ::::::::   */
 4  -/*   parsers.h                                          :+:      :+:    :+:   */
   4+/*   parser.h                                           :+:      :+:    :+:   */
 5 5 /*                                                    +:+ +:+         +:+     */
 6 6 /*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
 7 7 /*                                                +#+#+#+#+#+   +#+           */
 8 8 /*   Created: 2026/03/30 18:24:06 by mleitao           #+#    #+#             */
 9  -/*   Updated: 2026/03/31 01:01:32 by mleitao          ###   ########.fr       */
   9+/*   Updated: 2026/03/31 01:59:34 by mleitao          ###   ########.fr       */
1010 /*                                                                            */
1111 /* ************************************************************************** */
1212 
13  -#ifndef PARSERS_H
14  -# define PARSERS_H
  13+#ifndef PARSER_H
  14+# define PARSER_H
1515 
  16+/* REMOVE LATER */
  17+# include <stdio.h>
  18+/****************/
1619 # include <stdarg.h>
1720 # include <libft.h>
1821 # include <utils.h>
19  -# include <stdio.h>
2022 
2123 # define SPECIFIERS "cspdiuxX%"
2224 # define FLAGS "-0# +"
    @@ -6,11 +6,11 @@
 6 6 /*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
 7 7 /*                                                +#+#+#+#+#+   +#+           */
 8 8 /*   Created: 2026/03/30 17:45:38 by mleitao           #+#    #+#             */
 9  -/*   Updated: 2026/03/31 00:56:51 by mleitao          ###   ########.fr       */
   9+/*   Updated: 2026/03/31 02:11:02 by mleitao          ###   ########.fr       */
1010 /*                                                                            */
1111 /* ************************************************************************** */
1212 
13  -#include <parsers.h>
  13+#include <parser.h>
1414 /*
1515 static void	print_format(t_format *format)
1616 {
    @@ -22,8 +22,7 @@ static void	print_format(t_format *format)
2222 	printf("width -> %d\n", format->width);
2323 	printf("precision -> %d\n", format->precision);
2424 	printf("spec -> %c\n", format->spec);
25  -}
26  -*/
  25+} */
2726 
2827 int	parse_format(t_data *d)
2928 {
    @@ -6,11 +6,11 @@
 6 6 /*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
 7 7 /*                                                +#+#+#+#+#+   +#+           */
 8 8 /*   Created: 2026/03/30 18:11:57 by mleitao           #+#    #+#             */
 9  -/*   Updated: 2026/03/31 00:52:53 by mleitao          ###   ########.fr       */
   9+/*   Updated: 2026/03/31 01:34:43 by mleitao          ###   ########.fr       */
1010 /*                                                                            */
1111 /* ************************************************************************** */
1212 
13  -#include <parsers.h>
  13+#include <parser.h>
1414 
1515 void	reset_format(t_data *d)
1616 {