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

📄 vmstringlist.cpp

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

⌨️ 快捷键说明

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