]> www.ljiriste.work Git - FET_sim.git/commitdiff
Make empty command reopen the file given file_repeat_parsing
authorLukáš Jiřiště <redacted>
Fri, 25 Sep 2026 17:49:16 +0000 (19:49 +0200)
committerLukáš Jiřiště <redacted>
Fri, 25 Sep 2026 17:49:16 +0000 (19:49 +0200)
src/main.c

index a9aab7d3beaa4ae855e09ef9cd9af7f350ef65df..b830fddf36a632d66937c56779f9b482ccaf96be 100644 (file)
@@ -163,6 +163,45 @@ int        str_is_num(const char *str)
        return (str[i] == '\0');
 }
 
+int    parse_file(t_input *input, const char *file_name)
+{
+       static char     *file_name_last = NULL;
+
+       if (input->command == exitsim)
+       {
+               free(file_name_last);
+               file_name_last = NULL;
+               return (1);
+       }
+       if (file_name)
+       {
+               free(file_name_last);
+               file_name_last = ft_strdup(file_name);
+       }
+       if (!file_name_last)
+       {
+               return (0);
+       }
+       input->argv[0].type = file_descriptor;
+       if (input->command == load || input->command == openf)
+       {
+               input->argv[0].val.fd = open(file_name_last, O_RDONLY);
+       }
+       else if (input->command == save)
+       {
+               input->argv[0].val.fd = open(file_name_last, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+       }
+       return (input->argv[0].val.fd >= 0);
+}
+
+static int     uses_file(t_command c)
+{
+       return (c == load
+                       || c == save
+                       || c == openf
+                  );
+}
+
 // maybe atos (alphanum to size_t) should be implemented
 int    parse_arg(t_input *input, const char *str, size_t i)
 {
@@ -207,23 +246,10 @@ int       parse_arg(t_input *input, const char *str, size_t i)
                        res = parse_terminal(str, &input->argv[i].val.terminal);
                        input->argv[i].type = terminal;
                }
-               else if (c == load)
-               {
-                       input->argv[i].type = file_descriptor;
-                       input->argv[i].val.fd = open(str, O_RDONLY);
-                       res = (input->argv[i].val.fd >= 0);
-               }
-               else if (c == save)
+               else if (uses_file(c))
                {
+                       res = parse_file(input, str);
                        input->argv[i].type = file_descriptor;
-                       input->argv[i].val.fd = open(str, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
-                       res = (input->argv[i].val.fd >= 0);
-               }
-               else if (c == openf)
-               {
-                       input->argv[i].type = file_descriptor;
-                       input->argv[i].val.fd = open(str, O_RDONLY);
-                       res = (input->argv[i].val.fd >= 0);
                }
        }
        return (res);
@@ -235,7 +261,13 @@ int        construct_input(t_input *input, char **split_inp)
        t_input tmp;
 
        if (!split_inp || !split_inp[0])
+       {
+               if (uses_file(input->command))
+               {
+                       return (parse_file(input, NULL));
+               }
                return (1);
+       }
        tmp.argc = split_length(split_inp) - 1;
        if (!parse_command(split_inp[0], &tmp.command))
                return (0);
@@ -313,9 +345,14 @@ int        process_input(t_windows *windows, t_vec *nodes, t_vec *mosfets)
 
 void   cleanup(t_vec *nodes, t_vec *mosfets, t_windows *windows)
 {
+       t_input dummy_input;
+
+
        update_nodes(NULL, NULL);
        ft_vec_free(nodes, free_node);
        ft_vec_free(mosfets, NULL);
+       dummy_input.command = exitsim;
+       parse_file(&dummy_input, NULL);
        clean_terminal(windows);
        return ;
 }