Adapted to recent changes of %build_linklib.
[AROS-Contrib.git] / FryingPan / framework / Generic / DynList.cpp
blob4d483ff1df4432f978372141668a98f6c994c311
1 /*
2 * Amiga Generic Set - set of libraries and includes to ease sw development for all Amiga platforms
3 * Copyright (C) 2001-2011 Tomasz Wiszkowski Tomasz.Wiszkowski at gmail.com.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "DynList.h"
21 #include "String.h"
22 #include "LibrarySpool.h"
23 #include <libclass/exec.h>
24 #include <LibC/LibC.h>
26 using namespace GenNS;
28 EList::EList(MinList* pList)
30 bExternalList = true;
31 PrvList = pList;
32 Count = 0;
35 EList::EList()
37 bExternalList = false;
38 PrvList = new MinList;
39 PrvList->mlh_Head = (MinNode*)&PrvList->mlh_Tail;
40 PrvList->mlh_TailPred = (MinNode*)&PrvList->mlh_Head;
41 PrvList->mlh_Tail = 0;
42 Count = 0;
45 EList::~EList()
47 if (false == bExternalList)
49 while (Count > 0)
51 Rem(0UL);
53 delete PrvList;
57 void EList::Add(void* ApObject)
59 ENode *pNode = new ENode;
61 memset(pNode, 0, sizeof(ENode));
62 pNode->ln_Data = ApObject;
64 Exec->AddTail((List*)PrvList, (Node*)pNode);
65 if (false == bExternalList)
66 ++Count;
69 void EList::AddFeatured(void* ApObject, const char* ApName, signed char AlPri)
71 ENode *pNode = new ENode;
73 memset(pNode, 0, sizeof(ENode));
74 pNode->ln_Data = ApObject;
75 pNode->ln_Pri = AlPri;
76 pNode->ln_Name = const_cast<char*>(ApName);
78 Exec->Enqueue((List*)PrvList, (Node*)pNode);
79 if (false == bExternalList)
80 ++Count;
83 void EList::InsertAt(void* ApObject, unsigned long APos)
85 ENode *pNode = new ENode;
86 pNode->ln_Data = ApObject;
87 if (APos >=GetCount())
89 Exec->AddTail((List*)PrvList, (Node*)pNode);
91 else if (!APos)
93 Exec->AddHead((List*)PrvList, (Node*)pNode);
95 else
97 Node* pBefore = (Node*)PrvList->mlh_Head;
98 while (--APos)
100 pBefore = pBefore->ln_Succ;
102 Exec->Insert((List*)PrvList, (Node*)pNode, pBefore);
104 if (false == bExternalList)
105 ++Count;
108 void EList::Rem(void* ApObject)
110 ENode *pNode = (ENode*)PrvList->mlh_Head;
111 while (pNode->ln_Succ)
113 ENode *pNext = (ENode*)pNode->ln_Succ;
114 if (pNode->ln_Data == ApObject)
116 Exec->Remove((Node*)pNode);
117 if (false == bExternalList)
118 --Count;
119 delete pNode;
121 pNode = pNext;
125 void EList::Rem(unsigned long AItem)
127 if (false == bExternalList)
128 if (AItem > Count) return;
130 ENode *pNode = (ENode*)PrvList->mlh_Head;
131 while (pNode->ln_Succ)
133 ENode *pNext = (ENode*)pNode->ln_Succ;
134 if (!AItem)
136 Exec->Remove((Node*)pNode);
137 if (false == bExternalList)
138 --Count;
139 delete pNode;
140 return;
142 pNode = pNext;
143 --AItem;
147 void* EList::operator [] (unsigned long AItem)
149 if (false == bExternalList)
150 if (AItem > Count) return 0;
151 ENode *pNode = (ENode*)PrvList->mlh_Head;
152 while (pNode->ln_Succ)
154 ENode *pNext = (ENode*)pNode->ln_Succ;
155 if (!AItem)
157 return pNode->ln_Data;
159 pNode = pNext;
160 --AItem;
162 return 0;
165 unsigned long EList::GetCount()
167 if (false == bExternalList)
168 return Count;
170 ENode *pNode = (ENode*)PrvList->mlh_Head;
171 long lItems = 0;
173 while (pNode->ln_Succ)
175 pNode = (ENode*)pNode->ln_Succ;
176 ++lItems;
178 return lItems;
181 void *EList::Get(unsigned long AItem)
183 return (*this)[AItem];
186 Node* EList::GetNode(unsigned long AItem)
188 if (false == bExternalList)
189 if (AItem > Count) return 0;
190 ENode *pNode = (ENode*)PrvList->mlh_Head;
191 while (pNode->ln_Succ)
193 ENode *pNext = (ENode*)pNode->ln_Succ;
194 if (!AItem)
196 return pNode;
198 pNode = pNext;
199 --AItem;
201 return 0;
204 MinList *EList::GetList()
206 return PrvList;
209 void EList::Update()
211 bool bCopy = bExternalList;
212 bExternalList = true;
213 Count = GetCount();
214 bExternalList = bCopy;