~nloomans/ft_select

ref: a4ef762331941c5e33670f7bf81eb964fb761edb ft_select/src/state.c -rw-r--r-- 1.7 KiB
a4ef7623Noah Loomans fix build on linux 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <ft_printf.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "error.h"
#include "state.h"

struct s_state_option	*unpack_option(struct s_list2_conn *conn)
{
	if (conn == NULL)
		return (NULL);
	return (struct s_state_option *)(
		(char *)conn - offsetof(struct s_state_option, conn));
}

t_error					state_make_options(
							t_list2_meta *dest, int argc, char **argv)
{
	int						i;
	t_list2_conn			*prev;
	struct s_state_option	*new_option;

	if (argc <= 1)
		return (errorf("no options to select"));
	i = 1;
	prev = NULL;
	while (i < argc)
	{
		new_option = ft_memalloc(sizeof(*new_option));
		if (new_option == NULL)
			return (errorf("failed to allocate option"));
		new_option->name = argv[i];
		new_option->selected = false;
		list2_insert(dest, prev, &new_option->conn);
		prev = &new_option->conn;
		i++;
	}
	return (ERROR_NULL);
}