LCOV - code coverage report
Current view: top level - src - clusterlst.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 0 54 0.0 %
Date: 2024-11-12 08:59:49 Functions: 0 11 0.0 %

          Line data    Source code
       1             : /**
       2             :  *    @file  clusterlst.c
       3             :  *   @brief  Basic(non-thread-safe) single-chained list of record with sentinal
       4             :  *
       5             :  *  <+DETAILED+>
       6             :  *
       7             :  *  @author  François Cerbelle (Fanfan), francois@cerbelle.net
       8             :  *
       9             :  *  @internal
      10             :  *       Created:  25/10/2024
      11             :  *      Revision:  none
      12             :  * Last modified:  2024-11-10 09:38
      13             :  *      Compiler:  gcc
      14             :  *  Organization:  Cerbelle.net
      15             :  *     Copyright:  Copyright (c) 2024, François Cerbelle
      16             :  *
      17             :  *  This source code is released for free distribution under the terms of the
      18             :  *  GNU General Public License as published by the Free Software Foundation.
      19             :  */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : #include "config.h"
      23             : #endif
      24             : 
      25             : #include "clusterlst.h"
      26             : 
      27             : #include <stdlib.h>
      28             : #include <stdio.h>
      29             : #include <string.h>
      30             : 
      31             : typedef struct clusterrecord_s {
      32             :     struct clusterrecord_s* next;
      33             :     cluster_t* cluster;
      34             : } clusterrecord_t;
      35             : 
      36             : static clusterrecord_t* clusterlistfirst;
      37             : static clusterrecord_t* clusterlistlast;
      38             : static clusterrecord_t* clusterlistcursor;
      39             : 
      40             : static void clusterlist_add_postinit (cluster_t* cluster);
      41             : static cluster_t* clusterlist_find_postinit (const char* host);
      42             : static cluster_t* clusterlist_first_postinit();
      43             : static cluster_t* clusterlist_next_postinit();
      44             : static cluster_t* clusterlist_get_postinit();
      45             : 
      46           0 : static void clusterlist_init() {
      47           0 :     if (NULL==(clusterlistfirst=malloc(sizeof(struct clusterrecord_s)))) {
      48           0 :         perror("clusterlist_init");
      49           0 :         exit(EXIT_FAILURE);
      50             :     }
      51             :     /* Initialize chained structure */
      52           0 :     clusterlistlast = clusterlistfirst;
      53           0 :     clusterlistcursor = clusterlistfirst;
      54           0 :     clusterlistlast->next=NULL;
      55           0 :     clusterlistlast->cluster=NULL;
      56             : 
      57             :     /* From now, the application can use the real functions */
      58           0 :     clusterlist_add = clusterlist_add_postinit;
      59           0 :     clusterlist_find = clusterlist_find_postinit;
      60           0 :     clusterlist_first = clusterlist_first_postinit;
      61           0 :     clusterlist_next = clusterlist_next_postinit;
      62           0 :     clusterlist_get = clusterlist_get_postinit;
      63           0 : }
      64             : 
      65           0 : static void clusterlist_add_postinit (cluster_t* cluster) {
      66             :     /* Store the record in the sentinel */
      67           0 :     clusterlistlast->cluster=cluster;
      68             : 
      69             :     /* Create a new sentinel */
      70           0 :     if (NULL==(clusterlistlast->next=malloc(sizeof(struct clusterrecord_s)))) {
      71           0 :         perror("clusterlist_add_postinit malloc(sentinel)");
      72           0 :         exit(EXIT_FAILURE);
      73             :     }
      74           0 :     clusterlistlast = clusterlistlast->next;
      75           0 :     clusterlistlast->next=NULL;
      76           0 :     clusterlistlast->cluster=NULL;
      77           0 : }
      78           0 : static void clusterlist_add_preinit (cluster_t* cluster) {
      79           0 :     clusterlist_init();
      80           0 :     clusterlist_add(cluster);
      81           0 : }
      82             : void (*clusterlist_add)(cluster_t* cluster) = clusterlist_add_preinit;
      83             : 
      84           0 : static cluster_t* clusterlist_find_postinit(const char* host) {
      85           0 :     clusterlistcursor = clusterlistfirst;
      86           0 :     while (clusterlistcursor->cluster&&
      87           0 :             (strcmp(host,clusterlistcursor->cluster->host)))
      88           0 :         clusterlistcursor = clusterlistcursor->next;
      89           0 :     return (clusterlistcursor->cluster);
      90             : }
      91           0 : static cluster_t* clusterlist_find_preinit(const char* host) {
      92           0 :     clusterlist_init();
      93           0 :     return clusterlist_find(host);
      94             : }
      95             : cluster_t* (*clusterlist_find)(const char* host) = clusterlist_find_preinit;
      96             : 
      97           0 : static cluster_t* clusterlist_first_postinit() {
      98           0 :     clusterlistcursor = clusterlistfirst;
      99           0 :     return (clusterlistcursor->cluster);
     100             : }
     101           0 : static cluster_t* clusterlist_first_preinit() {
     102           0 :     clusterlist_init();
     103           0 :     return clusterlist_first();
     104             : }
     105             : cluster_t* (*clusterlist_first)() = clusterlist_first_preinit;
     106             : 
     107           0 : static cluster_t* clusterlist_next_postinit() {
     108           0 :     if (clusterlistcursor->next)
     109           0 :         clusterlistcursor = clusterlistcursor->next;
     110           0 :     return (clusterlistcursor->cluster);
     111             : }
     112           0 : static cluster_t* clusterlist_next_preinit() {
     113           0 :     clusterlist_init();
     114           0 :     return clusterlist_next();
     115             : }
     116             : cluster_t* (*clusterlist_next)() = clusterlist_next_preinit;
     117             : 
     118           0 : static cluster_t* clusterlist_get_postinit() {
     119           0 :     return (clusterlistcursor->cluster);
     120             : }
     121           0 : static cluster_t* clusterlist_get_preinit() {
     122           0 :     clusterlist_init();
     123           0 :     return clusterlist_get();
     124             : }
     125             : cluster_t* (*clusterlist_get)() = clusterlist_get_preinit;
     126             : 
     127             : /* vim: set tw=80: */

Generated by: LCOV version 1.16