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 55 : static void clusterlist_init() { 47 55 : if (NULL==(clusterlistfirst=malloc(sizeof(struct clusterrecord_s)))) { 48 0 : perror("clusterlist_init"); 49 0 : exit(EXIT_FAILURE); 50 : } 51 : /* Initialize chained structure */ 52 55 : clusterlistlast = clusterlistfirst; 53 55 : clusterlistcursor = clusterlistfirst; 54 55 : clusterlistlast->next=NULL; 55 55 : clusterlistlast->cluster=NULL; 56 : 57 : /* From now, the application can use the real functions */ 58 55 : clusterlist_add = clusterlist_add_postinit; 59 55 : clusterlist_find = clusterlist_find_postinit; 60 55 : clusterlist_first = clusterlist_first_postinit; 61 55 : clusterlist_next = clusterlist_next_postinit; 62 55 : clusterlist_get = clusterlist_get_postinit; 63 55 : } 64 : 65 118 : static void clusterlist_add_postinit (cluster_t* cluster) { 66 : /* Store the record in the sentinel */ 67 118 : clusterlistlast->cluster=cluster; 68 : 69 : /* Create a new sentinel */ 70 118 : if (NULL==(clusterlistlast->next=malloc(sizeof(struct clusterrecord_s)))) { 71 0 : perror("clusterlist_add_postinit malloc(sentinel)"); 72 0 : exit(EXIT_FAILURE); 73 : } 74 118 : clusterlistlast = clusterlistlast->next; 75 118 : clusterlistlast->next=NULL; 76 118 : clusterlistlast->cluster=NULL; 77 118 : } 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 61 : static cluster_t* clusterlist_find_postinit(const char* host) { 86 61 : clusterlistcursor = clusterlistfirst; 87 96 : while (clusterlistcursor->cluster&& 88 : ( 89 54 : ((NULL!=host)&&(NULL==clusterlistcursor->cluster->host))|| 90 42 : ((NULL==host)&&(NULL!=clusterlistcursor->cluster->host))|| 91 39 : ((NULL!=host)&&(NULL!=clusterlistcursor->cluster->host)&& 92 39 : (strcmp(host,clusterlistcursor->cluster->host))) 93 : )) 94 35 : clusterlistcursor = clusterlistcursor->next; 95 61 : return (clusterlistcursor->cluster); 96 : } 97 20 : static cluster_t* clusterlist_find_preinit(const char* host) { 98 20 : clusterlist_init(); 99 20 : return clusterlist_find(host); 100 : } 101 : cluster_t* (*clusterlist_find)(const char* host) = clusterlist_find_preinit; 102 : 103 : 104 48 : static cluster_t* clusterlist_first_postinit() { 105 48 : clusterlistcursor = clusterlistfirst; 106 48 : 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 98 : static cluster_t* clusterlist_next_postinit() { 116 98 : if (clusterlistcursor->next) 117 94 : clusterlistcursor = clusterlistcursor->next; 118 98 : 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: */