diff --git a/Makefile b/Makefile index e64c328..9faef73 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,9 @@ _LIBS = libft.a LIBS = $(_LIBS:lib%.a=%) SRCS = ft_printf.c \ - ft_next_word.c + ft_next_word.c \ + aside.c \ + ft_convert.c ODIR = ./builds OBJS = $(SRCS:%.c=$(ODIR)/%.o) diff --git a/aside.c b/aside.c new file mode 100644 index 0000000..3d2ba19 --- /dev/null +++ b/aside.c @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* aside.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/19 16:23:01 by hulamy #+# #+# */ +/* Updated: 2020/02/19 18:44:21 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +/* +** SPECIFIER : +** receive a word as a string, check if it start by '%', and return the +** specifier (diuxXspefgn) and th length (h hh l ll) +** -if s is a string, or is a single '%' +** return NULL (to print is as a string) +** -if s is a double '%%', remove one '%', and +** return NULL (to print is as a string) +** -then s is a conversion, go to the length and specifier +** -copy them in 'string' +** -and remove them from s +** -return the length and specifier in a string +*/ + +char *specifier(char *s) +{ + char *string; + int i; + + if (s[0] != '%' || s[1] == '\0') + return (NULL); + if (s[1] == '%') + { + s[1] = '\0'; + return (NULL); + } + i = 1; + while (ft_strchr("#0- +'0123456789.*", s[i]) != NULL) + i++; + string = ft_strdup(s + i); + while (s[i] != '\0') + { + s[i] = '\0'; + i++; + } + return (string); +} + +/* +** print the string +*/ + +int ft_put_word(char *s) +{ + int i; + + i = ft_strlen(s); + ft_putstr(s); + return (i); +} + +/* +** -receive 'i' the number in which '*' will expand +** -turn it into a string +** -calculate the total lentgh of the string '%...' for nbr replacing '*' +** -allocate a new string with this length +** -copy the original str with first '*' expanded into it's corresponding nbr +*/ + +int ft_expand_star(int nbr, char **string) +{ + char *s; + char *strnbr; + int i; + int j; + + strnbr = ft_itoa(nbr); + i = ft_strlen(strnbr) + ft_strlen(*string) - 1; + if (!(s = (char *)malloc(sizeof(char) * (i + 1)))) + return (0); + s[i] = '\0'; + i = 0; + j = 0; + while ((*string)[i] != '\0') + { + s[j] = (*string)[i]; + if (s[j] == '*') + while (*strnbr != '\0') + s[j++] = *(strnbr++); + else + j++; + i++; + } + free(*string); + *string = s; + return (1); +} diff --git a/ft_convert.c b/ft_convert.c new file mode 100644 index 0000000..9e4519f --- /dev/null +++ b/ft_convert.c @@ -0,0 +1,78 @@ + +#include "ft_printf.h" + +/* +** FT_CONVERT : +** -convert the next argument into a string according to the following +** correspondances for diuxXcspefgn : +** [char] [hhd, hhi, c] [int] [d i c] +** [short] [hd, hi] [int] +** [int] [d, i] [int] +** [long] [ld, li] [long] [ld li] +** [long long] [lld, lli] [long] +** [unsigned char] [hhu, hhx, hhX] [unsigned int] [u x X p s] +** [unsigned short] [hu, hx, hX] [unsigned int] +** [unsigned int] [u, x, X, p] [unsigned int] +** [unsigned long] [lu, lx, lX] [unsigned long] [lu lx lX] +** [unsigned long long][llu, llx, llX] [unsigned long] +** [char *] [s, hhn] +** [double] [e, le, f, lf, g, lg] +** [wint_t] [lc] +** [wchar_t] [ls] +** [short *] [hn] +** [int *] [n] +** [long *] [ln] +** [long long *] [lln] +** -'h' and 'hh', are traited just like regular size because of +** default promotion, that promote smaller type than int into int +*/ + +char *conv_i(char c, long int i) +{ + char *s; + + if (c == 'c') + { + s = ft_strdup("0"); + s[0] = i; + return (s); + } + if (c == 'd' || c == 'i') + return (ft_itoa(i)); + return (NULL); +} + +char *conv_u(char c, unsigned long int i) +{ + char *s; + + s = ft_utoa(i); + if (c == 's') + return (strdup((char *)i)); + if (c == 'u') + return (s); + if (c == 'x' || c == 'p') + return (ft_convertbase(s, "0123456789", "0123456789abcdef")); + if (c == 'X') + return (ft_convertbase(s, "0123456789", "0123456789ABCDEF")); + return (NULL); +} + +char *ft_convert(va_list ap, char *type) +{ + char *s; + + if ((s = ft_strchrset(type, "dic")) && ft_strchr(type, 'l')) + return (conv_i(s[0], va_arg(ap, long int))); + if ((s = ft_strchrset(type, "dic"))) + return (conv_i(s[0], va_arg(ap, int))); + if ((s = ft_strchrset(type, "uxX")) && ft_strchr(type, 'l')) + return (conv_u(s[0], va_arg(ap, unsigned long int))); + if ((s = ft_strchrset(type, "uxXp"))) + return (conv_u(s[0], va_arg(ap, unsigned int))); + if ((s = ft_strchrset(type, "s"))) + return (conv_u(s[0], va_arg(ap, unsigned long int))); + if (ft_strchrset(type, "efgn")) + return (NULL); + return (NULL); +} diff --git a/ft_printf b/ft_printf index 0b98061..dc86627 100755 Binary files a/ft_printf and b/ft_printf differ diff --git a/ft_printf.c b/ft_printf.c index 90fd998..f3688e9 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -1,161 +1,6 @@ #include "ft_printf.h" -/* -** -if s is a string, or is a single '%' -** return NULL (to print is as a string) -** -if s is a double '%%', remove one '%', and -** return NULL (to print is as a string) -** -then s is a conversion, go to the length and specifier -** -copy them in 'string' -** -and remove them from s -** -return the length and specifier in a string -*/ - -char *specifier(char *s) -{ - char *string; - int i; - - if (s[0] != '%' || s[1] == '\0') - return (NULL); - if (s[1] == '%') - { - s[1] = '\0'; - return (NULL); - } - i = 1; - while (ft_strchr("#0- +'0123456789.*", s[i]) != NULL) - i++; - string = ft_strdup(s + i); - while (s[i] != '\0') - { - s[i] = '\0'; - i++; - } - return (string); -} - -/* -** print the string -*/ - -int ft_put_word(char *s) -{ - int i; - - i = ft_strlen(s); - ft_putstr(s); - return (i); -} - -/* -** receive 'i' the number in which '*' will expand -** turn it into a string -** calculate the total lentgh of the string '%...' for nbr replacing '*' -** allocate a new string with this length -** copy the original str with first '*' expanded into it's corresponding nbr -*/ - -int ft_expand_star(int nbr, char **string) -{ - char *s; - char *strnbr; - int i; - int j; - - strnbr = ft_itoa(nbr); - i = ft_strlen(strnbr) + ft_strlen(*string) - 1; - if (!(s = (char *)malloc(sizeof(char) * (i + 1)))) - return (0); - s[i] = '\0'; - i = 0; - j = 0; - while ((*string)[i] != '\0') - { - s[j] = (*string)[i]; - if (s[j] == '*') - while (*strnbr != '\0') - s[j++] = *(strnbr++); - else - j++; - i++; - } - free(*string); - *string = s; - return (1); -} - -/* -** -convert the next argument into a string according to the following -** correspondances for diuxXcspefgn : -** [char] [hhd, hhi, c] [long] [d i c] -** [short] [hd, hi] [long] -** [int] [d, i] [long] -** [long] [ld, li] [long] -** [long long] [lld, lli] [long] -** [unsigned char] [hhu, hhx, hhX] [unsigned long] [u x X p s] -** [unsigned short] [hu, hx, hX] [unsigned long] -** [unsigned int] [u, x, X, p] [unsigned long] -** [unsigned long] [lu, lx, lX] [unsigned long] -** [unsigned long long][llu, llx, llX] [unsigned long] -** [char *] [s, hhn] -** [double] [e, le, f, lf, g, lg] -** [wint_t] [lc] -** [wchar_t] [ls] -** [short *] [hn] -** [int *] [n] -** [long *] [ln] -** [long long *] [lln] -** -'h', 'hh', 'l' and 'll' are traited just like regular size -*/ - -char *conv_li(char c, long int i) -{ - char *s; - - if (c == 'c') - { - s = ft_strdup("0"); - s[0] = i; - return (s); - } - if (c == 'd' || c == 'i') - return (ft_itoa(i)); - return (NULL); -} - -char *conv_lu(char c, unsigned long int i) -{ - char *s; - - s = ft_utoa(i); - if (c == 's') - return (strdup((char *)i)); - if (c == 'u') - return (s); - if (c == 'x') - return (ft_convertbase(s, "0123456789", "0123456789abcdef")); - if (c == 'X') - return (ft_convertbase(s, "0123456789", "0123456789ABCDEF")); - if (c == 'p') - return (ft_utoa(i)); - return (NULL); -} - -char *ft_convert(va_list ap, char *type) -{ - char *s; - - if ((s = ft_strchrset(type, "dic")) != NULL) - return (conv_li(s[0], va_arg(ap, long int))); - if ((s = ft_strchrset(type, "uxXps")) != NULL) - return (conv_lu(s[0], va_arg(ap, unsigned long int))); - if (ft_strchrset(type, "efgn")) - return (NULL); - return (NULL); -} - /* ** FT_PRINTF : ** va_list ap; @@ -178,13 +23,6 @@ char *ft_convert(va_list ap, char *type) ** ft_flag_transform() | -proceed all modification according to flags ** ft_put_word(print) | -print the string fully converted ** return (length) | -return the length of what was printed -** -** char *next_word(char *s); -** char *specifier(char **s); -** int ft_put_words(char *s); -** void ft_expand_star(int i, char **s); -** char *ft_convert(va_list ap, char *type); -** char *ft_flag_transform(char *s, char *print); */ int ft_printf(char *string, ...) @@ -207,10 +45,8 @@ int ft_printf(char *string, ...) if (!(ft_expand_star(va_arg(ap, int), &s))) return (-1); print = ft_convert(ap, type); - ft_putstr(print); -// printf("= %s | %s\n",s,print); // print = ft_flag_transform(s, print); - // length += ft_put_word(print); + length += ft_put_word(print); } } return (length); @@ -245,25 +81,27 @@ int ft_printf(char *string, ...) ** char *ft_lpadd(int i, char *print, char c); */ -// ft_flag_transform() -// { -// if ((i = flag_p(&s))) -// print = ft_precision(i, print); -// if ((i = flag_w(s))) -// { -// if (flag_-(&s)) -// print = ft_rpadd(i, print); -// else if (flag_0(&s)) -// print = ft_lpadd(i, print, '0'); -// else -// print = ft_lpadd(i, print, ' '); -// } - // if (flag_+(s)) // - // else if (flag_space(s)) // - // if (flag_'(s)) // - // if (flag_#(s)) // - // print = ft_alternate_form(print) // - // } +/* +char *ft_flag_transform(char *s, char *print) +{ + if ((i = flag_p(&s))) + print = ft_precision(i, print); + if ((i = flag_w(s))) + { + if (flag_-(&s)) + print = ft_rpadd(i, print); + else if (flag_0(&s)) + print = ft_lpadd(i, print, '0'); + else + print = ft_lpadd(i, print, ' '); + } +// if (flag_+(s)) // +// else if (flag_space(s)) // +// if (flag_'(s)) // +// if (flag_#(s)) // +// print = ft_alternate_form(print) // +} +*/ int ft_printf_test(char *string, ...) { @@ -335,10 +173,18 @@ int main(int ac, char **av) // ft_printf_test(str, d, i, u, x, X, c, s, e, f, g); // printf("%s : %i : %i : %li : %li : %u : %lu\n", str2, i1, i2, i3, i4, i5, i6); // ft_printf_test(str2, i1, i2, i3, i4, i5, i6); + printf("sdf\n"); ft_printf("sdf\n\n"); - printf("%i\n", -23); - ft_printf("%i\n\n", -23); + printf("%i\n", 23); + ft_printf("%i\n\n", 23); + long int k = -23; + printf("%li\n", k); + ft_printf("%li\n\n", k); + printf("%i\n", -32); + ft_printf("%i\n\n", -32); + printf("%li\n", 9223372036854775807); + ft_printf("%li\n\n", 9223372036854775807); printf("%c\n", 'f'); ft_printf("%c\n\n", 'f'); printf("%s\n", "sdffhk"); @@ -347,6 +193,10 @@ int main(int ac, char **av) ft_printf("%u\n\n", 1221879); printf("%x\n", 3287); ft_printf("%x\n\n", 3287); + printf("%lX\n", 9223372036854775807); + ft_printf("%lX\n\n", 9223372036854775807); + printf("%p\n", "dfgdf"); + ft_printf("%p\n\n", "dfgdf"); } return (0); } diff --git a/ft_printf.h b/ft_printf.h index cb769cd..d7c9a3c 100644 --- a/ft_printf.h +++ b/ft_printf.h @@ -12,5 +12,9 @@ int ft_printf(char *string, ...); int width_precision(char *s); int word_length(char *s); char *next_word(char **s); +char *ft_convert(va_list ap, char *type); +char *specifier(char *s); +int ft_put_word(char *s); +int ft_expand_star(int nbr, char **string); #endif