summaryrefslogtreecommitdiffstats
path: root/src/p_args_len.c
blob: 801406f69a8c6682f696104c4fd725e1ece4d060 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   p_args_len.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 <stddef.h>

#include "d_define.h"

static size_t
	p_skip_delim_size(const char word[], char c, size_t end)
{
	end++;
	if (word[end] == c)
		return (end + 1);
	while (word[end] != C_NUL && word[end] != c)
	{
		end++;
		if (word[end] == c && c == C_DQUOTE)
		{
			if (word[end - 1] == C_BACKS && word[end - 2] != C_BACKS)
			{
				end++;
			}
		}
	}
	if (word[end] != C_NUL)
	{
		end++;
	}
	return (end);
}

static size_t
	p_skip_unquote(const char word[], size_t end)
{
	while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE)
	{
		end++;
		if (ft_iswhitespace(word[end]) == TRUE && end == 1 &&
			word[end - 1] == C_BACKS)
		{
			end++;
		}
		else if (ft_iswhitespace(word[end]) == TRUE && end > 1 &&
			word[end - 1] == C_BACKS && word[end - 2] != C_BACKS)
		{
			end++;
		}
	}
	return (end);
}

static size_t
	p_skip_quote(const char word[], size_t end)
{
	while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE)
	{
		if (word[end] == C_SQUOTE || word[end] == C_DQUOTE)
		{
			end = p_skip_delim_size(word, word[end], end);
		}
		else
		{
			while (word[end] != C_NUL &&
				   ft_iswhitespace(word[end]) == FALSE)
			{
				end++;
			}
		}
	}
	return (end);
}

size_t
	p_arg_len(const char word[], const size_t start)
{
	size_t	end;

	end = start;
	if (word[start] != C_SQUOTE && word[start] != C_DQUOTE &&
		word[start] != C_NUL)
	{
		end = p_skip_unquote(word, end);
	}
	else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE)
	{
		end = p_skip_quote(word, end);
	}
	return (end);
}