Exemple #1
0
char		*extract_parameters(int input, char *matches, int ln)
{
	char		buf[1];
	t_list		*list;

	list = NULL;
	while ((input != 0 || ln < 2) && read(input, buf, 1) > 0)
	{
		if (buf[0] == '\n' && ln > 0 && input == 0)
			ln++;
		else if (buf[0] == '\n')
		{
			ft_lstpush(&list, buf, 1);
			ln++;
		}
		else
		{
			ft_lstpush(&list, buf, 1);
			ln = 0;
		}
	}
	if (!(matches = (char*)malloc(sizeof(char) * ft_lstsize(list))))
		return (NULL);
	matches = ft_lsttochar(list, matches);
	matches[ft_lstsize(list)] = '\0';
	free(list);
	return (matches);
}
Exemple #2
0
void	ft_lstadd_rev(t_lst **top, t_lst *elem, int (*cmp)(t_lst*, t_lst*))
{
	t_lst	*tmp;
	t_lst	*prev;

	if (!*top)
		ft_lstpush(top, elem);
	else
	{
		prev = *top;
		if ((*cmp)(prev, elem) >= 0)
		{
			elem->next = *top;
			*top = elem;
			return ;
		}
		tmp = (*top)->next;
		while (tmp)
		{
			if ((*cmp)(tmp, elem) >= 0)
				break ;
			prev = tmp;
			tmp = tmp->next;
		}
		elem->next = tmp;
		prev->next = elem;
	}
}
Exemple #3
0
int			read_tetri(int fd, t_list **tetris)
{
	size_t	i;
	t_tetri	*tetri;
	char	line[21];
	char	*buf;
	int		r_result;

	tetri = ft_memalloc(sizeof(*tetri));
	ft_bzero(line, 21);
	if ((r_result = read(fd, line, 21)) == -1 || !valid_line(line))
		return (-1);
	line[20] = '\0';
	while ((buf = ft_strchr(line, '#')))
	{
		*buf = '.';
		i = buf - line - 4;
		while ((buf = ft_strchr(buf, '\n')) && buf++)
			i++;
		tetri->value |= 1 << i;
	}
	shift_topleft(&tetri->value);
	if (!valid_tetri(tetri->value))
		return (-1);
	count_lines_columns(tetri);
	ft_lstpush(tetris, tetri, sizeof(*tetri));
	return (r_result == 21);
}
Exemple #4
0
int					ft_push_cmd(t_env *env, int cmd_num, char *opt, int resp)
{
	t_cmd			cmd;

	cmd.cmd = ft_strdup(env->cmds[cmd_num]);
	cmd.opt = opt;
	cmd.resp = resp;
	ft_lstpush(&env->moves, ft_lstnew(&cmd, sizeof(t_cmd)));
	return (OK);
}
void	ft_lstpushpop(t_list **a, t_list **b)
{
	t_list	*temp;

	if (*a)
	{
		temp = ft_lstpop(a);
		if (temp != NULL)
			ft_lstpush(b, temp);
	}
}
Exemple #6
0
static int	lst_add_elem(t_list **lst, int type, int *nb)
{
	t_list	*tmp;

	if (!(tmp = ft_lstnew(&type, sizeof(int))))
	{
		ft_lstdel(lst, &my_lstdel);
		return (0);
	}
	ft_lstpush(lst, tmp);
	(*nb)++;
	return (1);
}
Exemple #7
0
t_list	*ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
	t_list	*ret;
	t_list	*tmp;

	ret = NULL;
	while (lst != NULL)
	{
		if (!(tmp = ft_lstnew(lst->content, lst->content_size)))
			return (NULL);
		ft_lstpush(&ret, (*f)(tmp));
		lst = lst->next;
		free(tmp);
	}
	return (ret);
}
Exemple #8
0
void	ft_lstaddi(t_lst **top, t_lst *elem, t_uint pos)
{
	t_uint	i;
	t_lst	*tmp;

	if (!*top || pos == 0)
		ft_lstpush(top, elem);
	else
	{
		i = 0;
		tmp = *top;
		while (i < pos && tmp->next)
			tmp = tmp->next, ++i;
		elem->next = tmp->next;
		tmp->next = elem;
	}
}
Exemple #9
0
void				ft_treat_fork(t_env *env, int cs, char *rcv)
{
	t_egg			new_egg;
	char			*line;
	char			*itoa;

	(void)rcv;
	line = NULL;
	env->count_egg += 1;
	itoa = ft_itoa(env->count_egg);
	ft_action_tm(-1, 600, ft_treat_egg, itoa);
	ft_memdel((void **)&itoa);
	new_egg = st_fill_egg(env, cs);
	ft_lstpush(&env->eggs, ft_lstnew(&new_egg, sizeof(new_egg)));
	line = ft_graphic_enw(env, cs, env->count_egg);
	if (env->graphic != -1)
		ft_reply_in_buff(env, env->graphic, line);
	ft_memdel((void **)&line);
	ft_reply_in_buff(env, cs, "ok");
}
Exemple #10
0
void				ft_action_tm(int cs, int t, void (*fn)(), char *rcv)
{
	t_action		new_action;
	long int		tstmp;
	long int		diff;
	struct timeval	tv;
	t_env			*env;

	env = get_env();
	gettimeofday(&tv, NULL);
	tstmp = (1000000 * tv.tv_sec + tv.tv_usec) + ((t / env->time) * 1000000);
	new_action.cs = cs;
	new_action.rcv = ft_strdup(rcv);
	new_action.fn = fn;
	diff = env->fd_socket[cs].last_cmd - (1000000 * tv.tv_sec + tv.tv_usec);
	if (diff > 0)
		tstmp = tstmp + diff;
	new_action.timestamp = tstmp;
	ft_lstpush(&env->actions, ft_lstnew(&new_action, sizeof(new_action)));
}
Exemple #11
0
void				ft_snapshot(t_env *env, int cs, int stuff[NB_STUFF])
{
	t_snapshot	snapshot;
	char		*str;

	snapshot.master = cs;
	snapshot.pos.x = POSX(cs);
	snapshot.pos.y = POSY(cs);
	snapshot.players = ft_get_players_same_level(env, cs);
	snapshot.stuff[PLAYERS] = stuff[PLAYERS];
	snapshot.stuff[LINEMATE] = stuff[LINEMATE];
	snapshot.stuff[DERAUMERE] = stuff[DERAUMERE];
	snapshot.stuff[SIBUR] = stuff[SIBUR];
	snapshot.stuff[MENDIANE] = stuff[MENDIANE];
	snapshot.stuff[PHIRAS] = stuff[PHIRAS];
	snapshot.stuff[THYSTAME] = stuff[THYSTAME];
	ft_lstpush(&env->elevation, ft_lstnew(&snapshot, sizeof(snapshot)));
	asprintf(&str, "%d %d %d", POSX(cs), POSY(cs), env->fd_socket[cs].level);
	st_inform_players(env, cs);
	ft_action_special(cs, 300, ft_check_incantation, str);
	ft_memdel((void **)&str);
}
t_list	*ft_lstfrom_mapkeys_byvalue(t_map *map, t_data *value)
{
	t_data	tmp;
	t_list	*lst;
	t_list	*add;

	if (!value)
		return (NULL);
	lst = NULL;
	while (map)
	{
		tmp.data = ((t_list*)map->value)->content;
		tmp.data_size = ((t_list*)map->value)->content_size;
		if (!ft_datacmp(value, &tmp))
		{
			add = (t_list*)map->key;
			add = ft_lstnew(add->content, add->content_size);
			ft_lstpush(&lst, add);
		}
		map = map->next;
	}
	return (lst);
}
t_list			*ft_lstfrom_void_tab(const void *tab, size_t tab_size,
		size_t content_size)
{
	int		i;
	t_list	*lst;
	t_list	*add;
	char	*tmp;
	int		size;

	if (!tab || !content_size)
		return (NULL);
	i = -1;
	lst = NULL;
	size = tab_size / content_size;
	while (++i < size)
	{
		if (!(tmp = ft_strdup(tab + i * content_size)))
			return (free_lst(lst));
		if (!(add = ft_lstnew(tmp, ft_strlen(tmp))))
			return (free_lst(lst));
		ft_lstpush(&lst, add);
	}
	return (lst);
}
Exemple #14
0
void			print_ls_l(t_dir_item *items, char *params, int only_dirs)
{
    int		*spaces;
    int		total;
    t_list	*container;

    container = NULL;
    total = 0;
    while (items)
    {
        ft_lstpush(&container, ft_lstnew(print_list_item(items, &total, params),
                                         sizeof(t_litem)));
        items = items->next;
    }
    if (container && only_dirs)
    {
        ft_putstr("total ");
        ft_putnbr((total));
        ft_putchar('\n');
    }
    spaces = max_size_elem(container);
    print_padded_item(container, spaces);
    free(spaces);
}
Exemple #15
0
void	ft_lstcat(t_list **alst, t_list *other)
{
	ft_lstpush(alst, other);
}