From: Lukáš Jiřiště Date: Tue, 8 Sep 2026 20:41:13 +0000 (+0200) Subject: Implement saving the current schema X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=1b3d10779bd8b16439c1d653351a16f7312729cc;p=FET_sim.git Implement saving the current schema --- diff --git a/Makefile b/Makefile index 7a35b33..2489b97 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,8 @@ SOURCES := main.c \ c_next.c \ c_help.c \ c_load.c \ + c_save.c \ + c_open.c \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/inc/FET_sim.h b/inc/FET_sim.h index 1fc9fa1..fe72abb 100644 --- a/inc/FET_sim.h +++ b/inc/FET_sim.h @@ -104,6 +104,8 @@ typedef enum e_command load, refresh_schema, switch_to_schema, + save, + openf, exitsim, } t_command; @@ -208,6 +210,8 @@ int c_bind(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets); int c_connect(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets); int c_draw(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets); int c_load(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets); +int c_save(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets); +int c_open(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets); int c_help(WINDOW *command_win, t_input input); int c_next(t_input input, t_vec *nodes, t_vec *mosfets); int c_setnode(WINDOW *command_win, t_input input, t_vec *nodes); diff --git a/src/c_open.c b/src/c_open.c new file mode 100644 index 0000000..2e934fd --- /dev/null +++ b/src/c_open.c @@ -0,0 +1,10 @@ +#include "FET_sim.h" + +int c_open(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets) +{ + (void)command_win; + (void)input; + (void)nodes; + (void)mosfets; + return (0); +} diff --git a/src/c_save.c b/src/c_save.c new file mode 100644 index 0000000..0aecd70 --- /dev/null +++ b/src/c_save.c @@ -0,0 +1,163 @@ + +#include "FET_sim.h" +#include "libft.h" +#include +#include + +static const char g_signature[] = "FET_SIM"; + +typedef uint32_t t_save; + +static const t_save g_version = 0; + +typedef int t_el_write(int, const void *); + +static int write_vec(int fd, const t_vec *vec, t_el_write write_el); + +typedef struct +{ + t_save id; + t_save is_opened; + t_save type; + t_save gate; + t_save source; + t_save drain; + t_save position_x; + t_save position_y; + t_save orientation; +} t_mosfet_save; + +typedef struct +{ + t_save id; + t_save checked; + t_save state; + t_save set_state; +} t_node_save; + +typedef struct +{ + t_save position_x; + t_save position_y; + t_save connects; +} t_seg_save; + +static t_save to_big_endian(t_save arg) +{ + return (arg); +} + +static int write_save(int fd, t_save *buffer, size_t byte_count) +{ + size_t saves_count; + size_t i; + + saves_count = byte_count / sizeof(*buffer); + i = 0; + while (i < saves_count) + { + buffer[i] = to_big_endian(buffer[i]); + ++i; + } + return (write(fd, buffer, byte_count)); +} + +static int write_header(int fd) +{ + int res; + t_save version_copy; + + res = write(fd, g_signature, sizeof(g_signature)); + version_copy = g_version; + res = res || write_save(fd, &version_copy, sizeof(g_version)); + return (res); +} + +static int write_mosfet(int fd, const void *v_mosfet) +{ + const t_mosfet *mosfet = v_mosfet; + t_mosfet_save mosfet_copy; + + mosfet_copy.id = mosfet->id; + mosfet_copy.is_opened = mosfet->is_opened; + mosfet_copy.type = mosfet->type; + mosfet_copy.gate = mosfet->gate; + mosfet_copy.source = mosfet->source; + mosfet_copy.drain = mosfet->drain; + mosfet_copy.position_x = mosfet->position.x; + mosfet_copy.position_y = mosfet->position.y; + mosfet_copy.orientation = mosfet->orientation; + return (write_save(fd, (t_save *)&mosfet_copy, sizeof(mosfet_copy))); +} + +static int write_id(int fd, const void *v_id) +{ + const t_id *id = v_id; + t_save id_copy; + + id_copy = *id; + return (write_save(fd, &id_copy, sizeof(id_copy))); +} + +static int write_segment(int fd, const void *v_segment) +{ + const t_node_segment *seg = v_segment; + t_seg_save seg_copy; + + seg_copy.position_x = seg->position.x; + seg_copy.position_y = seg->position.y; + seg_copy.connects = seg->connects; + return (write_save(fd, (t_save *)&seg_copy, sizeof(seg_copy))); +} + +static int write_node(int fd, const void *v_node) +{ + const t_node *node = v_node; + t_node_save node_copy; + int res; + + node_copy.id = node->id; + node_copy.checked = node->checked; + node_copy.state = node->state; + node_copy.set_state = node->set_state; + res = write_save(fd, (t_save *)&node_copy, sizeof(node_copy)); + res = res || write_vec(fd, &node->connected, write_id); + res = res || write_vec(fd, &node->connected_gates, write_id); + res = res || write_vec(fd, &node->segments, write_segment); + return (res); +} + +static int write_vec(int fd, const t_vec *vec, t_el_write write_el) +{ + t_save val; + size_t i; + int res; + + val = to_big_endian(vec->size); + res = write(fd, &val, sizeof(val)); + i = 0; + while (i < vec->size) + { + if (res) + { + return (res); + } + res = write_el(fd, ft_vec_caccess(vec, i++)); + } + return (res); +} + + +int c_save(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets) +{ + const int fd = input.argv[0].val.fd; + + if (write_header(fd) + || write_vec(fd, mosfets, write_mosfet) + || write_vec(fd, nodes, write_node)) + { + wprintw(command_win, "Could not write into file\n"); + } + close(fd); + return (1); +} diff --git a/src/graph_builder.c b/src/graph_builder.c new file mode 100644 index 0000000..ef29d39 --- /dev/null +++ b/src/graph_builder.c @@ -0,0 +1,68 @@ +#include "FET_sim.h" +#include "libft.h" + +typedef union u_original_el_ptr +{ + t_node *node; + t_mosfet *mosfet; +} t_original_el_ptr; + +#define MAX_ORDER 4 + +typedef struct s_graph_node t_graph_node; + +struct s_graph_node +{ + int order; + unsigned int st_number; + int is_mosfet; + t_original_el_ptr original; + t_graph_node *connected_nodes[MAX_ORDER]; +} + +unsigned int get_st_number() +{ + static unsigned int st_number = 0; + + return (st_number++); +} + +t_list *new_node(u_original_el_ptr *el_ptr, int is_mosfet) +{ + t_list *list; + + node->st_number = get_st_number(); + node->original = el_ptr; + if (is_mosfet) + { + node->order = 3; + node->is_mosfet = is_mosfet; + } + for (size_t i = 0; i < MAX_ORDER; ++i) + { + node->connected_nodes[i] = NULL; + } + return (node); +} + +int create_proxy_graph(t_list *proxy_graph, t_vec *nodes, t_vec *mosfets) +{ + t_graph_node *node; + + proxy_graph = ft_lstnew(malloc(sizeof(*node))) + node = proxy_graph->content; + if (!list || !node) + { + free(list); + return (1); + } +} + +int construct_graph(t_vec *nodes, t_vec *mosfets) +{ + t_list *proxy_graph; + + create_proxy_graph(&proxy_graph, nodes, mosfets); + create_st_ordering(&proxy_graph); + draw(); +} diff --git a/src/main.c b/src/main.c index 1f8340b..2772e54 100644 --- a/src/main.c +++ b/src/main.c @@ -68,6 +68,10 @@ int parse_command(const char *str, t_command *cmd) *cmd = exitsim; else if (!ft_strcmp(str, "schema")) *cmd = switch_to_schema; + else if (!ft_strcmp(str, "save")) + *cmd = save; + else if (!ft_strcmp(str, "open")) + *cmd = openf; else return (0); return (1); @@ -129,6 +133,10 @@ int has_correct_argc(t_input input) return (0); if (c == load && argc != 1) return (0); + if (c == save || argc != 1) + return (0); + if (c == openf || argc != 1) + return (0); if (c == setnode && argc != 2) return (0); if (c == bind && argc != 3) @@ -204,6 +212,18 @@ int parse_arg(t_input *input, const char *str, size_t i) input->argv[i].val.fd = open(str, O_RDONLY); res = (input->argv[i].val.fd >= 0); } + else if (c == save) + { + input->argv[i].type = file_descriptor; + input->argv[i].val.fd = open(str, O_WRONLY); + 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); } @@ -272,6 +292,13 @@ int process_input(t_windows *windows, t_vec *nodes, t_vec *mosfets) res = c_load(windows->command_win, input, nodes, mosfets); refresh_schema_win(windows->schematics_win, nodes, mosfets); } + else if (input.command == save) + res = c_save(windows->command_win, input, nodes, mosfets); + else if (input.command == openf) + { + res = c_open(windows->command_win, input, nodes, mosfets); + refresh_schema_win(windows->schematics_win, nodes, mosfets); + } else if (input.command == help) res = c_help(windows->command_win, input); else if(input.command == refresh_schema)