Make FET_sim able to take input from a file
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 19 Feb 2024 20:59:04 +0000 (21:59 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 19 Feb 2024 20:59:04 +0000 (21:59 +0100)
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
inc/FET_sim.h
src/main.c

diff --git a/TODO b/TODO
index ed9fb54ae4d725371b15fa874a2a09280b29916d..f80ad086ea337c54a410240fa4ffe2c39d3594cb 100644 (file)
--- 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?)
index 4647b11c640edda92a005a225214eb3a2f3140e3..285a7a243894d1d6bc9a45482f9e2e5ed7e4e8e6 100644 (file)
@@ -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);
index ac6d4a0d4c1fbcd75fd098ae94af118e695f6b1c..7f2e2a446475586f3a2e0c90f13ab3a37d71a1ba 100644 (file)
@@ -1,16 +1,23 @@
 #include "FET_sim.h"
 #include "libft.h"
 #include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
 
-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);