LCOV - code coverage report
Current view: top level - src - clusterlst.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 53 57 93.0 %
Date: 2024-11-14 00:59:13 Functions: 11 11 100.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-13 18:50
      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          37 : static void clusterlist_init() {
      47          37 :     if (NULL==(clusterlistfirst=malloc(sizeof(struct clusterrecord_s)))) {
      48           0 :         perror("clusterlist_init");
      49           0 :         exit(EXIT_FAILURE);
      50             :     }
      51             :     /* Initialize chained structure */
      52          37 :     clusterlistlast = clusterlistfirst;
      53          37 :     clusterlistcursor = clusterlistfirst;
      54          37 :     clusterlistlast->next=NULL;
      55          37 :     clusterlistlast->cluster=NULL;
      56             : 
      57             :     /* From now, the application can use the real functions */
      58          37 :     clusterlist_add = clusterlist_add_postinit;
      59          37 :     clusterlist_find = clusterlist_find_postinit;
      60          37 :     clusterlist_first = clusterlist_first_postinit;
      61          37 :     clusterlist_next = clusterlist_next_postinit;
      62          37 :     clusterlist_get = clusterlist_get_postinit;
      63          37 : }
      64             : 
      65          82 : static void clusterlist_add_postinit (cluster_t* cluster) {
      66             :     /* Store the record in the sentinel */
      67          82 :     clusterlistlast->cluster=cluster;
      68             : 
      69             :     /* Create a new sentinel */
      70          82 :     if (NULL==(clusterlistlast->next=malloc(sizeof(struct clusterrecord_s)))) {
      71           0 :         perror("clusterlist_add_postinit malloc(sentinel)");
      72           0 :         exit(EXIT_FAILURE);
      73             :     }
      74          82 :     clusterlistlast = clusterlistlast->next;
      75          82 :     clusterlistlast->next=NULL;
      76          82 :     clusterlistlast->cluster=NULL;
      77          82 : }
      78           2 : static void clusterlist_add_preinit (cluster_t* cluster) {
      79           2 :     clusterlist_init();
      80           2 :     clusterlist_add(cluster);
      81           2 : }
      82             : void (*clusterlist_add)(cluster_t* cluster) = clusterlist_add_preinit;
      83             : 
      84             : 
      85          25 : static cluster_t* clusterlist_find_postinit(const char* host) {
      86          25 :     clusterlistcursor = clusterlistfirst;
      87          52 :     while (clusterlistcursor->cluster&&
      88             :             (
      89          36 :                 ((NULL!=host)&&(NULL==clusterlistcursor->cluster->host))||
      90          24 :                 ((NULL==host)&&(NULL!=clusterlistcursor->cluster->host))||
      91          21 :                 ((NULL!=host)&&(NULL!=clusterlistcursor->cluster->host)&&
      92          21 :                  (strcmp(host,clusterlistcursor->cluster->host)))
      93             :             ))
      94          27 :         clusterlistcursor = clusterlistcursor->next;
      95          25 :     return (clusterlistcursor->cluster);
      96             : }
      97           2 : static cluster_t* clusterlist_find_preinit(const char* host) {
      98           2 :     clusterlist_init();
      99           2 :     return clusterlist_find(host);
     100             : }
     101             : cluster_t* (*clusterlist_find)(const char* host) = clusterlist_find_preinit;
     102             : 
     103             : 
     104          10 : static cluster_t* clusterlist_first_postinit() {
     105          10 :     clusterlistcursor = clusterlistfirst;
     106          10 :     return (clusterlistcursor->cluster);
     107             : }
     108           2 : static cluster_t* clusterlist_first_preinit() {
     109           2 :     clusterlist_init();
     110           2 :     return clusterlist_first();
     111             : }
     112             : cluster_t* (*clusterlist_first)() = clusterlist_first_preinit;
     113             : 
     114             : 
     115          22 : static cluster_t* clusterlist_next_postinit() {
     116          22 :     if (clusterlistcursor->next)
     117          18 :         clusterlistcursor = clusterlistcursor->next;
     118          22 :     return (clusterlistcursor->cluster);
     119             : }
     120           2 : static cluster_t* clusterlist_next_preinit() {
     121           2 :     clusterlist_init();
     122           2 :     return clusterlist_next();
     123             : }
     124             : cluster_t* (*clusterlist_next)() = clusterlist_next_preinit;
     125             : 
     126             : 
     127          28 : static cluster_t* clusterlist_get_postinit() {
     128          28 :     return (clusterlistcursor->cluster);
     129             : }
     130           2 : static cluster_t* clusterlist_get_preinit() {
     131           2 :     clusterlist_init();
     132           2 :     return clusterlist_get();
     133             : }
     134             : cluster_t* (*clusterlist_get)() = clusterlist_get_preinit;
     135             : 
     136             : /* vim: set tw=80: */

Generated by: LCOV version 1.16