Browse Source

added sprintf and fprintf

master
nicolas-arnaud 2 years ago
parent
commit
55944fc02b
  1. 3
      .gitignore
  2. 32
      Makefile
  3. 130
      compile_commands.json
  4. 34
      includes/ft_printf.h
  5. 110
      includes/libft.h
  6. 2
      libft
  7. 55
      srcs/ft_print_chars.c
  8. 20
      srcs/ft_print_hexs.c
  9. 21
      srcs/ft_print_nbrs.c
  10. 0
      srcs/ft_print_opts.c
  11. 10
      srcs/ft_print_ptrs.c
  12. 45
      srcs/ft_print_strings.c
  13. 94
      srcs/ft_printf.c
  14. 1
      tester

3
.gitignore

@ -1,4 +1,5 @@
ft_printf_tester
tester
.cache
# ---> C

32
Makefile

@ -1,33 +1,41 @@
NAME = libftprintf.a
PRINTF = ./lib/libftprintf.a
SPRINTF = ./lib/libftsprintf.a
FPRINTF = ./lib/libftfprintf.a
SRCS = ft_print_chars.c ft_print_hexs.c ft_print_nbrs.c ft_print_opts.c ft_print_ptrs.c ft_printf.c
SRCS = ft_print_chars.c ft_print_hexs.c ft_print_nbrs.c ft_print_opts.c ft_print_ptrs.c ft_printf.c ft_print_strings.c
OBJS = ${SRCS:.c=.o}
RM = rm -rf
CC = gcc
CFLAGS = -Werror -Wall -Wextra
AR = ar -rcs
LIBFT = ./libft/libft.a
.c.o:
${CC} ${CFLAGS} -c $< -o ${<:.c=.o} -g
${CC} ${CFLAGS} -c $< -o ${<:.c=.o} -g -I./includes
all: $(NAME)
bonus:
bonus: $(NAME)
$(NAME): $(OBJS)
$(LIBFT):
${MAKE} -C ./libft
cp ./libft/libft.a libftprintf.a
${AR} ${NAME} ${OBJS}
cp ./libft/libft.a $(NAME)
cp ./libft/libft.h ./includes/libft.h
$(PRINTF): $(LIBFT) $(addprefix ./srcs/, ${OBJS})
ar -rcs $(NAME) $(addprefix ./srcs/, ${OBJS})
$(NAME): $(LIBFT) $(PRINTF)
clean:
${RM} ${OBJS}
${RM} ./libft/*.o
${RM} srcs/*.o
${MAKE} clean -C ./libft
fclean: clean
${RM} ${NAME}
${RM} ./libft/libft.a
${RM} ${NAME} $(PRINTF)
${MAKE} fclean -C ./libft
re: fclean all

130
compile_commands.json

@ -0,0 +1,130 @@
[
{
"arguments": [
"/usr/bin/gcc",
"-Werror",
"-Wall",
"-Wextra",
"-c",
"-g",
"-I",
"includes",
"-o",
"srcs/ft_printf.p.o",
"srcs/ft_printf.c"
],
"directory": "/home/nicolas/Development/ft_printf",
"file": "/home/nicolas/Development/ft_printf/srcs/ft_printf.c",
"output": "/home/nicolas/Development/ft_printf/srcs/ft_printf.p.o"
},
{
"arguments": [
"/usr/bin/gcc",
"-Werror",
"-Wall",
"-Wextra",
"-c",
"-g",
"-I",
"includes",
"-o",
"srcs/ft_print_chars.p.o",
"srcs/ft_print_chars.c"
],
"directory": "/home/nicolas/Development/ft_printf",
"file": "/home/nicolas/Development/ft_printf/srcs/ft_print_chars.c",
"output": "/home/nicolas/Development/ft_printf/srcs/ft_print_chars.p.o"
},
{
"arguments": [
"/usr/bin/gcc",
"-Werror",
"-Wall",
"-Wextra",
"-c",
"-g",
"-I",
"includes",
"-o",
"srcs/ft_print_hexs.p.o",
"srcs/ft_print_hexs.c"
],
"directory": "/home/nicolas/Development/ft_printf",
"file": "/home/nicolas/Development/ft_printf/srcs/ft_print_hexs.c",
"output": "/home/nicolas/Development/ft_printf/srcs/ft_print_hexs.p.o"
},
{
"arguments": [
"/usr/bin/gcc",
"-Werror",
"-Wall",
"-Wextra",
"-c",
"-g",
"-I",
"includes",
"-o",
"srcs/ft_print_nbrs.p.o",
"srcs/ft_print_nbrs.c"
],
"directory": "/home/nicolas/Development/ft_printf",
"file": "/home/nicolas/Development/ft_printf/srcs/ft_print_nbrs.c",
"output": "/home/nicolas/Development/ft_printf/srcs/ft_print_nbrs.p.o"
},
{
"arguments": [
"/usr/bin/gcc",
"-Werror",
"-Wall",
"-Wextra",
"-c",
"-g",
"-I",
"includes",
"-o",
"srcs/ft_print_opts.p.o",
"srcs/ft_print_opts.c"
],
"directory": "/home/nicolas/Development/ft_printf",
"file": "/home/nicolas/Development/ft_printf/srcs/ft_print_opts.c",
"output": "/home/nicolas/Development/ft_printf/srcs/ft_print_opts.p.o"
},
{
"arguments": [
"/usr/bin/gcc",
"-Werror",
"-Wall",
"-Wextra",
"-c",
"-g",
"-I",
"includes",
"-o",
"srcs/ft_print_ptrs.p.o",
"srcs/ft_print_ptrs.c"
],
"directory": "/home/nicolas/Development/ft_printf",
"file": "/home/nicolas/Development/ft_printf/srcs/ft_print_ptrs.c",
"output": "/home/nicolas/Development/ft_printf/srcs/ft_print_ptrs.p.o"
},
{
"arguments": [
"/usr/bin/gcc",
"-Werror",
"-Wall",
"-Wextra",
"-c",
"-g",
"-D",
"FPRINTF",
"-I",
"includes",
"-o",
"srcs/ft_printf.f.o",
"srcs/ft_printf.c"
],
"directory": "/home/nicolas/Development/ft_printf",
"file": "/home/nicolas/Development/ft_printf/srcs/ft_printf.c",
"output": "/home/nicolas/Development/ft_printf/srcs/ft_printf.f.o"
}
]

34
ft_printf.h → includes/ft_printf.h

@ -14,10 +14,9 @@
# define MEM_ADD_SIZE 6
# include "./libft/libft.h"
# include "libft.h"
# include <stdarg.h>
typedef struct s_opts {
int len;
int width;
@ -30,19 +29,30 @@ typedef struct s_opts {
int plus : 1;
} t_opts;
void add_string(char *str, size_t size);
t_dlist *get_last(void);
t_dlist *get_first(void);
void clean_list(void);
char *int_opts_transform(int n, char *nbr, t_opts *opts);
char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts);
char *ptr_opts_transform(char *ptr, t_opts *opts);
char *str_opts_transform(char *str, t_opts *opts);
int ft_print_char(int ch, t_opts *opts);
int ft_print_str(char *str, t_opts *opts);
int va_print_char(va_list va_ch, const char *str, t_opts *opts);
int va_print_str(va_list va_str, const char *str, t_opts *opts);
int va_print_ptr(va_list va_ptr, const char *str, t_opts *opts);
int va_print_nbr(va_list va_int, const char *str, t_opts *opts);
int va_print_unsign(va_list va_uint, const char *str, t_opts *opts);
int va_print_x(va_list va_uint, const char *str, t_opts *opts);
int va_print_x_cap(va_list va_uint, const char *str, t_opts *opts);
int va_print_perc(va_list va, const char *str, t_opts *opts);
void ft_print_char(int ch, t_opts *opts);
void ft_print_str(char *str, t_opts *opts);
void va_print_char(va_list va_ch, const char *str, t_opts *opts);
void va_print_str(va_list va_str, const char *str, t_opts *opts);
void va_print_ptr(va_list va_ptr, const char *str, t_opts *opts);
void va_print_nbr(va_list va_int, const char *str, t_opts *opts);
void va_print_unsign(va_list va_uint, const char *str, t_opts *opts);
void va_print_x(va_list va_uint, const char *str, t_opts *opts);
void va_print_x_cap(va_list va_uint, const char *str, t_opts *opts);
void va_print_perc(va_list va, const char *str, t_opts *opts);
void ft_format(const char *str, va_list args);
int ft_printf(const char *str, ...);
int ft_fprintf(int fd, const char *format, ...);
int ft_sprintf(char *ret, const char *format, ...);

110
includes/libft.h

@ -0,0 +1,110 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */
/* Updated: 2022/05/18 16:25:27 by narnaud@stude ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
# include <stddef.h>
# include <stdlib.h>
# include <unistd.h>
typedef struct s_slist
{
void *content;
struct s_slist *next;
} t_slist;
typedef struct s_i_slist
{
int nb;
struct s_i_slist *next;
} t_i_slist;
typedef struct s_dlist
{
void *content;
size_t size;
struct s_dlist *next;
struct s_dlist *previous;
} t_dlist;
int ft_atoi(const char *str);
void ft_bzero(void *s, size_t n);
void *ft_calloc( size_t num, size_t size );
int ft_isalpha(int ch);
int ft_isascii(int ch);
int ft_isdigit(int ch);
int ft_isprint(int ch);
int ft_isalnum(int ch);
int ft_isspace(int ch);
int ft_isnumber(char *str);
void *ft_memchr(const void *b, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memcpy(void *dst, const void *src, size_t n);
void *ft_memmove(void *dst, const void *src, size_t n);
void ft_free_split(char **split);
size_t ft_ilen(int nbr);
size_t ft_ulen(unsigned int nbr);
size_t ft_longbaselen(long nbr, size_t base);
int ft_croissant(int a, int b);
int ft_decroissant(int a, int b);
int ft_max(int a, int b);
int ft_min(int a, int b);
void *ft_memset(void *b, int c, size_t len);
char *ft_strchr(const char *s, int c);
char *ft_strdup(const char *src);
size_t ft_strlcat(char *dst, const char *src, size_t dstsize);
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize);
size_t ft_strlen(const char *s);
size_t ft_strlen_to(const char *str, char ch);
int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strnstr(const char *haystack, const char *needle, size_t len);
char *ft_strrchr(const char *s, int c);
void ft_strrev(char **str, unsigned int neg);
int ft_tolower(int c);
int ft_toupper(int c);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(const char *s1, const char *s2);
char *ft_strjoin_with(char *s1, char *s2, char *inter);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *str, char ch);
char *ft_itoa(int n);
char *ft_itox(unsigned long n, const char *base);
char *ft_utoa(unsigned int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putnstr_fd(size_t n, char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
char *ft_append(char *s1, char *s2);
char *ft_strnew(int n, char c);
t_slist *ft_slst_new(void *content);
void ft_slst_add_front(t_slist **alst, t_slist *new_e);
int ft_slst_size(t_slist *lst);
t_slist *ft_slst_last(t_slist *lst);
void ft_slst_add_back(t_slist **alst, t_slist *new_e);
void ft_slst_delone(t_slist *lst, void (*del)(void *));
void ft_slst_clear(t_slist **lst, void (*del)(void *));
void ft_slst_iter(t_slist *lst, void (*f)(void *));
t_slist *ft_slst_map(t_slist *lst, void *(*f)(void *), void (*del)(void *));
size_t ft_ilst_first(t_i_slist *lst, int (*fct)(int a, int b));
void ft_ilst_free(t_i_slist *list);
int ft_ilst_is_in(int value, t_i_slist lst);
t_dlist *ft_dlst_add(t_dlist *prev, void *content);
t_dlist *ft_dlst_n(t_dlist *lst, size_t n);
char **ft_dlst_to_arr(t_dlist *ptr);

2
libft

@ -1 +1 @@
Subproject commit 91542393af2673f7cba570fc4b868294810f1abc
Subproject commit 824c048555039c45207bd153a2333a1c2e2174a2

55
ft_print_chars.c → srcs/ft_print_chars.c

@ -12,34 +12,38 @@
#include "ft_printf.h"
int ft_print_char(int ch, t_opts *opts)
void ft_print_char(int ch, t_opts *opts)
{
char c;
int ret;
char *ret;
int len;
c = (char)ch;
ret = 1;
if (opts->width < 1)
len = 1;
else
len = opts->width;
ret = ft_calloc(len + 1, sizeof(char));
if (opts->minus)
{
ft_putchar_fd(c, 1);
while (opts->width-- > 1 && ++ret)
ft_putchar_fd(' ', 1);
ret[0] = c;
while (opts->width-- > 1)
ret[opts->width] = ' ';
}
else
{
while (opts->width-- > 1 && ++ret)
ft_putchar_fd(' ', 1);
ft_putchar_fd(c, 1);
ret[len - 1] = c;
while (opts->width-- > 1)
ret[opts->width - 1] = ' ';
}
return (ret);
add_string(ret, len);
}
int ft_print_str(char *str, t_opts *opts)
void ft_print_str(char *str, t_opts *opts)
{
int i;
char *formated;
i = 0;
char *ret;
if (!str)
{
if (!opts->dot || opts->precision >= 6)
@ -47,29 +51,26 @@ int ft_print_str(char *str, t_opts *opts)
else
str = "";
}
formated = str_opts_transform(str, opts);
while (formated[i])
ft_putchar_fd(formated[i++], 1);
free(formated);
return (i);
ret = str_opts_transform(str, opts);
add_string(ret, ft_strlen(ret));
}
int va_print_char(va_list va_ch, const char *str, t_opts *opts)
void va_print_char(va_list va_ch, const char *str, t_opts *opts)
{
(void)*str;
return (ft_print_char(va_arg(va_ch, int), opts));
ft_print_char(va_arg(va_ch, int), opts);
}
int va_print_str(va_list va_str, const char *str, t_opts *opts)
void va_print_str(va_list va_str, const char *str, t_opts *opts)
{
(void)*str;
return (ft_print_str(va_arg(va_str, char *), opts));
ft_print_str(va_arg(va_str, char *), opts);
}
int va_print_perc(va_list va, const char *str, t_opts *opts)
void va_print_perc(va_list va, const char *str, t_opts *opts)
{
(void)*str;
(void)va;
ft_print_char('%', opts);
return (1);
}

20
ft_print_hexs.c → srcs/ft_print_hexs.c

@ -12,12 +12,11 @@
#include "ft_printf.h"
int ft_print_x(unsigned int n, t_opts *opts)
void ft_print_x(unsigned int n, t_opts *opts)
{
char *raw;
char *nbr;
char *tmp;
int ret;
raw = ft_itox((unsigned long int)n, "0123456789abcdef");
if (opts->hash && n != 0)
@ -28,18 +27,16 @@ int ft_print_x(unsigned int n, t_opts *opts)
}
nbr = uint_opts_transform(n, raw, opts);
opts->dot = 0;
ret = ft_print_str(nbr, opts);
ft_print_str(nbr, opts);
free(raw);
free(nbr);
return (ret);
}
int ft_print_x_cap(unsigned int n, t_opts *opts)
void ft_print_x_cap(unsigned int n, t_opts *opts)
{
char *raw;
char *tmp;
char *nbr;
int ret;
raw = ft_itox((unsigned long int)n, "0123456789ABCDEF");
if (opts->hash && n != 0)
@ -50,20 +47,19 @@ int ft_print_x_cap(unsigned int n, t_opts *opts)
}
nbr = uint_opts_transform(n, raw, opts);
opts->dot = 0;
ret = ft_print_str(nbr, opts);
ft_print_str(nbr, opts);
free(raw);
free(nbr);
return (ret);
}
int va_print_x(va_list va_uint, const char *str, t_opts *opts)
void va_print_x(va_list va_uint, const char *str, t_opts *opts)
{
(void)*str;
return (ft_print_x(va_arg(va_uint, unsigned int), opts));
ft_print_x(va_arg(va_uint, unsigned int), opts);
}
int va_print_x_cap(va_list va_uint, const char *str, t_opts *opts)
void va_print_x_cap(va_list va_uint, const char *str, t_opts *opts)
{
(void)*str;
return (ft_print_x_cap(va_arg(va_uint, unsigned int), opts));
ft_print_x_cap(va_arg(va_uint, unsigned int), opts);
}

21
ft_print_nbrs.c → srcs/ft_print_nbrs.c

@ -12,44 +12,39 @@
#include "ft_printf.h"
int ft_print_nbr(int n, const char *str, t_opts *opts)
void ft_print_nbr(int n, const char *str, t_opts *opts)
{
char *nbr;
char *raw;
int ret;
(void)*str;
raw = ft_itoa(n);
nbr = int_opts_transform(n, raw, opts);
opts->dot = 0;
ret = ft_print_str(nbr, opts);
ft_print_str(nbr, opts);
free(raw);
free(nbr);
return (ret);
}
int ft_print_unsign(unsigned int n, const char *str, t_opts *opts)
void ft_print_unsign(unsigned int n, const char *str, t_opts *opts)
{
char *raw;
char *nbr;
int ret;
(void)*str;
raw = ft_utoa(n);
nbr = uint_opts_transform(n, raw, opts);
opts->dot = 0;
ret = ft_print_str(nbr, opts);
ft_print_str(nbr, opts);
free(raw);
free(nbr);
return (ret);
}
int va_print_nbr(va_list va, const char *str, t_opts *opts)
void va_print_nbr(va_list va, const char *str, t_opts *opts)
{
return (ft_print_nbr(va_arg(va, int), str, opts));
ft_print_nbr(va_arg(va, int), str, opts);
}
int va_print_unsign(va_list va, const char *str, t_opts *opts)
void va_print_unsign(va_list va, const char *str, t_opts *opts)
{
return (ft_print_unsign(va_arg(va, unsigned int), str, opts));
ft_print_unsign(va_arg(va, unsigned int), str, opts);
}

0
ft_print_opts.c → srcs/ft_print_opts.c

10
ft_print_ptrs.c → srcs/ft_print_ptrs.c

@ -12,11 +12,10 @@
#include "ft_printf.h"
int ft_print_ptr(void *ptr, t_opts *opts)
void ft_print_ptr(void *ptr, t_opts *opts)
{
char *raw;
char *nbr;
int ret;
if (ptr == NULL)
raw = ft_strdup("(nil)");
@ -27,14 +26,13 @@ int ft_print_ptr(void *ptr, t_opts *opts)
}
nbr = ptr_opts_transform(raw, opts);
opts->dot = 0;
ret = ft_print_str(nbr, opts);
ft_print_str(nbr, opts);
free(raw);
free(nbr);
return (ret);
}
int va_print_ptr(va_list va_ptr, const char *str, t_opts *opts)
void va_print_ptr(va_list va_ptr, const char *str, t_opts *opts)
{
(void)*str;
return (ft_print_ptr(va_arg(va_ptr, void *), opts));
ft_print_ptr(va_arg(va_ptr, void *), opts);
}

45
srcs/ft_print_strings.c

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_strings.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */
/* Updated: 2021/11/17 09:40:16 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static t_dlist *list = NULL;
void add_string(char *str, size_t size)
{
list = ft_dlst_add(list, str);
list->size = size;
}
t_dlist *get_last(void)
{
return list;
}
t_dlist *get_first(void)
{
t_dlist *tmp = list;
while (tmp->previous)
tmp = tmp->previous;
return tmp;
}
void clean_list(void)
{
while (list)
{
free(list->content);
free(list);
list = list->previous;
}
list = NULL;
}

94
ft_printf.c → srcs/ft_printf.c

@ -49,11 +49,11 @@ const char *handle_options(va_list args, const char *str, t_opts *opts)
return (++str);
}
int handle_functions(va_list args, const char *str, t_opts *opts)
void handle_functions(va_list args, const char *str, t_opts *opts)
{
size_t n;
const char *g_types = {"cspdiuxX%"};
static int (*g_printf_fcts[9])(va_list arg, const char *str,
static void(*g_printf_fcts[9])(va_list arg, const char *str,
t_opts *opts) = {va_print_char, va_print_str, va_print_ptr,
va_print_nbr, va_print_nbr, va_print_unsign, va_print_x,
va_print_x_cap, va_print_perc};
@ -62,7 +62,7 @@ int handle_functions(va_list args, const char *str, t_opts *opts)
while (g_types[n] && g_types[n] != *str && ++n)
;
if (!g_types[n])
return (0);
return ;
if (opts->minus)
opts->zero = 0;
if (opts->precision > 0)
@ -74,30 +74,88 @@ int handle_functions(va_list args, const char *str, t_opts *opts)
}
if (opts->precision < 0)
opts->dot = 0;
return ((g_printf_fcts[n])(args, str, opts));
(g_printf_fcts[n])(args, str, opts);
}
int ft_printf(const char *str, ...)
void ft_format(const char *format, va_list args)
{
va_list args;
size_t i;
t_opts opts;
i = 0;
va_start(args, str);
while (*str)
while (*format)
{
if (*str == '%' && str++)
if (*format == '%' && format++)
{
opts = (t_opts){0};
while (*str && !ft_strchr("cspdiuxX%", *str))
str = handle_options(args, str, &opts);
i += handle_functions(args, str, &opts);
while (*format && !ft_strchr("cspdiuxX%", *format))
format = handle_options(args, format, &opts);
handle_functions(args, format, &opts);
}
else if (++i)
ft_putchar_fd(*str, 1);
str++;
else
add_string(ft_strnew(1, *format), 1);
format++;
}
}
int ft_printf(const char *format, ...)
{
int ret;
t_dlist *strings;
va_list args;
va_start(args, format);
ft_format(format, args);
ret = 0;
strings = get_first();
while (strings)
{
ret += strings->size;
ft_putnstr_fd(strings->size, strings->content, 1);
strings = strings->next;
}
clean_list();
va_end(args);
return (ret);
}
int ft_fprintf(int fd, const char *format, ...)
{
int ret;
t_dlist *strings;
va_list args;
va_start(args, format);
ft_format(format, args);
ret = 0;
strings = get_first();
while (strings)
{
ret += strings->size;
ft_putnstr_fd(strings->size, strings->content, fd);
strings = strings->next;
}
clean_list();
va_end(args);
return (i);
return (ret);
}
int ft_sprintf(char *str, const char *format, ...)
{
int ret;
t_dlist *strings;
va_list args;
va_start(args, format);
ft_format(format, args);
ret = 0;
strings = get_first();
while (strings)
{
ret += strings->size;
ft_strlcat(str, strings->content, ret);
strings = strings->next;
}
clean_list();
va_end(args);
return (ret);
}

1
tester

@ -0,0 +1 @@
Subproject commit a053a3500c9124a5c64c95cd8ff9e78a783932c4
Loading…
Cancel
Save