first working version
This commit is contained in:
parent
146d6b4988
commit
fcb984218b
12
Makefile
12
Makefile
@ -20,19 +20,19 @@ CC = gcc
|
|||||||
|
|
||||||
CFLAGS = -Wall -Wextra -Werror
|
CFLAGS = -Wall -Wextra -Werror
|
||||||
|
|
||||||
AR = AR
|
AR = ar
|
||||||
|
|
||||||
all: $(NAME) libft.a
|
all: $(NAME)
|
||||||
|
|
||||||
libft.a:
|
lib/libft.a:
|
||||||
$(MAKE) -C libft
|
$(MAKE) -C libft
|
||||||
$(MAKE) -C libft clean
|
$(MAKE) -C libft clean
|
||||||
mkdir lib
|
mkdir -p lib
|
||||||
mkdir inc
|
mkdir -p inc
|
||||||
cp libft/libft.h inc/libft.h
|
cp libft/libft.h inc/libft.h
|
||||||
mv libft/libft.a lib/libft.a
|
mv libft/libft.a lib/libft.a
|
||||||
|
|
||||||
$(NAME): $(OBJECTS)
|
$(NAME): lib/libft.a $(OBJECTS)
|
||||||
$(AR) -r $@ $?
|
$(AR) -r $@ $?
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
|
|||||||
194
ft_printf.c
194
ft_printf.c
@ -11,9 +11,201 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
int ft_printf(const char *str, ...)
|
|
||||||
|
int ft_isbase(char *str)
|
||||||
{
|
{
|
||||||
|
int i;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
while (str[n])
|
||||||
|
{
|
||||||
|
i = 1;
|
||||||
|
if (str[n] == '-' || str[n] == '+')
|
||||||
|
return (0);
|
||||||
|
while (str[n + i])
|
||||||
|
{
|
||||||
|
if (str[n] == str[n + i])
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_write_str(char c, char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
str[i++] = c;
|
||||||
|
str[i] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putnbr_base(long nbr, char *base, char *result)
|
||||||
|
{
|
||||||
|
int b;
|
||||||
|
|
||||||
|
b = ft_isbase(base);
|
||||||
|
if (b < 2)
|
||||||
|
return ;
|
||||||
|
if (nbr >= 0 && nbr < b)
|
||||||
|
{
|
||||||
|
ft_write_str(base[nbr], result);
|
||||||
|
}
|
||||||
|
else if (nbr < 0)
|
||||||
|
{
|
||||||
|
ft_write_str('-', result);
|
||||||
|
if (nbr > -b)
|
||||||
|
ft_putnbr_base(-nbr, base, result);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_putnbr_base(nbr / (-b), base, result);
|
||||||
|
ft_putnbr_base(-(nbr % b), base, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (nbr >= b)
|
||||||
|
{
|
||||||
|
ft_putnbr_base(nbr / b, base, result);
|
||||||
|
ft_putnbr_base(nbr % b, base, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int print_char(va_list args)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
|
||||||
|
c = va_arg(args, int);
|
||||||
|
return (write(1, &c, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int print_string(va_list args)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
str = va_arg(args, char *);
|
||||||
|
len = ft_strlen(str);
|
||||||
|
return (write(1, str, len));
|
||||||
|
}
|
||||||
|
|
||||||
|
int print_number(va_list args)
|
||||||
|
{
|
||||||
|
long n;
|
||||||
|
size_t len;
|
||||||
|
char *str;
|
||||||
|
int wrote;
|
||||||
|
|
||||||
|
wrote = 0;
|
||||||
|
str = ft_calloc(sizeof(char), 12);
|
||||||
|
if (!str)
|
||||||
|
return (wrote);
|
||||||
|
n = va_arg(args, int);
|
||||||
|
ft_putnbr_base(n, "0123456789", str);
|
||||||
|
len = ft_strlen(str);
|
||||||
|
wrote = write(1, str, len);
|
||||||
|
free(str);
|
||||||
|
return (wrote);
|
||||||
|
}
|
||||||
|
|
||||||
|
int print_hex(va_list args, int uppercase)
|
||||||
|
{
|
||||||
|
long n;
|
||||||
|
size_t len;
|
||||||
|
char *str;
|
||||||
|
int wrote;
|
||||||
|
|
||||||
|
wrote = 0;
|
||||||
|
str = ft_calloc(sizeof(char), 20);
|
||||||
|
if (!str)
|
||||||
|
return (wrote);
|
||||||
|
n = va_arg(args, unsigned int);
|
||||||
|
if (uppercase)
|
||||||
|
ft_putnbr_base(n, "0123456789ABCDEF", str);
|
||||||
|
else
|
||||||
|
ft_putnbr_base(n, "0123456789abcdef", str);
|
||||||
|
len = ft_strlen(str);
|
||||||
|
wrote = write(1, str, len);
|
||||||
|
free(str);
|
||||||
|
return (wrote);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_conversion(const char **format, va_list args)
|
||||||
|
{
|
||||||
|
int wrote;
|
||||||
|
|
||||||
|
wrote = 0;
|
||||||
|
if (**format == 'c')
|
||||||
|
wrote = print_char(args);
|
||||||
|
else if (**format == 's')
|
||||||
|
wrote = print_string(args);
|
||||||
|
else if (ft_strchr("diu", **format))
|
||||||
|
wrote = print_number(args);
|
||||||
|
else if (ft_strchr("px", **format))
|
||||||
|
wrote = print_hex(args, 0);
|
||||||
|
else if (ft_strchr("X", **format))
|
||||||
|
wrote = print_hex(args, 1);
|
||||||
|
return (wrote);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_placeholder(const char **format, va_list args)
|
||||||
|
{
|
||||||
|
const char conversions[] = "cspdiuxX%";
|
||||||
|
int wrote;
|
||||||
|
|
||||||
|
(*format)++;
|
||||||
|
(void) args;
|
||||||
|
while (**format && !ft_strchr(conversions, **format))
|
||||||
|
(*format)++;
|
||||||
|
if (**format == '%')
|
||||||
|
write(1, *format, 1);
|
||||||
|
else
|
||||||
|
wrote = parse_conversion(format, args);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_printf(const char *format, ...)
|
||||||
|
{
|
||||||
|
int wrote;
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
wrote = 0;
|
||||||
|
va_start(args, format);
|
||||||
|
while (*format)
|
||||||
|
{
|
||||||
|
if (*format == '%')
|
||||||
|
wrote += parse_placeholder(&format, args);
|
||||||
|
else
|
||||||
|
wrote += write(1, format, 1);
|
||||||
|
format++;
|
||||||
|
}
|
||||||
|
va_end(args);
|
||||||
|
return (wrote);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
const char str[] = "HOIe";
|
||||||
|
|
||||||
|
ft_printf("test:\n");
|
||||||
|
ft_printf("%%%% = %%\n--\n");
|
||||||
|
ft_printf("%%c = %c\n--\n", 't');
|
||||||
|
ft_printf("%%s = %s\n--\n", str);
|
||||||
|
ft_printf("%%d = %d\n--\n", 255);
|
||||||
|
ft_printf("%%i = %i\n--\n", 255);
|
||||||
|
ft_printf("%%u = %u\n--\n", 255);
|
||||||
|
ft_printf("%%p = %p\n--\n", str);
|
||||||
|
ft_printf("%%x = %x\n--\n", 255);
|
||||||
|
ft_printf("%%X = %X\n--\n", 255);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
18
ft_printf.h
Normal file
18
ft_printf.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: o_ :::::: ::: */
|
||||||
|
/* ft_printf.h :+: / :+::+: :+: */
|
||||||
|
/* +:+ > +:++:+ +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||||
|
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||||
|
/* Created: 2024/10/15 13:04:31 by whaffman #+#+# #+#+# #+# #+# | */
|
||||||
|
/* Updated: 2024/10/15 13:07:34 by whaffman ### ### ### ### / \ */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_PRINTF_H
|
||||||
|
# define FT_PRINTF_H
|
||||||
|
|
||||||
|
int ft_printf(const char *format, ...);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user