Init commit

This commit is contained in:
Tanguy MAZE 2018-05-05 19:46:25 +02:00
commit 46ae7b6694
7 changed files with 391 additions and 0 deletions

40
Makefile Normal file
View File

@ -0,0 +1,40 @@
#******************************************************************************#
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/05/05 19:22:36 by tmaze #+# #+# #
# Updated: 2018/05/05 19:39:15 by tmaze ### ########.fr #
# #
#******************************************************************************#
CC = gcc
CCFLAGS = -Wall -Werror -Wextra
CCSTD =
NAME = libftprintf
SRCS = convert_d_i.c ft_printf.c main.c
OBJS = $(SRCS:.c=.o)
INCLS = -I.
LIBS = -L. -lft
.PHONY = all clean fclean re
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(CCFLAGS) $(CCSTD) $(INCLS) $^ -o $@ $(LIBS)
%.o: %.c libftprintf.h
$(CC) $(CCFLAGS) $(CCSTD) $(INCLS) -c $< -o $@
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all

84
convert_d_i.c Normal file
View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* convert_d_i.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/05 19:17:53 by tmaze #+# #+# */
/* Updated: 2018/05/05 19:40:49 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
char *apply_precision_d(char *s, int prec)
{
char *ret;
char *tmp;
int nb_fill;
nb_fill = prec - ft_strlen(s);
if ((ret = ft_strsub(s, 0, prec)) == NULL)
return (NULL);
ft_strdel(&s);
if (nb_fill > 0 && (s = ft_strnew(nb_fill)) != NULL)
{
ft_memset(s, '0', nb_fill);
tmp = ft_strjoin(s, ret);
ft_strdel(&s);
ft_strdel(&ret);
ret = tmp;
tmp = NULL;
}
ft_strdel(&tmp);
return (ret);
}
char *apply_width_d(char *s, int width, char align)
{
char *add;
char *tmp;
if ((add = ft_strnew(width - ft_strlen(s))) == NULL)
return (NULL);
ft_memset(add, ' ', width - ft_strlen(s));
if (align == '-')
{
tmp = add;
add = s;
s = tmp;
}
tmp = ft_strjoin(add, s);
ft_strdel(&add);
ft_strdel(&s);
return (tmp);
}
char *convert_d_i(char *flags, int *dim, int nb)
{
char *ret;
char *tmp;
char *atoi;
int atoi_len;
atoi = ft_itoa(ft_abs(nb));
atoi_len = ft_strlen(atoi);
if (dim[1] > atoi_len)
tmp = apply_precision_d(atoi, dim[1]);
else
tmp = atoi;
atoi = ft_strnew((((flags[1] == '+' || flags[1] == ' ') && nb >= 0) || nb < 0) ? 1 : 0);
if (flags[1] == '+' && nb >= 0)
atoi[0] = '+';
else if (flags[1] == ' ' && nb >= 0)
atoi[0] = ' ';
else if (nb <= 0)
atoi[0] = '-';
ret = ft_strjoin(atoi, tmp);
ft_strdel(&atoi);
ft_strdel(&tmp);
if ((size_t)dim[0] > ft_strlen(ret))
ret = apply_width_d(ret, dim[0], flags[0]);
return (ret);
}

81
ft_printf.c Normal file
View File

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/05 19:36:58 by tmaze #+# #+# */
/* Updated: 2018/05/05 19:38:02 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
int is_flag(char c)
{
return (c == '#' || c == '0' || c == '-' || c == '+' || c == ' ');
}
int ft_printf(const char *format, ...)
{
va_list ap;
int i;
char *ret;
char flags[4];
int dim[2];
i = 0;
ret = ft_strnew(0);
va_start(ap, format);
while (format[i])
{
if (format[i] == '%')
{
dim[0] = 0;
while (dim[0] < 4)
flags[dim[0]++] = 0;
dim[0] = 0;
dim[1] = 0;
i++;
while (is_flag(format[i]))
{
if (format[i] == '-')
{
flags[0] = '-';
flags[2] = 0;
}
else if (format[i] == '+')
flags[1] = '+';
else if (format[i] == ' ' && flags[1] == 0)
flags[1] = ' ';
else if (format[i] == '0' && flags[0] == 0)
flags[2] = '0';
else
flags[3] = '#';
i++;
}
dim[0] = ft_atoi(&format[i]);
while (ft_isdigit(format[i]))
i++;
if (format[i] == '.')
{
i++;
dim[1] = ft_atoi(&format[i]);
}
while (ft_isdigit(format[i]))
i++;
if (format[i] == '%')
write(1, "%", 1);
else if (format[i] == 'd' || format[i] == 'i')
ret = convert_d_i(flags, dim, va_arg(ap, int));
write(1, ret, ft_strlen(ret));
}
else
write(1, &format[i], 1);
i++;
}
ft_strdel(&ret);
va_end(ap);
return (0);
}

BIN
libft.a Normal file

Binary file not shown.

133
libft.h Normal file
View File

@ -0,0 +1,133 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */
/* Updated: 2018/05/05 16:49:16 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <string.h>
# include <stdlib.h>
# include <unistd.h>
/*
** définition type t_list
*/
typedef struct s_list
{
void *content;
size_t content_size;
struct s_list *next;
} t_list;
/*
** fonctions obligatoires
*/
void *ft_memset(void *b, int c, size_t len);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, const void *src,\
size_t n);
void *ft_memccpy(void *dest, const void *src,\
int c, size_t n);
void *ft_memmove(void *dest, const void *src, size_t len);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
size_t ft_strlen(const char *s);
char *ft_strdup(const char *s);
char *ft_strcpy(char *dest, const char *src);
char *ft_strncpy(char *dest, const char *src, size_t n);
char *ft_strcat(char *dest, const char *src);
char *ft_strncat(char *dest, const char *src,\
size_t n);
size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
char *ft_strstr(const char *haystack, const char *needle);
char *ft_strnstr(const char *haystack, const char *needle,\
size_t len);
int ft_strcmp(const char *s1, const char *s2);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_atoi(const char *str);
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
int ft_toupper(int c);
int ft_tolower(int c);
/*
** fonctions supplémentaires
*/
void *ft_memalloc(size_t size);
void ft_memdel(void **ap);
char *ft_strnew(size_t size);
void ft_strdel(char **as);
void ft_strclr(char *s);
void ft_striter(char *s, void (*f)(char *));
void ft_striteri(char *s, void (*f)(unsigned int, char *));
char *ft_strmap(char const *s, char (*f)(char));
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
int ft_strequ(char const *s1, char const *s2);
int ft_strnequ(char const *s1, char const *s2, size_t n);
char *ft_strsub(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s);
char **ft_strsplit(char const *s, char c);
char *ft_itoa(int n);
void ft_putchar(char c);
void ft_putstr(char const *s);
void ft_putendl(char const *s);
void ft_putnbr(int n);
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);
/*
** fonctions bonus
*/
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));
void ft_lstadd(t_list **alst, t_list *new);
void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
/*
** fonctions additionneles
*/
int ft_isupper(int c);
int ft_islower(int c);
int ft_str_is_alpha(char *str);
int ft_str_is_lowercase(char *str);
int ft_str_is_numeric(char *str);
int ft_str_is_printable(char *str);
int ft_str_is_uppercase(char *str);
char *ft_strlowcase(char *s);
char *ft_strupcase(char *s);
char *ft_strcapitalize(char *str);
size_t ft_strlcpy(char *dst, const char *src,\
size_t size);
size_t ft_lstsize(t_list *lst);
t_list *ft_lstgetat(t_list *lst, size_t ind);
t_list *ft_lstgetlast(t_list *lst);
void ft_sort_params(int ac, char **av);
void ft_print_words_tables(char **tab);
t_list *ft_lstaddend(t_list **alst, t_list *new);
char *ft_strndup(const char *s1, size_t n);
unsigned int ft_abs(int nb);
#endif

25
libftprintf.h Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libftprintf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/05 19:19:33 by tmaze #+# #+# */
/* Updated: 2018/05/05 19:40:06 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFTPRINTF_H
# define LIBFTPRINTF_H
# include "libft.h"
# include <stdlib.h>
# include <stdarg.h>
# include <unistd.h>
# include <wchar.h>
int ft_printf(const char *format, ...);
char *convert_d_i(char *flags, int *dim, int nb);
#endif

28
main.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/01 15:01:57 by tmaze #+# #+# */
/* Updated: 2018/05/05 19:37:56 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "libftprintf.h"
char happy[] = { 0xe2, 0x98, 0xba }; /* U+263A */
wchar_t Whappy[] = { 0xe2, 0x98, 0xba }; /* U+263A */
int main(void)
{
write(1, happy, 3);
write(1, "\n", 1);
printf("plop %++0 #%plap \"%+-20.10i\"\n", 15);
// printf("%lu %#% %S \"% -10.8d\" \"%-10.8s\"\n", sizeof(wchar_t), Whappy, -1475, "Je suis");
ft_printf("plop %++0 #%plap \"%+-20.10i\"\n", 15);
return (0);
}