/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Gem-graph client * * Finite State Machine - Journal * * * * Copyright © 2024 Libre en Communs * * Copyright © 2024 Jean Sirmai * * * * 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 . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include #include #include #include #include "../../include/fsm.h" /******************************************************************************/ /* A journal stores chronologically the fsm events */ /* during a session run (rules exec, mainly) */ /******************************************************************************/ /* time_t current_time = time(NULL); // Check if the time retrieval was successful if (current_time == ((time_t)-1)) printf("Error getting current time.\n"); // Convert to local time format and print printf(" Current timestamp: %ld %s\n", current_time, message); printf(" Current time: %s", ctime(¤t_time)); */ void fsm_journal_init (journal *jj, char *message) { jj->first = NULL; jj->last = NULL; } void fsm_journal_clear (journal *jj, char *message) { 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_push_front (journal *jj, char *message) { struct timeval tv; gettimeofday(&tv, NULL); unit *new_unit = malloc(sizeof(unit)); if (! new_unit) exit (EXIT_FAILURE); new_unit->yy_dd_mm = tv.tv_sec; new_unit->usec = tv.tv_usec; new_unit->message = message; 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; } long fsm_journal_pop_back (journal *jj, char *message) { long usec; unit *tmp = jj->last; if (! tmp) return -1; usec = tmp->usec; jj->last = tmp->prev; if (jj->last) jj->last->next = NULL; else jj->first = NULL; free (tmp); return usec; // retourne l'évènement retiré du journal. } int fsm_journal_length (journal jj) { unit *a_unit = jj.first; int nb = 0; while (a_unit) { nb ++; a_unit = a_unit->next; } // printf ("fsm journal length = %d\n", nb); return nb; } void fsm_journal_seek (journal jj, long usec, char *message) { unit *a_unit = jj.first; int nb = 0; while (a_unit) { if (usec == a_unit->usec) nb++; a_unit = a_unit->next; } if (nb > 0) printf ("> date (usec) %ld found %d times in journal\n", usec, nb); else printf ("> date (usec) %ld not found in journal\n", usec); } //------------------------------------------------------------------------------------ static void fsm_journal_get_event_date (char *event) { struct timeval tv; gettimeofday (&tv, NULL); long yy_dd_mm = tv.tv_sec; long usec = tv.tv_usec; char buf1[]=""; strftime (buf1, JOURNAL_DATE_MAX_LENGTH, "%D %T", localtime (&yy_dd_mm)); printf ("%s + %6ld ", buf1, usec); // *event = "ldkvbhej xadrhsjfvrejhs "; } void fsm_journal_publish (journal jj, char *message) { unit *a_unit = jj.last; char buf [JOURNAL_DATE_MAX_LENGTH]; char myString[] = "Hello World"; fsm_journal_get_event_date (myString); int nb = 0; while (a_unit) { // Format time, "ddd yyyy-mm-dd hh:mm:ss zzz" "%Y-%m-%d %H:%M:%S" // https://www.man7.org/linux/man-pages/man3/strftime.3.html strftime(buf, sizeof(buf), "%D %T", localtime(&a_unit->yy_dd_mm)); printf ("%s + %6ld %6d %s\n", buf, a_unit->usec, nb, a_unit->message); a_unit = a_unit->prev; nb ++; } // char *event [JOURNAL_DATE_MAX_LENGTH] = "gyufgbyfjuvfde"; // int **p_event [JOURNAL_DATE_MAX_LENGTH]; fsm_journal_get_event_date (message); printf ("%4d %s\n", nb, message); } // https://nicolasj.developpez.com/articles/libc/string/ // https://thelinuxcode.com/pass-string-function-c-language/ // /* void modifyString(char *str) { *str = ‘J‘; // Dereference to change first char } int main() { char myString[] = "Hello World"; modifyString(myString); printf("%s", myString); // Prints Jello World } Because modifyString receives a pointer to the string, * it can modify the original by dereferencing. */