Browse Source

[mod] add non used code for cote parsing.

Moul 2 years ago
parent
commit
1dc5e1f084
1 changed files with 117 additions and 0 deletions
  1. 117 0
      non_use_src/cotes_parsing.c

+ 117 - 0
non_use_src/cotes_parsing.c

@@ -0,0 +1,117 @@
+#include "sh.h"
+
+/*
+ * ** cotes_parsing.c
+ * */
+t_cotes *init_variables(t_cotes *c);
+void    replace_spaces_between_cotes(t_e *e, char *line);
+void    detect_coted_blocks(t_e *e, t_cotes *c, char *line);
+char    *replace_ack_by_space(char *cmd);
+
+————————————————————————————————————————
+
+t_cotes		*init_variables(t_cotes *c)
+{
+	c = (t_cotes*)malloc(sizeof(t_cotes));
+	c->cotes = 0;
+	c->pos_l = 0;
+	c->pos_c = 0;
+	return (c);
+}
+
+/*
+** Replace spaces between cotes by 'ack'/6 char
+** Allocate a new string
+** If found opening cote:
+**  - If found opening and closing cotes:
+** 	- copy string and replace ' ' by 6, don't copy parameters
+**  - If end of block: put close at 0
+** Else: simply copy char
+** free struct
+** launch comma split and all sub-functions
+*/
+
+void	replace_spaces_between_cotes(t_e *e, char *line)
+{
+	char	*cmd;
+	t_cotes *c;
+	int		i;
+
+	c = init_variables(c);
+	cmd = ft_strnew(ft_strlen(line));
+	while (line[c->pos_l] != '\0')
+	{
+		i = -1;
+		while (e->par_open[++i] != '\0')
+			if (line[c->pos_l] == e->par_open[i])
+			{
+				detect_coted_blocks(e, c, line);
+				if (c->cotes == 1 && line[c->pos_l++] == ' ')
+					cmd[c->pos_c++] = 6;
+				else if (c->cotes == 1 && (line[c->pos_l] == \
+				e->par_open[c->j] || line[c->pos_l] == e->par_close[c->j]))
+					if (line[++c->pos_l] == e->par_open[c->j])
+					   c->cotes = 0;	
+			}
+		cmd[c->pos_c++] = line[c->pos_l++];
+	}
+	free(c);
+	e->cmd = line;
+//ft_printf("|%s|\n", e->cmd); // debug
+	comma_split(e, e->cmd);
+}
+
+/*
+** for every coted types:
+** - if there is an open and close coted of same type:
+**  - change position to first cote, set it's a coted block, break 
+*/
+
+void	detect_coted_blocks(t_e *e, t_cotes *c, char *line)
+{
+	int		first;
+
+	c->j = -1;
+	while (e->par_open[++c->j])
+		if ((first = \
+		ft_search_pos_char(&line[c->pos_l], e->par_open[c->j])) != -1 &&\
+		ft_search_pos_char(&line[c->pos_l + first + 1], e->par_close[c->j]) != -1)
+		{
+			c->pos_l += first;
+			c->cotes = 1;
+			break ;
+		}
+}
+
+/*
+** After spliting at space and tab:
+** Replace 6 char by spaces
+*/
+
+char	*replace_ack_by_space(char *cmd)
+{
+	int		pos;
+
+	pos  = -1;
+	while (cmd[++pos])
+		if (cmd[pos] == 6)
+			cmd[pos] = ' ';
+	return (cmd);
+}
+
+——————————————————————
+
+/*
+** Struct
+** cotes: bollean if there is cotes at current position
+** pos_l: position on line
+** pos_c: position on cmd
+** j: position on parameters lists
+*/
+
+typedef struct          s_cotes {
+	int					cotes;
+	int                 pos_l;
+	int                 pos_c;
+	int                 j;
+}                       t_cotes;