split ft_printf

This commit is contained in:
whaffman 2024-10-27 16:57:05 +01:00
parent e30f6f7fa8
commit 8a15925a5b
14 changed files with 417 additions and 297 deletions

View File

@ -1,276 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_printf.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/11 14:49:07 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/15 12:43:16 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include "libft.h"
#include "libft/libft.h"
#include "ft_printf.h"
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(unsigned long nbr, char *base, char *result)
{
unsigned int b;
b = ft_isbase(base);
if (b < 2)
return ;
if (nbr < b)
ft_write_str(base[nbr], result);
else if (nbr >= b)
{
ft_putnbr_base(nbr / b, base, result);
ft_putnbr_base(nbr % b, base, result);
}
}
void ft_putnbr_signed(long nbr, char *base, char *result)
{
if (nbr < 0)
{
*result = '-';
ft_putnbr_base((unsigned long)(-nbr), base, result);
}
else
ft_putnbr_base((unsigned long) nbr, 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 *);
if (!str)
{
write(1, "(null)", 6);
return (6);
}
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_signed(n, "0123456789", str);
len = ft_strlen(str);
wrote = write(1, str, len);
free(str);
return (wrote);
}
int print_unumber(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, unsigned 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 print_pointer(va_list args)
{
long n;
size_t len;
char *str;
int wrote;
wrote = 0;
n = va_arg(args, unsigned long);
if (!n)
{
write(1, "(nil)", 5);
return (5);
}
str = ft_calloc(sizeof(char), 20);
if (!str)
return (wrote);
ft_strlcpy(str, "0x", 3);
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("di", **format))
wrote = print_number(args);
else if (ft_strchr("u", **format))
wrote = print_unumber(args);
else if (ft_strchr("p", **format))
wrote = print_pointer(args);
else if (ft_strchr("x", **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)++;
wrote = 0;
while (**format && !ft_strchr(conversions, **format))
(*format)++;
if (**format == '%')
wrote = write(1, *format, 1);
else
wrote = parse_conversion(format, args);
return (wrote);
}
int ft_printf(const char *format, ...)
{
int wrote;
int result;
va_list args;
result = 0;
va_start(args, format);
while (*format && result >= 0)
{
if (*format == '%')
wrote = parse_placeholder(&format, args);
else
wrote = write(1, format, 1);
if (wrote < 0)
{
result = wrote;
break;
}
result += wrote;
format++;
}
va_end(args);
return (result);
}
/*
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);
ft_printf(" %% %% %%\n");
ft_printf(" %% %% %%\n");
ft_printf(" %% %% %%\n");
return (0);
}
*/

33
ft_printf/ft_isbase.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_isbase.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:18:39 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:19:43 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
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);
}

42
ft_printf/ft_printf.c Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_printf.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/11 14:49:07 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:37:46 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include "libft.h"
#include "ft_printf.h"
int ft_printf(const char *format, ...)
{
int wrote;
int result;
va_list args;
result = 0;
va_start(args, format);
while (*format && result >= 0)
{
if (*format == '%')
wrote = parse_placeholder(&format, args);
else
wrote = write(1, format, 1);
if (wrote < 0)
{
result = wrote;
break ;
}
result += wrote;
format++;
}
va_end(args);
return (result);
}

View File

