Compare commits
8 commits
8ad0f35891
...
a7b7cda496
| Author | SHA1 | Date | |
|---|---|---|---|
| a7b7cda496 | |||
| 8c933384bb | |||
| 6910e9e96f | |||
| 5276d36f0c | |||
| 2a9b042122 | |||
| 9bf197acbd | |||
| 2bee25020c | |||
| 38d24f4915 |
8 changed files with 139 additions and 0 deletions
15
hdrs/libft.h
15
hdrs/libft.h
|
|
@ -3,7 +3,15 @@
|
|||
|
||||
# include <stdlib.h>
|
||||
|
||||
typedef struct s_string
|
||||
{
|
||||
size_t len;
|
||||
char *data;
|
||||
} t_string;
|
||||
|
||||
int ft_strcmp(const char *s1, const char *s2);
|
||||
size_t ft_strlen(const char *s);
|
||||
int ft_putstr(const char *s);
|
||||
int ft_islower(int c);
|
||||
int ft_isupper(int c);
|
||||
int ft_tolower(int c);
|
||||
|
|
@ -18,5 +26,12 @@ void *ft_memcpy(void *dst, const void *src, size_t n);
|
|||
void *ft_memcpy32(void *dst, const void *src, size_t n);
|
||||
void *ft_memcpy64(void *dst, const void *src, size_t n);
|
||||
int ft_atoi(const char *str);
|
||||
int ft_putchar(char c);
|
||||
int ft_putnbr(int n);
|
||||
int ft_putunbr(unsigned int n);
|
||||
int ft_puthex(unsigned int n, int uppercase);
|
||||
void ft_putptr(void *ptr);
|
||||
int ft_printf(const char *format, ...);
|
||||
int ft_putaddr(unsigned long n);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
45
srcs/ft_printf.c
Normal file
45
srcs/ft_printf.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include "libft.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static int ft_print_format(const char *fmt, va_list args)
|
||||
{
|
||||
if (*fmt == 'c')
|
||||
return (ft_putchar(va_arg(args, int)));
|
||||
else if (*fmt == 's')
|
||||
return (ft_putstr(va_arg(args, char *)));
|
||||
else if (*fmt == 'p')
|
||||
return (ft_putaddr((uintptr_t)va_arg(args, void *)));
|
||||
else if (*fmt == 'd' || *fmt == 'i')
|
||||
return (ft_putnbr(va_arg(args, int)));
|
||||
else if (*fmt == 'u')
|
||||
return (ft_putunbr(va_arg(args, unsigned int)));
|
||||
else if (*fmt == 'x')
|
||||
return (ft_puthex(va_arg(args, unsigned int), 0));
|
||||
else if (*fmt == 'X')
|
||||
return (ft_puthex(va_arg(args, unsigned int), 1));
|
||||
else if (*fmt == '%')
|
||||
return (ft_putchar('%'));
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_printf(const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
int i = 0;
|
||||
int count = 0;
|
||||
|
||||
va_start(args, format);
|
||||
while (format[i])
|
||||
{
|
||||
if (format[i] == '%' && format[i + 1])
|
||||
{
|
||||
count += ft_print_format(&format[++i], args);
|
||||
}
|
||||
else
|
||||
count += ft_putchar(format[i]);
|
||||
i++;
|
||||
}
|
||||
va_end(args);
|
||||
return (count);
|
||||
}
|
||||
12
srcs/ft_putaddr.c
Normal file
12
srcs/ft_putaddr.c
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_putaddr(unsigned long n)
|
||||
{
|
||||
int count = 0;
|
||||
char *digits = "0123456789abcdef";
|
||||
|
||||
if (n >= 16)
|
||||
count += ft_putaddr(n / 16);
|
||||
count += ft_putchar(digits[n % 16]);
|
||||
return count;
|
||||
}
|
||||
8
srcs/ft_putchar.c
Normal file
8
srcs/ft_putchar.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include "libft.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
return 1;
|
||||
}
|
||||
12
srcs/ft_puthex.c
Normal file
12
srcs/ft_puthex.c
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include "libft.h" // for ft_putchar
|
||||
|
||||
int ft_puthex(unsigned int n, int uppercase)
|
||||
{
|
||||
int count = 0;
|
||||
char *digits = uppercase ? "0123456789ABCDEF" : "0123456789abcdef";
|
||||
|
||||
if (n >= 16)
|
||||
count += ft_puthex(n / 16, uppercase);
|
||||
count += ft_putchar(digits[n % 16]);
|
||||
return count;
|
||||
}
|
||||
21
srcs/ft_putnbr.c
Normal file
21
srcs/ft_putnbr.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include "libft.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_putnbr(int n)
|
||||
{
|
||||
int count = 0;
|
||||
char c;
|
||||
|
||||
if (n == -2147483648)
|
||||
return write(1, "-2147483648", 11);
|
||||
if (n < 0)
|
||||
{
|
||||
count += ft_putchar('-');
|
||||
n = -n;
|
||||
}
|
||||
if (n >= 10)
|
||||
count += ft_putnbr(n / 10);
|
||||
c = '0' + (n % 10);
|
||||
count += ft_putchar(c);
|
||||
return count;
|
||||
}
|
||||
12
srcs/ft_putstr.c
Normal file
12
srcs/ft_putstr.c
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include "libft.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_putstr(const char *s)
|
||||
{
|
||||
int len = 0;
|
||||
if (!s)
|
||||
return write(1, "(null)", 6);
|
||||
while (s[len])
|
||||
write(1, &s[len++], 1);
|
||||
return len;
|
||||
}
|
||||
14
srcs/ft_putunbr.c
Normal file
14
srcs/ft_putunbr.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include "libft.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_putunbr(unsigned int n)
|
||||
{
|
||||
int count = 0;
|
||||
char c;
|
||||
|
||||
if (n >= 10)
|
||||
count += ft_putunbr(n / 10);
|
||||
c = '0' + (n % 10);
|
||||
count += ft_putchar(c);
|
||||
return count;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue