os/mm/mmplugins/lib3gp/impl/src/list.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/mmplugins/lib3gp/impl/src/list.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,246 @@
     1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "mp4list.h"
    1.20 +#include "mp4memwrap.h"
    1.21 +
    1.22 +
    1.23 +
    1.24 +/*
    1.25 + * Function:
    1.26 + *
    1.27 + *   list_s *listCreate()
    1.28 + *
    1.29 + * Description:
    1.30 + *
    1.31 + *   Create a list.
    1.32 + *
    1.33 + * Parameters:
    1.34 + *
    1.35 + *   None
    1.36 + *
    1.37 + * Return value:
    1.38 + *
    1.39 + *   Pointer to the allocated list.
    1.40 + *
    1.41 + */
    1.42 +list_s *listCreate()
    1.43 +{
    1.44 +  list_s *tmpList;
    1.45 +
    1.46 +  if ((tmpList = (list_s *)mp4malloc(sizeof(list_s))) == NULL)
    1.47 +    return NULL;
    1.48 +
    1.49 +  tmpList->elementsInList = 0;
    1.50 +  tmpList->bytesInList = 0;
    1.51 +  tmpList->cumulativeBytesInList = 0;
    1.52 +  tmpList->first = NULL;
    1.53 +  tmpList->last = NULL;
    1.54 +
    1.55 +  return tmpList;
    1.56 +}
    1.57 +
    1.58 +
    1.59 +/*
    1.60 + * Function:
    1.61 + *
    1.62 + *   void *listAppend(list_s *list,
    1.63 + *                    void *data,
    1.64 + *                    mp4_u32 dataSize)
    1.65 + *
    1.66 + * Description:
    1.67 + *
    1.68 + *   Append an element (void *) to the end of the list.
    1.69 + *
    1.70 + * Parameters:
    1.71 + *
    1.72 + *   list      Pointer to a list
    1.73 + *   data      Pointer to add to the list
    1.74 + *   dataSize  Size of data pointed to by data
    1.75 + *
    1.76 + * Return value:
    1.77 + *
    1.78 + *   NULL      Error
    1.79 + *   Non-NULL  Success
    1.80 + *
    1.81 + */
    1.82 +void *listAppend(list_s *list, void *data, mp4_u32 dataSize)
    1.83 +{
    1.84 +  node_s *tmpNode;
    1.85 +
    1.86 +  if ((tmpNode = (node_s *)mp4malloc(sizeof(node_s))) == NULL)
    1.87 +    return NULL;
    1.88 +
    1.89 +  if (list->elementsInList == 0) /* List is empty */
    1.90 +    list->first = tmpNode;
    1.91 +  else
    1.92 +    list->last->next = tmpNode;
    1.93 +
    1.94 +  list->last = tmpNode;
    1.95 +  list->elementsInList++;
    1.96 +  list->bytesInList += dataSize;
    1.97 +  list->cumulativeBytesInList += dataSize;
    1.98 +
    1.99 +  tmpNode->next = NULL;
   1.100 +  tmpNode->data = data;
   1.101 +  tmpNode->dataSize = dataSize;
   1.102 +
   1.103 +  return (void *)tmpNode;
   1.104 +}
   1.105 +
   1.106 +
   1.107 +/*
   1.108 + * Function:
   1.109 + *
   1.110 + *   void listDeleteFirst(list_s *list)
   1.111 + *
   1.112 + * Description:
   1.113 + *
   1.114 + *   Delete the first element in the list.
   1.115 + *
   1.116 + * Parameters:
   1.117 + *
   1.118 + *   list   Pointer to a list
   1.119 + *
   1.120 + * Return value:
   1.121 + *
   1.122 + *   None
   1.123 + *
   1.124 + */
   1.125 +void listDeleteFirst(list_s *list)
   1.126 +{
   1.127 +  if (list->elementsInList == 1)
   1.128 +  {
   1.129 +	mp4free(list->first->data);
   1.130 +    mp4free(list->first);
   1.131 +    list->first = NULL;
   1.132 +    list->last = NULL;
   1.133 +    list->elementsInList = 0;
   1.134 +    list->bytesInList = 0;
   1.135 +  }
   1.136 +
   1.137 +  if (list->elementsInList > 1)
   1.138 +  {
   1.139 +    node_s *tmpNode;
   1.140 +
   1.141 +    tmpNode = list->first;
   1.142 +    list->bytesInList -= tmpNode->dataSize;
   1.143 +    list->first = tmpNode->next;
   1.144 +	mp4free(tmpNode->data);
   1.145 +    mp4free(tmpNode);
   1.146 +    list->elementsInList--;
   1.147 +  }
   1.148 +}
   1.149 +
   1.150 +
   1.151 +/*
   1.152 + * Function:
   1.153 + *
   1.154 + *   void listDestroyList(list_s *list)
   1.155 + *
   1.156 + * Description:
   1.157 + *
   1.158 + *   Free list and all its elements from memory.
   1.159 + *
   1.160 + * Parameters:
   1.161 + *
   1.162 + *   list   Pointer to a list
   1.163 + *
   1.164 + * Return value:
   1.165 + *
   1.166 + *   None
   1.167 + *
   1.168 + */
   1.169 +void listDestroyList(list_s *list)
   1.170 +{
   1.171 +  while (list->elementsInList)
   1.172 +    listDeleteFirst(list);
   1.173 +
   1.174 +  mp4free(list);
   1.175 +}
   1.176 +
   1.177 +
   1.178 +/*
   1.179 + * Function:
   1.180 + *
   1.181 + *   mp4_u32 listElementsInList(list_s *list)
   1.182 + *
   1.183 + * Description:
   1.184 + *
   1.185 + *   Return the number of elements in the list.
   1.186 + *
   1.187 + * Parameters:
   1.188 + *
   1.189 + *   list   Pointer to a list
   1.190 + *
   1.191 + * Return value:
   1.192 + *
   1.193 + *   Number of elements in the list
   1.194 + *
   1.195 + */
   1.196 +mp4_u32 listElementsInList(list_s *list)
   1.197 +{
   1.198 +  return list->elementsInList;
   1.199 +}
   1.200 +
   1.201 +
   1.202 +/*
   1.203 + * Function:
   1.204 + *
   1.205 + *   mp4_u32 listBytesInList(list_s *list)
   1.206 + *
   1.207 + * Description:
   1.208 + *
   1.209 + *   Return the number of bytes currently in list.
   1.210 + *
   1.211 + * Parameters:
   1.212 + *
   1.213 + *   list   Pointer to a list
   1.214 + *
   1.215 + * Return value:
   1.216 + *
   1.217 + *   Number of bytes in the list
   1.218 + *
   1.219 + */
   1.220 +mp4_u32 listBytesInList(list_s *list)
   1.221 +{
   1.222 +  return list->bytesInList;
   1.223 +}
   1.224 +
   1.225 +
   1.226 +/*
   1.227 + * Function:
   1.228 + *
   1.229 + *   mp4_u32 listCumulativeBytesInList(list_s *list)
   1.230 + *
   1.231 + * Description:
   1.232 + *
   1.233 + *   Return the number of bytes that have been in the list since the list
   1.234 + *   was created.
   1.235 + *
   1.236 + * Parameters:
   1.237 + *
   1.238 + *   list   Pointer to a list
   1.239 + *
   1.240 + * Return value:
   1.241 + *
   1.242 + *   Number of bytes in the list from the creation
   1.243 + *
   1.244 + */
   1.245 +mp4_u32 listCumulativeBytesInList(list_s *list)
   1.246 +{
   1.247 +  return list->cumulativeBytesInList;
   1.248 +}
   1.249 +// End of File