@ -1,32 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_printf.h :+: / :+::+: :+: */
/* ft_putnbr_base.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* 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 ### ### ### ### / \ */
/* Created: 2024/10/27 16:21:37 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:22:22 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
#include "ft_printf.h"
# include <stdarg.h>
void ft_putnbr_base(unsigned long nbr, char *base, char *result)
{
unsigned int b;
int ft_printf(const char *format, ...);
int ft_isbase(char *str);
void ft_write_str(char c, char *str);
void ft_putnbr_base(unsigned long nbr, char *base, char *result);
void ft_putnbr_signed(long nbr, char *base, char *result);
int print_char(va_list args);
int print_string(va_list args);
int print_number(va_list args);
int print_unumber(va_list args);
int print_hex(va_list args, int uppercase);
int print_pointer(va_list args);
int parse_conversion(const char **format, va_list args);
int parse_placeholder(const char **format, va_list args);
#endif
b = ft_isbase(base);
if (b < 2)
return ;
if (nbr < b)
ft_write_str(base[nbr], result);
else if (nbr >= b)
{
ft_putnbr_base(nbr / b, base, result);
ft_putnbr_base(nbr % b, base, result);
}
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_putnbr_signed.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:22:39 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:23:02 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putnbr_signed(long nbr, char *base, char *result)
{
if (nbr < 0)
{
*result = '-';
ft_putnbr_base((unsigned long)(-nbr), base, result);
}
else
ft_putnbr_base((unsigned long) nbr, base, result);
}

24
ft_printf/ft_write_str.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_write_str.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:19:20 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:19:53 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
void ft_write_str(char c, char *str)
{
int i;
i = 0;
while (str[i])
{
i++;
}
str[i++] = c;
str[i] = '\0';
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* parse_conversion.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:34:43 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:34:49 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include "libft.h"
#include "ft_printf.h"
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("di", **format))
wrote = print_number(args);
else if (ft_strchr("u", **format))
wrote = print_unumber(args);
else if (ft_strchr("p", **format))
wrote = print_pointer(args);
else if (ft_strchr("x", **format))
wrote = print_hex(args, 0);
else if (ft_strchr("X", **format))
wrote = print_hex(args, 1);
return (wrote);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* parse_placeholder.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:34:28 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:35:09 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include "libft.h"
#include "ft_printf.h"
int parse_placeholder(const char **format, va_list args)
{
const char conversions[] = "cspdiuxX%";
int wrote;
(*format)++;
wrote = 0;
while (**format && !ft_strchr(conversions, **format))
(*format)++;
if (**format == '%')
wrote = write(1, *format, 1);
else
wrote = parse_conversion(format, args);
return (wrote);
}

22
ft_printf/print_char.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_char.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:28:10 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:28:12 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
int print_char(va_list args)
{
char c;
c = va_arg(args, int);
return (write(1, &c, 1));
}

39
ft_printf/print_hex.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_hex.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:29:01 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:30:00 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include "libft.h"
#include "ft_printf.h"
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);
}

36
ft_printf/print_number.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_number.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:31:23 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:31:37 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include "libft.h"
#include "ft_printf.h"
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_signed(n, "0123456789", str);
len = ft_strlen(str);
wrote = write(1, str, len);
free(str);
return (wrote);
}

42
ft_printf/print_pointer.c Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_pointer.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:26:58 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:31:19 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include "../libft/libft.h"
#include "ft_printf.h"
int print_pointer(va_list args)
{
long n;
size_t len;
char *str;
int wrote;
wrote = 0;
n = va_arg(args, unsigned long);
if (!n)
{
write(1, "(nil)", 5);
return (5);
}
str = ft_calloc(sizeof(char), 20);
if (!str)
return (wrote);
ft_strlcpy(str, "0x", 3);
ft_putnbr_base(n, "0123456789abcdef", str);
len = ft_strlen(str);
wrote = write(1, str, len);
free(str);
return (wrote);
}

31
ft_printf/print_string.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_string.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:31:48 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:32:23 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include "libft.h"
#include "ft_printf.h"
int print_string(va_list args)
{
char *str;
size_t len;
str = va_arg(args, char *);
if (!str)
{
write(1, "(null)", 6);
return (6);
}
len = ft_strlen(str);
return (write(1, str, len));
}

36
ft_printf/print_unumber.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_unumber.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/27 16:32:36 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/27 16:32:49 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include "libft.h"
#include "ft_printf.h"
int print_unumber(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, unsigned int);
ft_putnbr_base(n, "0123456789", str);
len = ft_strlen(str);
wrote = write(1, str, len);
free(str);
return (wrote);
}