Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / MFC

StL Data File Viewer

Rate me:
Please Sign up or sign in to vote.
4.87/5 (43 votes)
5 Mar 2003CPOL2 min read 508.8K   14.7K   60  
A simple StereoLithography data file viewer.
// ListOfCGLObject.cpp: implementation of the CListOfCGLObject class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ListOfCGLObject.h"
#include "ListIteratorOfListOfCGLObject.h"
#include "ListException.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CListOfCGLObject::CListOfCGLObject() : firstPtr(0), lastPtr(0)
{
}


CListOfCGLObject::~CListOfCGLObject()
{
	CListNodeOfCGLObject* curPtr = firstPtr, *tmp;
	if(!IsEmpty())
	{
		while(curPtr != 0)
		{
			tmp = curPtr;
			if(tmp->data)
			{
				delete tmp->data;
				tmp->data = 0;
			}
			curPtr = curPtr->nextPtr;
			delete tmp;
		}
	}
}


void CListOfCGLObject::Append( CGLObject* data)
{
	CListNodeOfCGLObject* newPtr = new CListNodeOfCGLObject(data);
	if(IsEmpty())
		firstPtr = lastPtr = newPtr;
	else
	{
		lastPtr->nextPtr = newPtr;
		lastPtr = newPtr;
	}
}


void CListOfCGLObject::Prepend( CGLObject* data)
{
	CListNodeOfCGLObject* newPtr = new CListNodeOfCGLObject(data);
	if(IsEmpty())
		firstPtr = lastPtr = newPtr;
	else
	{
		newPtr->nextPtr = firstPtr;
		firstPtr = newPtr;
	}
}

CGLObject* CListOfCGLObject::First() const
{
	if(IsEmpty())
		throw CListException(LIST_EMPTY);
	return firstPtr->GetData();
}

CGLObject* CListOfCGLObject::Last() const
{
	if(IsEmpty())
		throw CListException(LIST_EMPTY);
	return lastPtr->GetData();
}

CListNodeOfCGLObject* CListOfCGLObject::Previous(const CGLObject* O)
{
	CListIteratorOfListOfCGLObject it(this);
	for(it.Init(); it.More(); it.Next())
	{
		if(O == it.CurrentPtr()->nextPtr->data)
			break;
	}
	return it.CurrentPtr();
}

CGLObject* CListOfCGLObject::Remove(const CGLObject* O)
{
	if(IsEmpty())
		return 0;
	CGLObject* rO = 0;
	CListNodeOfCGLObject* tmp;
	if(O == First())
	{
		tmp = firstPtr;
		firstPtr = firstPtr->nextPtr;
		rO = tmp->data;
	}
	else
	{
		CListNodeOfCGLObject* p = Previous(O);
		if(p->nextPtr != NULL)
		{
			tmp = p->nextPtr;
			p->nextPtr = p->nextPtr->nextPtr;
			rO = tmp->data;
		}
	}
	tmp->data = 0;
	delete tmp->data;
	delete tmp;
	return rO;
}

bool CListOfCGLObject::IsEmpty() const
{
	return (firstPtr==0);
}		


void CListOfCGLObject::Clear()
{
	CListNodeOfCGLObject* curPtr = firstPtr, *tmp;
	if(!IsEmpty())
	{
		while(curPtr != 0)
		{
			tmp = curPtr;
			if(tmp->data)
			{
				delete tmp->data;
				tmp->data = 0;
			}
			curPtr = curPtr->nextPtr;
			delete tmp;
		}
		
		firstPtr = lastPtr = 0;
	}
}

CListNodeOfCGLObject* CListOfCGLObject::NewNode(CGLObject* P)
{
	CListNodeOfCGLObject* newPtr= new CListNodeOfCGLObject(P);
	if(!newPtr)
		throw CListException(LIST_OUT_OF_MEMORY);
	newPtr->nextPtr = 0;
	return newPtr;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Mahindra & Mahindra
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Comments and Discussions