terence zhang / wakaama-core
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers list.c Source File

list.c

00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2013 Intel Corporation and others.
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * The Eclipse Distribution License is available at
00011  *    http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    David Navarro, Intel Corporation - initial API and implementation
00015  *    
00016  *******************************************************************************/
00017 
00018 #include "internals.h"
00019 
00020 
00021 lwm2m_list_t * lwm2m_list_add(lwm2m_list_t * head,
00022                               lwm2m_list_t * node)
00023 {
00024     lwm2m_list_t * target;
00025 
00026     if (NULL == head) return node;
00027 
00028     if (head->id > node->id)
00029     {
00030         node->next = head;
00031         return node;
00032     }
00033 
00034     target = head;
00035     while (NULL != target->next && target->next->id < node->id)
00036     {
00037         target = target->next;
00038     }
00039 
00040     node->next = target->next;
00041     target->next = node;
00042 
00043     return head;
00044 }
00045 
00046 
00047 lwm2m_list_t * lwm2m_list_find(lwm2m_list_t * head,
00048                                uint16_t id)
00049 {
00050     while (NULL != head && head->id < id)
00051     {
00052         head = head->next;
00053     }
00054 
00055     if (NULL != head && head->id == id) return head;
00056 
00057     return NULL;
00058 }
00059 
00060 
00061 lwm2m_list_t * lwm2m_list_remove(lwm2m_list_t * head,
00062                                  uint16_t id,
00063                                  lwm2m_list_t ** nodeP)
00064 {
00065     lwm2m_list_t * target;
00066 
00067     if (head == NULL)
00068     {
00069         if (nodeP) *nodeP = NULL;
00070         return NULL;
00071     }
00072 
00073     if (head->id == id)
00074     {
00075         if (nodeP) *nodeP = head;
00076         return head->next;
00077     }
00078 
00079     target = head;
00080     while (NULL != target->next && target->next->id < id)
00081     {
00082         target = target->next;
00083     }
00084 
00085     if (NULL != target->next && target->next->id == id)
00086     {
00087         if (nodeP) *nodeP = target->next;
00088         target->next = target->next->next;
00089     }
00090     else
00091     {
00092         if (nodeP) *nodeP = NULL;
00093     }
00094 
00095     return head;
00096 }
00097 
00098 uint16_t lwm2m_list_newId(lwm2m_list_t * head)
00099 {
00100     uint16_t id;
00101     lwm2m_list_t * target;
00102 
00103     id = 0;
00104     target = head;
00105 
00106     while (target != NULL && id == target->id)
00107     {
00108         id = target->id + 1;
00109         target = target->next;
00110     }
00111 
00112     return id;
00113 }
00114 
00115 void lwm2m_list_free(lwm2m_list_t * head)
00116 {
00117     if (head != NULL)
00118     {
00119         lwm2m_list_t * nextP;
00120 
00121         nextP = head->next;
00122         lwm2m_free(head);
00123         lwm2m_list_free(nextP);
00124     }
00125 }