From c46c0c0699691b8b4f9db451224e53dc424f84fe Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Tue, 10 Sep 2024 22:37:31 +0200 Subject: [PATCH] =?UTF-8?q?une=20double=20liste=20chain=C3=A9e=20est=20pr?= =?UTF-8?q?=C3=AAte=20=C3=A0=20h=C3=A9berger=20le=20fsm=20journal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/fsm.h | 29 +++++--- src/fsm/journal.c | 146 ++++++++++++++++++++++++++++---------- src/widget/dispatch.c | 4 +- src/widget/state/middle.c | 2 +- 4 files changed, 132 insertions(+), 49 deletions(-) diff --git a/include/fsm.h b/include/fsm.h index fc4c05a..88fd96d 100644 --- a/include/fsm.h +++ b/include/fsm.h @@ -45,8 +45,26 @@ enum fsm_measure_type {DATE_RULE_EXEC, RULE_EXEC_NB, OBJECT_NB, ELAPSED_TIME }; typedef struct tool_list {int value; struct tool_list *suiv;} tool_list ; typedef struct data_list {int value; struct data_list *suiv;} data_list ; typedef struct disp_list {int value; struct disp_list *suiv;} disp_list ; -typedef struct journal {int value; struct journal *prev;} journal; + +// --------------------------------------------------------------- WIP ------ +typedef struct unit {int event; struct unit *prev; struct unit *next;} unit; +typedef struct {unit *first; unit *last;} journal; // structure d'accès au journal + +void fsm_journal_init (journal *l); // initialise +void fsm_journal_clear (journal *l); // vide tout le journal +int fsm_journal_push_back (journal *l, int event); // ajoute un évènement en fin de journal +int fsm_journal_push_front (journal *l, int event); // ajoute un évènement en début de journal +int fsm_journal_pop_back (journal *l); // retire un évènement en fin de journal +int fsm_journal_pop_front (journal *l); // retire un évènement en début de journal +int fsm_journal_length (journal l); // nombre d'évènements dans le journal +void fsm_journal_seek (journal *jj, int event); // recherche un évènement dans le journal +void fsm_journal_view (journal l); // affiche tout le journal +void fsm_journal_test(); +// --------------------------------------------------------------- WIP ------ // ref: sudo cat /var/log/messages +// --------------------------------------------------------------- WIP ------ + + void fsm_init(); // def: fsm/dispatch; call: main; void fsm_engine_init(); // def: fsm/engine/engine; call main @@ -96,15 +114,6 @@ void fsm_tools_list_test(); // def: measure/manage; call: measure/manage; void fsm_rule_trig_measure (int rule_id, int object_id, int measure_id); -// --------------------------------------------------------------- WIP ------ -int fsm_journal_push (journal **jj, int v_value); -int fsm_journal_pop (journal **jj); -void fsm_journal_clear (journal **jj); -int fsm_journal_length (journal *jj); -void fsm_journal_view (journal *jj); -void fsm_journal_seek (journal *jj, int vv); - - // --------------------------------------------------------------- WIP ------ void fsm_add_data (data_list d, int *p_data, int *p_target); int fsm_get_data (data_list d, int from, int to); diff --git a/src/fsm/journal.c b/src/fsm/journal.c index a479879..fba0285 100644 --- a/src/fsm/journal.c +++ b/src/fsm/journal.c @@ -29,68 +29,140 @@ #include "../../include/fsm.h" /******************************************************************************/ -/* A journal (a pile) stores chronologically the fsm events */ -/* during a session run (rules exec, mainly) */ +/* A journal stores chronologically the fsm events */ +/* during a session run (rules exec, mainly) */ /******************************************************************************/ -int fsm_journal_push (journal **jj, int vv) +void fsm_journal_init (journal *jj) { - journal *element = malloc (sizeof(journal)); - if (!element) exit (EXIT_FAILURE); // Si l'allocation a échoué. - element->value = vv; - element->prev = *jj; - *jj = element; // Le pointeur pointe sur le dernier élément. - return vv; + jj->first = NULL; + jj->last = NULL; } -int fsm_journal_pop (journal **jj) +void fsm_journal_clear (journal *jj) { - int vv; - journal *tmp; - if (!*jj) return -1; // Retourne -1 si le journal est vide. - tmp = (*jj)->prev; - vv = (*jj)->value; - free (*jj); - *jj = tmp; // Le pointeur pointe sur le dernier élément. - return vv; // retourne la value retirée du journal. + unit *tmp; + unit *a_unit = jj->first; + while(a_unit) + { + tmp = a_unit; + a_unit = a_unit->next; + free(tmp); + } + jj->first = NULL; + jj->last = NULL; } -void fsm_journal_clear (journal **jj) +int fsm_journal_push_back (journal *jj, int event) { - journal *tmp; - while (*jj) + unit *new_unit = malloc (sizeof(unit)); + if (! new_unit) exit (EXIT_FAILURE); + new_unit->event = event; + new_unit->prev = jj->last; + new_unit->next = NULL; + if (jj->last) jj->last->next = new_unit; + else jj->first = new_unit; + jj->last = new_unit; + return event; +} + +int fsm_journal_push_front (journal *jj, int event) +{ + unit *new_unit = malloc(sizeof(unit)); + if (! new_unit) exit (EXIT_FAILURE); + new_unit->event = event; + new_unit->next = jj->first; + new_unit->prev = NULL; + if (jj->first) jj->first->prev = new_unit; + else jj->last = new_unit; + jj->first = new_unit; + return event; +} + +int fsm_journal_pop_back (journal *jj) +{ + int event; + unit *tmp = jj->last; + if (! tmp) return -1; + event = tmp->event; + jj->last = tmp->prev; + if (jj->last) jj->last->next = NULL; + else jj->first = NULL; + free (tmp); + return event; // retourne l'évènement retiré du journal. +} + +int fsm_journal_pop_front (journal *jj) +{ + int event; + unit *tmp = jj->first; + if (! tmp) return -1; + event = tmp->event; + jj->first = tmp->next; + if (jj->first) jj->first->prev = NULL; + else jj->last = NULL; + free (tmp); + return event; // retourne l'évènement retiré du journal. +} + +int fsm_journal_length (journal jj) +{ + unit *a_unit = jj.first; + int nb = 0; + while (a_unit) { - tmp = (*jj)->prev; - free (*jj); - *jj = tmp; + nb ++; + a_unit = a_unit->next; } + return nb; } -int fsm_journal_length (journal *jj) +void fsm_journal_seek (journal *jj, int event) { - int n=0; - while (jj) - { - n++; - jj = jj->prev; - } - return n; + } -void fsm_journal_view (journal *jj) +void fsm_journal_view (journal jj) { printf ("view journal (n = %d)\n", fsm_journal_length (jj)); - while (jj) + unit *a_unit = jj.first; + while (a_unit) { - printf ("> %d\n", jj->value); - jj = jj->prev; + printf ("> %d <\n", a_unit->event); + a_unit = a_unit->next; } puts ("------"); } -void fsm_journal_seek (journal *jj, int vv) +void fsm_journal_test() { + journal exemple; + fsm_journal_init (&exemple); + + printf ("push front > %d\n", fsm_journal_push_front (&exemple, 10)); + printf ("push back > %d\n", fsm_journal_push_back (&exemple, 100)); + printf ("push back > %d\n", fsm_journal_push_back (&exemple, 1000)); + printf ("push front > %d\n", fsm_journal_push_front (&exemple, 1)); + + fsm_journal_view (exemple); + + printf ("pop back > %d\n", fsm_journal_pop_back (&exemple)); + printf ("pop back > %d\n", fsm_journal_pop_back (&exemple)); + printf ("pop back > %d\n", fsm_journal_pop_back (&exemple)); + printf ("push back > %d\n", fsm_journal_push_back (&exemple, 2)); + + fsm_journal_view (exemple); + + printf ("push back > %d\n", fsm_journal_push_back (&exemple, 3)); + printf ("push front > %d\n", fsm_journal_push_front (&exemple, 0)); + + fsm_journal_view (exemple); + + printf ("clear list\n"); fsm_journal_clear (&exemple); + + fsm_journal_view (exemple); } + diff --git a/src/widget/dispatch.c b/src/widget/dispatch.c index 3a83363..b33f4f1 100644 --- a/src/widget/dispatch.c +++ b/src/widget/dispatch.c @@ -27,6 +27,7 @@ #include "../../include/widget.h" #include "../../include/fsm.h" +#include "../../include/util.h" /******************************************************************************/ @@ -90,7 +91,8 @@ void on_windows_activation (GtkApplication *app) widget_design_dialog_window (main_window, dialog_window); widget_design_text_window (main_window, text_window); // ------------------------------------------------------------------------ - + util_trigger_test(); // then go to util/tests.c and change if (0) to if (1)... + fsm_journal_test(); diff --git a/src/widget/state/middle.c b/src/widget/state/middle.c index 4a632e4..b7d0592 100644 --- a/src/widget/state/middle.c +++ b/src/widget/state/middle.c @@ -90,7 +90,7 @@ static void *get_ZOOM_box() void *widget_get_space_view(int partition_space_vs_camera) { GtkBox *drawbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0)); - util_gl_setup_glarea (0, GTK_WIDGET (drawbox)); +// util_gl_setup_glarea (0, GTK_WIDGET (drawbox)); GtkBox *camera = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 2)); gtk_box_append (camera, GTK_WIDGET (get_XYZ_box()));