From 3895416895efa43741a1ed048d2f96d8f657db26 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Mon, 19 Feb 2024 21:59:04 +0100 Subject: [PATCH] Make FET_sim able to take input from a file The first argument is taken as a filename which FET_sim reads as it would read standard input. After EOF it switches to STDIN input. --- TODO | 1 + inc/FET_sim.h | 4 ++-- src/main.c | 41 +++++++++++++++++++++++------------------ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/TODO b/TODO index ed9fb54..f80ad08 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,5 @@ TODO +make build_graph not output rewrite command parsing (what I've created is just ugly) come up with better file structure create language to write circuits in (circuit language?) (something like MHRD language?) diff --git a/inc/FET_sim.h b/inc/FET_sim.h index 4647b11..285a7a2 100644 --- a/inc/FET_sim.h +++ b/inc/FET_sim.h @@ -99,8 +99,8 @@ void add_mosfet(t_vec *mosfets, t_type type); void bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal); void free_node(void *node); -int process_input(t_vec *nodes, t_vec *mosfets); -int get_input(t_input *input); +int process_input(t_vec *nodes, t_vec *mosfets, int fd); +int get_input(t_input *input, int fd); const char *state_color_escape(t_state state); void draw_single(t_vec *mosfets, size_t i); diff --git a/src/main.c b/src/main.c index ac6d4a0..7f2e2a4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,23 @@ #include "FET_sim.h" #include "libft.h" #include +#include +#include -void build_graph(__attribute__((unused)) const char *filename, t_vec *nodes, t_vec *mosfets) +void build_graph(const char *filename, t_vec *nodes, t_vec *mosfets) { - add_node(nodes, off); - add_node(nodes, off); - add_node(nodes, pull_up); - add_mosfet(mosfets, p); - bind_fet_node(ft_vec_access(mosfets, 0), ft_vec_access(nodes, 0), gate); - bind_fet_node(ft_vec_access(mosfets, 0), ft_vec_access(nodes, 1), drain); - bind_fet_node(ft_vec_access(mosfets, 0), ft_vec_access(nodes, 2), source); + int fd; + + fd = open(filename, O_RDONLY); + if (fd >= 0) + { + while (process_input(nodes, mosfets, fd)); + close(fd); + } + else + { + ft_printf("An error has occured reading file %s.\n", filename); + } return ; } @@ -206,13 +213,13 @@ int construct_input(t_input *input, char **split_inp) return (1); } -int get_input(t_input *input) +int get_input(t_input *input, int fd) { int res; char *str_inp; char **split_inp; - str_inp = get_next_line(0); + str_inp = get_next_line(fd); if (!str_inp) { input->command = exitsim; @@ -234,13 +241,13 @@ int get_input(t_input *input) return (res); } -int process_input(t_vec *nodes, t_vec *mosfets) +int process_input(t_vec *nodes, t_vec *mosfets, int fd) { int res; static t_input input = {.command = help, .argc = 0}; ft_printf("FET_sim> "); - if (!get_input(&input)) + if (!get_input(&input, fd)) return (1); res = 1; if (input.command == next) @@ -262,7 +269,7 @@ int process_input(t_vec *nodes, t_vec *mosfets) return (res); } -int main(__attribute__((unused)) int argc, char **argv) +int main(int argc, char **argv) { t_vec nodes; t_vec mosfets; @@ -270,11 +277,9 @@ int main(__attribute__((unused)) int argc, char **argv) print_start(); ft_vec_init(&nodes, sizeof(t_node)); ft_vec_init(&mosfets, sizeof(t_mosfet)); - build_graph(argv[1], &nodes, &mosfets); - while (process_input(&nodes, &mosfets)) - { - continue ; - } + if (argc > 1) + build_graph(argv[1], &nodes, &mosfets); + while (process_input(&nodes, &mosfets, STDIN_FILENO)); update_nodes(NULL); ft_vec_free(&nodes, free_node); ft_vec_free(&mosfets, NULL); -- 2.30.2