📄 utils.c
字号:
/* * Copyright Notice * Copyright (c) Ericsson, IBM, Lotus, Matsushita Communication * Industrial Co., Ltd., Motorola, Nokia, Openwave Systems, Inc., * Palm, Inc., Psion, Starfish Software, Symbian, Ltd. (2001). * All Rights Reserved. * Implementation of all or part of any Specification may require * licenses under third party intellectual property rights, * including without limitation, patent rights (such a third party * may or may not be a Supporter). The Sponsors of the Specification * are not responsible and shall not be held responsible in any * manner for identifying or failing to identify any or all such * third party intellectual property rights. * * THIS DOCUMENT AND THE INFORMATION CONTAINED HEREIN ARE PROVIDED * ON AN "AS IS" BASIS WITHOUT WARRANTY OF ANY KIND AND ERICSSON, IBM, * LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO. LTD, MOTOROLA, * NOKIA, PALM INC., PSION, STARFISH SOFTWARE AND ALL OTHER SYNCML * SPONSORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT * SHALL ERICSSON, IBM, LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO., * LTD, MOTOROLA, NOKIA, PALM INC., PSION, STARFISH SOFTWARE OR ANY * OTHER SYNCML SPONSOR BE LIABLE TO ANY PARTY FOR ANY LOSS OF * PROFITS, LOSS OF BUSINESS, LOSS OF USE OF DATA, INTERRUPTION OF * BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR EXEMPLARY, INCIDENTAL, * PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN CONNECTION WITH * THIS DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE. * * The above notice and this paragraph must be included on all copies * of this document that are made. * *//***************************************************************************** Obex Util Routines***************************************************************************/#include <utils.h>#include <iConstants.h>#include <stdlib.h>#include <stdio.h>#include <string.h>// luz %%%%#ifdef __MWERKS__#include <extras.h>#define strnicmp _strnicmp#endif/* **************************************** *//* ******* List Functions ***************** *//* **************************************** *//*** Returns an empty list, ready for use.*/ObxList *iobxListNew() { ObxList *newList = NULL; OBXDBGFLOW(("iobxListNew() entry.\n")); if ( (newList = (ObxList *)malloc( sizeof( ObxList ) )) ) { OBXDBGBUF(("iobxListNew() malloc, addr=0x%08x, len=%d.\n", newList, sizeof(ObxList) )); newList->head = NULL; newList->tail = NULL; iobxListReset( newList ); } return newList;}/*** Free's the contents of a list and the list struct itself.** WARNING: each element of the list is also free()'ed!** Use ObxListRelease() to remove the elements without freeing them.** The list should be considered invalid after this call.*/void iobxListFree( ObxList *list ) { OBXDBGFLOW(("iobxListFree() entry, list=0x%08x\n", list)); if ( list ) { iobxListReset( list ); free( list ); list = NULL; }}/*** Empty's the list, free()'ing all elements and resetting the list** to it's initial state.** The list is still viable and ready for use after this call.** Use ObxListFree() to free the list itself.*/void iobxListReset( ObxList *list ) { ObxListNode *thisNode = NULL; ObxListNode *nextNode = NULL; OBXDBGFLOW(("iobxListReset() entry, list=0x%08x\n", list)); if ( list ) { nextNode = list->head; while ( nextNode ) { thisNode = nextNode; nextNode->prev = NULL; nextNode = thisNode->next; if ( thisNode->data ) { free( thisNode->data ); thisNode->data = NULL; } free( thisNode ); thisNode = NULL; } list->head = NULL; list->tail = NULL; }}/*** Removes all contents of a list. They are not free'ed. It's assumed** that the caller is managing their storage.** The list is still viable and ready for use after this call.*/void iobxListRelease( ObxList *list ) { ObxListNode *thisNode = NULL; ObxListNode *nextNode = NULL; OBXDBGFLOW(("iobxListRelease() entry, list=0x%08x\n", list)); if ( list ) { nextNode = list->head; while ( nextNode ) { thisNode = nextNode; nextNode = thisNode->next; thisNode->data = NULL; /* Note we're not freeing the data */ free( thisNode ); thisNode = NULL; } list->head = NULL; list->tail = NULL; }}/*** Append a new data to the end of the list** On success, returns pointer to the data that was added.** Returns NULL on error.*/void *iobxListAppend( ObxList *list, void *data ) { ObxListNode *newNode = NULL; OBXDBGFLOW(("iobxListAppend() entry, list=0x%08x\tdata=0x%08x\n", list, data)); if ( (newNode = (ObxListNode *)malloc( sizeof( ObxListNode ) )) ) { OBXDBGBUF(("iobxListAppend() malloc, addr=0x%08x, len=%d.\n", newNode, sizeof(ObxListNode) )); newNode->data = data; if ( list->tail ) { list->tail->next = newNode; newNode->prev = list->tail; newNode->next = NULL; list->tail = newNode; } else { list->head = list->tail = newNode; newNode->next = newNode->prev = NULL; } } else { OBXDBGERR(("[ERROR] iobxListAppend() malloc failure\n")); return NULL; /* malloc failure */ } return newNode->data;}/*** Get an Iterator for the list.** Returns the newly created iterator or NULL on error.*/ObxIterator *iobxListGetIterator( ObxList *list ) { ObxIterator *newIterator = NULL; OBXDBGFLOW(("iobxListGetIterator() entry, list=0x%08x\n", list)); if ( list ) { if ( (newIterator = (ObxIterator *)malloc( sizeof( ObxIterator ) )) ) { memset(newIterator, 0, sizeof(ObxIterator)); OBXDBGBUF(("iobxListGetIterator() malloc, addr=0x%08x, len=%d.\n", newIterator, sizeof(ObxIterator) )); newIterator->list = list; iobxIteratorReset( newIterator ); } } return newIterator;}/*** Insert a new element into the list 'after' the current position of the Iterator.** Inserts to the front of the list are done with an iterator that has been** 'reset' (ObxIteratorReset()).** On success, returns pointer to the data that was added.** Returns NULL on error.*/void *iobxListInsert( ObxList *list, const ObxIterator *iterator, void *data ) { ObxListNode *newNode = NULL; OBXDBGFLOW(("iobxListInsert() entry, list=0x%08x\titerator=0x%08x\tdata=0x%08x\n", list, iterator, data)); if ( list && iterator ) { if ( (newNode = (ObxListNode *)malloc( sizeof( ObxListNode ) )) ) { OBXDBGBUF(("iobxListInsert() malloc, addr=0x%08x, len=%d.\n", newNode, sizeof(ObxListNode) )); newNode->next = NULL; newNode->prev = NULL; newNode->data = data; if ( iterator->cursor ) { /* ** Insert after current cursor, this could be the list tail. */ if ( iterator->cursor->prev ) { iterator->cursor->prev->next = newNode; } if ( iterator->cursor->next ) { iterator->cursor->next->prev = newNode; } if ( iterator->cursor == list->tail ) { list->tail = newNode; } } else { /* ** Insert before head */ if ( list->head ) { /* others existed */ newNode->next = list->head; newNode->prev = NULL; list->head->prev = newNode; list->head = newNode; } else { /* first one */ list->head = list->tail = newNode; newNode->next = newNode->prev = NULL; } } } else { OBXDBGERR(("[ERROR] iobxListInsert() malloc failure\n")); return NULL; /* malloc failure */ } } else { OBXDBGERR(("[ERROR] iobxListInsert() Bad plist on call\n")); return NULL; /* bad plist */ } return newNode->data;}/*** Remove the element being pointed to by the iterator. Return this data** as a pointer to the caller.**** The iterator is adjusted as follows:** 1) The iterator is backed up to the previous node.** 2) If the node being removed was the first node, the iterator is reset.**** On success, returns pointer to the data that was removed.** Returns NULL on error.**** Use ObxListDelete() to remove and free the data being managed by the** list.*/void *iobxListRemove( ObxList *list, ObxIterator *iterator ) { void *data = NULL; ObxListNode *newCursor = NULL; OBXDBGFLOW(("iobxListRemove() entry, list=0x%08x\titerator=0x%08x\n", list, iterator)); if ( list && iterator && list == iterator->list ) { if ( iterator->cursor ) { data = iterator->cursor->data; newCursor = iterator->cursor->prev; if ( list->head == iterator->cursor ) { list->head = iterator->cursor->next; } if ( list->tail == iterator->cursor ) { list->tail = iterator->cursor->prev; } if ( iterator->cursor->prev ) { iterator->cursor->prev->next = iterator->cursor->next; } if ( iterator->cursor->next ) { iterator->cursor->next->prev = iterator->cursor->prev; } iterator->cursor->data = NULL; /* Note we don't free data, */ free( iterator->cursor ); /* but, we do free the node */ iterator->cursor = newCursor; } } return data;}/*** Remove the element being pointed to by the iterator. Free any data associated** with the node.**** The iterator is adjusted as follows:** 1) The iterator is backed up to the previous node.** 2) If the node being removed was the first node, the iterator is reset.**** Use ObxListRemove() to remove the data and return it to the caller.*/void iobxListDelete( ObxList *list, ObxIterator *iterator ) { void *data = NULL; OBXDBGFLOW(("iobxListDelete() entry, list=0x%08x\titerator=0x%08x\n", list, iterator)); if ( (data = iobxListRemove( list, iterator ) )) { free( data ); data = NULL; }}/* **************************************** *//* ******* Iterator Functions ************* *//* **************************************** *//*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -