aboutsummaryrefslogtreecommitdiff
path: root/src/lexer/trim.c
blob: 24eb99ffdd740d6fc9f8d7c9dd78324eef65956d (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   trim.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: nahaddac <nahaddac@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/07/16 08:18:36 by nahaddac          #+#    #+#             */
/*   Updated: 2020/09/14 16:23:35 by nahaddac         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */


#include "lexer.h"
#include <stdio.h>

void	del_space(char *str)
{
    int i;

    i = ft_strlen(str);
    if (ft_isblank(str[i - 1]))
    {
        i -= 1;
        while (ft_isblank(str[i]))
        {
            if (str[i - 1] == '\\')
                break ;
            i--;
        }
		str[i + 1] = '\0';
    }
}

int 	del_quote(char *str)
{
    size_t	i;
    int nb_q;

    i = 0;
    nb_q = 1;
    if (str[0] == '\'')
	{
        while (str[i++] != '\0')
        {
            if (str[i] == '\\')
                i += 2;
            if (str[i] == '\'')
            {
                nb_q++;
                break ;
            }
        }
	}
    else if (str[0] == '"')
	{
        while (str[i++] != '\0')
        {
            if (str[i] == '\\')
                i += 2;
            if (str[i] == '"')
            {
                nb_q++;
                break ;
            }
        }
	}
    if (nb_q % 2 == 0)
    {
        str[i] = '\0';
    	if(!(ft_memmove(str, str + 1, ft_strlen(str + 1) + 1)))
            return 1;
        else
            return 0;
    }
    else
        return 2;
}

int	lexer_trim(t_tok_lst *tokens)
{
    int r = 0;
    while (tokens != NULL)
    {
        if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE))
        {
            r = del_quote(tokens->content);
            if (r == 0)
            {
                if (tokens->next == NULL)
                    tokens->tag &= ~TAG_STICK;
            }
            else
                return r;
        }
        else
        {
            del_space(tokens->content);
            if (tokens->next == NULL)
				tokens->tag &= ~TAG_STICK;
        }
        tokens = tokens->next;
    }
    return 0;
}