aboutsummaryrefslogtreecommitdiff
path: root/rush02/ex00/parse.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-07-21 15:26:32 +0200
committerCharles <sircharlesaze@gmail.com>2019-07-21 15:26:32 +0200
commit23ad79e8b41c25bb4992d103d29a17612a52e351 (patch)
tree9de3cde07cc38e59f08885171e9f99eeab8ab71b /rush02/ex00/parse.c
parent8b6e91bdb56bc01a588718472546f2a88e750b48 (diff)
downloadpiscine-23ad79e8b41c25bb4992d103d29a17612a52e351.tar.gz
piscine-23ad79e8b41c25bb4992d103d29a17612a52e351.tar.bz2
piscine-23ad79e8b41c25bb4992d103d29a17612a52e351.zip
c10 done, c11 on going, rush02 probably finished, bsq start
Diffstat (limited to 'rush02/ex00/parse.c')
-rw-r--r--rush02/ex00/parse.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/rush02/ex00/parse.c b/rush02/ex00/parse.c
new file mode 100644
index 0000000..af3d43d
--- /dev/null
+++ b/rush02/ex00/parse.c
@@ -0,0 +1,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);
+}