~nloomans/ft_select

ref: a4ef762331941c5e33670f7bf81eb964fb761edb ft_select/src/derive_rows.c -rw-r--r-- 2.3 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include <stddef.h>
#include <libft.h>
#include "error.h"
#include "derive.h"

static t_error	malloc_rows(
					struct s_state_option ****res,
					struct s_derived_dimensions dimensions)
{
	size_t			row_index;

	*res = ft_memalloc(sizeof(**res) * dimensions.rows);
	if (*res == NULL)
		return (errorf("unable to malloc rows array"));
	row_index = 0;
	while (row_index < dimensions.rows)
	{
		(*res)[row_index] = ft_memalloc(sizeof(***res) * dimensions.columns);
		if ((*res)[row_index] == NULL)
			return (errorf("unable to malloc column in rows array"));
		row_index++;
	}
	return (ERROR_NULL);
}

t_error			derive_rows(
					struct s_state_option ****res,
					struct s_derived_dimensions dimensions,
					t_list2_meta options)
{
	size_t			row_index;
	size_t			column_index;
	t_list2_conn	*conn;
	t_error			error;

	error = malloc_rows(res, dimensions);
	if (is_error(error))
		return (error);
	conn = options.first;
	column_index = 0;
	while (column_index < dimensions.columns)
	{
		row_index = 0;
		while (row_index < dimensions.rows && conn != NULL)
		{
			(*res)[row_index][column_index] = unpack_option(conn);
			conn = conn->next;
			row_index++;
		}
		column_index++;
	}
	return (ERROR_NULL);
}

void			derive_free_rows(
					struct s_derived_dimensions dimensions,
					struct s_state_option ****rows)
{
	size_t	row_index;

	row_index = 0;
	while (row_index < dimensions.rows)
	{
		ft_memdel((void **)&(*rows)[row_index]);
		row_index++;
	}
	ft_memdel((void **)rows);
}