summaryrefslogtreecommitdiffstats
path: root/src/p_split.c
blob: dc26c04a26b61f331de8eeec048eb7df8aff6e05 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   p_split.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 <stdlib.h>
#include <limits.h>

#include "f_fail.h"
#include "d_define.h"
#include "p_split.h"
#include "s_struct.h"
#include "u_parse.h"
#include "u_utils.h"

static void
	p_meet_splitter(char *ptr,
					const char line[],
					t_split_block *sp,
					t_quote_mode mode)
{
	if (mode == Q_NONE && *ptr == C_SEMIC && *(ptr + 1) != C_SEMIC &&
		u_is_not_escaped(line, ptr) == TRUE)
	{
		sp->pos[sp->count] = (ptr - line);
		sp->nextif[sp->count] = 0;
		sp->count += 1;
	}
	else if (mode == Q_NONE && *ptr == C_AMP && *(ptr + 1) == C_AMP &&
			 (*ptr + 2) != C_PIPE && u_is_not_escaped(line, ptr) == TRUE)
	{
		sp->pos[sp->count] = (ptr - line);
		sp->nextif[sp->count] = 1;
		sp->count += 1;
	}
	else if (mode == Q_NONE && *ptr == C_PIPE && *(ptr + 1) == C_PIPE &&
			 *(ptr + 2) != C_PIPE && u_is_not_escaped(line, ptr) == TRUE)
	{
		sp->pos[sp->count] = (ptr - line);
		sp->nextif[sp->count] = 2;
		sp->count += 1;
	}
}

static void
	p_fill_sp(t_split_block *sp, const char line[])
{
	char			*ptr;
	t_quote_mode	mode;

	sp->pos[0] = 0;
	sp->nextif[0] = 0;
	sp->count = 0;
	mode = Q_NONE;
	ptr = (char*)line;
	while (*ptr != C_NUL)
	{
		if (*ptr == C_SQUOTE)
			mode = u_meet_squote(line, ptr, mode);
		else if (*ptr == C_DQUOTE)
			mode = u_meet_dquote(line, ptr, mode);
		else if (*ptr == C_SEMIC || *ptr == C_AMP || *ptr == C_PIPE)
			p_meet_splitter(ptr, line, sp, mode);
		ptr++;
	}
	sp->pos[sp->count] = ptr - line;
	sp->nextif[sp->count] = 0;
	sp->count += 1;
}

static void
	*p_del_split(char *words[], size_t todel)
{
	size_t	i;

	i = 0;
	while (i < todel)
	{
		ft_memdel((void*)&words[i]);
	}
	ft_memdel((void*)&words);
	return (NULL);
}

static char
	**p_get_words(const char line[], const t_split_block *sp)
{
	char	**words;
	int64_t	i;
	size_t	oldpos;
	int8_t	oldif;

	if ((words = (char**)malloc((sp->count + 1) * sizeof(char*))) == NULL)
		return (NULL);
	oldpos = 0;
	oldif = -1;
	i = -1;
	while (++i < sp->count)
	{
		if ((words[i] = (char*)malloc(((sp->pos[i] - oldpos) + 2) *
			sizeof(char))) == NULL)
			return ((char**)p_del_split(words, i));
		ft_strlcpy(words[i], line + oldpos + ((oldif > 0) ? (2) : (oldif) + 1),
			sp->pos[i] - oldpos + 1 - ((oldif > 0) ? (2) : (1)) +
			((oldif < 0) ? (1) : (0)));
		words[i][ft_strlen(words[i]) + 1] = C_NUL;
		words[i][ft_strlen(words[i])] = sp->nextif[i] + 0x30;
		oldpos = sp->pos[i];
		oldif = sp->nextif[i];
	}
	words[i] = NULL;
	return (words);
}

char
	**p_split_line(char line[])
{
	t_split_block	sp;
	char			**words;

	words = NULL;
	p_fill_sp(&sp, line);
	if ((words = p_get_words(line, &sp)) == NULL)
	{
		ft_memdel((void*)&line);
		return (NULL);
	}
	ft_memdel((void*)&line);
	return (words);
}