-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fatih Cil
committed
Feb 21, 2022
1 parent
63dd287
commit b3dc314
Showing
8 changed files
with
259 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,10 @@ | |
# By: fcil <[email protected]> +#+ +:+ +#+ # | ||
# +#+#+#+#+#+ +#+ # | ||
# Created: 2022/02/17 12:46:43 by fcil #+# #+# # | ||
# Updated: 2022/02/17 13:02:43 by fcil ### ########.fr # | ||
# Updated: 2022/02/21 10:29:37 by fcil ### ########.fr # | ||
# # | ||
# **************************************************************************** # | ||
|
||
#Variables | ||
|
||
NAME = libftprintf.a | ||
INCLUDE = include | ||
LIBFT = libft | ||
|
@@ -20,11 +18,6 @@ CFLAGS = -Wall -Werror -Wextra | |
RM = rm -f | ||
AR = ar rcs | ||
|
||
#Sources | ||
|
||
#SRC_FILES = ft_printf ft_printf_utils ft_print_ptr ft_print_unsigned ft_print_hex | ||
|
||
|
||
SRC = $(wildcard *.c) | ||
OBJ = $(SRC:.c=.o) | ||
|
||
|
@@ -49,7 +42,4 @@ fclean: clean | |
|
||
re: fclean all | ||
|
||
norm: | ||
@norminette $(SRC) $(INCLUDE) $(LIBFT) | grep -v Norme -B1 || true | ||
|
||
.PHONY: all clean fclean re norm | ||
.PHONY: all clean fclean re |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,206 +6,55 @@ | |
/* By: fcil <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/02/16 13:06:06 by fcil #+# #+# */ | ||
/* Updated: 2022/02/17 15:19:31 by fcil ### ########.fr */ | ||
/* Updated: 2022/02/21 10:23:48 by fcil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include <stdio.h> | ||
#include "ft_printf.h" | ||
//printf hex | ||
int ft_hex_len(unsigned int num) | ||
{ | ||
int len; | ||
|
||
len = 0; | ||
while (num != 0) | ||
{ | ||
len++; | ||
num = num / 16; | ||
} | ||
return (len); | ||
} | ||
|
||
void ft_put_hex(unsigned int num, const char format) | ||
{ | ||
if (num >= 16) | ||
{ | ||
ft_put_hex(num / 16, format); | ||
ft_put_hex(num % 16, format); | ||
} | ||
else | ||
{ | ||
if (num <= 9) | ||
ft_putchar_fd((num + '0'), 1); | ||
else | ||
{ | ||
if (format == 'x') | ||
ft_putchar_fd((num - 10 + 'a'), 1); | ||
if (format == 'X') | ||
ft_putchar_fd((num - 10 + 'A'), 1); | ||
} | ||
} | ||
} | ||
|
||
int ft_print_hex(unsigned int num, const char format) | ||
int ft_printchar(int c) | ||
{ | ||
if (num == 0) | ||
return (write(1, "0", 1)); | ||
else | ||
ft_put_hex(num, format); | ||
return (ft_hex_len(num)); | ||
write(1, &c, 1); | ||
return (1); | ||
} | ||
|
||
// printf ptr | ||
|
||
int ft_ptr_len(uintptr_t num) | ||
int checkid(va_list arg, const char format) | ||
{ | ||
int len; | ||
|
||
len = 0; | ||
while (num != 0) | ||
{ | ||
len++; | ||
num = num / 16; | ||
} | ||
return (len); | ||
} | ||
|
||
void ft_put_ptr(uintptr_t num) | ||
{ | ||
if (num >= 16) | ||
{ | ||
ft_put_ptr(num / 16); | ||
ft_put_ptr(num % 16); | ||
} | ||
else | ||
{ | ||
if (num <= 9) | ||
ft_putchar_fd((num + '0'), 1); | ||
else | ||
ft_putchar_fd((num - 10 + 'a'), 1); | ||
} | ||
} | ||
//ft_printf uint | ||
int ft_num_len(unsigned int num) | ||
{ | ||
int len; | ||
|
||
len = 0; | ||
while (num != 0) | ||
{ | ||
len++; | ||
num = num / 10; | ||
} | ||
return (len); | ||
} | ||
|
||
char *ft_uitoa(unsigned int n) | ||
{ | ||
char *num; | ||
int len; | ||
|
||
len = ft_num_len(n); | ||
num = (char *)malloc(sizeof(char) * (len + 1)); | ||
if (!num) | ||
return (0); | ||
num[len] = '\0'; | ||
while (n != 0) | ||
{ | ||
num[len - 1] = n % 10 + 48; | ||
n = n / 10; | ||
len--; | ||
} | ||
return (num); | ||
} | ||
|
||
int ft_print_unsigned(unsigned int n) | ||
{ | ||
int print_length; | ||
char *num; | ||
|
||
print_length = 0; | ||
if (n == 0) | ||
print_length += write(1, "0", 1); | ||
else | ||
{ | ||
num = ft_uitoa(n); | ||
print_length += ft_printstr(num); | ||
free(num); | ||
} | ||
return (print_length); | ||
} | ||
int ft_print_ptr(unsigned long long ptr) | ||
{ | ||
int print_length; | ||
|
||
print_length = 0; | ||
print_length += write(1, "0x", 2); | ||
if (ptr == 0) | ||
print_length += write(1, "0", 1); | ||
else | ||
{ | ||
ft_put_ptr(ptr); | ||
print_length += ft_ptr_len(ptr); | ||
} | ||
return (print_length); | ||
} | ||
|
||
//printf utils | ||
int ft_printstr(char *str) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
if (str == NULL) | ||
{ | ||
ft_putstr_fd("(null)", 1); | ||
return (6); | ||
} | ||
while (str[i]) | ||
{ | ||
write(1, &str[i], 1); | ||
i++; | ||
} | ||
return (i); | ||
} | ||
|
||
//utils | ||
int checkid(va_list arg, const char format) | ||
{ | ||
int len = 0; | ||
if (format == 'd' || format == 'i') | ||
{ | ||
ft_putnbr_fd(va_arg(arg, int), 1); | ||
} | ||
len += ft_printnbr(va_arg(arg, int)); | ||
else if (format == 'c') | ||
{ | ||
ft_putchar_fd(va_arg(arg, int), 1); | ||
} | ||
len += ft_printchar(va_arg(arg, int)); | ||
else if (format == 's') | ||
{ | ||
ft_putstr_fd(va_arg(arg, char *), 1); | ||
} | ||
len += ft_printstr(va_arg(arg, char *)); | ||
else if (format == '%') | ||
{ | ||
ft_putchar_fd('%', 1); | ||
} | ||
//TODO: p u x X | ||
return len; | ||
len += ft_printchar('%'); | ||
else if (format == 'p') | ||
len += ft_print_ptr(va_arg(arg, unsigned long)); | ||
else if (format == 'u') | ||
len += ft_print_unsigned(va_arg(arg, unsigned int)); | ||
else if (format == 'x' || format == 'X') | ||
len += ft_print_hex(va_arg(arg, unsigned int), format); | ||
return (len); | ||
} | ||
|
||
//printf | ||
int ft_printf(const char * str, ...) | ||
int ft_printf(const char *str, ...) | ||
{ | ||
va_list arg; | ||
va_list arg; | ||
int i; | ||
int len; | ||
|
||
va_start(arg, str); | ||
int i = 0; | ||
int len = 0; | ||
i = 0; | ||
len = 0; | ||
while (str[i]) | ||
{ | ||
if (str[i] == '%') | ||
{ | ||
len += checkid(arg, str[i+1]); | ||
len += checkid(arg, str[i + 1]); | ||
i++; | ||
} | ||
else | ||
|
@@ -215,16 +64,25 @@ int ft_printf(const char * str, ...) | |
} | ||
i++; | ||
} | ||
return (len); // TODO calculate len | ||
} | ||
int main() | ||
{ | ||
int a = 10000; | ||
int b = 25; | ||
char c = 'd'; | ||
char *d = "OMG"; | ||
printf("\n%d\n", ft_printf("yey %d WOW %d %c %s %% |||| %i", a, b, c, d, a)); | ||
printf("\n%d\n", printf("yey %d WOW %d %c %s %% |||| %i", a, b, c, d, a)); | ||
printf("\n%zd\n", write(1, "Yey", 3)); | ||
va_end(arg); | ||
return (len); | ||
} | ||
|
||
// #include <stdio.h> | ||
// int main(void) | ||
// { | ||
// int a; | ||
// int b; | ||
// int c; | ||
// char *d; | ||
// unsigned int ui; | ||
// ui = 4294967295; | ||
// a = 50; | ||
// b = 44444; | ||
// c = 'd'; | ||
// d = "OMG"; | ||
// printf("\n%d\n", ft_printf("yey %d WOW %d %c %s %% | ||
//|||| %i %p || %u", a, b, c, d, a, &a, ui)); | ||
// printf("\n%d\n", ft_printf("yey %d WOW %d %c %s %% | ||
//|||| %i %p || %u", a, b, c, d, a, &a, ui)); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: fcil <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/02/17 13:03:48 by fcil #+# #+# */ | ||
/* Updated: 2022/02/17 15:02:57 by fcil ### ########.fr */ | ||
/* Updated: 2022/02/18 17:07:24 by fcil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -21,15 +21,15 @@ int ft_printf(const char *format, ...); | |
int ft_formats(va_list args, const char format); | ||
int ft_printchar(int c); | ||
int ft_printstr(char *str); | ||
int ft_print_ptr(unsigned long long ptr); | ||
int ft_print_ptr(unsigned long ptr); | ||
int ft_printnbr(int n); | ||
int ft_print_unsigned(unsigned int n); | ||
int ft_print_hex(unsigned int num, const char format); | ||
int ft_printpercent(void); | ||
|
||
void ft_putstr(char *str); | ||
void ft_put_ptr(uintptr_t num); | ||
int ft_ptr_len(uintptr_t num); | ||
void ft_put_ptr(unsigned long num); | ||
int ft_ptr_len(unsigned long num); | ||
char *ft_uitoa(unsigned int n); | ||
int ft_num_len(unsigned int num); | ||
void ft_put_hex(unsigned int num, const char format); | ||
|
Oops, something went wrong.