c_next.c \
c_help.c \
c_load.c \
+ c_save.c \
+ c_open.c \
SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
load,
refresh_schema,
switch_to_schema,
+ save,
+ openf,
exitsim,
} t_command;
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);
--- /dev/null
+#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);
+}
--- /dev/null
+
+#include "FET_sim.h"
+#include "libft.h"
+#include <stdint.h>
+#include <unistd.h>
+
+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);
+}
--- /dev/null
+#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();
+}
*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);
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)
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);
}
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)