/ src / parsing_utils.c
parsing_utils.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   parsing_utils.c                                    :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: ll-hotel <ll-hotel@student.42lyon.fr>      +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2024/07/26 15:56:33 by ll-hotel          #+#    #+#             */
 9  /*   Updated: 2024/10/20 17:01:40 by ll-hotel         ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "ft_basics.h"
14  #include "parsing.h"
15  #include <stddef.h>
16  #include <stdlib.h>
17  
18  int	line_from_str(t_line *line, char *str)
19  {
20  	int	i;
21  
22  	ft_memset(line, 0, sizeof(*line));
23  	if (str == NULL)
24  		return (0);
25  	line->ptr = str;
26  	line->key = &line->ptr[count_spaces(line->ptr, 0)];
27  	if (ft_isdigit(line->key[0]))
28  		return (1);
29  	i = 0;
30  	while (line->key[i] && line->key[i] != ' ')
31  		i += 1;
32  	if (line->key[i] == ' ')
33  	{
34  		line->key[i] = 0;
35  		i += count_spaces(line->key, i + 1) + 1;
36  		if (line->key[i] != 0)
37  			line->value = &line->key[i];
38  	}
39  	return (1);
40  }
41  
42  t_line	*find_line_by_key(t_line *lines, char const *key)
43  {
44  	int	i;
45  
46  	i = 0;
47  	while (lines[i].ptr != NULL)
48  	{
49  		if (lines[i].key != NULL && ft_strcmp(lines[i].key, key) == 0)
50  			return (&lines[i]);
51  		i += 1;
52  	}
53  	return (NULL);
54  }
55  
56  int	count_spaces(char const *str, int start)
57  {
58  	int	i;
59  
60  	if (!str)
61  		return (0);
62  	i = 0;
63  	while (str[start + i] == ' ')
64  		i += 1;
65  	return (i);
66  }
67  
68  void	free_line(void *ptr)
69  {
70  	t_line *const	line = ptr;
71  
72  	if (line->ptr)
73  		free(line->ptr);
74  }
75  
76  void	free_lines(t_line *lines)
77  {
78  	int	i;
79  
80  	i = 0;
81  	while (lines[i].ptr != NULL)
82  		free(lines[i++].ptr);
83  	free(lines);
84  }