mise a jour des parentheses/accolades

This commit is contained in:
gwojda 2017-02-04 18:28:05 +01:00
parent 288acbb378
commit c77af3da9f
4 changed files with 87 additions and 27 deletions

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */
/* Updated: 2017/02/04 14:50:04 by gwojda ### ########.fr */ /* Updated: 2017/02/04 18:16:37 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -113,9 +113,17 @@ typedef struct s_prompt_type
char *new_prompt; char *new_prompt;
} t_prompt_type; } t_prompt_type;
typedef struct s_brackets
{
int pos;
char tabl[100];
} t_brackets;
extern t_key g_keys[]; extern t_key g_keys[];
extern t_prompt_type g_prompt_tab[]; extern t_prompt_type g_prompt_tab[];
extern t_brackets g_brackets;
void ft_reset_tab(char *tabl);
void ft_putnc(char c, int n); void ft_putnc(char c, int n);
int ft_nbr_len(int nbr); int ft_nbr_len(int nbr);
void ft_puttermcaps(char *str); void ft_puttermcaps(char *str);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 13:32:52 by gwojda #+# #+# */ /* Created: 2017/01/26 13:32:52 by gwojda #+# #+# */
/* Updated: 2017/02/04 14:51:44 by gwojda ### ########.fr */ /* Updated: 2017/02/04 18:26:59 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,8 @@ t_prompt_type g_prompt_tab[] =
{0 , 0 , 0 }, {0 , 0 , 0 },
}; };
t_brackets g_brackets = {0 , {0}};
void ft_read_more(short c) void ft_read_more(short c)
{ {
char *str_tmp; char *str_tmp;
@ -33,35 +35,35 @@ void ft_read_more(short c)
str_tmp2 = data_singleton()->line.input; str_tmp2 = data_singleton()->line.input;
str_tmp = ft_strjoin(str_tmp2, "\n"); str_tmp = ft_strjoin(str_tmp2, "\n");
free(str_tmp2); free(str_tmp2);
data_singleton()->line.input = NULL; STR = NULL;
data_singleton()->line.pos = 0; POS = 0;
while (g_prompt_tab[i].key && !(g_prompt_tab[i].key & c)) while (g_prompt_tab[i].key && !(g_prompt_tab[i].key & c))
++i; ++i;
data_singleton()->line.prompt_size = g_prompt_tab[i].value; data_singleton()->line.prompt_size = g_prompt_tab[i].value;
ft_printf("\n%s", g_prompt_tab[i].new_prompt); ft_printf("\n%s", g_prompt_tab[i].new_prompt);
str_tmp2 = ft_read_stdin(); str_tmp2 = ft_read_stdin();
str_tmp2 = ft_strjoin(str_tmp, data_singleton()->line.input); str_tmp2 = ft_strjoin(str_tmp, STR);
free(str_tmp); free(str_tmp);
free(data_singleton()->line.input); free(STR);
data_singleton()->line.input = str_tmp2; STR = str_tmp2;
ft_check_line(); ft_check_line();
} }
void ft_check_this_char(char c, short *status) static void ft_check_this_char_quotes(char c, short *status)
{ {
int i; int i;
char *stats; char *stats;
i = 0; i = 0;
stats = "\'`\"{(\\"; stats = "\'`\"\\";
while (stats[i] && stats[i] != c) while (stats[i] && stats[i] != c)
++i; ++i;
if (!stats[i]) if (!stats[i])
return ; return ;
if (((1 << i) & ~(*status))) if ((1 << i) & ~(*status))
{ {
if (((1 << i) > *status && if (((1 << i) > *status && (*status == 0 && !(IS_QUOTES & *status)
(*status == 0 && !(IS_QUOTES & *status) && (!(IS_DQUOTES & *status)))) && (!(IS_DQUOTES & *status))))
|| (((1 << i) == IS_BQUOTES) && !(IS_QUOTES & *status))) || (((1 << i) == IS_BQUOTES) && !(IS_QUOTES & *status)))
(*status) = (*status) | (1 << i); (*status) = (*status) | (1 << i);
} }
@ -69,6 +71,33 @@ void ft_check_this_char(char c, short *status)
(*status) = (*status) ^ (1 << i); (*status) = (*status) ^ (1 << i);
} }
static int ft_brackets(char c)
{
if (c == '(')
{
g_brackets.tabl[g_brackets.pos] = '(';
++g_brackets.pos;
}
else if (c == '{')
{
g_brackets.tabl[g_brackets.pos] = '{';
++g_brackets.pos;
}
else if (c == '}')
{
if (!g_brackets.pos || g_brackets.tabl[g_brackets.pos - 1] != '{')
return (1);
g_brackets.tabl[--g_brackets.pos] = '\0';
}
else if (c == ')')
{
if (!g_brackets.pos || g_brackets.tabl[g_brackets.pos - 1] != '(')
return (1);
g_brackets.tabl[--g_brackets.pos] = '\0';
}
return (0);
}
void ft_check_line(void) void ft_check_line(void)
{ {
int i; int i;
@ -77,7 +106,9 @@ void ft_check_line(void)
i = 0; i = 0;
status = 0; status = 0;
str = data_singleton()->line.input; str = STR;
ft_reset_tab((char *)g_brackets.tabl);
g_brackets.pos = 0;
if (!str) if (!str)
return ; return ;
while (str[i]) while (str[i])
@ -85,9 +116,17 @@ void ft_check_line(void)
if (IS_BSLASH & status) if (IS_BSLASH & status)
status = status ^ IS_BSLASH; status = status ^ IS_BSLASH;
else else
ft_check_this_char(str[i], &status); {
ft_check_this_char_quotes(str[i], &status);
if (!status && ft_brackets(str[i]))
return ;
}
++i; ++i;
} }
if (g_brackets.pos && g_brackets.tabl[0] == '(')
status = status | IS_BRACKET;
else if (g_brackets.pos && g_brackets.tabl[0] == '{')
status = status | IS_ACCOLADE;
if (status) if (status)
ft_read_more(status); ft_read_more(status);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 13:21:40 by gwojda #+# #+# */ /* Created: 2017/01/09 13:21:40 by gwojda #+# #+# */
/* Updated: 2017/02/04 15:36:25 by gwojda ### ########.fr */ /* Updated: 2017/02/04 16:35:33 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,11 +47,12 @@ void ft_up(void)
i = 0; i = 0;
str = data_singleton()->line.input; str = data_singleton()->line.input;
pos = &data_singleton()->line.pos; pos = &data_singleton()->line.pos;
if (!str) if (!str || !*pos)
return ; return ;
if (str[*pos - 1] == '\n') if (str[*pos - 1] == '\n')
{ {
ft_puttermcaps("cd"); ft_puttermcaps("cd");
if (*pos >= 2)
(*pos) -= 2; (*pos) -= 2;
ft_get_beggin(str, pos); ft_get_beggin(str, pos);
ft_current_str(str, *pos); ft_current_str(str, *pos);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/16 17:06:30 by gwojda #+# #+# */ /* Created: 2016/12/16 17:06:30 by gwojda #+# #+# */
/* Updated: 2017/02/04 16:19:49 by gwojda ### ########.fr */ /* Updated: 2017/02/04 18:16:34 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -64,3 +64,15 @@ char *ft_strdupi(char const *s)
} }
return (str); return (str);
} }
void ft_reset_tab(char *tabl)
{
int i;
i = 0;
while (tabl[i])
{
tabl[i] = '\0';
++i;
}
}