-
Notifications
You must be signed in to change notification settings - Fork 0
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
Ryusuke Yashiro
committed
May 4, 2024
0 parents
commit 8d7a488
Showing
11 changed files
with
361 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Empty file.
Submodule Libft
added at
325a40
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_printf.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ryyashir <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/05 03:16:52 by ryyashir #+# #+# */ | ||
/* Updated: 2024/05/05 03:18:20 by ryyashir ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef FT_PRINTF_H | ||
#define FT_PRINTF_H | ||
|
||
#include <unistd.h> | ||
#include <stdarg.h> | ||
|
||
|
||
#endif |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* libft.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ryusukeyashiro <ryusukeyashiro@student. +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/26 16:55:08 by ryusukeyash #+# #+# */ | ||
/* Updated: 2024/04/27 17:27:12 by ryyashir ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef LIBFT_H | ||
# define LIBFT_H | ||
|
||
# include <limits.h> | ||
# include <stdint.h> | ||
# include <stdlib.h> | ||
# include <unistd.h> | ||
|
||
typedef struct s_list | ||
{ | ||
void *content; | ||
struct s_list *next; | ||
} t_list; | ||
|
||
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_memcmp(const void *s1, const void *s2, size_t n); | ||
int ft_strncmp(const char *s1, const char *s2, size_t n); | ||
int ft_tolower(int c); | ||
int ft_toupper(int c); | ||
int ft_atoi(const char *str); | ||
|
||
size_t ft_strlen(const char *str); | ||
size_t ft_strlcat(char *dest, const char *src, size_t size); | ||
size_t ft_strlcpy(char *dest, const char *src, size_t size); | ||
|
||
char *ft_strchr(const char *str, int n); | ||
char *ft_strdup(const char *str); | ||
char *ft_strnstr(const char *haystack, const char *needle, | ||
size_t len); | ||
char *ft_strrchr(const char *str, int c); | ||
char *ft_substr(char const *s, unsigned int start, size_t len); | ||
char *ft_strjoin(char const *s1, char const *s2); | ||
char *ft_strtrim(char const *s1, char const *set); | ||
char **ft_split(char const *str, char c); | ||
char *ft_itoa(int n); | ||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); | ||
|
||
void ft_bzero(void *s, size_t n); | ||
void *ft_calloc(size_t count, size_t size); | ||
void *ft_memchr(const void *s, int c, size_t n); | ||
void *ft_memcpy(void *dest, const void *src, size_t n); | ||
void *ft_memmove(void *s1, const void *s2, size_t n); | ||
void *ft_memset(void *b, int c, size_t len); | ||
void ft_striteri(char *s, void (*f)(unsigned int, char *)); | ||
void ft_putchar_fd(char c, int fd); | ||
void ft_putstr_fd(char *s, int fd); | ||
void ft_putendl_fd(char *s, int fd); | ||
void ft_putnbr_fd(int n, int fd); | ||
|
||
t_list *ft_lstnew(void *content); | ||
t_list *ft_lstlast(t_list *lst); | ||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), | ||
void (*del)(void *)); | ||
void ft_lstadd_front(t_list **lst, t_list *new); | ||
void ft_lstadd_back(t_list **lst, t_list *new); | ||
void ft_lstdelone(t_list *lst, void (*del)(void *)); | ||
void ft_lstclear(t_list **lst, void (*del)(void *)); | ||
void ft_lstiter(t_list *lst, void (*f)(void *)); | ||
int ft_lstsize(t_list *lst); | ||
|
||
#endif |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_print_alpha.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ryyashir <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/05 05:57:42 by ryyashir #+# #+# */ | ||
/* Updated: 2024/05/05 06:51:52 by ryyashir ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft.h" | ||
#include "ft_printf.h" | ||
|
||
|
||
int ft_alpha_six_len(unsigned int num) | ||
{ | ||
int len; | ||
|
||
len = 0; | ||
while(num > 0) | ||
{ | ||
len++; | ||
num = num / 16; | ||
} | ||
return (len); | ||
} | ||
|
||
void ft_alpha_six(unsigned int num , const char format) | ||
{ | ||
if(num < 16) | ||
{ | ||
ft_putchar_fd(num + '0', 1); | ||
} | ||
else | ||
{ | ||
ft_alpha_six(num / 16 , format); | ||
ft_putchar_fd(num % 16 + '0', 1); | ||
} | ||
} | ||
|
||
|
||
int ft_print_alpha(unsigned int c , const char format) | ||
{ | ||
if(c == 0) | ||
return (write(1, "0" , 1)); | ||
else | ||
ft_alpha_six(c , format); | ||
return (ft_alpha_six_len(c)); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_print_int.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ryyashir <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/05 03:20:55 by ryyashir #+# #+# */ | ||
/* Updated: 2024/05/05 05:33:49 by ryyashir ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft.h" | ||
#include "ft_printf.h" | ||
|
||
int ft_print_int(int c) | ||
{ | ||
int len; | ||
char *hold; | ||
|
||
len = 0; | ||
hold = ft_itoa(c); | ||
len = ft_print_str(hold); | ||
return (len); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_print_percent.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ryyashir <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/05 05:37:58 by ryyashir #+# #+# */ | ||
/* Updated: 2024/05/05 05:40:00 by ryyashir ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "ft_printf.h" | ||
|
||
int ft_print_percent(void) | ||
{ | ||
return (ft_putchar_fd('%' , 1)); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_print_str.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ryyashir <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/05 02:59:44 by ryyashir #+# #+# */ | ||
/* Updated: 2024/05/05 05:24:14 by ryyashir ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft.h" | ||
#include "ft_printf.h" | ||
|
||
int ft_print_str(char *s) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
if(!s[i]) | ||
{ | ||
ft_putstr_fd("(null)", 1); | ||
return (6); | ||
} | ||
while(s[i]) | ||
{ | ||
write(1 , &s[i] ,1); | ||
i++; | ||
} | ||
return (i); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_print_unit.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ryyashir <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/05 03:35:14 by ryyashir #+# #+# */ | ||
/* Updated: 2024/05/05 05:34:19 by ryyashir ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft.h" | ||
#include "ft_printf.h" | ||
|
||
int ft_uni_num_len(unsigned int c) | ||
{ | ||
int num_len; | ||
|
||
if(c == 0) | ||
num_len = 1; | ||
while (c > 0) | ||
{ | ||
c /= 10; | ||
num_len++; | ||
} | ||
return (num_len); | ||
} | ||
|
||
char *ft_uitoa(unsigned int c) | ||
{ | ||
char *str; | ||
int len; | ||
|
||
len =ft_uni_num_len(c); | ||
str = (char *)malloc(sizeof(char) * (len + 1)); | ||
if(!str) | ||
return (0); | ||
str[len] = '\0'; | ||
if(c == 0) | ||
*str = '0'; | ||
while(c > 0) | ||
{ | ||
str[len] = (c & 10) + '0'; | ||
c /= 10; | ||
len--; | ||
} | ||
return (str); | ||
} | ||
|
||
int ft_print_unit(unsigned int c) | ||
{ | ||
unsigned int len; | ||
char *hold; | ||
|
||
hold = ft_uitoa(c); | ||
len = 0; | ||
if(!hold) | ||
return (0); | ||
while(hold[len]) | ||
len++; | ||
ft_putstr_fd(hold , 1); | ||
return (len); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_printf.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ryyashir <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/05 05:34:45 by ryyashir #+# #+# */ | ||
/* Updated: 2024/05/05 06:46:33 by ryyashir ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft.h" | ||
#include "ft_printf.h" | ||
|
||
int ft_print_char(int c) | ||
{ | ||
char p; | ||
|
||
p = (char)c; | ||
return(write(1, &p , 1)); | ||
} | ||
|
||
|
||
int ft_convert_format(va_list args , const char format) | ||
{ | ||
if(format == 'c') | ||
return (ft_print_char(va_arg(args , int))); | ||
if(format == 's') | ||
return (ft_print_str(va_arg(args , char *))); | ||
if(format == 'd' || format == 'i') | ||
return (ft_print_int(va_arg(args , int))); | ||
if(format == 'u') | ||
return (ft_print_unit(va_arg(args , unsigned int))); | ||
if(format == 'x' || format == 'X') | ||
return (ft_print_alpha(va_arg(args , unsigned int),format)); | ||
if(format == '%') | ||
return (ft_print_percent()); | ||
return (0); | ||
} | ||
|
||
|
||
int ft_printf(const char *format, ...) | ||
{ | ||
int i; | ||
int ft_print_len; //printfは戻り値がintの値を返す。実際に書き込まれた文字数。負の値の場合はエラーあり。 | ||
va_list args; | ||
char *str; | ||
|
||
va_start(args,format); | ||
i = 0; | ||
ft_print_len = 0; | ||
str = (char *)format; | ||
while(str[i]) | ||
{ | ||
if(str[i] == '%') | ||
{ | ||
ft_print_len = ft_convert_format(args , str[i+1]); | ||
i++; | ||
} | ||
else | ||
ft_print_len += ft_print_char(str[i]); | ||
i++; | ||
} | ||
va_end(args); | ||
return (ft_print_len); | ||
} | ||
|
||
int main(void) | ||
{ | ||
ft_printf("test" , 3 , 'a'); | ||
return (0); | ||
} |