Parsing done
This commit is contained in:
parent
c8eeb1dc65
commit
675cab80a7
|
@ -5,6 +5,7 @@ SRC = main.c \
|
|||
dictparse.c \
|
||||
ft_linked_list.c \
|
||||
printing.c \
|
||||
ft_convert.c \
|
||||
|
||||
HEADERS = ft_strlib.h \
|
||||
ft_io.h \
|
||||
|
@ -13,6 +14,7 @@ HEADERS = ft_strlib.h \
|
|||
dictparse.h \
|
||||
ft_linked_list.h \
|
||||
printing.h \
|
||||
ft_convert.h \
|
||||
|
||||
OBJDIR = obj
|
||||
INCDIR = include
|
||||
|
@ -55,7 +57,7 @@ $(NAME): $(OBJ)
|
|||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.c $(DEPS)
|
||||
@norminette $< >/dev/null || { printf '\033[101;97m%s\033[m\n' "!Norminette Failed>>>"; norminette $<; printf '\033[101;97m%s\033[m\n' "<<<Norminette Failed!"; exit 1; }
|
||||
@norminette $< >/dev/null || { printf '\033[101;37m%s\033[m\n' "!Norminette Failed>>>"; norminette $<; printf '\033[101;37m%s\033[m\n' "<<<Norminette Failed!"; exit 1; }
|
||||
@$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(OBJDIR):
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
Activate norminette in Makefile!!!
|
||||
Change .DEFAULT_GOAL in Makefile to 'all'
|
||||
Handle empty lines
|
||||
|
|
|
@ -6,42 +6,67 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 21:45:39 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/01 21:59:39 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 18:40:32 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "dictparse.h"
|
||||
#include "ft_io.h"
|
||||
#include "ft_convert.h"
|
||||
#include "ft_strlib.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#define MAX_DIGITS 100
|
||||
|
||||
int parse_line(char *line, char **key, char **value)
|
||||
int parse_line(char *line, char *key, char *value)
|
||||
{
|
||||
(void)line;
|
||||
*key = "20";
|
||||
*value = "twenty";
|
||||
int idx;
|
||||
int i;
|
||||
int only_whitespace;
|
||||
|
||||
idx = 0;
|
||||
only_whitespace = 1;
|
||||
while (line[idx] && line[idx] != ':')
|
||||
if (!ft_isspace(line[idx++]))
|
||||
only_whitespace = 0;
|
||||
if (!only_whitespace && !line[idx])
|
||||
return (0);
|
||||
ft_itoa(key, ft_atoi(line));
|
||||
line += idx + 1;
|
||||
i = 0;
|
||||
while (ft_isspace(line[i++]))
|
||||
;
|
||||
if (!line[i])
|
||||
return (0);
|
||||
*(last_non_whitespace(line) + 1) = 0;
|
||||
line += i - 1;
|
||||
ft_strcpy(value, line);
|
||||
return (1);
|
||||
}
|
||||
|
||||
// `value' instead of `buf` in ll_map_push
|
||||
int parse_dict(char *path, t_map_entry *map)
|
||||
{
|
||||
char buf[MAX_LINE_LENGTH];
|
||||
int offset;
|
||||
char *key;
|
||||
char *value;
|
||||
int idx;
|
||||
|
||||
offset = 0;
|
||||
idx = -1;
|
||||
while (++idx < 20)
|
||||
key = malloc(sizeof(char) * MAX_DIGITS);
|
||||
value = malloc(sizeof(char) * MAX_LINE_LENGTH);
|
||||
while (1)
|
||||
{
|
||||
offset = readline(buf, MAX_LINE_LENGTH, path, offset);
|
||||
if (offset == FILE_READ_ERROR)
|
||||
break ;
|
||||
else if (offset == LINE_TOO_LONG)
|
||||
return (0);
|
||||
if (!parse_line(buf, &key, &value))
|
||||
if (!parse_line(buf, key, value))
|
||||
return (0);
|
||||
ll_map_push(map, key, buf);
|
||||
ll_map_push(map, key, value);
|
||||
}
|
||||
free(key);
|
||||
free(value);
|
||||
return (1);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_convert.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: shou <shou@student.42berlin.de> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/27 12:05:10 by shou #+# #+# */
|
||||
/* Updated: 2023/04/02 18:41:07 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_convert.h"
|
||||
|
||||
int rm_spaces(char *str, int *ptr_i)
|
||||
{
|
||||
int count;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
count = 1;
|
||||
while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32)
|
||||
i++;
|
||||
while ((str[i] == 43 || str[i] == 45))
|
||||
{
|
||||
if (str[i] == 45)
|
||||
count *= -1;
|
||||
i++;
|
||||
}
|
||||
*ptr_i = i;
|
||||
return (count);
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int ft_atoi(char *str)
|
||||
{
|
||||
int sign;
|
||||
int result;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
result = 0;
|
||||
sign = rm_spaces(str, &i);
|
||||
while (str[i] && str[i] >= 48 && str[i] <= 57)
|
||||
{
|
||||
result *= 10;
|
||||
result += str[i] - 48;
|
||||
i++;
|
||||
}
|
||||
result *= sign;
|
||||
return (result);
|
||||
}
|
||||
int ft_itoa(char *buf, int nb)
|
||||
{
|
||||
int offset;
|
||||
|
||||
if (nb > 9)
|
||||
{
|
||||
offset = ft_itoa(buf, nb / 10);
|
||||
return (offset + ft_itoa(buf + offset, nb % 10));
|
||||
}
|
||||
else if (nb == INT_MIN)
|
||||
{
|
||||
offset = ft_itoa(buf, nb / 10);
|
||||
return (ft_itoa(buf + offset + 1, -(nb % 10)));
|
||||
}
|
||||
else if (nb < 0)
|
||||
{
|
||||
*buf = '-';
|
||||
return (ft_itoa(buf + 1, -nb));
|
||||
}
|
||||
*buf = nb % 10 + '0';
|
||||
*(buf + 1) = 0;
|
||||
return (1);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 15:25:42 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/01 22:08:37 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 18:12:47 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -18,11 +18,14 @@ t_map_entry *ll_map_new_entry(char *key, char *value)
|
|||
{
|
||||
t_map_entry *new_entry;
|
||||
char *new_value;
|
||||
char *new_key;
|
||||
|
||||
new_value = (char *) malloc(sizeof(char) * (ft_strlen(value) + 1));
|
||||
new_key = (char *) malloc(sizeof(char) * (ft_strlen(key) + 1));
|
||||
ft_strcpy(new_value, value);
|
||||
ft_strcpy(new_key, key);
|
||||
new_entry = (t_map_entry *) malloc(sizeof(t_map_entry));
|
||||
new_entry->key = key;
|
||||
new_entry->key = new_key;
|
||||
new_entry->value = new_value;
|
||||
new_entry->next = NULL;
|
||||
return (new_entry);
|
||||
|
@ -58,7 +61,13 @@ void ll_map_push(t_map_entry *head, char *key, char *value)
|
|||
}
|
||||
}
|
||||
|
||||
t_map_entry *ll_pop_last(t_map_entry *head, t_ret_flag noretval, int free_vals)
|
||||
void free_entry(t_map_entry *entry)
|
||||
{
|
||||
free(entry->key);
|
||||
free(entry->value);
|
||||
}
|
||||
|
||||
t_map_entry *ll_pop_last(t_map_entry *head, t_ret_flag noretval, int free_data)
|
||||
{
|
||||
t_map_entry *current;
|
||||
t_map_entry *retval;
|
||||
|
@ -70,8 +79,8 @@ t_map_entry *ll_pop_last(t_map_entry *head, t_ret_flag noretval, int free_vals)
|
|||
{
|
||||
if (noretval != POP_NO_RETURN_VAL)
|
||||
retval = ll_map_new_entry(head->key, head->value);
|
||||
if (free_vals)
|
||||
free(head->value);
|
||||
if (free_data)
|
||||
free_entry(head);
|
||||
free(head);
|
||||
return (retval);
|
||||
}
|
||||
|
@ -80,19 +89,19 @@ t_map_entry *ll_pop_last(t_map_entry *head, t_ret_flag noretval, int free_vals)
|
|||
current = current->next;
|
||||
if (noretval != POP_NO_RETURN_VAL)
|
||||
retval = ll_map_new_entry(current->next->key, current->next->value);
|
||||
if (free_vals)
|
||||
free(current->next->value);
|
||||
if (free_data)
|
||||
free_entry(current->next);
|
||||
free(current->next);
|
||||
current->next = NULL;
|
||||
return (retval);
|
||||
}
|
||||
|
||||
void ll_clear(t_map_entry *head, int free_vals)
|
||||
void ll_clear(t_map_entry *head, int free_data)
|
||||
{
|
||||
if (head != NULL)
|
||||
{
|
||||
while (head->next != NULL)
|
||||
ll_pop_last(head, POP_NO_RETURN_VAL, free_vals);
|
||||
ll_pop_last(head, POP_NO_RETURN_VAL, free_vals);
|
||||
ll_pop_last(head, POP_NO_RETURN_VAL, free_data);
|
||||
ll_pop_last(head, POP_NO_RETURN_VAL, free_data);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 07:13:31 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/01 21:55:16 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 18:38:52 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -42,3 +42,22 @@ int ft_strlen(char *str)
|
|||
++i;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_isspace(char c)
|
||||
{
|
||||
return (c == ' '
|
||||
|| c == '\t'
|
||||
|| c == '\v'
|
||||
|| c == '\n'
|
||||
|| c == '\r'
|
||||
|| c == '\f');
|
||||
}
|
||||
|
||||
char *last_non_whitespace(char *ptr)
|
||||
{
|
||||
while (*ptr)
|
||||
++ptr;
|
||||
while (ft_isspace(*ptr))
|
||||
--ptr;
|
||||
return (ptr);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 21:48:18 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/01 21:51:46 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 17:36:47 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
|||
# define MAX_LINE_LENGTH 1000
|
||||
# include "ft_linked_list.h"
|
||||
|
||||
int parse_line(char *line, char **key, char **value);
|
||||
int parse_line(char *line, char *key, char *value);
|
||||
int parse_dict(char *path, t_map_entry *map);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_convert.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 17:06:51 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 18:41:32 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_CONVERT_H
|
||||
# define FT_CONVERT_H
|
||||
|
||||
int ft_atoi(char *str);
|
||||
int ft_itoa(char *buf, int nb)
|
||||
|
||||
#endif
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 15:25:59 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/01 22:08:28 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 18:12:59 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -27,8 +27,8 @@ typedef enum
|
|||
|
||||
char *ll_map_get(t_map_entry *head, char *key);
|
||||
void ll_map_push(t_map_entry *head, char *key, char *value);
|
||||
void ll_clear(t_map_entry *head, int free_vals);
|
||||
void ll_clear(t_map_entry *head, int free_data);
|
||||
t_map_entry *ll_map_new_entry(char *key, char *value);
|
||||
t_map_entry *ll_pop_last(t_map_entry *head, t_ret_flag noretval, int free_vals);
|
||||
t_map_entry *ll_pop_last(t_map_entry *head, t_ret_flag noretval, int free_data);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 07:14:29 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/01 22:01:32 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 18:39:09 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -18,5 +18,7 @@
|
|||
char *ft_strcpy(char *dest, char *src);
|
||||
int ft_strcmp(char *s1, char *s2);
|
||||
int ft_strlen(char *str);
|
||||
char *last_non_whitespace(char *ptr);
|
||||
int ft_isspace(char c);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/01 22:02:19 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 18:30:05 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -27,7 +27,7 @@ int main(int argc, char **argv)
|
|||
map = ll_map_new_entry("about", "Dict for the data from the .dict file");
|
||||
if (!parse_dict(path, map))
|
||||
return (puterr("Dict Error\n", 2));
|
||||
printf("%s\n", ll_map_get(map, "20"));
|
||||
printf("%s\n", ll_map_get(map, "1000000"));
|
||||
ll_clear(map, 1);
|
||||
return (0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue