Skip to content

Commit

Permalink
Merge pull request #81 from mingxoxo/78-implement-와일드카드-검색패턴-적용
Browse files Browse the repository at this point in the history
Implement : #78/와일드카드 검색패턴 적용
  • Loading branch information
wonyangs authored Jan 16, 2023
2 parents 7c8f040 + 5937862 commit 3fa87f0
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 21 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: wonyang <wonyang@student.42seoul.kr> +#+ +:+ +#+ #
# By: jeongmin <jeongmin@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/28 21:52:18 by wonyang #+# #+# #
# Updated: 2023/01/16 17:40:57 by wonyang ### ########seoul.kr #
# Updated: 2023/01/16 20:59:55 by jeongmin ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -78,7 +78,8 @@ _PARSING_SRCS = token.c \
subst_env_lst.c \
subst_env_second.c \
subst_wildcard.c \
del_quote.c
del_quote.c \
wildcard.c

PARSING_SRCS = $(addprefix $(PARSING_DIR), $(_PARSING_SRCS))

Expand Down
4 changes: 3 additions & 1 deletion ds_tree/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jeongmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/08 23:33:50 by wonyang #+# #+# */
/* Updated: 2023/01/11 17:57:31 by jeongmin ### ########.fr */
/* Updated: 2023/01/16 19:04:02 by jeongmin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,6 +19,7 @@ t_tnode *add_lchild(t_tnode *node, void *content)
left = init_node(content);
if (!left)
return (NULL);
set_lchild(left, node->left);
set_lchild(node, left);
return (left);
}
Expand All @@ -30,6 +31,7 @@ t_tnode *add_rchild(t_tnode *node, void *content)
right = init_node(content);
if (!right)
return (NULL);
set_rchild(right, node->right);
set_rchild(node, right);
return (right);
}
14 changes: 9 additions & 5 deletions ds_tree/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jeongmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/08 23:33:50 by wonyang #+# #+# */
/* Updated: 2023/01/11 18:10:46 by jeongmin ### ########.fr */
/* Updated: 2023/01/16 19:03:45 by jeongmin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,12 +35,16 @@ void set_content(t_tnode *node, void *content)

void set_lchild(t_tnode *node, t_tnode *left)
{
node->left = left;
left->parent = node;
if (node)
node->left = left;
if (left)
left->parent = node;
}

void set_rchild(t_tnode *node, t_tnode *right)
{
node->right = right;
right->parent = node;
if (node)
node->right = right;
if (right)
right->parent = node;
}
7 changes: 6 additions & 1 deletion includes/make_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jeongmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/10 16:01:53 by jeongmin #+# #+# */
/* Updated: 2023/01/16 17:11:07 by jeongmin ### ########.fr */
/* Updated: 2023/01/16 20:26:51 by jeongmin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,6 +20,7 @@

# define OPEN_PAREN 1
# define CLOSE_PAREN -1
# define WILDCARD -42

// make_tree.c
t_tnode *make_tree(t_list *head);
Expand Down Expand Up @@ -59,6 +60,10 @@ t_error subst_env_lst(t_token *token, int *arr, t_list **head);

// subst_wildcard.c
t_error subst_wildcard(t_tnode *node);
void resubst_wildcard(char *str);

// wildcard.c
t_error wildcard(t_tnode *node);

// del_quote.c
t_error del_quote(t_tnode *node);
Expand Down
22 changes: 18 additions & 4 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jeongmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/15 17:20:31 by wonyang #+# #+# */
/* Updated: 2023/01/16 17:01:47 by jeongmin ### ########.fr */
/* Updated: 2023/01/16 20:58:35 by jeongmin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,6 +31,19 @@ static void del_t_paren(void *content)
content = NULL;
}

static t_error post_processing(t_tnode *node)
{
if (!node)
return (ERROR);
if (subst(node) == ERROR)
return (ERROR);
if (del_quote(node) == ERROR)
return (ERROR);
if (wildcard(node) == ERROR)
return (ERROR);
return (SCS);
}

t_tnode *parse_line(char *str)
{
t_list *lst;
Expand All @@ -44,15 +57,16 @@ t_tnode *parse_line(char *str)
}
if (!is_correct_syntax(lst->next))
{
ft_lstclear(&lst, del_t_token);
ft_lstclear(&lst, &del_t_token);
free(str);
return (NULL);
}
node = make_tree(lst->next);
ft_lstclear(&lst, del_t_paren);
if (subst(node) == ERROR || del_quote(node) == ERROR || !node)
ft_lstclear(&lst, &del_t_paren);
if (post_processing(node) == ERROR)
{
free(str);
clear_node(node, del_t_token);
return (NULL);
}
return (node);
Expand Down
10 changes: 4 additions & 6 deletions parsing/subst.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jeongmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 17:42:37 by jeongmin #+# #+# */
/* Updated: 2023/01/16 17:06:55 by jeongmin ### ########.fr */
/* Updated: 2023/01/16 18:56:34 by jeongmin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,8 +38,6 @@ static bool is_wildcard_in_cmd(t_tnode *node)

t_error subst(t_tnode *node)
{
t_error errno;

if (!node)
return (SCS);
if (is_this_symbol(node->content, T_WORD))
Expand All @@ -52,7 +50,7 @@ t_error subst(t_tnode *node)
return (ERROR);
return (SCS);
}
errno = subst(node->left);
errno = subst(node->right);
return (errno);
if (subst(node->left) == ERROR || subst(node->right) == ERROR)
return (ERROR);
return (SCS);
}
12 changes: 11 additions & 1 deletion parsing/subst_wildcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jeongmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 17:42:37 by jeongmin #+# #+# */
/* Updated: 2023/01/16 17:12:43 by jeongmin ### ########.fr */
/* Updated: 2023/01/16 20:27:17 by jeongmin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,3 +38,13 @@ t_error subst_wildcard(t_tnode *node)
}
return (SCS);
}

void resubst_wildcard(char *str)
{
while (*str)
{
if (*str == WILDCARD)
*str *= -1;
str++;
}
}
143 changes: 143 additions & 0 deletions parsing/wildcard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wildcard.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jeongmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 17:42:37 by jeongmin #+# #+# */
/* Updated: 2023/01/16 20:55:23 by jeongmin ### ########.fr */
/* */
/* ************************************************************************** */

