diff --git a/.ft_printf.c.swn b/.ft_printf.c.swn index c0a6dca..e71ca7f 100644 Binary files a/.ft_printf.c.swn and b/.ft_printf.c.swn differ diff --git a/ft_printf.c b/ft_printf.c index 9399900..02112ed 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -1,8 +1,8 @@ #include "ft_printf.h" -#include /* +** FT_PRINTF : ** va_list ap; ** int length; ** char *print; | -contain the arg converted into a string @@ -31,71 +31,6 @@ ** char *ft_flag_transform(char *s, char *print); */ -int word_length(char *s) -{ - int i; - - i = 1; - if (s[0] == '\0') - return (0); - if (s[0] != '%') - { - while (s[i] != '%' && s[i] != '\0') - i++; - printf("%02i.:",i); - return (i); - } - while (strchr("#0- +'", s[i]) != NULL) - i++; - if (strchr("*", s[i]) != NULL) - i++; - else if (strchr("123456789", s[i]) != NULL) - { - i++; - while (strchr("0123456789", s[i]) != NULL) - i++; - } - if (strchr(".", s[i]) != NULL) - { - i++; - if (strchr("*", s[i]) != NULL) - i++; - else if (strchr("123456789", s[i]) != NULL) - { - i++; - while (strchr("0123456789", s[i]) != NULL) - i++; - } - } - if (strchr("hl", s[i]) != NULL) - i++; - if (strchr("diuxXcspefgn%", s[i]) != NULL) - i++; - printf("%02i::",i); - return (i); -} - -char *next_word(char **string) -{ - char *s; - char *word; - int i; - - s = *string; - if (*s == '\0') - return (NULL); - if ((i = word_length(s)) < 0) - { - printf("error\n"); - return (NULL); - } - word = (char *)malloc(sizeof(char) * (i + 1)); - word[i] = '\0'; - memmove(word, s, i); - *string += i; - return (word); -} - int ft_printf(char *string, ...) { char *s; @@ -176,7 +111,6 @@ int ft_printf(char *string, ...) int ft_printf_test(char *string, ...) { - t_prist *lst; va_list ap; va_start(ap, string); diff --git a/ft_printf.h b/ft_printf.h index 96aa670..d9651b3 100644 --- a/ft_printf.h +++ b/ft_printf.h @@ -4,20 +4,12 @@ # include # include +# include + int ft_printf(char *string, ...); -typedef struct s_prist -{ - char *str; - int flag; - int width; - int prec; - char *length; - char spec; - char *arg; - int p_nb; - int a_nb; - struct s_prist *next; -} t_prist; +int width_precision(char *s); +int word_length(char *s); +char *next_word(char **s); #endif diff --git a/next_word.c b/next_word.c new file mode 100644 index 0000000..216f64f --- /dev/null +++ b/next_word.c @@ -0,0 +1,107 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* next_word.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 13:58:30 by hulamy #+# #+# */ +/* Updated: 2020/02/10 13:58:34 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +/* +** -placed outside of "word_length" for lake of space +** -check if there is a '*' or a number +** -usefull as such for the 'width', and after a check +** for a '.' for the 'precision' flag +*/ + +int width_precision(char *s) +{ + int i; + + i = 0; + if (strchr("*", s[i]) != NULL) + i++; + else if (strchr("123456789", s[i]) != NULL) + { + i++; + while (strchr("0123456789", s[i]) != NULL) + i++; + } + return (i); +} + +/* +** -return the length of the next word to print +** -for that it got through the characters expecting +** in the following order : +** [%][flags][width][.precision][length][specifier] +** knowing that 'flags' can repeat themselves +** -a single '%' is treated as a word of length 1 +** (unlike the real printf) +** -it's written : +** i += width_precision(s + i + 1) + 1; +** instead of : +** i++; +** i += width_precision(s + i); +** to save a line (3 with the brackets) +*/ + +int word_length(char *s) +{ + int i; + + i = 1; + if (s[0] == '\0') + return (0); + if (s[0] != '%') + { + while (s[i] != '%' && s[i] != '\0') + i++; + printf("%02i.:",i); + return (i); + } + while (strchr("#0- +'", s[i]) != NULL) + i++; + i += width_precision(s + i); + if (strchr(".", s[i]) != NULL) + i += width_precision(s + i + 1) + 1; + if (strchr("hl", s[i]) != NULL) + i++; + if (strchr("diuxXcspefgn%", s[i]) != NULL) + i++; + printf("%02i::",i); + return (i); +} + +/* +** -return the next sequence to be print +** (either a string, or a conversion) +** -a single '%' is an error in real printf +** but is treated as a '%' here +*/ + +char *next_word(char **string) +{ + char *s; + char *word; + int i; + + s = *string; + if (*s == '\0') + return (NULL); + if ((i = word_length(s)) < 0) + { + printf("error\n"); + return (NULL); + } + word = (char *)malloc(sizeof(char) * (i + 1)); + word[i] = '\0'; + memmove(word, s, i); + *string += i; + return (word); +}