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 25 : static void clusterlist_init() { 47 25 : if (NULL==(clusterlistfirst=malloc(sizeof(struct clusterrecord_s)))) { 48 0 : perror("clusterlist_init"); 49 0 : exit(EXIT_FAILURE); 50 : } 51 : /* Initialize chained structure */ 52 25 : clusterlistlast = clusterlistfirst; 53 25 : clusterlistcursor = clusterlistfirst; 54 25 : clusterlistlast->next=NULL; 55 25 : clusterlistlast->cluster=NULL; 56 : 57 : /* From now, the application can use the real functions */ 58 25 : clusterlist_add = clusterlist_add_postinit; 59 25 : clusterlist_find = clusterlist_find_postinit; 60 25 : clusterlist_first = clusterlist_first_postinit; 61 25 : clusterlist_next = clusterlist_next_postinit; 62 25 : clusterlist_get = clusterlist_get_postinit; 63 25 : } 64 : 65 7 : static void clusterlist_add_postinit (cluster_t* cluster) { 66 : /* Store the record in the sentinel */ 67 7 : clusterlistlast->cluster=cluster; 68 : 69 : /* Create a new sentinel */ 70 7 : if (NULL==(clusterlistlast->next=malloc(sizeof(struct clusterrecord_s)))) { 71 0 : perror("clusterlist_add_postinit malloc(sentinel)"); 72 0 : exit(EXIT_FAILURE); 73 : } 74 7 : clusterlistlast = clusterlistlast->next; 75 7 : clusterlistlast->next=NULL; 76 7 : clusterlistlast->cluster=NULL; 77 7 : } 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 4 : static cluster_t* clusterlist_find_postinit(const char* host) { 85 4 : clusterlistcursor = clusterlistfirst; 86 4 : while (clusterlistcursor->cluster&& 87 0 : (strcmp(host,clusterlistcursor->cluster->host))) 88 0 : clusterlistcursor = clusterlistcursor->next; 89 4 : return (clusterlistcursor->cluster); 90 : } 91 2 : static cluster_t* clusterlist_find_preinit(const char* host) { 92 2 : clusterlist_init(); 93 2 : return clusterlist_find(host); 94 : } 95 : cluster_t* (*clusterlist_find)(const char* host) = clusterlist_find_preinit; 96 : 97 4 : static cluster_t* clusterlist_first_postinit() { 98 4 : clusterlistcursor = clusterlistfirst; 99 4 : return (clusterlistcursor->cluster); 100 : } 101 2 : static cluster_t* clusterlist_first_preinit() { 102 2 : clusterlist_init(); 103 2 : return clusterlist_first(); 104 : } 105 : cluster_t* (*clusterlist_first)() = clusterlist_first_preinit; 106 : 107 4 : static cluster_t* clusterlist_next_postinit() { 108 4 : if (clusterlistcursor->next) 109 0 : clusterlistcursor = clusterlistcursor->next; 110 4 : return (clusterlistcursor->cluster); 111 : } 112 2 : static cluster_t* clusterlist_next_preinit() { 113 2 : clusterlist_init(); 114 2 : return clusterlist_next(); 115 : } 116 : cluster_t* (*clusterlist_next)() = clusterlist_next_preinit; 117 : 118 4 : static cluster_t* clusterlist_get_postinit() { 119 4 : return (clusterlistcursor->cluster); 120 : } 121 2 : static cluster_t* clusterlist_get_preinit() { 122 2 : clusterlist_init(); 123 2 : return clusterlist_get(); 124 : } 125 : cluster_t* (*clusterlist_get)() = clusterlist_get_preinit; 126 : 127 : /* vim: set tw=80: */