aboutsummaryrefslogtreecommitdiff
path: root/rush02/ex00/parse.c
blob: af3d43d53c0a6e6f6cf3d9ef3150e5b599936ea2 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   file.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: agassin <agassin@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/07/20 10:47:14 by cacharle          #+#    #+#             */
/*   Updated: 2019/07/21 14:44:22 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "include.h"

/*
** Open the dictionnary file, pass it to `parse_dict`
** return the dict, NULL if an error occurs
*/

t_dict	parse_dict_file(char *filename)
{
	char	*file;
	int		fildes;
	int		file_size;
	t_dict	dict;

	if ((fildes = open(filename, O_RDONLY)) < 0)
		return (NULL);
	file = NULL;
	if ((file_size = read_file(fildes, &file)) < 0)
		return (NULL);
	if ((dict = parse_dict(file, file_size)) == NULL)
	{
		destroy_dict(dict, -1);
		free(file);
		return (NULL);
	}
	free(file);
	if (close(fildes) < 0)
		return (NULL);
	return (dict);
}

/*
** Parse the file and return a dictionary where the keys are
** the numbers and the values the string attached to it
*/

t_dict	parse_dict(char *file, int file_size)
{
	int		i;
	int		j;
	t_dict	dict;

	dict = (t_dict)malloc(sizeof(t_entry) * (count_lines(file) + 1));
	if (dict == NULL)
		return (NULL);
	i = 0;
	j = 0;
	while (i < file_size)
	{
		while (file[i] && file[i] == '\n')
			i++;
		if (file[i] && (set_entry(&file[i], &dict[j++])) == -1)
		{
			destroy_dict(dict, j - 1);
			return (NULL);
		}
		while (file[i] && file[i] != '\n')
			i++;
	}
	dict[j].key = -1;
	dict[j].value = NULL;
	return (dict);
}

/*
** Set the key and value of an entry based on the given line
** return -1 if an error occurs
*/

int		set_entry(char *line, t_entry *entry)
{
	int		i;
	char	*tmp;

	i = 0;
	while (line[i] >= '0' && line[i] <= '9')
		i++;
	if (i == 0)
		return (-1);
	tmp = ft_strndup(line, i);
	entry->key = strict_atoi(tmp);
	free(tmp);
	while (line[i] == ' ')
		i++;
	if (line[i] != ':')
		return (-1);
	i++;
	while (line[i] == ' ')
		i++;
	entry->value = filtered_value(&line[i]);
	if (entry->value == NULL)
		return (-1);
	return (0);
}

/*
** Duplicate the line and compress spaces until there is only one left
*/

char	*filtered_value(char *str)
{
	int		i;
	int		j;
	char	*value;

	i = 0;
	while (str[i] != '\n')
		i++;
	if (i == 0)
		return (NULL);
	if ((value = (char*)malloc(sizeof(char) * (i + 1))) == NULL)
		return (NULL);
	i = 0;
	j = 0;
	while (str[i] && str[i] != '\n')
	{
		value[j++] = str[i++];
		while (str[i] && str[i] == ' ')
		{
			if (str[i + 1] && str[i + 1] != ' ')
				break ;
			i++;
		}
	}
	value[i] = '\0';
	return (value);
}

/*
** count the number of line in the file
** without counting empty lines
*/

int		count_lines(char *file)
{
	int	counter;

	counter = 0;
	while (*file)
	{
		if (*file == '\n')
		{
			counter++;
			while (*file == '\n')
				file++;
		}
		file++;
	}
	return (counter);
}