From 50c900d3dc695154e067ffadf869e9f580cbe89f Mon Sep 17 00:00:00 2001 From: Timo Schmidt Date: Sat, 1 Apr 2023 22:10:42 +0200 Subject: [PATCH] Normed and beautified --- ex00/Makefile | 37 ++++++++++-------- ex00/argparse.c | 4 +- ex00/dictparse.c | 60 +++++++++-------------------- ex00/ft_io.c | 36 ++++++++++++++++- ex00/ft_linked_list.c | 58 +++++++++++++--------------- ex00/ft_strlib.c | 42 +------------------- ex00/include/colors.h | 3 +- ex00/include/dictparse.h | 21 ++++++++++ ex00/include/ft_io.h | 5 ++- ex00/include/ft_linked_list.h | 10 ++--- ex00/include/ft_strlib.h | 7 +--- ex00/include/{main.h => printing.h} | 15 +++++--- ex00/main.c | 6 +-- ex00/printing.c | 56 +++++++++++++++++++++++++++ 14 files changed, 203 insertions(+), 157 deletions(-) create mode 100644 ex00/include/dictparse.h rename ex00/include/{main.h => printing.h} (67%) create mode 100644 ex00/printing.c diff --git a/ex00/Makefile b/ex00/Makefile index ea9f36f..a54884c 100644 --- a/ex00/Makefile +++ b/ex00/Makefile @@ -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' "<</dev/null || { printf '\033[101;97m%s\033[m\n' "!Norminette Failed>>>"; norminette $<; printf '\033[101;97m%s\033[m\n' "<< +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/ex00/dictparse.c b/ex00/dictparse.c index d78d42f..b11c259 100644 --- a/ex00/dictparse.c +++ b/ex00/dictparse.c @@ -1,42 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dictparse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 -#include -#include -#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); } - diff --git a/ex00/ft_io.c b/ex00/ft_io.c index 7fcbc1d..c51bda1 100644 --- a/ex00/ft_io.c +++ b/ex00/ft_io.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +} diff --git a/ex00/ft_linked_list.c b/ex00/ft_linked_list.c index cc12829..8193f03 100644 --- a/ex00/ft_linked_list.c +++ b/ex00/ft_linked_list.c @@ -6,18 +6,17 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include 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); } } diff --git a/ex00/ft_strlib.c b/ex00/ft_strlib.c index 46c111b..1f0a153 100644 --- a/ex00/ft_strlib.c +++ b/ex00/ft_strlib.c @@ -6,52 +6,12 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) diff --git a/ex00/include/colors.h b/ex00/include/colors.h index 9326c41..bba3139 100644 --- a/ex00/include/colors.h +++ b/ex00/include/colors.h @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/ex00/include/dictparse.h b/ex00/include/dictparse.h new file mode 100644 index 0000000..4eb92e0 --- /dev/null +++ b/ex00/include/dictparse.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dictparse.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/ex00/include/ft_io.h b/ex00/include/ft_io.h index e882a77..7fa5455 100644 --- a/ex00/include/ft_io.h +++ b/ex00/include/ft_io.h @@ -6,13 +6,16 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/ex00/include/ft_linked_list.h b/ex00/include/ft_linked_list.h index 150a025..de337c3 100644 --- a/ex00/include/ft_linked_list.h +++ b/ex00/include/ft_linked_list.h @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/ex00/include/ft_strlib.h b/ex00/include/ft_strlib.h index 7122cb9..5aa44ab 100644 --- a/ex00/include/ft_strlib.h +++ b/ex00/include/ft_strlib.h @@ -6,20 +6,15 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -# include # 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); diff --git a/ex00/include/main.h b/ex00/include/printing.h similarity index 67% rename from ex00/include/main.h rename to ex00/include/printing.h index 6d73526..c4b3d8d 100644 --- a/ex00/include/main.h +++ b/ex00/include/printing.h @@ -1,16 +1,21 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* main.h :+: :+: :+: */ +/* printing.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* 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 diff --git a/ex00/main.c b/ex00/main.c index 192d141..1c81dd3 100644 --- a/ex00/main.c +++ b/ex00/main.c @@ -6,15 +6,15 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 int main(int argc, char **argv) { diff --git a/ex00/printing.c b/ex00/printing.c new file mode 100644 index 0000000..4d62980 --- /dev/null +++ b/ex00/printing.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* printing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +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); +}