From a63defb0b90f8d12655128ef16797e5d0ab209c6 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Mon, 10 Feb 2020 13:59:22 +0100 Subject: [PATCH] nex_word ok --- .ft_printf.c.swn | Bin 32768 -> 28672 bytes ft_printf.c | 68 +----------------------------- ft_printf.h | 18 +++----- next_word.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 80 deletions(-) create mode 100644 next_word.c diff --git a/.ft_printf.c.swn b/.ft_printf.c.swn index c0a6dca4fcbf6dfe689aa33f862f3e55a7601996..e71ca7f60b4c0b107bb910a79f7b2972f8392edc 100644 GIT binary patch delta 4775 zcmbu?4{#LK9l-HN2uUsw2+#?XSo0c<7cL=x1S}FGMyx|Z(;EJCwBgKLlD*tEm%Gf~ zo&hax5UEN<4W~Gc0s*6g7HIW^A`UI3#X)Hi>u6iF1;sX`Oa$={Gfb!U`!;uhT>gx- zH}kpM+kJn3@9pk;?d?hJCq`x0m;A7@TFxmd6?`&<&=yzR`Rv3DvC1J{%I==h&LOW`ZgCv|8C&a;Vgyg%O2d5eC*RTThC_pbm_%|HEVeG^X{2ZGszMe!7P4L2vTHJ(stfCMT zZp=gpzKL}Fo9mz8O{fCbp#jw>ffLDipLzBwcB2bhu?4P8e7RAGlT4$1Sc9d=$LS5gF?v86E|)cI^oMNF|6ir%@yk|2513vxRMHw|?1!wK8F6{dRpB2w zk9Bd%3<}f8IY-E754MVLytBiXWI2cK>413?FKYN=5=;5oDXqa1^=s!HnDu|lX2tJ~ zcpqDtl$Srq@q6sD^I8J~P5u#!h+iE1Ai4B8a>;XO(4z87A+o>7xxUz$RP2gO z3EY?G&}BinY||S}UtO!AHRvHd7|0LF0?7qgI$eF0MQ#q}UOjxm>1D#<&2QoiYvrcaaghNq!fGegtuds5XgOy!iRLLlv8)sY?unVP3j z2EB6bh?%VcO;<^0=%zCy+YH?_wLmO)emLOQn>D*p{dbS)wcDj0>jT0$;C;~+H0)OR zwLlZSDzzKa90<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h&R}L(vNHCO%I#ILc@c z*&y~G)gUwbG!EfabYL&`U;`e929XhF;}Da%5Md@{C-z}CR$&Un%k?00Oiaj1%&F%~ zOgiz_T!UhmAad /* +** 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); +}