Click here to Skip to main content
15,898,020 members
Articles / Desktop Programming / MFC

FiveLoaves v1.0

Rate me:
Please Sign up or sign in to vote.
3.84/5 (10 votes)
2 Jul 20028 min read 84.8K   4.4K   49  
FiveLoaves is an Internet utility designed to meet the most common needs of internet users - primarily secure connectivity
// --------------------------------------------------------------------------
//					www.UnitedBusinessTechnologies.com
//			  Copyright (c) 1998 - 2002  All Rights Reserved.
//
// Source in this file is released to the public under the following license:
// --------------------------------------------------------------------------
// This toolkit may be used free of charge for any purpose including corporate
// and academic use.  For profit, and Non-Profit uses are permitted.
//
// This source code and any work derived from this source code must retain 
// this copyright at the top of each source file.
// 
// UBT welcomes any suggestions, improvements or new platform ports.
// email to: XMLFoundation@UnitedBusinessTechnologies.com
// --------------------------------------------------------------------------

#ifndef _XML_GENERIC_LIST_ABSTRACTION
#define _XML_GENERIC_LIST_ABSTRACTION

//
// ****  You need only one ListAbstraction instance per list type.
// The ListAbstraction class is stateless so a single global 
// instance can handle all your lists without any thread safety 
// concerns.  


#include "ListAbstraction.h"
#include "GList.h"
#include "GArray.h"
#include "GString.h"
#include "GStringList.h"
#include "GHash.h"
#include "GBTree.h"


class GenericListAbstraction : public ListAbstraction
{
public:
	int itemCount(xmlObjectList pList)
	{
		GList *pTypedList = (GList *)pList;
		return pTypedList->Size();
	}
	void append(xmlObjectList pList, XMLObject *pObject)
	{
		GList *pTypedList = (GList *)pList;

		
//		GListIterator it(pTypedList);
//		while(it())
//		{
//			// don't add the same memory address twice, It's just another
//			// reference to the existing object which has already been updated.
//			if (pObject == it++)
//				return;
//
//		}


		pTypedList->AddLast(pObject);
	}
	
	XMLObject *getLast(xmlObjectList pList)
	{
		GList *pTypedList = (GList *)pList;
		return (XMLObject *)pTypedList->Last();

	}
	
	void removeObject(xmlObjectList pList, xmlObjectIterator Iterator)
	{
		GList *pTypedList = (GList *)pList;
		GListIterator *pIter = (GListIterator *)Iterator;
		pTypedList->SetCurrent(pIter->iCurrentNode);
		pTypedList->RemoveCurrent();
	}

	XMLObject *getFirst(xmlObjectList pList, xmlObjectIterator *pIterator)
	{
		GList *pTypedList = (GList *)pList;
		XMLObject *pObject = 0;
		if ( pTypedList->Size() )
		{
			GListIterator *pIter = new GListIterator(pTypedList);
			*pIterator = pIter;
			pObject = (XMLObject *)((*pIter)++);
		}
		return pObject;
	}
	XMLObject *getNext(xmlObjectList /*pList*/, xmlObjectIterator Iterator)
	{
		GListIterator *pIter = (GListIterator *)Iterator;
		XMLObject *pObject = 0;
		if ( (*pIter)() )
		{
			pObject = (XMLObject *)((*pIter)++);
		}
		else
		{
			delete pIter;
		}
		return pObject;
	}
	void releaseIterator(xmlObjectIterator Iterator)
	{
		delete (GListIterator *)Iterator;
	}
};

class GHashAbstraction : public KeyedDataStructureAbstraction
{
public:
	void AddObjectToStructure(KeyedDataStructure kds, XMLObject *pObj, const char *pzKey)
	{
		GHash *pTyped = (GHash *)kds;
		pTyped->Insert(pzKey, pObj);
	}
	XMLObject *getFirst(KeyedDataStructure kds, userHashIterator *pIterator)
	{
		GHashIterator *pIter = new GHashIterator((GHash *)kds);
		*pIterator = pIter;

		XMLObject *pRet = (XMLObject *)(*pIter)++;
		if (!pRet)
			delete pIter;
		return pRet;
	}
	XMLObject *getNext(KeyedDataStructure /*kds*/, userHashIterator Iterator)
	{
		GHashIterator *pIter = (GHashIterator *)Iterator;

		XMLObject *pRet = (XMLObject *)(*pIter)++;
		if (!pRet)
			delete pIter;
		return pRet;
	}
	int itemCount(KeyedDataStructure kds)
	{
		const GHash *pTyped = (GHash *)kds;
		return pTyped->GetCount();
	}
	void releaseIterator(xmlObjectIterator Iterator)
	{
		delete (GHashIterator *)Iterator;
	}
};



