new dlst for minishell project

This commit is contained in:
Jack Halford 2016-10-28 21:23:13 +02:00
parent a22dd81d2a
commit 26652ca579
20 changed files with 242 additions and 73 deletions

15
libft/includes/dlst.h Normal file
View file

@ -0,0 +1,15 @@
typedef struct s_dlist
{
void *content;
size_t content_size;
struct s_dlist *next;
struct s_dlist *prev;
} t_dlist;
void ft_dlst_add_after(t_dlist **alst, t_dlist *new);
void ft_dlst_add_before(t_dlist **alst, t_dlist *new);
void ft_dlst_delone(t_dlist **alst, void (*del)(void *, size_t));
int ft_dlst_size(t_dlist *list);
t_dlist *ft_dlst_new(void const *content, size_t content_size);
t_dlist *ft_dlst_last(t_dlist *list);
char *ft_dlsttostr(t_dlist *list);

View file

@ -41,7 +41,6 @@ void ft_printf_parse_width(t_fmt *fmt, char **format, va_list ap);
void ft_printf_parse_precision(t_fmt *fmt, char **format, va_list ap);
void ft_printf_parse_modifiers(t_fmt *fmt, char **format);
int ft_printf(const char *format, ...);
char *ft_transform(t_fmt *fmt, va_list ap);
void ft_fmt_error_conv(char conv);

View file

@ -1,6 +1,8 @@
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# define BUFF_SIZE 1
# include "libft.h"
# define BUFF_SIZE 10
int get_next_line(int const fd, char **line);

View file

@ -1,9 +1,9 @@
#ifndef LIBFT_H
#define LIBFT_H
# include "ftprintf.h"
# include "ftxattr.h"
# include "getnextline.h"
# include "lst.h"
# include "dlst.h"
# include <string.h>
# include <unistd.h>
@ -11,7 +11,7 @@
# include <stdlib.h>
# include <time.h>
# define FT_SEP(x) (x == ' ' || x == '\t' || x == '\n')
# define FT_WS(x) (x == ' ' || x == '\t' || x == '\n')
# define FT_ABS(x) (((x) < 0) ? -(x) : (x))
# define FT_NEG(x) (((x) < 0) ? 1 : 0)
# define FT_POS(x) (((x) > 0) ? 1 : 0)
@ -19,13 +19,6 @@
# define FT_MAX(a, b) ((a) > (b) ? (a) : (b))
# define FT_DIST(a, b) (FT_ABS((a) - (b)))
typedef struct s_list
{
void *content;
size_t content_size;
struct s_list *next;
} t_list;
void ft_debug(void);
void *ft_memset(void *b, int c, size_t len);
@ -73,7 +66,7 @@ char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s);
char **ft_strsplit(char const *s, char c);
char *ft_itoa(int n);
void ft_putchar(char c);
int ft_putchar(int c);
void ft_putstr(char const *s);
void ft_putendl(char const *s);
void ft_putnbr(int n);
@ -82,37 +75,10 @@ void ft_putstr_fd(char const *s, int fd);
void ft_putendl_fd(char const *s, int fd);
void ft_putnbr_fd(int n, int fd);
t_list *ft_lstnew(void const *content, size_t content_size);
void ft_lstdelone(t_list **alst, void (*del)(void *, size_t));
void ft_lstadd(t_list **alst, t_list *new);
void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
void ft_lst_sort(t_list **begin_list, int (*cmp)());
void ft_lst_print(t_list *list, void (*printer)());
int ft_lstsize(t_list *lst);
void ft_lsteadd(t_list **alst, t_list *new);
void ft_lstnadd(t_list **alst, t_list *new, int n);
t_list *ft_lstlast(t_list *lst);
void ft_lst_sorted_merge(t_list **begin_list1, t_list *begin_list2, int (*cmp)());
void ft_lst_sorted_insert(t_list **begin_list, t_list *insert, int (*cmp)());
t_list *ft_lstnew_range(int a, int b);
void ft_lst_delif(t_list **alist, void *data_ref, int (*cmp)(), void (*del)(void *, size_t));
void ft_lst_delsub(t_list **alst, t_list *sub, int (*cmp)(), void (*del)(void *, size_t));
void ft_lst_cfree(void *ptr, size_t size);
t_list *ft_lst_filter(t_list *lst, void const *data_ref, t_list *(*f)(t_list *elem, void const *));
t_list *ft_lst_removeif(t_list **alst, void *data_ref, int (*cmp)());
t_list *ft_lst_find(t_list *begin_list, void *data_ref, int (*cmp)());
t_list *ft_lstpop(t_list **lst);
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);
t_list *ft_id(t_list *a);
char *ft_strrev(char *str);
char **ft_strsplit(char const *s, char c);
char *ft_str3join(char const *s1, char const *s2, char const *s3);
char *ft_strcut(char *str, char *cut);
char **ft_split_whitespaces(char *str);
char *ft_convert_base(char *str, char *base_from, char *base_to, char *flags);
@ -127,8 +93,11 @@ void ft_sstrsort(char **list, int size, int (*cmp)());
void ft_sstrprint(char **list, char sep);
char **ft_sstrdup(char **list);
char **ft_sstradd(char **list, char *new);
void ft_sstrdel(char **sstr, int index);
int ft_time_isrecent(time_t event);
char *ft_path_notdir(char *path);
int ft_printf(const char *format, ...);
#endif

