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)
{
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);
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);
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 ;
}