/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   glob.h                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: wescande <wescande@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/01/04 16:31:18 by wescande          #+#    #+#             */
/*   Updated: 2017/02/20 19:03:45 by wescande         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef GLOB_H
# define GLOB_H

# include "minishell.h"

# define CH(x) ((char **)(x)->content)
# define UCH(x) ((unsigned char **)(x)->content)
# define SCH(x) ((char **)(x).content)
# define SUCH(x) ((unsigned char **)(x).content)

typedef struct	s_ld
{
	void			*content;
	struct s_ld		*next;
	struct s_ld		*prev;
}				t_ld;

typedef struct	s_glob
{
	short int			found;
	short int			cur_dir;
	char				*pat;
	unsigned char		*esc;
	unsigned char		*esc2;
	t_ld				*match;
	t_ld				*m_pat;
}				t_glob;

typedef struct	s_expand
{
	t_ld			**wk;
	char			*str;
	unsigned char	*esc;
	char			**split;
	unsigned char	**m_esc;
	char			*s1;
}				t_expand;

typedef struct	s_bquote
{
	t_ld			**wk;
	char			*str;
	unsigned char	*esc;
	unsigned char	*esc2;
	char			*s1;
	char			*s2;
	char			*mid;
}				t_bquote;
/*
** interface of glob.
*/
char			**glob(char *str, unsigned char *esc,
										unsigned char *dbl_esc);
void	 		esc_print(char *str, unsigned char *esc);

/*
** return TRUE if path file is a directory.
*/

int				is_directory(const char *path);

/*
** return TRUE if char at str_pos in ini_str is escape.
** five the possibility to set if the char is esc or not.
*/
int				is_char_esc(const unsigned char *esc,
							const char *ini_str, const char *str_pos);
void			set_char_esc_mode(unsigned char *esc,
							const char *ini_str, const char *str_pos, int mode);
void			set_char_esc(unsigned char *esc,
							const char *ini_str, const char *str_pos);
void			set_char_no_esc(unsigned char *esc,
							const char *ini_str, const char *str_pos);

/*
** Internal function.
*/
unsigned char	*ft_sub_esc(const unsigned char *esc, int start, int len);
unsigned char	*dup_char_esc(const unsigned char *esc, const int size);
unsigned char	*calc_expand_esc(const unsigned char *esc,
								int nb_start, int *nb_middle, int *nb_end);
void			modify_esc_split(unsigned char *esc_dest,
								unsigned char *esc_src, int start, int len);
void			expand_brace(t_glob *tglob);
void			expand_bquote(t_glob *gl);
void			expand_var(t_glob *tglob);
int				match_pattern(t_glob *tglob, char *str, char *full_word);
int				dir_research(t_glob *tglob, char *p, char *pat, int rec);
char			**gen_tab(const char *pat, const unsigned char *esc,
				const unsigned char *esc2, int dup);
char			**ft_strsplit_spe(const char *str,
								const unsigned char *esc, char c);
unsigned char	**ft_strsplit_esc(const char *str,
								const unsigned char *esc, char c);
/*
** LIST D:
*/
void			ft_ld_new(t_ld **alst, void *content);
t_ld			*ft_ld_front(t_ld	*ld);
void			ft_ld_pushfront(t_ld **alst, void *content);
void			ft_ld_pushback(t_ld **alst, void *content);
size_t			ft_ld_size(t_ld *ld);
void			ft_ld_del(t_ld **ld, void (*del)());
void			ft_ld_clear(t_ld **ld, void (*del)());
void			ft_ld_reverse(t_ld **lst);
t_ld			*ft_ld_back(t_ld *ld);
t_ld			*ft_ld_swap(t_ld *l_cur);
char			**ft_ld_to_tab(t_ld *ld);
t_ld			*ft_ld_order(t_ld *ld, int (*f)(), void (*del)());

/*
** str:
*/

char			*ft_strjoinf(char *str, char *str2, int mode);
char			*ft_strsubf(char *s, unsigned int start,
							size_t len, short int mode);
void			ft_tabdel(char ***mytab);
int				ft_tablen(char **mytab);

void			*ft_memrealloc(void *ptr, size_t old_s, size_t new_s);
#endif