40
libft/includes/lst.h Normal file
View file

@ -0,0 +1,40 @@
#ifndef LST_H
# define LST_H
typedef struct s_list
{
void *content;
size_t content_size;
struct s_list *next;
} t_list;
t_list *ft_lstnew(void const *content, size_t content_size);
void ft_lstdelone(t_list **alst, void (*del)(void *, size_t));
void ft_lstadd(t_list **alst, t_list *new);
void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
void ft_lst_sort(t_list **begin_list, int (*cmp)());
void ft_lst_print(t_list *list, void (*printer)());
int ft_lstsize(t_list *lst);
void ft_lsteadd(t_list **alst, t_list *new);
void ft_lstnadd(t_list **alst, t_list *new, int n);
t_list *ft_lstlast(t_list *lst);
void ft_lst_sorted_merge(t_list **begin_list1, t_list *begin_list2, int (*cmp)());
void ft_lst_sorted_insert(t_list **begin_list, t_list *insert, int (*cmp)());
t_list *ft_lstnew_range(int a, int b);
void ft_lst_delif(t_list **alist, void *data_ref, int (*cmp)(), void (*del)(void *, size_t));
void ft_lst_delsub(t_list **alst, t_list *sub, int (*cmp)(), void (*del)(void *, size_t));
void ft_lst_cfree(void *ptr, size_t size);
t_list *ft_lst_filter(t_list *lst, void const *data_ref, t_list *(*f)(t_list *elem, void const *));
t_list *ft_lst_removeif(t_list **alst, void *data_ref, int (*cmp)());
t_list *ft_lst_find(t_list *begin_list, void *data_ref, int (*cmp)());
t_list *ft_lstpop(t_list **lst);
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);
t_list *ft_id(t_list *a);
#endif

View file

@ -0,0 +1,16 @@
#include "libft.h"
void ft_dlst_add_after(t_dlist **alst, t_dlist *new)
{
if (new)
{
new->prev = (*alst);
if (*alst)
new->next = (*alst)->next;
if (new->next)
new->next->prev = new;
if (new->prev)
new->prev->next = new;
*alst = new;
}
}

View file

@ -0,0 +1,16 @@
#include "libft.h"
void ft_dlst_add_before(t_dlist **alst, t_dlist *new)
{
if (new)
{
new->next = (*alst);
if (*alst)
new->prev = (*alst)->prev;
if (new->next)
new->next->prev = new;
if (new->prev)
new->prev->next = new;
*alst = new;
}
}

View file

@ -0,0 +1,22 @@
#include "libft.h"
void ft_dlst_delone(t_dlist **alst, void (*del)(void *, size_t))
{
t_dlist *tmp;
tmp = *alst;
if (tmp)
{
if (del)
(*del)(tmp->content, tmp->content_size);
if (tmp->next)
tmp->next->prev = tmp->prev;
if (tmp->prev)
tmp->prev->next = tmp->next;
if (tmp->prev)
*alst = tmp->prev;
else
*alst = tmp->next;
free(tmp);
}
}

View file

@ -0,0 +1,8 @@
#include "libft.h"
t_dlist *ft_dlst_last(t_dlist *list)
{
while (list && list->next)
list = list->next;
return (list);
}

View file

@ -0,0 +1,27 @@
#include "libft.h"
t_dlist *ft_dlst_new(void const *content, size_t content_size)
{
t_dlist *new;
if (!content)
{
new = malloc(sizeof(*new));
if (!new)
return (NULL);
new->content_size = 0;
new->content = NULL;
}
else
{
new = (t_dlist *)malloc(sizeof(*new));
if (!new)
return (NULL);
new->content_size = content_size;
new->content = ft_memalloc(content_size + 1);
ft_memcpy(new->content, content, content_size);
}
new->next = NULL;
new->prev = NULL;
return (new);
}

View file

