Line data Source code
1 : /** 2 : * @file clusterlst.c 3 : * @brief Self initialized cluster records list (non thread-safe) 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-19 18:42 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 : #include "sclist.h" 27 : 28 : #include <stdlib.h> 29 : #include <string.h> 30 : 31 : static sclist_t* clusterlist=NULL; 32 : static sclistrecord_t* clusterlist_cursor=NULL; 33 : 34 : static void clusterlist_add_postinit (cluster_t* cluster); 35 : static cluster_t* clusterlist_find_postinit (const char* host); 36 : static cluster_t* clusterlist_first_postinit(); 37 : static cluster_t* clusterlist_next_postinit(); 38 : static cluster_t* clusterlist_get_postinit(); 39 : 40 45 : static void clusterlist_init() { 41 : /* Initialize the structure */ 42 45 : clusterlist = sclist_new(); 43 45 : clusterlist_cursor = NULL; 44 : 45 : /* From now, the application can directly use the real functions */ 46 45 : clusterlist_add = clusterlist_add_postinit; 47 45 : clusterlist_find = clusterlist_find_postinit; 48 45 : clusterlist_first = clusterlist_first_postinit; 49 45 : clusterlist_next = clusterlist_next_postinit; 50 45 : clusterlist_get = clusterlist_get_postinit; 51 45 : } 52 : 53 116 : static void clusterlist_add_postinit (cluster_t* cluster) { 54 116 : sclist_addrecord(clusterlist,cluster); 55 116 : } 56 10 : static void clusterlist_add_preinit (cluster_t* cluster) { 57 10 : clusterlist_init(); 58 10 : clusterlist_add(cluster); 59 10 : } 60 : void (*clusterlist_add)(cluster_t* cluster) = clusterlist_add_preinit; 61 : 62 : 63 58 : static cluster_t* clusterlist_find_postinit(const char* host) { 64 58 : cluster_t* cluster=NULL; 65 58 : clusterlist_cursor = sclist_firstrecord(clusterlist); 66 105 : while (clusterlist_cursor) { 67 66 : cluster = sclist_getvalue(clusterlist_cursor); 68 66 : if ( 69 66 : ((host==NULL)&&(cluster->host==NULL))|| 70 63 : ((host!=NULL)&&(cluster->host!=NULL)&&(0==strcmp(host,cluster->host)))) 71 : break; 72 47 : clusterlist_cursor = sclist_nextrecord(clusterlist_cursor); 73 : }; 74 58 : return (clusterlist_cursor?cluster:NULL); 75 : } 76 20 : static cluster_t* clusterlist_find_preinit(const char* host) { 77 20 : clusterlist_init(); 78 20 : return clusterlist_find(host); 79 : } 80 : cluster_t* (*clusterlist_find)(const char* host) = clusterlist_find_preinit; 81 : 82 : 83 46 : static cluster_t* clusterlist_first_postinit() { 84 46 : clusterlist_cursor = sclist_firstrecord(clusterlist); 85 46 : return (clusterlist_cursor?sclist_getvalue(clusterlist_cursor):NULL); 86 : } 87 2 : static cluster_t* clusterlist_first_preinit() { 88 2 : clusterlist_init(); 89 2 : return clusterlist_first(); 90 : } 91 : cluster_t* (*clusterlist_first)() = clusterlist_first_preinit; 92 : 93 : 94 96 : static cluster_t* clusterlist_next_postinit() { 95 96 : clusterlist_cursor = (clusterlist_cursor?sclist_nextrecord(clusterlist_cursor):NULL); 96 96 : return (clusterlist_cursor?sclist_getvalue(clusterlist_cursor):NULL); 97 : } 98 2 : static cluster_t* clusterlist_next_preinit() { 99 2 : clusterlist_init(); 100 2 : return clusterlist_next(); 101 : } 102 : cluster_t* (*clusterlist_next)() = clusterlist_next_preinit; 103 : 104 : 105 32 : static cluster_t* clusterlist_get_postinit() { 106 32 : return (clusterlist_cursor?sclist_getvalue(clusterlist_cursor):NULL); 107 : } 108 11 : static cluster_t* clusterlist_get_preinit() { 109 11 : clusterlist_init(); 110 11 : return clusterlist_get(); 111 : } 112 : cluster_t* (*clusterlist_get)() = clusterlist_get_preinit; 113 : 114 : /* vim: set tw=80: */