gem-graph-client/journal.c

102 lines
3.8 KiB
C

/* * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Gem-graph client *
* *
* State machine / Measures *
* *
* Copyright © 2024 Libre en Communs <contact@a-lec.org> *
* Copyright © 2024 Adrien Bourmault <neox@a-lec.org> *
* Copyright © 2024 Jean Sirmai <jean@a-lec.org> *
* *
* This file is part of Gem-graph. *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, *
* either version 3 of the License, *
* or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; *
* without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../../include/fsm.h"
/******************************************************************************/
/* A journal (a pile) stores chronologically the fsm events */
/* during a session run (rules exec, mainly) */
/******************************************************************************/
int fsm_journal_push (journal **jj, int vv)
{
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;
}
int fsm_journal_pop (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.
}
void fsm_journal_clear (journal **jj)
{
journal *tmp;
while (*jj)
{
tmp = (*jj)->prev;
free (*jj);
*jj = tmp;
}
}
int fsm_journal_length (journal *jj)
{
int n=0;
while (jj)
{
n++;
jj = jj->prev;
}
return n;
}
void fsm_journal_view (journal *jj)
{
printf ("view journal (n = %d)\n", fsm_journal_length (jj));
while (jj)
{
printf ("> %d\n", jj->value);
jj = jj->prev;
}
puts ("------");
}
void fsm_journal_seek (journal *jj, int value)
{
}