Files
pvvmud/common/lib/srvcli/objectlist.C
2025-03-05 08:37:43 +01:00

205 lines
4.1 KiB
C

/*
* PVVMUD a 3D MUD
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include "pvvmud.H"
#include "objectlist.H"
CObjectListItem::CObjectListItem(){
m_next = NULL;
m_prev = NULL;
m_object = NULL;
}
CObjectListItem::CObjectListItem(CObject * object){
m_next = NULL;
m_prev = NULL;
m_object = object;
}
void CObjectListItem::setObject(CObject * object){
m_object = object;
}
CObject * CObjectListItem::getObject(){
return m_object;
}
void CObjectListItem::setNext(CObjectListItem * next){
m_next = next;
}
CObjectListItem * CObjectListItem::getNext(){
return m_next;
}
void CObjectListItem::setPrev(CObjectListItem * prev){
m_prev = prev;
}
CObjectListItem * CObjectListItem::getPrev(){
return m_prev;
}
CObjectList::CObjectList(){
m_first = NULL;
m_last = NULL;
}
CObjectList::~CObjectList(){
deleteList();
}
void CObjectList::deleteList(){
CObjectListItem *item;
while ((item = removeFirst()) != NULL){
delete item;
}
}
void CObjectList::deleteAll(){
CObjectListItem *item;
while ((item = removeFirst()) != NULL){
CObject * object = item->getObject();
delete object;
delete item;
}
}
CObjectListItem * CObjectList::addFirst(CObject * obj){
CObjectListItem * item = new CObjectListItem(obj);
if (m_first != NULL){
item->setNext(m_first);
m_first->setPrev(item);
m_first = item;
} else {
m_first = m_last = item;
}
return item;
}
CObjectListItem * CObjectList::addLast(CObject * obj){
CObjectListItem * item = new CObjectListItem(obj);
if (m_last != NULL){
m_last->setNext(item);
item->setPrev(m_last);
m_last = item;
} else {
m_first = m_last = item;
}
return item;
}
CObjectListItem * CObjectList::removeFirst(){
if (m_first == NULL) return NULL;
CObjectListItem *item = m_first;
m_first = m_first->getNext();
if (m_first != NULL){
m_first->setPrev(NULL);
} else {
m_last = NULL;
}
if (item != NULL){
item->setNext(NULL);
item->setPrev(NULL);
}
return item;
}
CObjectListItem * CObjectList::removeLast(){
if (m_last == NULL) return NULL;
CObjectListItem *item = m_last;
m_last = m_last->getPrev();
if (m_last != NULL){
m_last->setNext(NULL);
} else {
m_first = NULL;
}
if (item != NULL){
item->setNext(NULL);
item->setPrev(NULL);
}
return item;
}
void CObjectList::remove(CObject * obj){
CObjectListItem *curr,*next,*prev;
curr = m_first;
prev = NULL;
while (curr != NULL){
next = curr->getNext();
if ( obj == curr->getObject()){
if (prev != NULL){
prev->setNext(next);
} else {
m_first = next;
}
if (next != NULL){
next->setPrev(prev);
} else {
m_last = prev;
}
delete curr;
return;
}
prev = curr;
curr = next;
}
}
CObjectListItem * CObjectList::getFirst(){
return m_first;
}
CObjectListItem * CObjectList::getLast(){
return m_last;
}
//CObjectListItem * CObjectList::getNext(CObjectListItem * objListItem){
// return objListItem->getNext();
//}
CObjectListItem * CObjectList::getItem(int index) {
int i=0;
CObjectListItem * item = getFirst();
while(item!=NULL && i<index) {
item = item->getNext();
i++;
}
if (i==index) return item;
else return NULL;
}
int CObjectList::getNumItems(){
CObjectListItem * item = getFirst();
int numItems = 0;
while (item != NULL){
item = item->getNext();
numItems++;
}
return numItems;
}