/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: The class contains support for doublely linked lists of strings
TOOL And XML FORMS License
==========================
Except where otherwise noted, all of the documentation
and software included in the TOOL package is
copyrighted by Michael Swartzendruber.
Copyright (C) 2005 Michael John Swartzendruber.
All rights reserved.
Access to this code, whether intentional or accidental,
does NOT IMPLY any transfer of rights.
This software is provided "as-is," without any express
or implied warranty. In no event shall the author be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for
any purpose, including commercial applications, and to
alter and redistribute it, provided that the following
conditions are met:
1. All redistributions of source code files must retain
all copyright notices that are currently in place,
and this list of conditions without modification.
2. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
3. If you use this software in another product, an acknowledgment
in the product documentation would be appreciated but is
not required.
4. Modified versions in source or binary form must be plainly
marked as such, and must not be misrepresented as being
the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/
#include "../../../stdafx.h"
#include "VMStringList.h"
/*****************************************************************************/
/*
FUNCTION NAME: VMStringList
DESCRIPTION: ctor - initialize all members
INPUT: void
RETURNS: void
*/
VMStringList::VMStringList( void )
{
m_pxHead = NULL;
m_pxCurrent = NULL;
m_pxTail = NULL;
m_iCount = 0;
}
/* end of function "VMStringList" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: ~VMStringList
DESCRIPTION: dtor, call member to remove all heap allocations
INPUT: void
RETURNS: void
*/
VMStringList::~VMStringList( void )
{
RemoveAll();
}
/* end of function "~VMStringList" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Find
DESCRIPTION: determines if the string passed in to pStr exists in
this class as an entry
INPUT: pchToFind - the string to hunt for
piIndex - a pointer to an int where the string was found
RETURNS: SUCCESS if string was found, F_NOTFOUND otherwise
if the string was found the contents of pnSel is
modified
*/
VMStringList::retcodes VMStringList::Find( LPCSTR pchToFind, int* piIndex )
{
char* pchText;
int iLoop;
for ( iLoop = 0, pchText = GetFirstString(); pchText; pchText = GetNextString(), iLoop++ )
{
if ( strcmp( pchToFind, pchText ) == 0 )
{
if ( piIndex )
{
*piIndex = iLoop;
}
return success;
}
}
return( notfound );
}
/* end of function "Find" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: FindNodeByName
DESCRIPTION: determines if the string passed in to pStr exists in
this class as an entry
INPUT: pchToFind - the string to hunt for
piIndex - a pointer to an int where the string was found
RETURNS: SUCCESS if string was found, F_NOTFOUND otherwise
if the string was found the contents of pnSel is
modified
*/
P_STRING_LIST VMStringList::FindNodeByName( LPCSTR pchToFind, int* piIndex )
{
P_STRING_LIST pxList;
int iLoop;
for ( iLoop = 0, pxList = GetFirstNode(); pxList; pxList = GetNextNode(), iLoop++ )
{
if ( strcmp( pchToFind, pxList->m_pchString ) == 0 )
{
if ( piIndex )
{
*piIndex = iLoop;
}
return( pxList );
}
}
return( NULL );
}
/* end of function "FindNodeByName" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetStringAt
DESCRIPTION: returns the string located at the given index.
INPUT: iIndex - the element to get
RETURNS: the string found at the index, or NULL if there is
no string at the given location
*/
char* VMStringList::GetStringAt( short iIndex )
{
char* pchText;
int iLoop;
if ( iIndex > m_iCount )
{
return( NULL );
}
for ( iLoop = 0, pchText = GetFirstString(); pchText; pchText = GetNextString(), iLoop++ )
{
if ( iLoop == iIndex )
{
return( pchText );
}
}
return( NULL );
}
/* end of function "GetStringAt" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetNodeAt
DESCRIPTION: returns the string located at the given index.
INPUT: nIndex - the element to get
RETURNS: the string found at the index, or NULL if there is
no string at the given location
*/
P_STRING_LIST VMStringList::GetNodeAt( short iIndex )
{
P_STRING_LIST pxNode;
int iLoop;
if ( iIndex > m_iCount )
{
return( NULL );
}
for ( iLoop = 0, pxNode = GetFirstNode(); pxNode; pxNode = GetNextNode(), iLoop++ )
{
if ( iLoop == iIndex )
return( pxNode );
}
return( NULL );
}
/* end of function "GetNodeAt" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SetMarked
DESCRIPTION: marks the element at the given index with the value
passed in nMark
INPUT: iIndex - the element to mark
bMarked - the mark value
RETURNS: void
*/
void VMStringList::SetMarked( short iIndex, bool bMarked )
{
int iLoop;
P_STRING_LIST pxNode;
if ( iIndex > m_iCount )
{
return;
}
for ( iLoop = 0, pxNode = m_pxHead; pxNode; pxNode = pxNode->m_pxNext, iLoop++ )
{
if ( iLoop == iIndex )
{
pxNode->m_bMarked = bMarked;
return;
}
}
}
/* end of function "SetMarked" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: IsMarked
DESCRIPTION: determines if the element at the given index is marked
INPUT: nIndex - the element to check for a mark
RETURNS: mark value if element is marked, false otherwise
*/
bool VMStringList::IsMarked( short iIndex )
{
int iLoop;
P_STRING_LIST pxNode;
if ( iIndex > m_iCount )
{
return( false );
}
for ( iLoop = 0, pxNode = m_pxHead; pxNode; pxNode = pxNode->m_pxNext, iLoop++ )
{
if ( iLoop == iIndex )
{
return( pxNode->m_bMarked );
}
}
return( false );
}
/* end of function "IsMarked" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: MakeNewEntry
DESCRIPTION: creates and initializes a list element
INPUT: pchText - the string to add
RETURNS: NULL if not successful, a new pointer otherwise
*/
P_STRING_LIST VMStringList::MakeNewEntry( LPCSTR pchText, void* pvData )
{
int iLength;
P_STRING_LIST pxNew = NULL;
pxNew = new STRING_LIST;
if ( pxNew == NULL )
{
return( pxNew );
}
// Init the entry
//
pxNew->m_pxNext = NULL;
pxNew->m_pxPrev = NULL;
iLength = strlen( pchText );
pxNew->m_pchString = new char[ iLength + 1 ];
if ( pxNew->m_pchString == NULL )
{
delete pxNew;
return( NULL );
}
strcpy( pxNew->m_pchString, pchText );
pxNew->m_bMarked = false;
pxNew->m_pvData = pvData;
return( pxNew );
}
/* end of function "MakeNewEntry" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Add
DESCRIPTION: add an element to the list in its sorted position
INPUT: pchText - the string to add
RETURNS: SUCCESS if successful, F_NOMEMORY if not
*/
VMStringList::retcodes VMStringList::Add( LPCSTR pchText, void* pvData )
{
if ( m_pxHead == NULL )
{
m_pxHead = MakeNewEntry( pchText, pvData );
if ( m_pxHead == NULL )
{
return( nomemory );
}
m_pxHead->m_pxPrev = NULL;
m_pxHead->m_pxNext = NULL;
m_pxTail = m_pxHead;
}
else
{
m_pxTail->m_pxNext = MakeNewEntry( pchText, pvData );
if ( m_pxTail->m_pxNext == NULL )
{
RemoveAll();
return( nomemory );
}
m_pxTail->m_pxNext->m_pxPrev = m_pxTail;
m_pxTail = m_pxTail->m_pxNext;
}
m_iCount++;
return( success );
}
/* end of function "Add" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: SortedAdd
DESCRIPTION: add an element to the list in its sorted position
INPUT: pchText - the string to add
RETURNS: SUCCESS if successful, F_NOMEMORY if not
*/
VMStringList::retcodes VMStringList::SortedAdd( LPCSTR pchText, void* pvData )
{
if ( m_pxHead == NULL )
{
m_pxHead = MakeNewEntry( pchText, pvData );
if ( m_pxHead == NULL )
{
return( nomemory );
}
m_pxHead->m_pxPrev = NULL;
m_pxHead->m_pxNext = NULL;
m_pxTail = m_pxHead;
}
else
if ( m_pxHead == m_pxTail )
{
P_STRING_LIST pxNew = MakeNewEntry( pchText, pvData );
if ( pxNew == NULL )
{
RemoveAll();
return( nomemory );
}
if ( 0 > strcmp( m_pxHead->m_pchString, pxNew->m_pchString ) )
{
m_pxHead->m_pxNext = pxNew;
pxNew->m_pxPrev = m_pxHead;
m_pxTail = pxNew;
}
else
{
m_pxTail = m_pxHead;
m_pxTail->m_pxNext = NULL;
m_pxHead = pxNew;
m_pxHead->m_pxPrev = NULL;
m_pxTail->m_pxPrev = m_pxHead;
m_pxHead->m_pxNext = m_pxTail;
}
}
else
{
P_STRING_LIST pxNew = MakeNewEntry( pchText, pvData );
if ( pxNew == NULL )
{
RemoveAll();
return nomemory;
}
P_STRING_LIST pxCurrent = m_pxHead;
bool bInserted = false;
while ( !bInserted )
{
if ( 0 < strcmp( pxCurrent->m_pchString, pxNew->m_pchString ) )
{
P_STRING_LIST pxBefore;
P_STRING_LIST pxAfter;
pxBefore = pxCurrent->m_pxPrev;
pxAfter = pxCurrent->m_pxNext;
pxBefore->m_pxNext = pxNew;
pxNew->m_pxPrev = pxBefore;
pxNew->m_pxNext = pxAfter;
if ( pxAfter )
{
pxAfter->m_pxPrev = pxNew;
}
bInserted = true;
}
else
{
if ( pxCurrent->m_pxNext )
{
pxCurrent = pxCurrent->m_pxNext;
}
else
{
pxCurrent->m_pxNext = pxNew;
pxNew->m_pxPrev = pxCurrent;
m_pxTail = pxNew;
bInserted = true;
}
}
}
}
m_iCount++;
return( success );
}
/* end of function "SortedAdd" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Remove
DESCRIPTION: removes an element from the list
INPUT: iIndex - the element index to remove
RETURNS: SUCCESS if the element was dound are moved,
F_NOTFOUND is the element did not exist
*/
VMStringList::retcodes VMStringList::Remove( int iIndex )
{
int iLoop;
for ( iLoop = 0, m_pxCurrent = m_pxHead; m_pxCurrent; m_pxCurrent = m_pxCurrent->m_pxNext, iLoop++ )
{
if ( iLoop == iIndex )
{
// is this the head
if ( m_pxHead == m_pxCurrent )
{
m_pxHead = m_pxCurrent->m_pxNext;
if ( m_pxHead )
{
m_pxHead->m_pxPrev = NULL;
}
}
else
if ( m_pxCurrent == m_pxTail ) // is this the tail
{
m_pxCurrent->m_pxPrev->m_pxNext = NULL;
m_pxTail = m_pxCurrent->m_pxPrev;
}
else
{
m_pxCurrent->m_pxPrev->m_pxNext = m_pxCurrent->m_pxNext;
m_pxCurrent->m_pxNext->m_pxPrev = m_pxCurrent->m_pxPrev;
}
if ( m_pxCurrent->m_pchString )
{
delete [] m_pxCurrent->m_pchString;
}
delete m_pxCurrent;
m_iCount--;
m_pxCurrent = NULL;
return( success );
}
}
return( notfound );
}
/* end of function "Remove" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetLastNode
DESCRIPTION: gets the last element in the list
INPUT: void
RETURNS: a pointer to the string stored in the last element
returns NULL if there is no valid 'last element'
*/
P_STRING_LIST VMStringList::GetLastNode( void )
{
if ( m_pxTail == NULL )
{
return( NULL );
}
m_pxCurrent = m_pxTail;
return( m_pxCurrent );
}
/* end of function "GetLastNode" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetPrevNode
DESCRIPTION: returns the string previous to the cursor
the cursor value is changed
INPUT: void
RETURNS: a pointer to the string in the element previous to
to the cursor position
returns NULL if there are no elements prior to the
cursor
*/
P_STRING_LIST VMStringList::GetPrevNode( void )
{
if ( m_pxCurrent->m_pxPrev == NULL )
{
return( NULL );
}
m_pxCurrent = m_pxCurrent->m_pxPrev;
return( m_pxCurrent );
}
/* end of function "GetPrevNode" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetFirstNode
DESCRIPTION: retrieves the string stored in the first element of
the list
INPUT: void
RETURNS: a pointer to the string in the first element
returns NULL if this is empty
*/
P_STRING_LIST VMStringList::GetFirstNode( void )
{
if ( m_pxHead == NULL )
{
return( NULL );
}
m_pxCurrent = m_pxHead;
return( m_pxCurrent );
}
/* end of function "GetFirstNode" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetNextNode
DESCRIPTION: retrieves the string in the element just after the
cursor. The cursor value is changed.
INPUT: none
RETURNS: a pointer to the string stored in the element just
after the cursor
returns NULL if there are no elements after the cursor
*/
P_STRING_LIST VMStringList::GetNextNode( void )
{
if ( m_pxCurrent->m_pxNext == NULL )
{
return( NULL );
}
m_pxCurrent = m_pxCurrent->m_pxNext;
return( m_pxCurrent );
}
/* end of function "GetNextNode" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: RemoveAll
DESCRIPTION: removes all elements from this, and deletes all heap
allocations associated with each element
INPUT: void
RETURNS: void
*/
void VMStringList::RemoveAll( void )
{
// Free all the memory allocated
//
m_pxCurrent = m_pxHead;
while ( m_pxHead != NULL )
{
m_pxHead = m_pxCurrent->m_pxNext;
if ( m_pxCurrent->m_pchString )
{
delete [] m_pxCurrent->m_pchString;
}
delete m_pxCurrent;
m_pxCurrent = m_pxHead;
}
m_pxHead = NULL;
m_pxTail = NULL;
m_pxCurrent = NULL;
m_iCount = 0;
}
/* end of function "RemoveAll" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetLastString
DESCRIPTION: gets the last element in the list
INPUT: void
RETURNS: a pointer to the string stored in the last element
returns NULL if there is no valid 'last element'
*/
LPSTR VMStringList::GetLastString( void )
{
if ( m_pxTail == NULL )
{
return( NULL );
}
m_pxCurrent = m_pxTail;
return( m_pxCurrent->m_pchString );
}
/* end of function "GetLastString" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetPrevString
DESCRIPTION: returns the string previous to the cursor
the cursor value is changed
INPUT: void
RETURNS: a pointer to the string in the element previous to
to the cursor position
returns NULL if there are no elements prior to the
cursor
*/
LPSTR VMStringList::GetPrevString( void )
{
if ( m_pxCurrent->m_pxPrev == NULL )
{
return( NULL );
}
m_pxCurrent = m_pxCurrent->m_pxPrev;
return( m_pxCurrent->m_pchString );
}
/* end of function "GetPrevString" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetFirstString
DESCRIPTION: retrieves the string stored in the first element of
the list
INPUT: void
RETURNS: a pointer to the string in the first element
returns NULL if this is empty
*/
LPSTR VMStringList::GetFirstString( void )
{
if ( m_pxHead == NULL )
{
return( NULL );
}
m_pxCurrent = m_pxHead;
return( m_pxCurrent->m_pchString );
}
/* end of function "GetFirstString" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetNextString
DESCRIPTION: retrieves the string in the element just after the
cursor. The cursor value is changed.
INPUT: none
RETURNS: a pointer to the string stored in the element just
after the cursor
returns NULL if there are no elements after the cursor
*/
LPSTR VMStringList::GetNextString( void )
{
if ( m_pxCurrent->m_pxNext == NULL )
{
return( NULL );
}
m_pxCurrent = m_pxCurrent->m_pxNext;
return( m_pxCurrent->m_pchString );
}
/* end of function "GetNextString" */
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/