summaryrefslogtreecommitdiffstats
path: root/src/u_alias.c
blob: b505e2ac1244476251d2909880e7449c6bfb7a69 (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   u_alias.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdint.h>
#include <stddef.h>

#include "d_define.h"
#include "s_lalias.h"
#include "s_struct.h"

size_t	u_get_alias_value(char str[],
						const char name[],
						size_t dstsize,
						t_msh *msh)
{
	t_lalias	*ptr;

	if (str != NULL)
	{
		str[0] = C_NUL;
	}
	ptr = msh->alias;
	while (ptr != NULL && ft_strncmp(ptr->name, name, ft_strlen(name) + 1) != 0)
	{
		ptr = ptr->next;
	}
	if (ptr == NULL)
	{
		return (0);
	}
	if (str != NULL)
	{
		ft_strlcpy(str, ptr->val, dstsize);
	}
	return (ptr->id);
}

void	u_set_alias_value(const char name[], const char value[], t_msh *msh)
{
	t_lalias	*new;

	if (u_get_alias_value(NULL, name, 0, msh) != 0)
	{
		s_lalias_rebind(&msh->alias, name, value);
	}
	else
	{
		if ((new = s_lalias_new(name, value)) == NULL)
		{
			return ;
		}
		s_lalias_add_front(&msh->alias, new);
	}
}