#include "make_tree.h"
#include <dirent.h>
#include <errno.h>

static t_token *make_node_token(char *line, t_ttype type)
{
t_token *token;

token = (t_token *)malloc(sizeof(t_token));
if (!token)
return (NULL);
token->str = ft_strdup(line);
if (!token->str)
{
free(token);
return (NULL);
}
token->type = type;
return (token);
}

static bool match_wcard(char *pattern, char *dname)
{
int len_p;
int len_w;
int nth;
int skip;

nth = 0;
len_p = ft_strlen(pattern);
len_w = ft_strlen(dname);
while (nth < len_p && nth < len_w && (pattern[nth] == dname[nth]))
nth++;
if (len_p == nth)
return (nth == len_w);
if (pattern[nth] == WILDCARD)
{
skip = 0;
while (skip + nth <= len_w)
{
if (match_wcard(pattern + nth + 1, dname + skip + nth))
return (true);
skip += 1;
}
}
return (false);
}

static t_error add_wcard(t_tnode **node, t_token *token, int *cnt, char *dname)
{
char *new_str;
t_token *new;

if (*cnt == 0)
{
new_str = ft_strdup(dname);
if (new_str == NULL)
return (ERROR);
free(token->str);
token->str = new_str;
}
else
{
new = make_node_token(dname, T_WORD);
if (!new)
return (ERROR);
*node = add_lchild(*node, new);
if (!(*node))
return (ERROR);
}
(*cnt)++;
return (SCS);
}

static t_error check_wcard(t_tnode *node, t_token *token, char *str)
{
int cnt;
DIR *dir;
struct dirent *dirent;

cnt = 0;
dir = opendir(".");
if (!dir)
return (ERROR);
while (1)
{
dirent = readdir(dir);
if (!dirent)
break ;
if (match_wcard(str, dirent->d_name)
&& add_wcard(&node, token, &cnt, dirent->d_name) == ERROR)
{
closedir(dir);
return (ERROR);
}
}
if (!cnt)
resubst_wildcard(token->str);
if (closedir(dir) == -1 || errno)
return (ERROR);
return (SCS);
}

t_error wildcard(t_tnode *node)
{
char *str;
t_token *token;

if (!node)
return (SCS);
if (is_this_symbol(node->content, T_WORD))
{
if (wildcard(node->left) == ERROR)
return (ERROR);
token = (t_token *)(node->content);
if (!ft_strchr(token->str, WILDCARD))
return (SCS);
str = ft_strdup(token->str);
if (!str || check_wcard(node, token, str) == ERROR)
{
free(str);
return (ERROR);
}
free(str);
return (SCS);
}
free(NULL);
if (wildcard(node->left) == ERROR || wildcard(node->right) == ERROR)
return (ERROR);
return (SCS);
}

0 comments on commit 3fa87f0

Please sign in to comment.