From: Lukáš Jiřiště Date: Wed, 9 Sep 2026 21:00:18 +0000 (+0200) Subject: Implement opening saved files X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=d056fbc083294b1e4a9ae29d0bf6009a981610b9;p=FET_sim.git Implement opening saved files --- diff --git a/inc/FET_sim.h b/inc/FET_sim.h index fe72abb..08b2f6c 100644 --- a/inc/FET_sim.h +++ b/inc/FET_sim.h @@ -1,6 +1,7 @@ #ifndef FET_SIM_H # define FET_SIM_H +# include "signature.h" # include "libft.h" # include @@ -67,6 +68,14 @@ typedef struct s_node_segment t_bitflag connects; } t_node_segment; +typedef struct s_segment_save +{ + t_save position_x; + t_save position_y; + t_save connects; +} t_seg_save; + + typedef struct s_node { t_id id; @@ -78,6 +87,15 @@ typedef struct s_node t_vec segments; } t_node; +typedef struct s_node_save +{ + t_save id; + t_save checked; + t_save state; + t_save set_state; +} t_node_save; + + typedef struct s_mosfet { t_id id; @@ -90,6 +108,19 @@ typedef struct s_mosfet t_orientation orientation; } t_mosfet; +typedef struct s_mosfet_save +{ + 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 enum e_command { none, @@ -204,6 +235,10 @@ t_state resolve_state(t_vec *connected_nodes); void apply_state(t_state state, t_vec *connected_nodes); t_state reduce_state(t_state state); +t_save convert_big_endian(t_save arg); + +t_id manage_id(t_id request); + int c_addfet(WINDOW *command_win, t_input inputs, t_vec *mosfets); int c_addnode(WINDOW *command_win, t_input input, t_vec *nodes); int c_bind(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets); diff --git a/inc/signature.h b/inc/signature.h new file mode 100644 index 0000000..da6a1e5 --- /dev/null +++ b/inc/signature.h @@ -0,0 +1,13 @@ +#ifndef SIGNATURE_H +# define SIGNATURE_H + +# include + +typedef uint32_t t_save; + +static const char g_signature[] = "FET_SIM"; + +static const t_save g_version = 0; + + +#endif //SIGNATURE_H diff --git a/src/build_helper.c b/src/build_helper.c index eb7f73f..61bbb91 100644 --- a/src/build_helper.c +++ b/src/build_helper.c @@ -146,13 +146,22 @@ void bind_fet_node(t_vec *nodes, t_mosfet *mosfet, t_node *node, t_terminal term return ; } -static t_id get_new_id(void) +t_id manage_id(t_id request) { static t_id id = INVALID_ID + 1; + if (request != INVALID_ID && request > id) + { + id = request; + } return (id++); } +static t_id get_new_id(void) +{ + return (manage_id(INVALID_ID)); +} + t_node *add_node(t_vec *nodes, t_state set_state) { t_node node; diff --git a/src/c_open.c b/src/c_open.c index 2e934fd..ed47c1e 100644 --- a/src/c_open.c +++ b/src/c_open.c @@ -1,10 +1,161 @@ #include "FET_sim.h" +#include -int c_open(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets) +typedef int t_el_read(int fd, t_vec *vec); + +static int read_vec(int fd, t_vec *vec, t_el_read read_el); + +static int read_save(int fd, t_save *buffer, size_t byte_count) { - (void)command_win; - (void)input; - (void)nodes; - (void)mosfets; + size_t saves_count; + size_t i; + int res; + + saves_count = byte_count / sizeof(*buffer); + res = read(fd, buffer, byte_count); + if (res < 0 || (size_t)res != byte_count) + { + return (1); + } + i = 0; + while (i < saves_count) + { + buffer[i] = convert_big_endian(buffer[i]); + ++i; + } return (0); } + +static int read_header(int fd) +{ + int res; + char signature[sizeof(g_signature)]; + t_save version; + + res = read(fd, signature, sizeof(signature)); + if (res < 0 || (size_t)res != sizeof(signature) || ft_strcmp(signature, g_signature)) + { + return (1); + } + return (read_save(fd, &version, sizeof(version)) || version != g_version); +} + +static int read_mosfet(int fd, t_vec *mosfets) +{ + t_mosfet mosfet; + t_mosfet_save mosfet_copy; + int res; + + res = read_save(fd, (t_save *)&mosfet_copy, sizeof(mosfet_copy)); + if (!res) + { + mosfet.id = mosfet_copy.id; + mosfet.is_opened = mosfet_copy.is_opened; + mosfet.type = mosfet_copy.type; + mosfet.gate = mosfet_copy.gate; + mosfet.source = mosfet_copy.source; + mosfet.drain = mosfet_copy.drain; + mosfet.position.x = mosfet_copy.position_x; + mosfet.position.y = mosfet_copy.position_y; + mosfet.orientation = mosfet_copy.orientation; + res = ft_vec_append(mosfets, &mosfet); + manage_id(mosfet.id); + } + return (res); +} + +static int read_id(int fd, t_vec *ids) +{ + t_id id; + t_save id_copy; + int res; + + res = read_save(fd, &id_copy, sizeof(id_copy)); + if (!res) + { + id = id_copy; + res = ft_vec_append(ids, &id); + } + return (res); +} + +static int read_segment(int fd, t_vec *segments) +{ + t_node_segment segment; + t_seg_save seg_copy; + int res; + + res = read_save(fd, (t_save *)&seg_copy, sizeof(seg_copy)); + if (!res) + { + segment.position.x = seg_copy.position_x; + segment.position.y = seg_copy.position_y; + segment.connects = seg_copy.connects; + res = ft_vec_append(segments, &segment); + } + return (res); +} + +static int read_node(int fd, t_vec *nodes) +{ + t_node node; + t_node_save node_copy; + int res; + + res = read_save(fd, (t_save *)&node_copy, sizeof(node_copy)); + if (!res) + { + node.id = node_copy.id; + node.checked = node_copy.checked; + node.state = node_copy.state; + node.set_state = node_copy.set_state; + res = res || ft_vec_init(&node.connected, sizeof(t_id)); + res = res || ft_vec_init(&node.connected_gates, sizeof(t_id)); + res = res || ft_vec_init(&node.segments, sizeof(t_node_segment)); + res = res || read_vec(fd, &node.connected, read_id); + res = res || read_vec(fd, &node.connected_gates, read_id); + res = res || read_vec(fd, &node.segments, read_segment); + res = res || ft_vec_append(nodes, &node); + manage_id(node.id); + } + return (res); +} + +static int read_vec(int fd, t_vec *vec, t_el_read read_el) +{ + t_save size; + size_t i; + int res; + + res = read(fd, &size, sizeof(size)); + res = (res < 0 || (size_t)res != sizeof(size)); + size = convert_big_endian(size); + ft_vec_reserve(vec, size); + i = 0; + while (i < size) + { + if (res) + { + return (res); + } + res = read_el(fd, vec); + ++i; + } + return (res); +} + +int c_open(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets) +{ + const int fd = input.argv[0].val.fd; + + if (read_header(fd) + || read_vec(fd, mosfets, read_mosfet) + || read_vec(fd, nodes, read_node)) + { + close(fd); + wprintw(command_win, "Could not read file.\nAborting!\n"); + return (0); + } + close(fd); + return (1); +} diff --git a/src/c_save.c b/src/c_save.c index 9451d9e..efb8e9b 100644 --- a/src/c_save.c +++ b/src/c_save.c @@ -4,45 +4,11 @@ #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 *); +typedef int t_el_write(int fd, const void *el); 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) +t_save convert_big_endian(t_save arg) { return (arg); } @@ -57,7 +23,7 @@ static int write_save(int fd, t_save *buffer, size_t byte_count) i = 0; while (i < saves_count) { - buffer[i] = to_big_endian(buffer[i]); + buffer[i] = convert_big_endian(buffer[i]); ++i; } res = write(fd, buffer, byte_count); @@ -136,7 +102,7 @@ static int write_vec(int fd, const t_vec *vec, t_el_write write_el) size_t i; int res; - val = to_big_endian(vec->size); + val = convert_big_endian(vec->size); res = write(fd, &val, sizeof(val)); res = (res < 0 || (size_t)res != sizeof(val)); i = 0;