📄 list.c
字号:
/******************************************************************/
/* Copyright (C) 2007 ROCK-CHIPS FUZHOU . All Rights Reserved. */
/*******************************************************************
File : List.c
Desc : 动态List处理
Author : GUI Develop Group
Date : 2007-08-10
Notes :
$Log: List.c,v $
Revision 1.2 2008/06/19 04:43:20 Administrator
代码整理!
Revision 1.1.1.1 2008/05/07 04:15:08 Administrator
no message
Revision 1.1.1.1 2008/03/06 13:29:09 Lingzhaojun
no message
Revision 1.3 2007/10/08 04:02:23 Lingzhaojun
去除GUI的VC编译警告
Revision 1.2 2007/10/08 02:01:53 Lingzhaojun
GUI修改Listkk
*********************************************************************/
#include "List.h"
#define GUI_RK
/*-------------------------------------------------------------------*/
/*
Name: ListCreate
Desc: Allocates space for the management of a list, and returns the pointer
to this UILIST.
*/
UILIST *ListCreate(UINT16 Attr, UILISTFTABLE * funcTable)
{
UILIST *z = MallocClass(UILIST);
if (z)
{
z->attr = Attr;
z->f = funcTable;
z->first = z->last = NULL;
}
return z;
}
/*-------------------------------------------------------------------*/
/*
Name: ListDestroy
Desc: Destroys the contents of the list, and then the list itself.
*/
void ListDestroy(UILIST * list)
{
if (NULL != list)
{
ListClear(list);
FreeClass(list);
}
}
/*-------------------------------------------------------------------*/
/*
Name: ListAddItem
Desc: Adds item to the list box, returning its position in the list (0 is the
first position). Returns a negative value if an error occurs.
Check for duplicates only occurs in AddItem, not in InsertItem.
If a duplicate is found, and NODUPLICATES is selected, then the index
of the duplicate is returned.
*/
INT32 ListAddItem(UILIST * list, void *item)
{
UINT16 count = 0;
UILISTENTRY *curr = NULL;
if (list->attr & LS_NODUPLICATES)
{
INT32 pos = ListFindItem(list, item);
if (pos >= 0)
return pos;
}
curr = list->first;
while (curr)
{
if (list->attr & LS_SORTED)
{
if (list->f && list->f->CompareItem)
{
if ((*(list->f->CompareItem))(item, curr->data) < 0)
break;
}
else
{ /* By default just use stricmp */
if (RKstricmp((char *) item, (char *) curr->data) < 0)
break;
}
}
curr = curr->next;
count++;
}
return ListInsertItem(list, item, count);
}
/*-------------------------------------------------------------------*/
/*
Name: ListFindItem
Desc: Find an item in the list, by using the compare function.
It returns the index of the item in the list or -1 if not found
*/
INT32 ListFindItem(const UILIST * list, void *item)
{
UINT16 count = 0;
UILISTENTRY *curr;
curr = list->first;
while (curr)
{
if (list->f && list->f->CompareItem)
{
if ((*(list->f->CompareItem))(item, curr->data) == 0)
break;
}
else
{ /* By default just use stricmp */
if (listStrcmp((char *) item, (char *) curr->data) == 0)
break;
}
curr = curr->next;
count++;
}
return curr == NULL ? -1 : count;
}
/*-------------------------------------------------------------------*/
/*
Name: ListCountOccurences
Desc: Counts the instances of an item in the list, by using the compare
function.
It returns the occurrences of the item in the list.
*/
INT32 ListCountOccurrences(const UILIST * list, void *item)
{
UINT16 count = 0;
UILISTENTRY *curr;
curr = list->first;
while (curr)
{
if (list->f && list->f->CompareItem)
{
if ((*(list->f->CompareItem))(item, curr->data) == 0)
count++;
}
else
{ /* By default just use stricmp */
if (listStrcmp(item, curr->data) == 0)
count++;
}
curr = curr->next;
}
return count;
}
/*-------------------------------------------------------------------*/
/*
Name: ListFindItemBackwardsTo
Desc: Find an item in the list, by using the compare function, starting
from the end up to limitItem.
It returns the index of the item in the list.
*/
INT32 ListFindItemBackwardsTo(const UILIST * list, void *searchItem, void *limitItem)
{
UINT16 count = 0;
UILISTENTRY *curr;
BOOLEAN limitReached = FALSE;
BOOLEAN found = FALSE;
curr = list->last;
while (curr && !limitReached && !found)
{
if (list->f && list->f->CompareItem)
{
if ((*(list->f->CompareItem))(limitItem, curr->data) == 0)
limitReached = TRUE;
else if ((*(list->f->CompareItem))(searchItem, curr->data) == 0)
found = TRUE;
}
else
{ /* By default just use stricmp */
if (listStrcmp(limitItem, curr->data) == 0)
limitReached = TRUE;
else if (listStrcmp(searchItem, curr->data) == 0)
found = TRUE;
}
curr = curr->prev;
count++;
}
return found ? (ListGetCount(list) - count) : -1;
}
/*-------------------------------------------------------------------*/
/*
Name: ListFindItemBackwards
Desc: Find an item in the list, by using the compare function, starting
from the end.
It returns the index of the item in the list.
*/
INT32 ListFindItemBackwards(const UILIST * list, void *item)
{
UINT16 count = 0;
UILISTENTRY *curr;
curr = list->last;
while (curr)
{
if (list->f && list->f->CompareItem)
{
if ((*(list->f->CompareItem))(item, curr->data) == 0)
break;
}
else
{ /* By default just use stricmp */
if (listStrcmp(item, curr->data) == 0)
break;
}
curr = curr->prev;
count++;
}
/* return curr == NULL ? -1 : count;*/
return (curr == NULL) ? -1 : ((ListGetCount(list) - count) - 1);
}
/*-------------------------------------------------------------------*/
/*
Name: ListGetItem
Desc: Returns the item at the index position in the list.
(0 is the first position). Returns a negative value if an error occurs.
*/
void *ListGetItem(const UILIST * list, UINT16 index)
{
UILISTENTRY *curr = ListGetListEntry(list, index);
return curr == NULL ? NULL : curr->data;
}
/*-------------------------------------------------------------------*/
/*
Name: ListGetListEntry
Desc: Returns the actually UILISTENTRY at the index position in the list.
(0 is the first position). Returns NULL if an error occurs.
*/
UILISTENTRY *ListGetListEntry(const UILIST * list, UINT16 index)
{
UINT16 count = 0;
UILISTENTRY *curr;
curr = list->first;
while (count < index && curr)
{
curr = curr->next;
count++;
}
return curr == NULL ? NULL : curr;
}
/*-------------------------------------------------------------------*/
/*
Name: ListInsertItem
Desc: Inserts item in the list box at the position supplied in index, and
returns the item's actual position (starting at 0) in the list. A negative
value is returned if an error occurs. The list is not resorted. If Index is
-1, the string is appended to the end of the list.
*/
INT32 ListInsertItem(UILIST * list, void *item, UINT16 index)
{
UINT16 count = 0;
UILISTENTRY *curr;
UILISTENTRY *thisentry = MallocClass(UILISTENTRY);
thisentry->data = item;
thisentry->selected = FALSE;
curr = list->first;
while (count < index && curr)
{
curr = curr->next;
count++;
}
if (curr == NULL)
{
thisentry->next = NULL;
/* adding to the end of the list */
if (list->last != NULL)
{
/* already entries in this list */
thisentry->prev = list->last;
list->last->next = thisentry;
}
else
{
/* adding to an empty list */
thisentry->prev = NULL;
list->first = thisentry;
}
list->last = thisentry;
}
else
{
/* inserting into the list */
thisentry->prev = curr->prev;
thisentry->next = curr;
if (curr == list->first)
/* adding at the beginning of the list */
list->first = thisentry;
else
/* inserting between existing entries */
curr->prev->next = thisentry;
curr->prev = thisentry;
}
return count < index ? -1 : count;
}
/*-------------------------------------------------------------------*/
/*
Name: ListDeleteItem
Desc: Deletes the item in the list at the position (starting at 0) supplied in
Index.
Returns the number of remaining list items, or a negative value if an error
occurs.
*/
INT32 ListDeleteItem(UILIST * list, UINT16 index)
{
UINT16 count = 0;
UILISTENTRY *curr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -