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

📄 lists.c

📁 An complete pmp solution for mattel juicebox player. Using crossworks for arm.
💻 C
字号:
#include "lists.h"
//===================================================================
//                       Static List
//------------------------------------------------------------------- 
#ifdef dIncludeStaticList
//-------------------------------------------------------------------
void wStaticListAdd (xTStaticList *pxList, void *pvValue)
{
  if (pxList->ulMaxLength == 0)
    {
    pxList->ulMaxLength = 20;
	pxList->pxItems = wMalloc(sizeof (void*)*pxList->ulMaxLength);
	pxList->ulLength = 1;
    *(pxList->pxItems) = (u32) pvValue;
	return;
	}
  *(pxList->pxItems + pxList->ulLength) = (u32) pvValue;
  pxList->ulLength++;
  if (pxList->ulLength >= pxList->ulMaxLength)
    {
	pxList->ulMaxLength = (pxList->ulMaxLength*3)>>1;
	u32 *pvNewItems = wMalloc (sizeof (void*)*pxList->ulMaxLength);
    u32 *pvItems = pxList->pxItems;
    u32 *pvOItems = pvItems;
    pxList->pxItems = pvNewItems;
	u32 i;
    for (i=0; i<pxList->ulLength; i++)
      {
      *pvNewItems++ = *pvItems++;
      }
    wFree(pvOItems);
    }
  
}
//-------------------------------------------------------------------
void wStaticSortedListAdd (xTStaticList *pxList, void *pvValue)
{
  wStaticListAdd(pxList, pvValue);
  u32 *pvItems = pxList->pxItems;
  u32 ulStart = 0;
  u32 ulEnd = pxList->ulLength - 1;
  u32 ulMid;
  
  while(ulStart - ulEnd >0)
    {
    ulMid = (ulStart+ulEnd)/2;
    if (pxList->wCompare(pvValue, pvItems+ulMid)>0)
      ulStart = ulMid;
    else ulEnd = ulMid;
    }  
  pvItems+=pxList->ulLength - 1;
  u32 i;
  for (i = pxList->ulLength - ulEnd -1; i > 0; i--)
    {
      *(pvItems-1) = *pvItems;
      *pvItems--;
    }
  *pvItems = (u32)pvValue;
}
//-------------------------------------------------------------------
void wStaticListRemove (xTStaticList *pxList, u32 ulPosition)
{
  u32 i;
  u32 *pvItems = pxList->pxItems;
  pxList->ulLength--;
  *(pvItems + ulPosition) = *(pvItems + pxList->ulLength);  
}
//-------------------------------------------------------------------
void wStaticSortedListRemove (xTStaticList *pxList, u32 ulPosition)
{
  u32 i;
  u32 *pvItems = pxList->pxItems + ulPosition;
  for (i = pxList->ulLength - ulPosition - 1; i > 0; i--)
    {
      *pvItems = *(pvItems + 1);
      *pvItems++;
    }
  pxList->ulLength--;
}

//-------------------------------------------------------------------
void wStaticListSort (xTStaticList *pxList)
{
  wQSort (pxList->pxItems, pxList->ulLength, pxList->wCompare);
}
//-------------------------------------------------------------------
#endif
//===================================================================
//                          Linked List
//-------------------------------------------------------------------
#ifdef dIncludeLinkedList
//-------------------------------------------------------------------
void wListAdd (xTLinkedList *pxList, void *pvValue)
{
  xTListItem *xNewItem = wMalloc(sizeof(xTListItem));
  xNewItem->pvValue = pvValue;
  xNewItem->pvNext = NULL;
  xNewItem->pvPrevious = pxList->pxListEnd;
  pxList->pxListEnd = xNewItem; 
}

void wListRemove (xTLinkedList *pxList, xTListItem *xItem)
{
  if (xItem->pvNext == NULL) pxList->pxListEnd = xItem->pvPrevious;
    else xItem->pvNext->pvPrevious = xItem->pvPrevious;
  if (xItem->pvPrevious == NULL) pxList->pxListStart = xItem->pvNext;
    else xItem->pvPrevious->pvNext = xItem->pvNext;
  wFree (xItem);
}
//-------------------------------------------------------------------
#endif
//===================================================================
//                          String List
//-------------------------------------------------------------------
#ifdef dIncludeStringList
//-------------------------------------------------------------------
//------------------------------------------------------------------- 
inline void wStringListAdd (xTStringList *pxList, char *pvValue)
{
  wStaticListAdd ((xTStaticList*)pxList, (void*)pvValue);
}
//-------------------------------------------------------------------
inline void wStringSortedListAdd (xTStringList *pxList, char *pvValue)
{
  pxList->wCompare = (void*)strcmp;
  wStaticSortedListAdd((xTStaticList*)pxList, (void *)pvValue);
}
//-------------------------------------------------------------------
inline void wStringListRemove (xTStringList *pxList, u32 ulPosition)
{
  wStaticListRemove ((xTStaticList*)pxList, ulPosition);  
}
//-------------------------------------------------------------------
inline void wStringSortedListRemove (xTStringList *pxList, u32 ulPosition)
{
  wStaticSortedListRemove ((xTStaticList*)pxList, ulPosition); 
}
//-------------------------------------------------------------------
inline void wStringListSort (xTStringList *pxList)
{
  wQSort (pxList->pxItems, pxList->ulLength, (i16(*)(void*, void*))strcmp);
}
//-------------------------------------------------------------------
void wStringListClear(xTStringList *pxList)
{
  u32 i = 0;
  for (;i<pxList->ulLength; i++)
    {
	wFree(*((u32*)pxList->pxItems+i));
	}
  pxList->ulLength = 0;
}
//-------------------------------------------------------------------
void wStringListReadFromBuffer (xTStringList *pxList, u32 ulBufferLength, u08 *sBuffer)
{
  static u32 ulLineLength;
  u32 i;
  u08 c;
  if (ulBufferLength == 0) 
    {
	ulLineLength = 0;
	return;
	}
  u08 *sOBuffer = sBuffer;
  u08 *sSBuffer = sBuffer;
  u08 l = (ulLineLength == 0);
  for (i=0; i<ulBufferLength; i++)
    {
	c = *sBuffer++;
	ulLineLength++;
	if (c>0x1F) continue;
	else
   	  {
	  if (c == NULL) break;
	  if (c == '\r')
	    {
	    ulLineLength--;
	    continue;
	    }
	  if (c == '\n')
	    {
		if (l)
		  {
		  char *sNew =wMalloc (sizeof(char)*ulLineLength);
		  strncpy (sNew, sSBuffer, ulLineLength-1);
		  *(sNew+ulLineLength-1) = NULL;
		  wStringListAdd (pxList, sNew);
		  sSBuffer = sBuffer;
		  }
	      else
		  {
          char *sNew =wMalloc (sizeof(char)*ulLineLength);
		  strncpy (sNew, *((u32*)pxList->pxItems + pxList->ulLength - 1), sBuffer-sOBuffer);
		  strncat (sNew+(u32)(sBuffer-sOBuffer), sOBuffer, sOBuffer-sBuffer);
		  *(sNew+ulLineLength-1) = NULL;
		  wFree (*((u32*)pxList->pxItems + pxList->ulLength - 1));
          *((u32*)pxList->pxItems + pxList->ulLength - 1) = sNew;
          sSBuffer = sBuffer;
		  l = 1;
		  }
		ulLineLength = 0;
	    }
	  }
	}
  if (ulLineLength > 0)
    {
    if (l)
		  {
		  char *sNew =wMalloc (sizeof(char)*ulLineLength);
		  strncpy (sNew, sBuffer - ulLineLength, ulLineLength-1);
		  *(sNew+ulLineLength-1) = NULL;
		  wStringListAdd (pxList, sNew);
		  }
	      else
		  {
          char *sNew =wMalloc (sizeof(char)*ulLineLength);
		  strncpy (sNew, *((u32*)pxList->pxItems + pxList->ulLength - 1), sBuffer-sOBuffer);
		  strncat (sNew, sOBuffer, sOBuffer-sBuffer);
		  *(sNew+ulLineLength-1) = NULL;
		  wFree (*((u32*)pxList->pxItems + pxList->ulLength - 1));
          *((u32*)pxList->pxItems + pxList->ulLength - 1) = sNew;
		  }
	}
}
//-------------------------------------------------------------------
#endif

⌨️ 快捷键说明

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