⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmstringlist.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/*                             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 )
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -