added c conversion suport
This commit is contained in:
parent
973ac2479a
commit
dcb8f8ea1d
5
Makefile
5
Makefile
@ -6,7 +6,7 @@
|
||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2019/02/09 15:12:22 by tmaze #+# #+# #
|
||||
# Updated: 2019/02/09 15:45:01 by tmaze ### ########.fr #
|
||||
# Updated: 2019/02/11 14:01:32 by tmaze ### ########.fr #
|
||||
# #
|
||||
#******************************************************************************#
|
||||
|
||||
@ -16,9 +16,10 @@ CCSTD :=
|
||||
|
||||
NAME := libftprintf.a
|
||||
|
||||
SRCS := ft_printf.c pf_getflags.c
|
||||
SRCS := ft_printf.c pf_getflags.c pf_strchr.c pf_isdigit.c pf_atoi.c pf_toupper.c pf_convc.c pf_memset.c pf_strlen.c pf_strnew.c pf_convs.c
|
||||
OBJS_DIR := objs
|
||||
OBJS := $(SRCS:.c=.o)
|
||||
INCLS := -Iincludes
|
||||
|
||||
.PHONY = all clean fclean re
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/07 13:03:44 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/09 14:38:22 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/02/11 10:06:43 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,8 +14,11 @@
|
||||
# define LIBFTPRINTF_H
|
||||
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <stdarg.h>
|
||||
|
||||
# include <stdio.h>
|
||||
|
||||
typedef struct s_flags
|
||||
{
|
||||
enum e_convtype
|
||||
@ -39,8 +42,17 @@ typedef struct s_flags
|
||||
int width;
|
||||
int precision;
|
||||
char size;
|
||||
char *buffer;
|
||||
} t_flags;
|
||||
|
||||
void *pf_memset(void *b, int c, size_t len);
|
||||
size_t pf_strlen(const char *s);
|
||||
char *pf_strnew(size_t size);
|
||||
char *pf_strchr(const char *s, int c);
|
||||
int pf_isdigit(int c);
|
||||
int pf_atoi(const char *str);
|
||||
int pf_toupper(int c);
|
||||
|
||||
void pf_putflags(t_flags *flags);
|
||||
size_t pf_getflags(const char *format, t_flags *flags);
|
||||
|
||||
|
@ -6,23 +6,73 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/07 13:31:16 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/07 14:21:34 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/02/11 14:00:11 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
int pf_convp(t_flags *flags, va_list *ap)
|
||||
{
|
||||
flags = NULL;
|
||||
ap = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int pf_convd(t_flags *flags, va_list *ap)
|
||||
{
|
||||
flags = NULL;
|
||||
ap = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int pf_convi(t_flags *flags, va_list *ap)
|
||||
{
|
||||
flags = NULL;
|
||||
ap = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int pf_convo(t_flags *flags, va_list *ap)
|
||||
{
|
||||
flags = NULL;
|
||||
ap = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int pf_convu(t_flags *flags, va_list *ap)
|
||||
{
|
||||
flags = NULL;
|
||||
ap = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int pf_convx(t_flags *flags, va_list *ap)
|
||||
{
|
||||
flags = NULL;
|
||||
ap = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int pf_convf(t_flags *flags, va_list *ap)
|
||||
{
|
||||
flags = NULL;
|
||||
ap = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_printf(const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
size_t i;
|
||||
size_t j;
|
||||
t_flags flags;
|
||||
static int (*convf[10])(t_flags *flags, va_list *ap) = {pf_convc,
|
||||
static int (*convf[11])(t_flags *flags, va_list *ap) = {pf_convc, pf_convc,
|
||||
pf_convs, pf_convp, pf_convd, pf_convi, pf_convo, pf_convu, pf_convx,
|
||||
pf_convx, pf_convf};
|
||||
|
||||
va_start(ap, format);
|
||||
flags.buffer = NULL;
|
||||
i = 0;
|
||||
while (format[i])
|
||||
{
|
||||
@ -31,6 +81,18 @@ int ft_printf(const char *format, ...)
|
||||
j++;
|
||||
write(1, &format[i], j);
|
||||
i += j;
|
||||
j = pf_getflags(&format[i], &flags);
|
||||
if (format[i] == '%')
|
||||
{
|
||||
pf_getflags(&format[i], &flags);
|
||||
if ((j = (*convf[flags.convtype])(&flags, &ap)) != -1)
|
||||
{
|
||||
va_end(ap);
|
||||
return (-1);
|
||||
}
|
||||
i += j;
|
||||
write(1, flags.buffer, pf_strlen(flags.buffer));
|
||||
free(flags.buffer);
|
||||
}
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
40
srcs/pf_atoi.c
Normal file
40
srcs/pf_atoi.c
Normal file
@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pf_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/11 07:51:09 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 07:51:31 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
int pf_atoi(const char *str)
|
||||
{
|
||||
int nbnum;
|
||||
int mult;
|
||||
int ret;
|
||||
|
||||
nbnum = 0;
|
||||
while (str[nbnum] == ' ' || str[nbnum] == '\t' || str[nbnum] == '\n'\
|
||||
|| str[nbnum] == '\v' || str[nbnum] == '\f' || str[nbnum] == '\r')
|
||||
nbnum++;
|
||||
if (str[nbnum] == '+' || str[nbnum] == '-')
|
||||
nbnum++;
|
||||
while (str[nbnum] != '\0' && pf_isdigit(str[nbnum]))
|
||||
nbnum++;
|
||||
nbnum--;
|
||||
ret = 0;
|
||||
mult = 1;
|
||||
while (nbnum >= 0 && pf_isdigit(str[nbnum]))
|
||||
{
|
||||
ret += (str[nbnum--] - '0') * mult;
|
||||
mult *= 10;
|
||||
}
|
||||
if (nbnum >= 0 && str[nbnum] == '-')
|
||||
ret *= -1;
|
||||
return (ret);
|
||||
}
|
@ -1,41 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* libftprintf.h :+: :+: :+: */
|
||||
/* pf_convc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/07 13:03:44 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/07 13:12:20 by tmaze ### ########.fr */
|
||||
/* Created: 2019/02/11 09:07:45 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 11:36:49 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBFTPRINTF_H
|
||||
# define LIBFTPRINTF_H
|
||||
#include "libftprintf.h"
|
||||
|
||||
# include <unistd.h>
|
||||
# include <stdarg.h>
|
||||
|
||||
typedef struct s_flags
|
||||
int pf_convc(t_flags *flags, va_list *ap)
|
||||
{
|
||||
enum e_convtype
|
||||
{
|
||||
c,
|
||||
s,
|
||||
p,
|
||||
d,
|
||||
i,
|
||||
o,
|
||||
u,
|
||||
x,
|
||||
X,
|
||||
f
|
||||
} convtype;
|
||||
char minus;
|
||||
char sign;
|
||||
int width;
|
||||
int precision;
|
||||
|
||||
}
|
||||
char c;
|
||||
size_t i;
|
||||
|
||||
#endif
|
||||
c = (flags->convtype == 0) ? '%' : (char)va_arg(*ap, int);
|
||||
i = 0;
|
||||
flags->width = (flags->width == 0) ? 1 : flags->width;
|
||||
if ((flags->buffer = pf_strnew(flags->width)) == NULL)
|
||||
return (1);
|
||||
if (flags->minus)
|
||||
flags->buffer[i++] = c;
|
||||
pf_memset(&(flags->buffer[i]), ' ', flags->width - 1);
|
||||
i += flags->width - 1;
|
||||
if (!flags->minus)
|
||||
flags->buffer[i] = c;
|
||||
return (0);
|
||||
}
|
26
srcs/pf_convs.c
Normal file
26
srcs/pf_convs.c
Normal file
@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pf_convs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/11 11:46:29 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 14:02:21 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
int pf_convs(t_flags *flags, va_list *ap)
|
||||
{
|
||||
char *s;
|
||||
size_t i;
|
||||
|
||||
|
||||
flags = NULL;
|
||||
i = 0;
|
||||
s = va_arg(*ap, char*);
|
||||
printf("--- pf_convs ---\n%s\n", s);
|
||||
return (-1);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/07 14:14:38 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/09 14:36:30 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/02/11 11:34:27 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -25,31 +25,18 @@ void pf_initflags(t_flags *flags)
|
||||
|
||||
void pf_putflags(t_flags *flags)
|
||||
{
|
||||
ft_putendl("--- putflags ---");
|
||||
ft_putstr("convtype->");
|
||||
ft_putnbr(flags->convtype);
|
||||
ft_putstr("/nminus->");
|
||||
ft_putchar(flags->minus);
|
||||
ft_putstr("/nsign->");
|
||||
ft_putchar(flags->sign);
|
||||
ft_putstr("/nhash->");
|
||||
ft_putchar(flags->hash);
|
||||
ft_putstr("/nwidth->");
|
||||
ft_putnbr(flags->width);
|
||||
ft_putstr("/nprecision->");
|
||||
ft_putnbr(flags->precision);
|
||||
ft_putstr("/nsize->");
|
||||
ft_putchar(flags->size);
|
||||
ft_putchar("/n");
|
||||
printf("--- putflags ---\nconvtype-> %d\nminus-> %c\nsign-> %c\nhash-> %c\nwidth-> %d\nprecision-> %d\nsize-> %c\nbuffer-> '%s'\n", flags->convtype, flags->minus, flags->sign, flags->hash, flags->width, flags->precision, flags->size, flags->buffer);
|
||||
}
|
||||
|
||||
size_t pf_getflags(const char *format, t_flags *flags)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
static char convs[12] = "%cspdiouxXf\0";
|
||||
|
||||
pf_initflags(flags);
|
||||
i = 0;
|
||||
while (format[++i] && ft_strchr("%cspdiouxXf", format[i]) != NULL)
|
||||
while (format[++i] && pf_strchr(convs, format[i]) == NULL)
|
||||
{
|
||||
if (format[i] == '-')
|
||||
flags->minus = '-';
|
||||
@ -57,14 +44,14 @@ size_t pf_getflags(const char *format, t_flags *flags)
|
||||
flags->hash = '#';
|
||||
if (format[i] == ' ' || format[i] == '+')
|
||||
flags->sign = format[i];
|
||||
if (ft_isdigit(format[i])
|
||||
|| (format[i] == '.' && ft_isdigit(format[i + 1])))
|
||||
if (pf_isdigit(format[i])
|
||||
|| (format[i] == '.' && pf_isdigit(format[i + 1])))
|
||||
{
|
||||
if (ft_isdigit(format[i]))
|
||||
flags->width = ft_atoi(&format[i]);
|
||||
if (pf_isdigit(format[i]))
|
||||
flags->width = pf_atoi(&format[i]);
|
||||
else
|
||||
flags->precision = ft_atoi(&format[i + 1]);
|
||||
while (format[i + 1] && ft_isdigit(format[i + 1]))
|
||||
flags->precision = pf_atoi(&format[i + 1]);
|
||||
while (format[i + 1] && pf_isdigit(format[i + 1]))
|
||||
i++;
|
||||
continue ;
|
||||
}
|
||||
@ -72,9 +59,15 @@ size_t pf_getflags(const char *format, t_flags *flags)
|
||||
{
|
||||
flags->size = format[i];
|
||||
if (format[i + 1] && format[i + 1] == format[i])
|
||||
flags->size = ft_toupper(format[++i]);
|
||||
flags->size = pf_toupper(format[++i]);
|
||||
}
|
||||
}
|
||||
flags->convtype = format[i];
|
||||
return (i);
|
||||
j = 0;
|
||||
while (convs[j])
|
||||
{
|
||||
if (format[i] == convs[j])
|
||||
flags->convtype = j;
|
||||
j++;
|
||||
}
|
||||
return (i + 1);
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pf_getflags.c :+: :+: :+: */
|
||||
/* pf_isdigit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/07 14:14:38 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/07 14:20:13 by tmaze ### ########.fr */
|
||||
/* Created: 2019/02/11 07:43:44 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 07:53:22 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
size_t *pf_getflags(const char *format, t_flags *flags)
|
||||
int pf_isdigit(int c)
|
||||
{
|
||||
|
||||
return (c >= '0' && c <= '9');
|
||||
}
|
@ -1,20 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putendl.c :+: :+: :+: */
|
||||
/* pf_memset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2018/04/07 22:37:44 by tmaze #+# #+# */
|
||||
/* Updated: 2019/01/13 17:44:21 by tmaze ### ########.fr */
|
||||
/* Created: 2019/02/11 09:57:23 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 09:59:01 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include "libftprintf.h"
|
||||
|
||||
void ft_putendl(char const *s)
|
||||
void *pf_memset(void *b, int c, size_t len)
|
||||
{
|
||||
if (s != NULL)
|
||||
ft_putstr(s);
|
||||
ft_putchar('\n');
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < len)
|
||||
((char*)b)[i++] = c;
|
||||
return (b);
|
||||
}
|
@ -1,19 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putstr.c :+: :+: :+: */
|
||||
/* pf_strchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2018/04/07 19:13:17 by tmaze #+# #+# */
|
||||
/* Updated: 2018/04/12 11:23:29 by tmaze ### ########.fr */
|
||||
/* Created: 2019/02/11 07:42:57 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 07:43:10 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include "libftprintf.h"
|
||||
|
||||
void ft_putstr(char const *s)
|
||||
char *pf_strchr(const char *s, int c)
|
||||
{
|
||||
int i;
|
||||
char *tmp;
|
||||
|
||||
if (s != NULL)
|
||||
write(1, s, ft_strlen(s));
|
||||
{
|
||||
i = -1;
|
||||
tmp = (char*)s;
|
||||
while (++i == 0 || tmp[i - 1])
|
||||
if (tmp[i] == c)
|
||||
return (&tmp[i]);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
@ -1,18 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar.c :+: :+: :+: */
|
||||
/* pf_strlen.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2018/04/07 19:03:57 by tmaze #+# #+# */
|
||||
/* Updated: 2018/04/07 23:02:29 by tmaze ### ########.fr */
|
||||
/* Created: 2019/02/11 10:05:26 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 10:05:29 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include "libftprintf.h"
|
||||
|
||||
void ft_putchar(char c)
|
||||
size_t pf_strlen(const char *s)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s != NULL && s[i] != '\0')
|
||||
i++;
|
||||
return (i);
|
||||
}
|
22
srcs/pf_strnew.c
Normal file
22
srcs/pf_strnew.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pf_strnew.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/11 09:54:18 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 09:59:23 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
char *pf_strnew(size_t size)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
if ((ret = malloc(size + 1)) != NULL)
|
||||
pf_memset(ret, 0, size + 1);
|
||||
return (ret);
|
||||
}
|
@ -1,21 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.c :+: :+: :+: */
|
||||
/* pf_toupper.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/07 13:31:16 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/07 13:34:43 by tmaze ### ########.fr */
|
||||
/* Created: 2019/02/11 07:51:45 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/11 07:51:54 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
int ft_printf(const char *format, ...)
|
||||
int pf_toupper(int c)
|
||||
{
|
||||
va_list ap;
|
||||
size_t i;
|
||||
|
||||
va_start();
|
||||
return ((c >= 'a' && c <= 'z') ? c - 32 : c);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user