Dictparse save
This commit is contained in:
parent
450bd4607a
commit
9f747e3214
|
@ -69,15 +69,18 @@ SUCCESS=&& $(SUCCESS_MSG) || $(FAIL_MSG)
|
||||||
SUCCESS_VALG=&& $(SUCCESS_MSG_VALG) || $(FAIL_MSG_VALG)
|
SUCCESS_VALG=&& $(SUCCESS_MSG_VALG) || $(FAIL_MSG_VALG)
|
||||||
FAIL=&& $(FAIL_MSG) || $(SUCCESS_MSG)
|
FAIL=&& $(FAIL_MSG) || $(SUCCESS_MSG)
|
||||||
VALGRIND=valgrind --leak-check=full --error-exitcode=1
|
VALGRIND=valgrind --leak-check=full --error-exitcode=1
|
||||||
|
YLW=:[33m;
|
||||||
|
CYN=:[36m;
|
||||||
|
CLR_RST=&& : [m
|
||||||
run:
|
run:
|
||||||
@clear
|
@clear
|
||||||
./$(NAME) $(SUCCESS)
|
$(YLW) ./$(NAME) $(CLR_RST) $(SUCCESS)
|
||||||
./$(NAME) 1 $(SUCCESS)
|
$(YLW) ./$(NAME) 1 $(CLR_RST) $(SUCCESS)
|
||||||
./$(NAME) "./dicts/numbers2.dict" 1 $(SUCCESS)
|
$(YLW) ./$(NAME) "./dicts/numbers2.dict" 1 $(CLR_RST) $(SUCCESS)
|
||||||
./$(NAME) 1 "./dicts/numbers2.dict" $(FAIL)
|
$(YLW) ./$(NAME) 1 "./dicts/numbers2.dict" $(CLR_RST) $(FAIL)
|
||||||
./$(NAME) 1 "./dicts/numb.dict" $(FAIL)
|
$(YLW) ./$(NAME) 1 "./dicts/numb.dict" $(CLR_RST) $(FAIL)
|
||||||
./$(NAME) 1 2 3 $(FAIL)
|
$(YLW) ./$(NAME) 1 2 3 $(CLR_RST) $(FAIL)
|
||||||
|
|
||||||
valgrind: re
|
valgrind: re
|
||||||
$(VALGRIND) ./$(NAME) 1 $(SUCCESS_VALG)
|
$(CYN) $(VALGRIND) ./$(NAME) 1 $(CLR_RST) $(SUCCESS_VALG)
|
||||||
$(VALGRIND) ./$(NAME) "./dicts/numbers2.dict" 1 $(SUCCESS_VALG)
|
$(CYN) $(VALGRIND) ./$(NAME) "./dicts/numbers2.dict" 1 $(CLR_RST) $(SUCCESS_VALG)
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
#include "ft_io.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#define MAX_LINE_LENGTH 1000
|
||||||
|
#define FILE_READ_ERROR -1
|
||||||
|
#define LINE_TOO_LONG -2
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copy to buf from file at `path' with `offset' continuously until
|
||||||
|
* either EOF, '\n', or `size' is reached. Return new offset.
|
||||||
|
*/
|
||||||
|
int readline(char *buf, int size, char *path, int offset)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
int fd;
|
||||||
|
int bytes_read;
|
||||||
|
|
||||||
|
fd = open(path, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
return (FILE_READ_ERROR);
|
||||||
|
bytes_read = -1;
|
||||||
|
while (++bytes_read < offset)
|
||||||
|
if (read(fd, &c, sizeof(c)) <= 0)
|
||||||
|
return (FILE_READ_ERROR);
|
||||||
|
bytes_read = 0;
|
||||||
|
while (read(fd, &c, sizeof(c)) > 0 && bytes_read < size)
|
||||||
|
{
|
||||||
|
if (c == '\n')
|
||||||
|
break ;
|
||||||
|
*buf++ = c;
|
||||||
|
++bytes_read;
|
||||||
|
}
|
||||||
|
if (bytes_read == size)
|
||||||
|
return (LINE_TOO_LONG);
|
||||||
|
*buf = 0;
|
||||||
|
close(fd);
|
||||||
|
return (offset + bytes_read + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_line(char *line, char **key, char **value)
|
||||||
|
{
|
||||||
|
(void)line;
|
||||||
|
*key = "20";
|
||||||
|
*value = "twenty";
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_dict(char *path, t_map_entry *map)
|
||||||
|
{
|
||||||
|
char buf[MAX_LINE_LENGTH];
|
||||||
|
int offset;
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
|
||||||
|
(void)map;
|
||||||
|
offset = 0;
|
||||||
|
for (int i = 0; i < 20; ++i)
|
||||||
|
{
|
||||||
|
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))
|
||||||
|
return (0);
|
||||||
|
ll_map_push(map, key, /* value */buf);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/01 15:25:42 by tischmid #+# #+# */
|
/* Created: 2023/04/01 15:25:42 by tischmid #+# #+# */
|
||||||
/* Updated: 2023/04/01 17:44:52 by tischmid ### ########.fr */
|
/* Updated: 2023/04/01 20:59:06 by tischmid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -18,10 +18,13 @@
|
||||||
t_map_entry *ll_map_new_entry(char *key, char *value)
|
t_map_entry *ll_map_new_entry(char *key, char *value)
|
||||||
{
|
{
|
||||||
t_map_entry *new_entry;
|
t_map_entry *new_entry;
|
||||||
|
char *new_value;
|
||||||
|
|
||||||
|
new_value = (char *) malloc(sizeof(char) * (ft_strlen(value) + 1));
|
||||||
|
ft_strcpy(new_value, value);
|
||||||
new_entry = (t_map_entry *) malloc(sizeof(t_map_entry));
|
new_entry = (t_map_entry *) malloc(sizeof(t_map_entry));
|
||||||
new_entry->key = key;
|
new_entry->key = key;
|
||||||
new_entry->value = value;
|
new_entry->value = new_value;
|
||||||
new_entry->next = NULL;
|
new_entry->next = NULL;
|
||||||
return (new_entry);
|
return (new_entry);
|
||||||
}
|
}
|
||||||
|
@ -54,20 +57,22 @@ void ll_map_push(t_map_entry *head, char *key, char *value)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
current->next = ll_map_new_entry(key, value);
|
current->next = ll_map_new_entry(key, value);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
ft_putstr("head is NULL\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t_map_entry *ll_pop_last(t_map_entry *head)
|
t_map_entry *ll_pop_last(t_map_entry *head, t_ll_flag noretval, int free_values)
|
||||||
{
|
{
|
||||||
t_map_entry *current;
|
t_map_entry *current;
|
||||||
t_map_entry *retval;
|
t_map_entry *retval;
|
||||||
|
|
||||||
|
retval = NULL;
|
||||||
if (head != NULL)
|
if (head != NULL)
|
||||||
{
|
{
|
||||||
if (head->next == NULL)
|
if (head->next == NULL)
|
||||||
{
|
{
|
||||||
retval = ll_map_new_entry(head->key, head->value);
|
if (noretval != POP_NO_RETURN_VAL)
|
||||||
|
retval = ll_map_new_entry(head->key, head->value);
|
||||||
|
if (free_values)
|
||||||
|
free(head->value);
|
||||||
free(head);
|
free(head);
|
||||||
return (retval);
|
return (retval);
|
||||||
}
|
}
|
||||||
|
@ -76,21 +81,24 @@ t_map_entry *ll_pop_last(t_map_entry *head)
|
||||||
while (current->next->next != NULL)
|
while (current->next->next != NULL)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
|
|
||||||
retval = ll_map_new_entry(current->next->key, current->next->value);
|
if (noretval != POP_NO_RETURN_VAL)
|
||||||
|
retval = ll_map_new_entry(current->next->key, current->next->value);
|
||||||
|
if (free_values)
|
||||||
|
free(current->next->value);
|
||||||
free(current->next);
|
free(current->next);
|
||||||
current->next = NULL;
|
current->next = NULL;
|
||||||
return (retval);
|
return (retval);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return (NULL);
|
return (retval);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ll_clear(t_map_entry *head)
|
void ll_clear(t_map_entry *head, int free_values)
|
||||||
{
|
{
|
||||||
if (head != NULL)
|
if (head != NULL)
|
||||||
{
|
{
|
||||||
while (head->next != NULL)
|
while (head->next != NULL)
|
||||||
free(ll_pop_last(head));
|
ll_pop_last(head, POP_NO_RETURN_VAL, free_values);
|
||||||
free(ll_pop_last(head));
|
ll_pop_last(head, POP_NO_RETURN_VAL, free_values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/01 07:13:31 by tischmid #+# #+# */
|
/* Created: 2023/04/01 07:13:31 by tischmid #+# #+# */
|
||||||
/* Updated: 2023/04/01 17:20:44 by tischmid ### ########.fr */
|
/* Updated: 2023/04/01 20:41:50 by tischmid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -61,3 +61,24 @@ int ft_strcmp(char *s1, char *s2)
|
||||||
}
|
}
|
||||||
return (*s1 - *s2);
|
return (*s1 - *s2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *ft_strcpy(char *dest, char *src)
|
||||||
|
{
|
||||||
|
char *o_dest;
|
||||||
|
|
||||||
|
o_dest = dest;
|
||||||
|
while (*src)
|
||||||
|
*dest++ = *src++;
|
||||||
|
*dest = 0;
|
||||||
|
return (o_dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_strlen(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (*str++)
|
||||||
|
++i;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/01 15:25:59 by tischmid #+# #+# */
|
/* Created: 2023/04/01 15:25:59 by tischmid #+# #+# */
|
||||||
/* Updated: 2023/04/01 17:44:27 by tischmid ### ########.fr */
|
/* Updated: 2023/04/01 20:58:03 by tischmid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -20,10 +20,15 @@ typedef struct s_map_entry
|
||||||
struct s_map_entry *next;
|
struct s_map_entry *next;
|
||||||
} t_map_entry;
|
} t_map_entry;
|
||||||
|
|
||||||
|
typedef enum e_ll_flag
|
||||||
|
{
|
||||||
|
POP_NO_RETURN_VAL
|
||||||
|
} t_ll_flag;
|
||||||
|
|
||||||
char *ll_map_get(t_map_entry *head, char *key);
|
char *ll_map_get(t_map_entry *head, char *key);
|
||||||
void ll_map_push(t_map_entry *head, char *key, char *value);
|
void ll_map_push(t_map_entry *head, char *key, char *value);
|
||||||
void ll_clear(t_map_entry *head);
|
void ll_clear(t_map_entry *head, int free_values);
|
||||||
t_map_entry *ll_map_new_entry(char *key, char *value);
|
t_map_entry *ll_map_new_entry(char *key, char *value);
|
||||||
t_map_entry *ll_pop_last(t_map_entry *head);
|
t_map_entry *ll_pop_last(t_map_entry *head, t_ll_flag noretval, int free_values);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/01 07:14:29 by tischmid #+# #+# */
|
/* Created: 2023/04/01 07:14:29 by tischmid #+# #+# */
|
||||||
/* Updated: 2023/04/01 17:21:46 by tischmid ### ########.fr */
|
/* Updated: 2023/04/01 20:42:50 by tischmid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -20,6 +20,8 @@ void ft_putchar(char c);
|
||||||
void ft_putstr(char *str);
|
void ft_putstr(char *str);
|
||||||
void ft_putnbr(int nb);
|
void ft_putnbr(int nb);
|
||||||
int puterr(char *errmsg, int errcode);
|
int puterr(char *errmsg, int errcode);
|
||||||
|
char *ft_strcpy(char *dest, char *src);
|
||||||
int ft_strcmp(char *s1, char *s2);
|
int ft_strcmp(char *s1, char *s2);
|
||||||
|
int ft_strlen(char *str);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
54
ex00/main.c
54
ex00/main.c
|
@ -6,66 +6,28 @@
|
||||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */
|
/* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */
|
||||||
/* Updated: 2023/04/01 18:19:25 by tischmid ### ########.fr */
|
/* Updated: 2023/04/01 21:45:18 by tischmid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "ft_strlib.h"
|
#include "ft_strlib.h"
|
||||||
#include "argparse.h"
|
#include "argparse.h"
|
||||||
|
#include "dictparse.h"
|
||||||
#include "ft_linked_list.h"
|
#include "ft_linked_list.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "ft_io.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
/* read `size' bytes from file `path' beginning at `offset', storing
|
|
||||||
* all chars in `buf', where `buf' is terminated at the first newline
|
|
||||||
* found. On success, returns number of characters found, -1 on error
|
|
||||||
* or EOF with 0 chars read.
|
|
||||||
*/
|
|
||||||
int readline(/*char *buf, unsigned int size, */ char *path/*, int offset*/)
|
|
||||||
{
|
|
||||||
int counter;
|
|
||||||
int fd;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
fd = open(path, O_RDONLY);
|
|
||||||
if (fd < 0)
|
|
||||||
return (-1);
|
|
||||||
counter = 0;
|
|
||||||
while (read(fd, &c, sizeof(c)) > 0)
|
|
||||||
if (c == 1)
|
|
||||||
++counter;
|
|
||||||
close(fd);
|
|
||||||
return (counter);
|
|
||||||
}
|
|
||||||
|
|
||||||
int parse_dict(char *path/*, t_map_entry *map*/)
|
|
||||||
{
|
|
||||||
printf("Number of 1's: %d\n", readline(path));
|
|
||||||
|
|
||||||
// if (fd < 0)
|
|
||||||
// return (0);
|
|
||||||
// if (close(fd) < 0)
|
|
||||||
// return (1);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char *str_nbr;
|
char *str_nbr;
|
||||||
char *path;
|
char *path;
|
||||||
// t_map_entry *map;
|
t_map_entry *map;
|
||||||
|
|
||||||
if (!parse_args(argc, argv, &path, &str_nbr))
|
if (!parse_args(argc, argv, &path, &str_nbr))
|
||||||
return (puterr("Error\n", 1));
|
return (puterr("Error\n", 1));
|
||||||
// if (!parse_dict(path, map))
|
map = ll_map_new_entry("about", "Dict for the data from the .dict file");
|
||||||
// return (puterr("Dict Error\n", 2));
|
if (!parse_dict(path, map))
|
||||||
// ll_clear(map);
|
return (puterr("Dict Error\n", 2));
|
||||||
parse_dict(path);
|
printf("%s\n", ll_map_get(map, "20"));
|
||||||
|
ll_clear(map, 1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue