~nloomans/ft_select

0364e8344aaffc1e2d27a51db163302adf1cf910 — Noah Loomans 1 year, 8 months ago dbeabde
fix derive norm
3 files changed, 81 insertions(+), 64 deletions(-)

M meson.build
M src/derive.c
A src/derive_rows.c
M meson.build => meson.build +1 -0
@@ 32,6 32,7 @@ src_files = files([
  'src/action/select.c',
  'src/action/update_size.c',
  'src/derive.c',
  'src/derive_rows.c',
  'src/error.c',
  'src/event.c',
  'src/list2.c',

M src/derive.c => src/derive.c +0 -64
@@ 63,55 63,6 @@ t_error			derive_column_width(
	return (ERROR_NULL);
}

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);
}

bool			derive_enough_columns(
					struct s_state_terminal terminal,
					struct s_derived_dimensions dimensions,


@@ 130,18 81,3 @@ bool			derive_enough_columns(
	}
	return (total_width <= terminal.columns);
}

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);
}

A src/derive_rows.c => src/derive_rows.c +80 -0
@@ 0,0 1,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);
}