From: Lukáš Jiřiště Date: Fri, 25 Sep 2026 17:49:16 +0000 (+0200) Subject: Make empty command reopen the file given X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=refs%2Fheads%2Ffile_repeat_parsing;p=FET_sim.git Make empty command reopen the file given --- diff --git a/src/main.c b/src/main.c index a9aab7d..b830fdd 100644 --- a/src/main.c +++ b/src/main.c @@ -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 ; }