@ -0,0 +1,24 @@
#include "libft.h"
int ft_dlst_size(t_dlist *list)
{
int size;
t_dlist *tmp;
size = 0;
if (list)
size++;
tmp = list;
while (tmp->next)
{
size++;
tmp = tmp->next;
}
tmp = list;
while (tmp->prev)
{
size++;
tmp = tmp->prev;
}
return (size);
}

View file

@ -0,0 +1,20 @@
#include "libft.h"
char *ft_dlsttostr(t_dlist *list)
{
char *str;
if (!list)
return (NULL);
while (list->prev)
list = list->prev;
str = (char *)ft_strnew(sizeof(char) * (ft_dlst_size(list) + 2));
/* list = ft_dlst_last(list); */
while (list)
{
ft_strcat(str, (char *)list->content);
list = list->next;
}
/* str = ft_strrev(str); */
return (str);
}

View file

@ -1,4 +1,4 @@
#include "libft.h"
#include "getnextline.h"
static char *ft_realloc(char *line, int size)
{
@ -23,9 +23,6 @@ int get_next_line(int const fd, char **line)
*line = ft_strcpy(*line, save);
if ((pos = ft_strchr(*line, '\n')))
{
/* printf("found \\n in save\n"); */
/* fflush(stdout); */
/* ft_putstr(save); */
ft_strcpy(save, pos + 1);
*pos = '\0';
return (1);
@ -33,8 +30,6 @@ int get_next_line(int const fd, char **line)
while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
{
buf[ret] = '\0';
/* ft_putstr(buf); */
/* ft_putchar('/'); */
if ((pos = ft_strchr(buf, '\n')))
{
ft_strcpy(save, pos + 1);

View file

@ -1,10 +0,0 @@
#include "libft.h"
void ft_lstadd(t_list **alst, t_list *new)
{
if (new)
{
new->next = *alst;
*alst = new;
}
}

View file

@ -1,6 +1,7 @@
#include "libft.h"
void ft_putchar(char c)
int ft_putchar(int c)
{
write(1, &c, 1);
return (0);
}

View file

@ -0,0 +1,13 @@
#include "libft.h"
void ft_sstrdel(char **sstr, int index)
{
int i;
i = index;
while (sstr[i])
{
sstr[i] = sstr[i + 1];
i++;
}
}

View file

@ -10,10 +10,7 @@
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#define SEP(x) (x==' ' || x=='\n' || x=='\t')
#include "libft.h"
char **alloc_table(char **table, char *str)
{
@ -22,19 +19,19 @@ char **alloc_table(char **table, char *str)
i = 0;
n_words = 0;
while (SEP(str[i]))
while (FT_WS(str[i]))
i++;
while (str[i] != '\0')
{
i++;
if (SEP(str[i]))
if (FT_WS(str[i]))
{
n_words++;
while (SEP(str[i]))
while (FT_WS(str[i]))
i++;
}
}
if (!SEP(str[i - 1]))
if (!FT_WS(str[i - 1]))
n_words++;
table = (char**)malloc(sizeof(*table) * (n_words + 1));
table[n_words] = 0x0;
@ -50,17 +47,17 @@ char **alloc_words(char **table, char *str)
i = 0;
j = 0;
k = 0;
while (SEP(str[i]))
while (FT_WS(str[i]))
i++;
while (str[i] != '\0')
{
i++;
if (SEP(str[i]) || str[i] == '\0')
if (FT_WS(str[i]) || str[i] == '\0')
{
table[j] = (char*)malloc(sizeof(**table) * (k + 1));
j++;
k = 0;
while (SEP(str[i]))
while (FT_WS(str[i]))
i++;
}
k++;
@ -77,19 +74,19 @@ char **fill_table(char **table, char *str)
i = 0;
j = 0;
k = 0;
while (SEP(str[i]))
while (FT_WS(str[i]))
i++;
while (str[i] != '\0')
{
table[j][k] = str[i];
i++;
k++;
if (SEP(str[i]))
if (FT_WS(str[i]))
{
table[j][k] = '\0';
j++;
k = 0;
while (SEP(str[i]))
while (FT_WS(str[i]))
i++;
}
}

12
libft/src/str/ft_strcut.c Normal file
View file

@ -0,0 +1,12 @@
#include "libft.h"
char *ft_strcut(char *str, char *cut)
{
char *target;
while ((target = ft_strstr(str, cut)))
{
ft_strcpy(target, target + ft_strlen(cut));
}
return (str);
}

View file

@ -2,6 +2,7 @@
void ft_strdel(char **as)
{
if (as)
free(*as);
*as = NULL;
}

View file

@ -85,6 +85,8 @@ char **ft_strsplit(char const *s, char c)
{
char **table;
if (!s)
return (NULL);
table = 0;
table = alloc_table(table, s, c);
table = alloc_words(table, s, c);