piscine-rush02/ex00/main.c

126 lines
2.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */
/* Updated: 2023/04/01 16:35:17 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "main.h"
#include "ft_lib.h"
#include "argparse.h"
#include "ft_linked_list.h"
#include <stdio.h>
#include <stdlib.h>
int ft_strcmp(char *s1, char *s2)
{
while (*s1 && *s2 && *s1 == *s2)
{
++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));
}
}
int main(int argc, char **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);
}