|
| 1 | +/* |
| 2 | + chipsetList.c |
| 3 | + chipset list functions source for iGame |
| 4 | +
|
| 5 | + Copyright (c) 2018-2023, Emmanuel Vasilakis |
| 6 | +
|
| 7 | + This file is part of iGame. |
| 8 | +
|
| 9 | + iGame is free software: you can redistribute it and/or modify |
| 10 | + it under the terms of the GNU General Public License as published by |
| 11 | + the Free Software Foundation, either version 3 of the License, or |
| 12 | + (at your option) any later version. |
| 13 | +
|
| 14 | + iGame is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + GNU General Public License for more details. |
| 18 | +
|
| 19 | + You should have received a copy of the GNU General Public License |
| 20 | + along with iGame. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include <string.h> |
| 25 | +#include <stdlib.h> |
| 26 | + |
| 27 | +#include <exec/types.h> |
| 28 | + |
| 29 | +#include "iGameExtern.h" |
| 30 | +#include "chipsetList.h" |
| 31 | +#include "strfuncs.h" |
| 32 | + |
| 33 | +chipsetList *chipsetListHead = NULL; |
| 34 | + |
| 35 | +static int isListEmpty(const void *head) |
| 36 | +{ |
| 37 | + return head == NULL; |
| 38 | +} |
| 39 | + |
| 40 | +void chipsetListAddTail(chipsetList *node) |
| 41 | +{ |
| 42 | + if (node != NULL) |
| 43 | + { |
| 44 | + node->next = NULL; |
| 45 | + |
| 46 | + if (isListEmpty(chipsetListHead)) |
| 47 | + { |
| 48 | + chipsetListHead = node; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + // find the last node |
| 53 | + chipsetList *currPtr = chipsetListHead; |
| 54 | + while (currPtr->next != NULL) |
| 55 | + { |
| 56 | + currPtr = currPtr->next; |
| 57 | + } |
| 58 | + |
| 59 | + currPtr->next = node; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +static BOOL chipsetListRemoveHead(void) { |
| 65 | + |
| 66 | + if (isListEmpty(chipsetListHead)) { |
| 67 | + return FALSE; |
| 68 | + } |
| 69 | + |
| 70 | + chipsetList *nextPtr = chipsetListHead->next; |
| 71 | + free(chipsetListHead); |
| 72 | + chipsetListHead = nextPtr; |
| 73 | + return TRUE; |
| 74 | +} |
| 75 | + |
| 76 | +void chipsetListPrint(void) |
| 77 | +{ |
| 78 | + int cnt = 0; |
| 79 | + chipsetList *currPtr = chipsetListHead; |
| 80 | + while (currPtr != NULL) |
| 81 | + { |
| 82 | + printf("----> %s\n", currPtr->title); |
| 83 | + currPtr = currPtr->next; |
| 84 | + cnt++; |
| 85 | + } |
| 86 | + printf("END OF LIST: %d items\n", cnt); |
| 87 | +} |
| 88 | + |
| 89 | +chipsetList *chipsetListSearchByTitle(char *title, unsigned int titleSize) |
| 90 | +{ |
| 91 | + if (isListEmpty(chipsetListHead)) |
| 92 | + { |
| 93 | + return NULL; |
| 94 | + } |
| 95 | + |
| 96 | + chipsetList *currPtr = chipsetListHead; |
| 97 | + while ( |
| 98 | + currPtr != NULL && |
| 99 | + strncmp(currPtr->title, title, titleSize) |
| 100 | + ) { |
| 101 | + currPtr = currPtr->next; |
| 102 | + } |
| 103 | + |
| 104 | + return currPtr; |
| 105 | +} |
| 106 | + |
| 107 | +chipsetList *getChipsetListHead(void) |
| 108 | +{ |
| 109 | + return chipsetListHead; |
| 110 | +} |
| 111 | + |
| 112 | +void emptyChipsetList(void) |
| 113 | +{ |
| 114 | + while(chipsetListRemoveHead()) |
| 115 | + {} |
| 116 | +} |
| 117 | + |
| 118 | +int chipsetListNodeCount(int cnt) |
| 119 | +{ |
| 120 | + static int nodeCount = 0; |
| 121 | + |
| 122 | + if (cnt == -1) |
| 123 | + { |
| 124 | + return nodeCount; |
| 125 | + } |
| 126 | + |
| 127 | + nodeCount = cnt; |
| 128 | + return nodeCount; |
| 129 | +} |
| 130 | + |
| 131 | +void addChipsetInList(char *title) |
| 132 | +{ |
| 133 | + if (isStringEmpty(title)) |
| 134 | + return; |
| 135 | + |
| 136 | + if (chipsetListSearchByTitle(title, sizeof(char) * MAX_CHIPSET_SIZE) == NULL) |
| 137 | + { |
| 138 | + chipsetList *node = malloc(sizeof(chipsetList)); |
| 139 | + if(node == NULL) |
| 140 | + { |
| 141 | + return; |
| 142 | + } |
| 143 | + strncpy(node->title, title, sizeof(char) * MAX_CHIPSET_SIZE); |
| 144 | + chipsetListAddTail(node); |
| 145 | + } |
| 146 | +} |
0 commit comments