sl@0: // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include "mp4list.h"
sl@0: #include "mp4memwrap.h"
sl@0: 
sl@0: 
sl@0: 
sl@0: /*
sl@0:  * Function:
sl@0:  *
sl@0:  *   list_s *listCreate()
sl@0:  *
sl@0:  * Description:
sl@0:  *
sl@0:  *   Create a list.
sl@0:  *
sl@0:  * Parameters:
sl@0:  *
sl@0:  *   None
sl@0:  *
sl@0:  * Return value:
sl@0:  *
sl@0:  *   Pointer to the allocated list.
sl@0:  *
sl@0:  */
sl@0: list_s *listCreate()
sl@0: {
sl@0:   list_s *tmpList;
sl@0: 
sl@0:   if ((tmpList = (list_s *)mp4malloc(sizeof(list_s))) == NULL)
sl@0:     return NULL;
sl@0: 
sl@0:   tmpList->elementsInList = 0;
sl@0:   tmpList->bytesInList = 0;
sl@0:   tmpList->cumulativeBytesInList = 0;
sl@0:   tmpList->first = NULL;
sl@0:   tmpList->last = NULL;
sl@0: 
sl@0:   return tmpList;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0:  * Function:
sl@0:  *
sl@0:  *   void *listAppend(list_s *list,
sl@0:  *                    void *data,
sl@0:  *                    mp4_u32 dataSize)
sl@0:  *
sl@0:  * Description:
sl@0:  *
sl@0:  *   Append an element (void *) to the end of the list.
sl@0:  *
sl@0:  * Parameters:
sl@0:  *
sl@0:  *   list      Pointer to a list
sl@0:  *   data      Pointer to add to the list
sl@0:  *   dataSize  Size of data pointed to by data
sl@0:  *
sl@0:  * Return value:
sl@0:  *
sl@0:  *   NULL      Error
sl@0:  *   Non-NULL  Success
sl@0:  *
sl@0:  */
sl@0: void *listAppend(list_s *list, void *data, mp4_u32 dataSize)
sl@0: {
sl@0:   node_s *tmpNode;
sl@0: 
sl@0:   if ((tmpNode = (node_s *)mp4malloc(sizeof(node_s))) == NULL)
sl@0:     return NULL;
sl@0: 
sl@0:   if (list->elementsInList == 0) /* List is empty */
sl@0:     list->first = tmpNode;
sl@0:   else
sl@0:     list->last->next = tmpNode;
sl@0: 
sl@0:   list->last = tmpNode;
sl@0:   list->elementsInList++;
sl@0:   list->bytesInList += dataSize;
sl@0:   list->cumulativeBytesInList += dataSize;
sl@0: 
sl@0:   tmpNode->next = NULL;
sl@0:   tmpNode->data = data;
sl@0:   tmpNode->dataSize = dataSize;
sl@0: 
sl@0:   return (void *)tmpNode;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0:  * Function:
sl@0:  *
sl@0:  *   void listDeleteFirst(list_s *list)
sl@0:  *
sl@0:  * Description:
sl@0:  *
sl@0:  *   Delete the first element in the list.
sl@0:  *
sl@0:  * Parameters:
sl@0:  *
sl@0:  *   list   Pointer to a list
sl@0:  *
sl@0:  * Return value:
sl@0:  *
sl@0:  *   None
sl@0:  *
sl@0:  */
sl@0: void listDeleteFirst(list_s *list)
sl@0: {
sl@0:   if (list->elementsInList == 1)
sl@0:   {
sl@0: 	mp4free(list->first->data);
sl@0:     mp4free(list->first);
sl@0:     list->first = NULL;
sl@0:     list->last = NULL;
sl@0:     list->elementsInList = 0;
sl@0:     list->bytesInList = 0;
sl@0:   }
sl@0: 
sl@0:   if (list->elementsInList > 1)
sl@0:   {
sl@0:     node_s *tmpNode;
sl@0: 
sl@0:     tmpNode = list->first;
sl@0:     list->bytesInList -= tmpNode->dataSize;
sl@0:     list->first = tmpNode->next;
sl@0: 	mp4free(tmpNode->data);
sl@0:     mp4free(tmpNode);
sl@0:     list->elementsInList--;
sl@0:   }
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0:  * Function:
sl@0:  *
sl@0:  *   void listDestroyList(list_s *list)
sl@0:  *
sl@0:  * Description:
sl@0:  *
sl@0:  *   Free list and all its elements from memory.
sl@0:  *
sl@0:  * Parameters:
sl@0:  *
sl@0:  *   list   Pointer to a list
sl@0:  *
sl@0:  * Return value:
sl@0:  *
sl@0:  *   None
sl@0:  *
sl@0:  */
sl@0: void listDestroyList(list_s *list)
sl@0: {
sl@0:   while (list->elementsInList)
sl@0:     listDeleteFirst(list);
sl@0: 
sl@0:   mp4free(list);
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0:  * Function:
sl@0:  *
sl@0:  *   mp4_u32 listElementsInList(list_s *list)
sl@0:  *
sl@0:  * Description:
sl@0:  *
sl@0:  *   Return the number of elements in the list.
sl@0:  *
sl@0:  * Parameters:
sl@0:  *
sl@0:  *   list   Pointer to a list
sl@0:  *
sl@0:  * Return value:
sl@0:  *
sl@0:  *   Number of elements in the list
sl@0:  *
sl@0:  */
sl@0: mp4_u32 listElementsInList(list_s *list)
sl@0: {
sl@0:   return list->elementsInList;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0:  * Function:
sl@0:  *
sl@0:  *   mp4_u32 listBytesInList(list_s *list)
sl@0:  *
sl@0:  * Description:
sl@0:  *
sl@0:  *   Return the number of bytes currently in list.
sl@0:  *
sl@0:  * Parameters:
sl@0:  *
sl@0:  *   list   Pointer to a list
sl@0:  *
sl@0:  * Return value:
sl@0:  *
sl@0:  *   Number of bytes in the list
sl@0:  *
sl@0:  */
sl@0: mp4_u32 listBytesInList(list_s *list)
sl@0: {
sl@0:   return list->bytesInList;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0:  * Function:
sl@0:  *
sl@0:  *   mp4_u32 listCumulativeBytesInList(list_s *list)
sl@0:  *
sl@0:  * Description:
sl@0:  *
sl@0:  *   Return the number of bytes that have been in the list since the list
sl@0:  *   was created.
sl@0:  *
sl@0:  * Parameters:
sl@0:  *
sl@0:  *   list   Pointer to a list
sl@0:  *
sl@0:  * Return value:
sl@0:  *
sl@0:  *   Number of bytes in the list from the creation
sl@0:  *
sl@0:  */
sl@0: mp4_u32 listCumulativeBytesInList(list_s *list)
sl@0: {
sl@0:   return list->cumulativeBytesInList;
sl@0: }
sl@0: // End of File