Browse Source

[enh] Add printf and dprintf functions. [Fix] ortho in Makefile.

Moul 4 years ago
parent
commit
e87fdd161d
4 changed files with 89 additions and 4 deletions
  1. 5 3
      Makefile
  2. 4 1
      libft.h
  3. 40 0
      src/ft_dprintf.c
  4. 40 0
      src/ft_printf.c

+ 5 - 3
Makefile

@@ -6,7 +6,7 @@
 #    By: mazimi <mazimi@student.42.fr>              +#+  +:+       +#+         #
 #                                                 +#+#+#+#+#+   +#+            #
 #    Created: 2014/11/05 11:09:24 by mazimi            #+#    #+#              #
-#    Updated: 2015/12/17 18:02:11 by mazimi           ###   ########.fr        #
+#    Updated: 2016/02/12 18:00:50 by mazimi           ###   ########.fr        #
 #                                                                              #
 # **************************************************************************** #
 
@@ -72,6 +72,8 @@ ft_putchar_fd.c \
 ft_putstr_fd.c \
 ft_putendl_fd.c \
 ft_putnbr_fd.c \
+ft_printf.c \
+ft_dprintf.c \
 ft_lstnew.c \
 ft_lstdelone.c \
 ft_lstdel.c \
@@ -107,11 +109,11 @@ $(NAME):
 clean:
 	@rm -f $(OBJ_NAME)
 #	@rmdir $(OBJ_PATH) 2> /dev/null || true
-	@echo "Lifbt: clean: objects cleaned."
+	@echo "Libft: clean: objects cleaned."
 
 fclean: clean
 	@rm -f $(NAME)
-	@echo "Lifbt: fclean: library deleted."
+	@echo "Libft: fclean: library deleted."
 
 re: fclean all
 

+ 4 - 1
libft.h

@@ -6,7 +6,7 @@
 /*   By: mazimi <mazimi@student.42.fr>              +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2014/11/23 19:50:04 by mazimi            #+#    #+#             */
-/*   Updated: 2015/12/11 11:50:07 by mazimi           ###   ########.fr       */
+/*   Updated: 2016/02/12 17:59:51 by mazimi           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,6 +18,7 @@
 # include <stdlib.h>
 # include <ctype.h>
 # include <stddef.h>
+# include <stdarg.h>
 
 typedef struct		s_list
 {
@@ -86,6 +87,8 @@ void				ft_putchar_fd(char c, int fd);
 void				ft_putstr_fd(char const *s, int fd);
 void				ft_putendl_fd(char const *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, ...);
 t_list				*ft_lstnew(void const *content, size_t content_size);
 void				ft_lstdelone(t_list **alst, void (*del)(void*, size_t));
 void				ft_lstdel(t_list **alst, void (*del)(void*, size_t));

+ 40 - 0
src/ft_dprintf.c

@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_dprintf.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: mazimi <marvin@42.fr>                      +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2016/02/12 17:47:53 by mazimi            #+#    #+#             */
+/*   Updated: 2016/02/12 17:48:01 by mazimi           ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "../libft.h"
+
+int	ft_dprintf(int fd, const char *format, ...)
+{
+	va_list	ap;
+	int		i;
+
+	va_start(ap, format);
+	i = 0;
+	while (format[i])
+	{
+		if (format[i] == '%')
+		{
+			i++;
+			if (format[i] == 's')
+				ft_putstr_fd(va_arg(ap, char*), fd);
+			if (format[i] == 'd')
+				ft_putnbr_fd(va_arg(ap, int), fd);
+			i++;
+		}
+		while ((format[i] != '%') && (format[i]))
+		{
+			ft_putchar_fd(format[i], fd);
+			i++;
+		}
+	}
+	return (0);
+}

+ 40 - 0
src/ft_printf.c

@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_printf.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: mazimi <marvin@42.fr>                      +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2016/02/12 17:47:22 by mazimi            #+#    #+#             */
+/*   Updated: 2016/02/12 17:47:41 by mazimi           ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "../libft.h"
+
+int	ft_printf(const char *format, ...)
+{
+	va_list	ap;
+	int		i;
+
+	va_start(ap, format);
+	i = 0;
+	while (format[i])
+	{
+		if (format[i] == '%')
+		{
+			i++;
+			if (format[i] == 's')
+				ft_putstr(va_arg(ap, char*));
+			if (format[i] == 'd')
+				ft_putnbr(va_arg(ap, int));
+			i++;
+		}
+		while ((format[i] != '%') && (format[i]))
+		{
+			ft_putchar(format[i]);
+			i++;
+		}
+	}
+	return (0);
+}