-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from mingxoxo/78-implement-와일드카드-검색패턴-적용
Implement : #78/와일드카드 검색패턴 적용
- Loading branch information
Showing
8 changed files
with
198 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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)) | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |