Line data Source code
1 : /** 2 : * @file sclist.c 3 : * @brief Basic single chained generic list 4 : * 5 : * @author François Cerbelle (Fanfan), francois@cerbelle.net 6 : * 7 : * @internal 8 : * Created: 15/11/2024 9 : * Revision: none 10 : * Last modified: 2024-11-25 08:56 11 : * Compiler: gcc 12 : * Organization: Cerbelle.net 13 : * Copyright: Copyright (c) 2024, François Cerbelle 14 : * 15 : * This source code is released for free distribution under the terms of the 16 : * GNU General Public License as published by the Free Software Foundation. 17 : */ 18 : 19 : #ifdef HAVE_CONFIG_H 20 : #include "config.h" 21 : #endif 22 : 23 : #include "sclist.h" 24 : 25 : #include <stdlib.h> 26 : #include <stdio.h> 27 : #include <assert.h> 28 : 29 : /** Private list record structure 30 : */ 31 : typedef struct sclistrecord_s { 32 : void* value; /**< Pointer to value */ 33 : struct sclistrecord_s* next; /**< Next record in the list */ 34 : } sclistrecord_t; 35 : 36 : /** Opaque sclist structure 37 : */ 38 : typedef struct sclist_s { 39 : sclistrecord_t* first; /**< Pointer to the first record */ 40 : sclistrecord_t* last; /**< Pointer to the last record */ 41 : } sclist_t; 42 : 43 192 : sclist_t* sclist_new() { 44 : sclist_t* sclist; 45 192 : if (NULL==(sclist=malloc(sizeof(struct sclist_s)))) { 46 0 : perror("sclist_new OOM"); 47 0 : abort(); 48 : } 49 192 : sclist->first=NULL; 50 192 : sclist->last=NULL; 51 : 52 192 : return sclist; 53 : } 54 : 55 14 : void sclist_del(sclist_t* sclist) { 56 14 : assert(sclist); 57 : 58 23 : while (sclist->first!=NULL) { 59 9 : sclistrecord_t* first = sclist->first; 60 9 : sclist->first = sclist->first->next; 61 9 : free(first); 62 : } 63 14 : free(sclist); 64 14 : } 65 : 66 328 : sclistrecord_t* sclist_addrecord(sclist_t* sclist, void* value) { 67 : sclistrecord_t* record; 68 : 69 328 : assert(sclist); 70 : 71 : /* Create the record record */ 72 328 : if (NULL==(record=malloc(sizeof(struct sclistrecord_s)))) { 73 0 : perror("sclist_addrecord OOM"); 74 0 : abort(); 75 : } 76 328 : record->value = value; 77 328 : record->next = NULL; 78 : 79 : /* Add to the list */ 80 328 : if (NULL==sclist->first) { 81 : /* First record to be added in an empty list */ 82 : /* A sentinel record would avoid this test and optimize performances 83 : * at the cost of sentinel size RAM consumption */ 84 140 : sclist->first = record; 85 140 : sclist->last = record; 86 : } else { 87 : /* Add to the end of a non-empty list */ 88 188 : sclist->last->next = record; 89 188 : sclist->last = record; 90 : } 91 328 : return record; 92 : } 93 : 94 196 : sclist_t* sclist_remrecord(sclist_t* sclist, sclistrecord_t* record) { 95 : sclistrecord_t* cur; 96 : sclistrecord_t* prev; 97 : 98 196 : assert(sclist); 99 196 : assert(record); 100 : 101 229 : for (cur=sclist->first, prev=NULL; (cur)&&(cur!=record); prev=cur,cur=cur->next); 102 : 103 : /* If found */ 104 196 : if (NULL!=cur) { 105 : /* Remove from the chain */ 106 188 : if (sclist->first==cur) 107 173 : sclist->first = cur->next; 108 : else 109 15 : prev->next = cur->next; 110 : /* Update the last pointer if needed */ 111 188 : if (sclist->last==cur) 112 104 : sclist->last = prev; 113 188 : return sclist; 114 : } else { 115 8 : fprintf(stderr,"ERROR: sclist_remrecord record not found.\n"); 116 8 : return NULL; 117 : } 118 : } 119 : 120 303 : sclistrecord_t* sclist_firstrecord(const sclist_t* sclist) { 121 303 : assert(sclist); 122 : 123 303 : return sclist->first; 124 : } 125 : 126 405 : sclistrecord_t* sclist_nextrecord(const sclistrecord_t* record) { 127 405 : assert(record); 128 : 129 405 : return record->next; 130 : } 131 : 132 538 : void* sclist_getvalue(sclistrecord_t* record) { 133 538 : assert(record); 134 : 135 538 : return record->value; 136 : } 137 : /* vim: set tw=80: */