Atlas - SDL_list.c
Home / ext / SDL / src Lines: 1 | Size: 3687 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#include "./SDL_list.h" 24 25// Append 26bool SDL_ListAppend(SDL_ListNode **head, void *ent) 27{ 28 SDL_ListNode *cursor; 29 SDL_ListNode *node; 30 31 if (!head) { 32 return false; 33 } 34 35 node = (SDL_ListNode *)SDL_malloc(sizeof(*node)); 36 if (!node) { 37 return false; 38 } 39 node->entry = ent; 40 node->next = NULL; 41 42 if (*head) { 43 cursor = *head; 44 while (cursor->next) { 45 cursor = cursor->next; 46 } 47 cursor->next = node; 48 } else { 49 *head = node; 50 } 51 52 return true; 53} 54 55bool SDL_ListInsertAtPosition(SDL_ListNode **head, int pos, void *ent) 56{ 57 SDL_ListNode *cursor; 58 SDL_ListNode *node; 59 int i; 60 61 if (pos == -1) { 62 return SDL_ListAppend(head, ent); 63 } 64 65 if (!pos) { 66 node = (SDL_ListNode *)SDL_malloc(sizeof(*node)); 67 if (!node) { 68 return false; 69 } 70 node->entry = ent; 71 72 if (*head) { 73 node->next = *head; 74 } else { 75 node->next = NULL; 76 } 77 78 *head = node; 79 } 80 81 cursor = *head; 82 for (i = 1; i < pos - 1 && cursor; i++) { 83 cursor = cursor->next; 84 } 85 86 if (!cursor) { 87 return SDL_ListAppend(head, ent); 88 } 89 90 node = (SDL_ListNode *)SDL_malloc(sizeof(*node)); 91 if (!node) { 92 return false; 93 } 94 node->entry = ent; 95 node->next = cursor->next; 96 cursor->next = node; 97 98 return true; 99} 100 101// Push 102bool SDL_ListAdd(SDL_ListNode **head, void *ent) 103{ 104 SDL_ListNode *node = (SDL_ListNode *)SDL_malloc(sizeof(*node)); 105 106 if (!node) { 107 return false; 108 } 109 110 node->entry = ent; 111 node->next = *head; 112 *head = node; 113 return true; 114} 115 116// Pop from end as a FIFO (if add with SDL_ListAdd) 117void SDL_ListPop(SDL_ListNode **head, void **ent) 118{ 119 SDL_ListNode **ptr = head; 120 121 // Invalid or empty 122 if (!head || !*head) { 123 return; 124 } 125 126 while ((*ptr)->next) { 127 ptr = &(*ptr)->next; 128 } 129 130 if (ent) { 131 *ent = (*ptr)->entry; 132 } 133 134 SDL_free(*ptr); 135 *ptr = NULL; 136} 137 138void SDL_ListRemove(SDL_ListNode **head, void *ent) 139{ 140 SDL_ListNode **ptr = head; 141 142 while (*ptr) { 143 if ((*ptr)->entry == ent) { 144 SDL_ListNode *tmp = *ptr; 145 *ptr = (*ptr)->next; 146 SDL_free(tmp); 147 return; 148 } 149 ptr = &(*ptr)->next; 150 } 151} 152 153void SDL_ListClear(SDL_ListNode **head) 154{ 155 SDL_ListNode *l = *head; 156 *head = NULL; 157 while (l) { 158 SDL_ListNode *tmp = l; 159 l = l->next; 160 SDL_free(tmp); 161 } 162} 163 164int SDL_ListCountEntries(SDL_ListNode **head) 165{ 166 SDL_ListNode *node; 167 int count = 0; 168 169 for (node = *head; node; node = node->next) { 170 ++count; 171 } 172 return count; 173} 174[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.