From fa4bf89263e897695dbf48061369a23d695fef8b Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 15 Oct 2019 13:02:53 +0200 Subject: Parsing rewrite - 4 extraction functions which parse some format attributes and remove them. - Not handling wildcard yet --- utils.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index bf30662..0784e12 100644 --- a/utils.c +++ b/utils.c @@ -103,3 +103,103 @@ t_bool ft_isdigit(char c) { return (c >= '0' && c <= '9'); } + +int nbrlen_radix(long int nbr, int radix) +{ + int counter; + long unsigned int u_nbr; + + if (nbr == 0) + return (1); + counter = 0; + u_nbr = nbr; + if (nbr < 0) + { + counter++; + u_nbr = -nbr; + } + while (u_nbr > 0) + { + u_nbr /= radix; + counter++; + } + return (counter); +} + +char *ft_itoa_base(long int n, char *base) +{ + char *str; + int len; + int is_negative; + int radix; + long unsigned int u_nbr; + + + radix = ft_strlen(base); + len = nbrlen_radix(n, radix); + if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + str[len] = '\0'; + is_negative = 0; + u_nbr = n; + if (n < 0) + { + is_negative = 1; + str[0] = '-'; + u_nbr = -n; + } + len--; + while (len >= (is_negative ? 1 : 0)) + { + str[len] = base[u_nbr % radix]; + u_nbr /= radix; + len--; + } + return (str); +} + +char *ft_strnew(int size) +{ + char *str; + + if ((str = (char*)malloc(sizeof(char) * (size + 1))) == NULL) + return (NULL); + str[size] = '\0'; + while (size-- > 0) + str[size] = '\0'; + return (str); +} + +char *ft_strdup(char *s) +{ + char *clone; + size_t i; + size_t len; + + len = ft_strlen(s); + if ((clone = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + i = 0; + while (i < len) + { + clone[i] = s[i]; + i++; + } + clone[i] = '\0'; + return (clone); +} + +char *ft_strcpy(char *dest, const char *src) +{ + int i; + + i = 0; + while (src[i]) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (dest); +} + -- cgit