From aa34dc1c54bb25c0064c4296e849e33d5628273e Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 27 Sep 2016 05:14:32 +0200 Subject: [PATCH] split whitespaces --- libft/includes/libft.h | 14 +++- libft/src/str/ft_split_whitespaces.c | 108 +++++++++++++++++++++++++++ libft/src/strl/ft_strlprint.c | 10 +++ 3 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 libft/src/str/ft_split_whitespaces.c create mode 100644 libft/src/strl/ft_strlprint.c diff --git a/libft/includes/libft.h b/libft/includes/libft.h index 4b3b5523..dda65502 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -105,19 +105,25 @@ void ft_lst_merge(t_list **begin_list1, t_list *begin_list2); void ft_lst_reverse(t_list **begin_list); int ft_diff(void *a, void *b); -char *ft_strrev(char *str); t_list *ft_id(t_list *a); + +char *ft_strrev(char *str); +char **ft_strsplit(char const *s, char c); +char **ft_split_whitespaces(char *str); +char *ft_convert_base(char *str, char *base_from, char *base_to, char *flags); + char *ft_itoa_base(int nbr, char *base, char *flags); char *ft_lltoa_base(long long nbr, char *base, char *flags); char *ft_ulltoa_base(unsigned long long nbr, char *base); char *ft_uitoa_base(unsigned int nbr, char *base); - -void ft_strlsort(char **list, int size, int (*cmp)()); -char *ft_convert_base(char *str, char *base_from, char *base_to, char *flags); size_t ft_ilen(int n); size_t ft_uilen(unsigned int n); +void ft_strlsort(char **list, int size, int (*cmp)()); +void ft_strlprint(char **strl, char sep); + int ft_time_isrecent(time_t event); + int ft_xattr_print(char *path); int ft_xattr_count(char *path); diff --git a/libft/src/str/ft_split_whitespaces.c b/libft/src/str/ft_split_whitespaces.c new file mode 100644 index 00000000..76c8acfd --- /dev/null +++ b/libft/src/str/ft_split_whitespaces.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_whitespaces.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/08 09:33:37 by jhalford #+# #+# */ +/* Updated: 2016/08/20 23:23:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#define SEP(x) (x==' ' || x=='\n' || x=='\t') + +char **alloc_table(char **table, char *str) +{ + int i; + int n_words; + + i = 0; + n_words = 0; + while (SEP(str[i])) + i++; + while (str[i] != '\0') + { + i++; + if (SEP(str[i])) + { + n_words++; + while (SEP(str[i])) + i++; + } + } + if (!SEP(str[i - 1])) + n_words++; + table = (char**)malloc(sizeof(*table) * (n_words + 1)); + table[n_words] = 0x0; + return (table); +} + +char **alloc_words(char **table, char *str) +{ + int i; + int j; + int k; + + i = 0; + j = 0; + k = 0; + while (SEP(str[i])) + i++; + while (str[i] != '\0') + { + i++; + if (SEP(str[i]) || str[i] == '\0') + { + table[j] = (char*)malloc(sizeof(**table) * (k + 1)); + j++; + k = 0; + while (SEP(str[i])) + i++; + } + k++; + } + return (table); +} + +char **fill_table(char **table, char *str) +{ + int i; + int j; + int k; + + i = 0; + j = 0; + k = 0; + while (SEP(str[i])) + i++; + while (str[i] != '\0') + { + table[j][k] = str[i]; + i++; + k++; + if (SEP(str[i])) + { + table[j][k] = '\0'; + j++; + k = 0; + while (SEP(str[i])) + i++; + } + } + return (table); +} + +char **ft_split_whitespaces(char *str) +{ + char **table; + + table = NULL; + table = alloc_table(table, str); + table = alloc_words(table, str); + table = fill_table(table, str); + return (table); +} diff --git a/libft/src/strl/ft_strlprint.c b/libft/src/strl/ft_strlprint.c new file mode 100644 index 00000000..4a659e23 --- /dev/null +++ b/libft/src/strl/ft_strlprint.c @@ -0,0 +1,10 @@ +#include "libft.h" + +void ft_strlprint(char **strl, char sep) +{ + int i; + + i = 0; + while (strl[i]) + ft_printf("%s%c", strl[i], sep); +}