aboutsummaryrefslogtreecommitdiff
path: root/src/lexer/trim.c
blob: fcd62afeaffdcd7cdc5091fb47a2c9adedff4230 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   trim.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: nahaddac <nahaddac@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/07/16 08:18:36 by nahaddac          #+#    #+#             */
/*   Updated: 2020/09/13 08:51:56 by nahaddac         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */


#include "lexer.h"

char                *del_space(t_tok_lst *tok)
{
    int             i;

    i = ft_strlen(tok->content);
    if(tok->content[i - 1] == ' ')
    {
        i -= 1;
        while(tok->content[i] == ' ')
        {
            if (tok->content[i - 1] == '\\')
                break;
            i--;
        }
        tok->content = ft_strsubf(tok->content, 0, i + 1);
        return (tok->content);
    }
    return(tok->content);
}

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

    i = lexer_check_between_quote(str, 0);
    if(str[i] != '\'' && str[i] != '"')
        return str;
    return (ft_strsubf(str, 1, i - 1));
}

t_tok_lst             *lexer_trim_out(t_tok_lst *tokens)
{
    t_tok_lst	*first;

    first = tokens;
    while (tokens != NULL)
    {
        if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE))
        {
            tokens->content = del_quote(tokens->content);
            if (tokens->next == NULL)
                if (tokens->tag & TAG_STICK)
                    tokens->tag -= TAG_STICK;
        }
        else
        {
            tokens->content = del_space(tokens);
            if (tokens->next == NULL)
                if (tokens->tag & TAG_STICK)
                    tokens->tag -= TAG_STICK;
        }
        tokens = tokens->next;
    }
    return (first);
}