merge sort in libft library (partialy copied :)) and new src files)
This commit is contained in:
parent
d07491045d
commit
1a9ab90258
8 changed files with 203 additions and 109 deletions
|
@ -8,7 +8,7 @@ link_directories(libft) # libraries
|
|||
|
||||
set(SOURCE_FILES
|
||||
src/main.c
|
||||
) # sources
|
||||
src/ft_ls.c src/parse_input.c src/flags.c src/sort.c src/output.c libft/ft_lst_merge_sort.c) # sources
|
||||
|
||||
add_executable(ft_ls ${SOURCE_FILES}) # compilation
|
||||
|
||||
|
|
75
libft/ft_lst_merge_sort.c
Normal file
75
libft/ft_lst_merge_sort.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include "libft.h"
|
||||
|
||||
t_list *SortedMerge(t_list *a, t_list *b)
|
||||
{
|
||||
t_list *result;
|
||||
|
||||
result = NULL;
|
||||
|
||||
if (a == NULL)
|
||||
return(b);
|
||||
else if (b==NULL)
|
||||
return(a);
|
||||
|
||||
/* Pick either a or b, and recur */
|
||||
if (a->data <= b->data)
|
||||
{
|
||||
result = a;
|
||||
result->next = SortedMerge(a->next, b);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = b;
|
||||
result->next = SortedMerge(a, b->next);
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
|
||||
void split(t_list *source, t_list **front_ptr, t_list **back_ptr)
|
||||
{
|
||||
t_list *fast;
|
||||
t_list *slow;
|
||||
|
||||
if (source==NULL || source->next==NULL)
|
||||
{
|
||||
*front_ptr = source;
|
||||
*back_ptr = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
slow = source;
|
||||
fast = source->next;
|
||||
while (fast != NULL)
|
||||
{
|
||||
fast = fast->next;
|
||||
if (fast != NULL)
|
||||
{
|
||||
slow = slow->next;
|
||||
fast = fast->next;
|
||||
}
|
||||
}
|
||||
|
||||
*front_ptr = source;
|
||||
*back_ptr = slow->next;
|
||||
slow->next = NULL;
|
||||
}
|
||||
}
|
||||
void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)())
|
||||
{
|
||||
t_list *head;
|
||||
t_list *a;
|
||||
t_list *b;
|
||||
|
||||
head = *head_ptr;
|
||||
if ((head == NULL) || (head->next == NULL))
|
||||
return;
|
||||
|
||||
split(head, &a, &b);
|
||||
|
||||
/* Recursively sort the sublists */
|
||||
ft_lst_merge_sort(&a, cmp);
|
||||
ft_lst_merge_sort(&b, cmp);
|
||||
|
||||
/* answer = merge the two sorted lists together */
|
||||
*head_ptr = SortedMerge(a, b);
|
||||
}
|
22
src/flags.c
Normal file
22
src/flags.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
void set_flags_to_zero(t_flags *flgs)
|
||||
{
|
||||
flgs->a = 0;
|
||||
flgs->l = 0;
|
||||
flgs->R = 0;
|
||||
flgs->t = 0;
|
||||
flgs->col = 0;
|
||||
}
|
||||
|
||||
int chck_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
if ((!*(flg + 1)) && (flg += 1))
|
||||
return (1);
|
||||
if (flgs->l == 0) flgs->l = (ft_strchr(flg, 'l') != NULL) ? 1 : 0;
|
||||
if (flgs->a == 0) flgs->a = (ft_strchr(flg, 'a') != NULL) ? 1 : 0;
|
||||
if (flgs->R == 0) flgs->R = (ft_strchr(flg, 'R') != NULL) ? 1 : 0;
|
||||
if (flgs->t == 0) flgs->t = (ft_strchr(flg, 't') != NULL) ? 1 : 0;
|
||||
if (flgs->col == 0) flgs->col = (ft_strchr(flg, '1') != NULL) ? 1 : 0;
|
||||
return (0);
|
||||
}
|
47
src/ft_ls.c
Normal file
47
src/ft_ls.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
void put_error(char *arg)
|
||||
{
|
||||
char *final_str;
|
||||
char *arg_colon_errno;
|
||||
char *colon_errno;
|
||||
|
||||
colon_errno = ft_strjoin(": ", strerror(errno));
|
||||
arg_colon_errno = ft_strjoin(arg, colon_errno);
|
||||
final_str = ft_strjoin("ls: ", arg_colon_errno);
|
||||
ft_putstr(final_str);
|
||||
ft_putstr("\n");
|
||||
free(final_str);
|
||||
free(arg_colon_errno);
|
||||
free(colon_errno);
|
||||
}
|
||||
|
||||
void init_dir_ent_list(DIR *dp, t_list **del, t_flags *flgs)
|
||||
{
|
||||
t_dir_entity de;
|
||||
|
||||
|
||||
while ((de.dirent = readdir(dp)) && (de.flags = flgs))
|
||||
ft_lstadd(del, ft_lstnew(&de, sizeof(t_dir_entity)));
|
||||
}
|
||||
|
||||
void ft_ls(char *path, t_flags *flgs)
|
||||
{
|
||||
DIR *dp;
|
||||
// int blocks;
|
||||
// char *tmp_path;
|
||||
t_list *dir_ent_list;
|
||||
|
||||
// tmp_path = 0;
|
||||
// blocks = 0;
|
||||
dir_ent_list = NULL;
|
||||
if (!(dp = opendir(path)) || open(path, O_RDONLY) == -1)
|
||||
put_error(path);
|
||||
else
|
||||
{
|
||||
init_dir_ent_list(dp, &dir_ent_list, flgs);
|
||||
sort(dir_ent_list, flgs);
|
||||
output(dir_ent_list);
|
||||
}
|
||||
|
||||
}
|
108
src/main.c
108
src/main.c
|
@ -12,114 +12,6 @@
|
|||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void put_error(char *arg)
|
||||
{
|
||||
char *final_str;
|
||||
char *arg_colon_errno;
|
||||
char *colon_errno;
|
||||
|
||||
colon_errno = ft_strjoin(": ", strerror(errno));
|
||||
arg_colon_errno = ft_strjoin(arg, colon_errno);
|
||||
final_str = ft_strjoin("ls: ", arg_colon_errno);
|
||||
ft_putstr(final_str);
|
||||
ft_putstr("\n");
|
||||
free(final_str);
|
||||
free(arg_colon_errno);
|
||||
free(colon_errno);
|
||||
}
|
||||
|
||||
int chck_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
if ((!*(flg + 1)) && (flg += 1))
|
||||
return (1);
|
||||
if (flgs->l == 0) flgs->l = (ft_strchr(flg, 'l') != NULL) ? 1 : 0;
|
||||
if (flgs->a == 0) flgs->a = (ft_strchr(flg, 'a') != NULL) ? 1 : 0;
|
||||
if (flgs->R == 0) flgs->R = (ft_strchr(flg, 'R') != NULL) ? 1 : 0;
|
||||
if (flgs->t == 0) flgs->t = (ft_strchr(flg, 't') != NULL) ? 1 : 0;
|
||||
if (flgs->col == 0) flgs->col = (ft_strchr(flg, '1') != NULL) ? 1 : 0;
|
||||
return (0);
|
||||
}
|
||||
|
||||
void init_dir_ent_list(DIR *dp, t_list **del, t_flags *flgs)
|
||||
{
|
||||
t_dir_entity de;
|
||||
|
||||
|
||||
while ((de.dirent = readdir(dp)) && (de.flags = flgs))
|
||||
ft_lstadd(del, ft_lstnew(&de, sizeof(t_dir_entity)));
|
||||
}
|
||||
|
||||
void column_out(t_list *entity)
|
||||
{
|
||||
t_dir_entity *d_ent;
|
||||
|
||||
d_ent = (t_dir_entity *)entity->content;
|
||||
if (d_ent->flags->a)
|
||||
printf("%s\t", d_ent->dirent->d_name);
|
||||
else
|
||||
if (*d_ent->dirent->d_name != '.')
|
||||
printf("%s\t\n", d_ent->dirent->d_name);
|
||||
}
|
||||
|
||||
void output(t_list *del)
|
||||
{
|
||||
ft_lstiter(del, column_out);
|
||||
}
|
||||
|
||||
void ft_ls(char *path, t_flags *flgs)
|
||||
{
|
||||
DIR *dp;
|
||||
// int blocks;
|
||||
// char *tmp_path;
|
||||
t_list *dir_ent_list;
|
||||
|
||||
// tmp_path = 0;
|
||||
// blocks = 0;
|
||||
dir_ent_list = NULL;
|
||||
if (!(dp = opendir(path)) || open(path, O_RDONLY) == -1)
|
||||
put_error(path);
|
||||
else
|
||||
{
|
||||
init_dir_ent_list(dp, &dir_ent_list, flgs);
|
||||
output(dir_ent_list);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void set_flags_to_zero(t_flags *flgs)
|
||||
{
|
||||
flgs->a = 0;
|
||||
flgs->l = 0;
|
||||
flgs->R = 0;
|
||||
flgs->t = 0;
|
||||
flgs->col = 0;
|
||||
}
|
||||
|
||||
void parse_input(int ac, char **av, t_flags *flgs)
|
||||
{
|
||||
int i;
|
||||
int paths;
|
||||
|
||||
i = 0;
|
||||
set_flags_to_zero(flgs);
|
||||
if (ac == 1)
|
||||
ft_ls(".", flgs);
|
||||
else if (ac > 1)
|
||||
{
|
||||
while (i < ac - 1 && *av[i + 1] == '-')
|
||||
if (chck_flgs(av[i + 1], flgs))
|
||||
break ;
|
||||
else
|
||||
i++;
|
||||
paths = i;
|
||||
if (paths == ac - 1)
|
||||
ft_ls("./", flgs);
|
||||
else
|
||||
while (paths < ac - 1)
|
||||
ft_ls(av[paths++ + 1], flgs);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// DIR *dp;
|
||||
|
|
18
src/output.c
Normal file
18
src/output.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
void column_out(t_list *entity)
|
||||
{
|
||||
t_dir_entity *d_ent;
|
||||
|
||||
d_ent = (t_dir_entity *)entity->content;
|
||||
if (d_ent->flags->a)
|
||||
printf("%s\t", d_ent->dirent->d_name);
|
||||
else
|
||||
if (*d_ent->dirent->d_name != '.')
|
||||
printf("%s\t\n", d_ent->dirent->d_name);
|
||||
}
|
||||
|
||||
void output(t_list *del)
|
||||
{
|
||||
ft_lstiter(del, column_out);
|
||||
}
|
26
src/parse_input.c
Normal file
26
src/parse_input.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
void parse_input(int ac, char **av, t_flags *flgs)
|
||||
{
|
||||
int i;
|
||||
int paths;
|
||||
|
||||
i = 0;
|
||||
set_flags_to_zero(flgs);
|
||||
if (ac == 1)
|
||||
ft_ls(".", flgs);
|
||||
else if (ac > 1)
|
||||
{
|
||||
while (i < ac - 1 && *av[i + 1] == '-')
|
||||
if (chck_flgs(av[i + 1], flgs))
|
||||
break ;
|
||||
else
|
||||
i++;
|
||||
paths = i;
|
||||
if (paths == ac - 1)
|
||||
ft_ls("./", flgs);
|
||||
else
|
||||
while (paths < ac - 1)
|
||||
ft_ls(av[paths++ + 1], flgs);
|
||||
}
|
||||
}
|
14
src/sort.c
Normal file
14
src/sort.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
|
||||
|
||||
void sort_by(t_list **del, int (*f)())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void sort(t_list *del, t_flags *flgs)
|
||||
{
|
||||
if (!flgs->t)
|
||||
sort_by(&del, lex);
|
||||
}
|
Loading…
Add table
Reference in a new issue