Linked List Map Implementation half baked

This commit is contained in:
Timo Schmidt 2023-04-01 16:35:54 +02:00
parent 9f4413439d
commit 83768e22d7
9 changed files with 195 additions and 15 deletions

36
ex00/argparse.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* argparse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 15:12:53 by tischmid #+# #+# */
/* Updated: 2023/04/01 15:13:56 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "argparse.h"
#include "ft_io.h"
int parse_args(int argc, char **argv, char **path, char **str_nbr)
{
if (argc > 3)
return (0);
*path = "./numbers.dict";
if (argc < 2)
{
// TODO: Bonus: Use read to take nbr from stdin
*str_nbr = "123";
}
else
*str_nbr = argv[1];
if (argc == 3)
{
if (!file_exists(argv[1]))
return (0);
*path = argv[1];
*str_nbr = argv[2];
}
return (1);
}

View File

@ -6,7 +6,7 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 08:51:59 by tischmid #+# #+# */
/* Updated: 2023/04/01 08:58:01 by tischmid ### ########.fr */
/* Updated: 2023/04/01 14:57:53 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,9 @@
int file_exists(char *path)
{
int fd_num_dict = open(path, O_RDONLY, 0);
int fd_num_dict;
fd_num_dict = open(path, O_RDONLY, 0);
if (fd_num_dict < 0)
return (0);
if (close(fd_num_dict) < 0)

13
ex00/ft_linked_list.c Normal file
View File

@ -0,0 +1,13 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_linked_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 15:25:42 by tischmid #+# #+# */
/* Updated: 2023/04/01 15:25:50 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_linked_list.h"

18
ex00/include/argparse.h Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* argparse.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 15:13:05 by tischmid #+# #+# */
/* Updated: 2023/04/01 15:13:21 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARGPARSE_H
# define ARGPARSE_H
int parse_args(int argc, char **argv, char **path, char **str_nbr);
#endif

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_linked_list.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 15:25:59 by tischmid #+# #+# */
/* Updated: 2023/04/01 15:26:10 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_LINKED_LIST_H
# define FT_LINKED_LIST_H
typedef struct s_map_entry
{
char *key;
char *value;
struct s_map_entry *next;
} t_map_entry;
#endif

View File

@ -6,32 +6,120 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */
/* Updated: 2023/04/01 14:59:26 by tischmid ### ########.fr */
/* Updated: 2023/04/01 16:35:17 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "main.h"
#include "ft_lib.h"
#include "ft_io.h"
#include "argparse.h"
#include "ft_linked_list.h"
int parse_args(int argc, char **argv)
#include <stdio.h>
#include <stdlib.h>
int ft_strcmp(char *s1, char *s2)
{
if (argc > 3)
return (0);
if (argc == 3)
if (!file_exists(argv[1]))
return (0);
if (argc < 2)
while (*s1 && *s2 && *s1 == *s2)
{
// TODO: Bonus: Use read to take nbr from stdin
return (0);
++s1;
++s2;
}
return (*s1 - *s2);
}
t_map_entry *ll_map_new_node(char *key, char *value)
{
t_map_entry *new_node;
new_node = (t_map_entry *) malloc(sizeof(t_map_entry));
new_node->key = key;
new_node->value = value;
return (new_node);
}
char *ll_map_get(t_map_entry *head, char *key)
{
t_map_entry *current;
if (head != NULL)
{
current = head;
while (current != NULL)
{
if (!ft_strcmp(current->key, key))
return (current->value);
current = current->next;
}
}
return (NULL);
}
void ll_map_push(t_map_entry *head, char *key, char *value)
{
t_map_entry *current;
current = head;
if (head != NULL)
{
while (current->next != NULL)
current = current->next;
current->next = ll_map_new_node(key, value);
}
else
ft_putstr("head is NULL\n");
}
t_map_entry *ll_pop_last(t_map_entry *head)
{
t_map_entry *current;
t_map_entry *retval;
if (head != NULL)
{
if (head->next == NULL)
{
retval = ll_map_new_node(head->key, head->value);
free(head);
return (retval);
}
current = head;
while (current->next->next != NULL)
current = current->next;
retval = ll_map_new_node(current->next->key, current->next->value);
free(current->next);
current->next = NULL;
return (retval);
}
else
return (NULL);
}
void ll_clear(t_map_entry *head)
{
if (head != NULL)
{
while (head->next != NULL)
free(ll_pop_last(head));
free(ll_pop_last(head));
}
return (1);
}
int main(int argc, char **argv)
{
if (!parse_args(argc, argv))
char *str_nbr;
char *path;
// t_map_entry *map;
if (!parse_args(argc, argv, &path, &str_nbr))
return (puterr("Error\n", 1));
// if (!parse_dict(path, map))
// return (puterr("Dict Error\n", 2));
t_map_entry *head = ll_map_new_node("hello", "world");
ll_map_push(head, "hello2", "world2");
// printf("%s\n", ll_map_get(head, "hello2"));
ll_clear(head);
return (0);
}

BIN
ex00/obj/argparse.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.