Normed and beautified

This commit is contained in:
Timo Schmidt 2023-04-01 22:10:42 +02:00
parent 9f747e3214
commit 50c900d3dc
14 changed files with 203 additions and 157 deletions

View File

@ -2,13 +2,16 @@ SRC = main.c \
ft_strlib.c \
ft_io.c \
argparse.c \
ft_linked_list.c
HEADERS = main.h \
ft_strlib.h \
dictparse.c \
ft_linked_list.c \
printing.c
HEADERS = ft_strlib.h \
ft_io.h \
colors.h \
argparse.h \
ft_linked_list.h
dictparse.h \
ft_linked_list.h \
printing.h
OBJDIR = obj
INCDIR = include
@ -28,7 +31,7 @@ DEPS = $(addprefix $(INCDIR)/,$(HEADERS))
RM = /bin/rm -f
RMDIR = /bin/rmdir
.DEFAULT_GOAL=test
.DEFAULT_GOAL = test
NAME ?= rush-02
@ -51,7 +54,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;97m%s\033[m\n' "!Norminette Failed>>>"; norminette $<; printf '\033[101;97m%s\033[m\n' "<<<Norminette Failed!"; exit 1; }
@$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR):
@ -61,17 +64,17 @@ tests: test
test: re run fclean
SUCCESS_MSG=printf '\n\033[102;30m --- %s --- \033[m\n\n' "Test passed!"
FAIL_MSG=printf '\n\033[101;37m --- %s --- \033[m\n\n' "Test failed!"
SUCCESS_MSG_VALG=printf '\n\033[102;30m --- %s --- \033[m\n\n' "Valgrind ran without errors!"
FAIL_MSG_VALG=printf '\n\033[101;37m --- %s --- \033[m\n\n' "Valgrind Failed!"
SUCCESS=&& $(SUCCESS_MSG) || $(FAIL_MSG)
SUCCESS_VALG=&& $(SUCCESS_MSG_VALG) || $(FAIL_MSG_VALG)
FAIL=&& $(FAIL_MSG) || $(SUCCESS_MSG)
VALGRIND=valgrind --leak-check=full --error-exitcode=1
YLW=:;
CYN=:;
CLR_RST=&& : 
SUCCESS_MSG = printf '\n\033[102;30m --- %s --- \033[m\n\n' "Test passed!"
FAIL_MSG = printf '\n\033[101;37m --- %s --- \033[m\n\n' "Test failed!"
SUCCESS_MSG_VALG = printf '\n\033[102;30m --- %s --- \033[m\n\n' "Valgrind ran without errors!"
FAIL_MSG_VALG = printf '\n\033[101;37m --- %s --- \033[m\n\n' "Valgrind Failed!"
SUCCESS = && $(SUCCESS_MSG) || $(FAIL_MSG)
SUCCESS_VALG = && $(SUCCESS_MSG_VALG) || $(FAIL_MSG_VALG)
FAIL = && $(FAIL_MSG) || $(SUCCESS_MSG)
VALGRIND = valgrind --leak-check=full --error-exitcode=1
YLW = :;
CYN = :;
CLR_RST = && : 
run:
@clear
$(YLW) ./$(NAME) $(CLR_RST) $(SUCCESS)

View File

@ -6,13 +6,14 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 15:12:53 by tischmid #+# #+# */
/* Updated: 2023/04/01 17:48:25 by tischmid ### ########.fr */
/* Updated: 2023/04/01 21:58:44 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "argparse.h"
#include "ft_io.h"
// TODO: Bonus for (argc < 2): Use read to take nbr from stdin
int parse_args(int argc, char **argv, char **path, char **str_nbr)
{
if (argc > 3)
@ -20,7 +21,6 @@ int parse_args(int argc, char **argv, char **path, char **str_nbr)
*path = "./numbers.dict";
if (argc < 2)
{
// TODO: Bonus: Use read to take nbr from stdin
*str_nbr = "123";
}
else

View File

@ -1,42 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dictparse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 21:45:39 by tischmid #+# #+# */
/* Updated: 2023/04/01 21:59:39 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "dictparse.h"
#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)
{
@ -46,16 +21,18 @@ int parse_line(char *line, char **key, char **value)
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;
(void)map;
offset = 0;
for (int i = 0; i < 20; ++i)
idx = -1;
while (++idx < 20)
{
offset = readline(buf, MAX_LINE_LENGTH, path, offset);
if (offset == FILE_READ_ERROR)
@ -64,8 +41,7 @@ int parse_dict(char *path, t_map_entry *map)
return (0);
if (!parse_line(buf, &key, &value))
return (0);
ll_map_push(map, key, /* value */buf);
ll_map_push(map, key, buf);
}
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 18:03:28 by tischmid ### ########.fr */
/* Updated: 2023/04/01 21:58:18 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,10 +19,42 @@ int file_readable(char *path)
{
int fd;
fd = open(path, O_RDONLY, 0);
fd = open(path, O_RDONLY);
if (fd < 0)
return (0);
if (close(fd) < 0)
return (0);
return (1);
}
/*
* 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);
}

View File

@ -6,18 +6,17 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 15:25:42 by tischmid #+# #+# */
/* Updated: 2023/04/01 20:59:06 by tischmid ### ########.fr */
/* Updated: 2023/04/01 22:08:37 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_linked_list.h"
#include "ft_strlib.h"
#include <stdio.h>
#include <stdlib.h>
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));
@ -48,7 +47,7 @@ char *ll_map_get(t_map_entry *head, char *key)
void ll_map_push(t_map_entry *head, char *key, char *value)
{
t_map_entry *current;
t_map_entry *current;
if (head != NULL)
{
@ -59,46 +58,41 @@ void ll_map_push(t_map_entry *head, char *key, char *value)
}
}
t_map_entry *ll_pop_last(t_map_entry *head, t_ll_flag noretval, int free_values)
t_map_entry *ll_pop_last(t_map_entry *head, t_ret_flag noretval, int free_vals)
{
t_map_entry *current;
t_map_entry *retval;
t_map_entry *current;
t_map_entry *retval;
retval = NULL;
if (head != NULL)
if (head == NULL)
return (retval);
if (head->next == NULL)
{
if (head->next == NULL)
{
if (noretval != POP_NO_RETURN_VAL)
retval = ll_map_new_entry(head->key, head->value);
if (free_values)
free(head->value);
free(head);
return (retval);
}
current = head;
while (current->next->next != NULL)
current = current->next;
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);
current->next = NULL;
retval = ll_map_new_entry(head->key, head->value);
if (free_vals)
free(head->value);
free(head);
return (retval);
}
else
return (retval);
current = head;
while (current->next->next != NULL)
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);
free(current->next);
current->next = NULL;
return (retval);
}
void ll_clear(t_map_entry *head, int free_values)
void ll_clear(t_map_entry *head, int free_vals)
{
if (head != NULL)
{
while (head->next != NULL)
ll_pop_last(head, POP_NO_RETURN_VAL, free_values);
ll_pop_last(head, POP_NO_RETURN_VAL, free_values);
ll_pop_last(head, POP_NO_RETURN_VAL, free_vals);
ll_pop_last(head, POP_NO_RETURN_VAL, free_vals);
}
}

View File

@ -6,52 +6,12 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 07:13:31 by tischmid #+# #+# */
/* Updated: 2023/04/01 20:41:50 by tischmid ### ########.fr */
/* Updated: 2023/04/01 21:55:16 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_strlib.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
while (*str)
ft_putchar(*str++);
}
void ft_putnbr(int nb)
{
if (nb > 9)
{
ft_putnbr(nb / 10);
ft_putchar(nb % 10 + '0');
}
else if (nb == INT_MIN)
{
ft_putnbr(nb / 10);
ft_putnbr(-(nb % 10));
}
else if (nb < 0)
{
ft_putchar('-');
ft_putnbr(-nb);
}
else
ft_putchar(nb % 10 + '0');
}
int puterr(char *errmsg, int errcode)
{
ft_putstr(RED);
ft_putstr(errmsg);
ft_putstr(RESET);
return (errcode);
}
int ft_strcmp(char *s1, char *s2)
{
while (*s1 && *s2 && *s1 == *s2)

View File

@ -6,7 +6,7 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 08:20:09 by tischmid #+# #+# */
/* Updated: 2023/04/01 08:20:46 by tischmid ### ########.fr */
/* Updated: 2023/04/01 22:09:16 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,4 +16,5 @@
# define RED "\x1b[31m"
# define GREEN "\x1b[32m"
# define YELLOW "\x1b[33m"
# define CYAN "\x1b[36m"
#endif

21
ex00/include/dictparse.h Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dictparse.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 21:48:18 by tischmid #+# #+# */
/* Updated: 2023/04/01 21:51:46 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DICTPARSE_H
# define DICTPARSE_H
# define MAX_LINE_LENGTH 1000
# include "ft_linked_list.h"
int parse_line(char *line, char **key, char **value);
int parse_dict(char *path, t_map_entry *map);
#endif

View File

@ -6,13 +6,16 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 08:52:17 by tischmid #+# #+# */
/* Updated: 2023/04/01 18:19:46 by tischmid ### ########.fr */
/* Updated: 2023/04/01 21:51:56 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_IO_H
# define FT_IO_H
# define FILE_READ_ERROR -1
# define LINE_TOO_LONG -2
int file_readable(char *path);
int readline(char *buf, int size, char *path, int offset);
#endif

View File

@ -6,7 +6,7 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 15:25:59 by tischmid #+# #+# */
/* Updated: 2023/04/01 20:58:03 by tischmid ### ########.fr */
/* Updated: 2023/04/01 22:08:28 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,15 +20,15 @@ typedef struct s_map_entry
struct s_map_entry *next;
} t_map_entry;
typedef enum e_ll_flag
typedef enum
{
POP_NO_RETURN_VAL
} t_ll_flag;
} t_ret_flag;
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_values);
void ll_clear(t_map_entry *head, int free_vals);
t_map_entry *ll_map_new_entry(char *key, char *value);
t_map_entry *ll_pop_last(t_map_entry *head, t_ll_flag noretval, int free_values);
t_map_entry *ll_pop_last(t_map_entry *head, t_ret_flag noretval, int free_vals);
#endif

View File

@ -6,20 +6,15 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 07:14:29 by tischmid #+# #+# */
/* Updated: 2023/04/01 20:42:50 by tischmid ### ########.fr */
/* Updated: 2023/04/01 22:01:32 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_STRLIB_H
# define FT_STRLIB_H
# include <unistd.h>
# include <limits.h>
# include "colors.h"
void ft_putchar(char c);
void ft_putstr(char *str);
void ft_putnbr(int nb);
int puterr(char *errmsg, int errcode);
char *ft_strcpy(char *dest, char *src);
int ft_strcmp(char *s1, char *s2);
int ft_strlen(char *str);

View File

@ -1,16 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.h :+: :+: :+: */
/* printing.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 08:15:08 by tischmid #+# #+# */
/* Updated: 2023/04/01 08:56:56 by tischmid ### ########.fr */
/* Created: 2023/04/01 21:56:26 by tischmid #+# #+# */
/* Updated: 2023/04/01 21:57:00 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAIN_H
# define MAIN_H
#ifndef PRINTING_H
# define PRINTING_H
int puterr(char *errmsg, int errcode);
void ft_putnbr(int nb);
void ft_putstr(char *str);
void ft_putchar(char c);
#endif

View File

@ -6,15 +6,15 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */
/* Updated: 2023/04/01 21:45:18 by tischmid ### ########.fr */
/* Updated: 2023/04/01 22:02:19 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "main.h"
#include "ft_strlib.h"
#include "argparse.h"
#include "dictparse.h"
#include "ft_linked_list.h"
#include "printing.h"
#include <stdio.h>
int main(int argc, char **argv)
{

56
ex00/printing.c Normal file
View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 21:55:18 by tischmid #+# #+# */
/* Updated: 2023/04/01 22:01:07 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "printing.h"
#include "colors.h"
#include <unistd.h>
#include <limits.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
while (*str)
ft_putchar(*str++);
}
void ft_putnbr(int nb)
{
if (nb > 9)
{
ft_putnbr(nb / 10);
ft_putchar(nb % 10 + '0');
}
else if (nb == INT_MIN)
{
ft_putnbr(nb / 10);
ft_putnbr(-(nb % 10));
}
else if (nb < 0)
{
ft_putchar('-');
ft_putnbr(-nb);
}
else
ft_putchar(nb % 10 + '0');
}
int puterr(char *errmsg, int errcode)
{
ft_putstr(RED);
ft_putstr(errmsg);
ft_putstr(RESET);
return (errcode);
}