class GStringListAbstraction : public StringCollectionAbstraction
{
public:
	void append(userStringList pList, const char *pString)
	{
		GStringList *pTypedList = (GStringList *)pList;
		pTypedList->AddLast(pString);
	}
	const char *getFirst(xmlObjectList pList, xmlObjectIterator *pIterator)
	{
		GStringList *pTypedList = (GStringList *)pList;
		const char *pzRetVal = 0;
		if ( pTypedList->Size() )
		{
			GStringIterator *pIter = new GStringIterator(pTypedList);
			*pIterator = pIter;

			pzRetVal = ((*pIter)++);
			
		}
		return pzRetVal;
	}
	const char *getNext(xmlObjectList/*pList*/, xmlObjectIterator Iterator)
	{
		GStringIterator *pIter = (GStringIterator *)Iterator;
		const char *pzReturnValue = 0;
		if ( (*pIter)() )
		{
			pzReturnValue = ((*pIter)++);
		}
		else
		{
			delete pIter;
		}
		return pzReturnValue;
	}
	int itemCount(userStringList pList)
	{
		GStringList *pTypedList = (GStringList *)pList;
		return pTypedList->Size();
	}
};

class GArrayAbstraction : public IntegerArrayAbstraction
{
public:	
	void append(userArray pArray, int nValue)
	{
		GArray *pTypedArray = (GArray *)pArray;
		pTypedArray->AddElement( (void *)nValue );
	}
	unsigned int getAt(userArray pArray, unsigned int nIndex, int *bIsValidIndex)
	{
		GArray *pTypedArray = (GArray *)pArray;
		if (nIndex < pTypedArray->GetItemCount())
		{
			*bIsValidIndex = 1;
			union CAST_THIS_TYPE_SAFE_COMPILERS
			{
				void *       mbrVoid;
				unsigned int mbrInt;
			}Member;  
			Member.mbrVoid = (*pTypedArray)[nIndex];
			return Member.mbrInt;
		}
		*bIsValidIndex = 0;
		return 0;
	}
	unsigned int itemCount(userArray pArray)
	{
		GArray *pTypedArray = (GArray *)pArray;
		return (unsigned int)pTypedArray->Size();
	}
};


class GQSortAbstraction : public KeyedDataStructureAbstraction
{
public:
	void AddObjectToStructure(KeyedDataStructure kds, XMLObject *pObj, const char *pzKey)
	{
		GArray *pTyped = (GArray *)kds;
		pTyped->AddElement (pObj,pzKey);
	}
	XMLObject *getFirst(KeyedDataStructure kds, userHashIterator *pIterator)
	{
		GArray *pTyped = (GArray *)kds;
		*pIterator = new unsigned int;
		unsigned int *nIndex = (unsigned int *)*pIterator;
		*nIndex = 0;
		
		if (*nIndex < pTyped->GetItemCount())
		{
			XMLObject *pO =  (XMLObject *)(*pTyped)[*nIndex];
			if (pO)
				return pO;
		}
		delete nIndex;
		return 0;
	}
	XMLObject *getNext(KeyedDataStructure kds, userHashIterator Iterator)
	{
		unsigned int *nIndex = (unsigned int *)Iterator;
		GArray *pTyped = (GArray *)kds;
		(*nIndex)++;

		if (*nIndex < pTyped->GetItemCount())
		{
			XMLObject *pO =  (XMLObject *)(*pTyped)[*nIndex];
			if (pO)
				return pO;
		}
		delete nIndex;
		return 0;
	}
	int itemCount(KeyedDataStructure kds)
	{
		GArray *pTyped = (GArray *)kds;
		return pTyped->GetItemCount();
	}
	void releaseIterator(xmlObjectIterator Iterator)
	{
		delete (int *)Iterator;
	}
};


class GBTreeAbstraction : public KeyedDataStructureAbstraction
{
public:
	void AddObjectToStructure(KeyedDataStructure kds, XMLObject *pObj, const char *pzKey)
	{
		GBTree *pTyped = (GBTree *)kds;
		pTyped->insert (pzKey,  pObj);
	}
	
	XMLObject *getFirst(KeyedDataStructure kds, userHashIterator *pIterator)
	{
		GBTreeIterator *pIter = new GBTreeIterator((GBTree *)kds, 2);
		*pIterator = pIter;		
		XMLObject *pRet = (XMLObject *)((*pIter)++);
		if (!pRet)
			delete pIter;
		return pRet;
	}

	XMLObject *getNext(KeyedDataStructure/* kds*/, userHashIterator Iterator)
	{
		
		GBTreeIterator *pIter = (GBTreeIterator *)Iterator;
		XMLObject *pRet = (XMLObject *)((*pIter)++);
		if (!pRet)
			delete (GBTreeIterator *)Iterator;
		return pRet;
	}
	int itemCount(KeyedDataStructure kds)
	{
		GBTree *pTyped = (GBTree *)kds;
		return pTyped->getNodeCount();
	}
	void releaseIterator(xmlObjectIterator Iterator)
	{
		delete (GBTreeIterator *)Iterator;
	}
};



// the implementation is in xmlObject.cpp
extern GStringListAbstraction gGStringListHandler;
extern GArrayAbstraction gGArrayHandler;
extern GenericListAbstraction gGListHandler;

#endif //_XML_GENERIC_LIST_ABSTRACTION

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder United Business Technologies
United States United States
http://about.me/brian.aberle
https://www.linkedin.com/in/brianaberle
http://SyrianRue.org/Brian

Comments and Discussions