Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Revision:
34:0a87c89bdcd0
Parent:
30:ce3813c5a692
Child:
45:1119d0f2c4d8
--- a/singlylinkedlist.c	Mon Sep 25 13:39:31 2017 -0700
+++ b/singlylinkedlist.c	Sat Oct 21 20:13:50 2017 +0000
@@ -16,6 +16,7 @@
 typedef struct SINGLYLINKEDLIST_INSTANCE_TAG
 {
     LIST_ITEM_INSTANCE* head;
+    LIST_ITEM_INSTANCE* tail;
 } LIST_INSTANCE;
 
 SINGLYLINKEDLIST_HANDLE singlylinkedlist_create(void)
@@ -28,6 +29,7 @@
     {
         /* Codes_SRS_LIST_01_002: [If any error occurs during the list creation, singlylinkedlist_create shall return NULL.] */
         result->head = NULL;
+        result->tail = NULL;
     }
 
     return result;
@@ -60,7 +62,7 @@
     if ((list == NULL) ||
         (item == NULL))
     {
-		LogError("Invalid argument (list=%p, item=%p)", list, item);
+        LogError("Invalid argument (list=%p, item=%p)", list, item);
         result = NULL;
     }
     else
@@ -82,16 +84,12 @@
             if (list_instance->head == NULL)
             {
                 list_instance->head = result;
+                list_instance->tail = result;
             }
             else
             {
-                LIST_ITEM_INSTANCE* current = list_instance->head;
-                while (current->next != NULL)
-                {
-                    current = (LIST_ITEM_INSTANCE*)current->next;
-                }
-
-                current->next = result;
+                list_instance->tail->next = result;
+                list_instance->tail = result;
             }
         }
     }
@@ -103,11 +101,11 @@
 {
     int result;
 
-	/* Codes_SRS_LIST_01_024: [If any of the arguments list or item_handle is NULL, singlylinkedlist_remove shall fail and return a non-zero value.] */
-	if ((list == NULL) ||
+    /* Codes_SRS_LIST_01_024: [If any of the arguments list or item_handle is NULL, singlylinkedlist_remove shall fail and return a non-zero value.] */
+    if ((list == NULL) ||
         (item == NULL))
     {
-		LogError("Invalid argument (list=%p, item=%p)", list, item);
+        LogError("Invalid argument (list=%p, item=%p)", list, item);
         result = __FAILURE__;
     }
     else
@@ -129,24 +127,30 @@
                     list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next;
                 }
 
+                if (current_item == list_instance->tail)
+                {
+                    list_instance->tail = previous_item;
+                }
+
                 free(current_item);
 
                 break;
             }
+
             previous_item = current_item;
-			current_item = (LIST_ITEM_INSTANCE*)current_item->next;
+            current_item = (LIST_ITEM_INSTANCE*)current_item->next;
         }
 
-		if (current_item == NULL)
-		{
-			/* Codes_SRS_LIST_01_025: [If the item item_handle is not found in the list, then singlylinkedlist_remove shall fail and return a non-zero value.] */
-			result = __FAILURE__;
-		}
-		else
-		{
-			/* Codes_SRS_LIST_01_023: [singlylinkedlist_remove shall remove a list item from the list and on success it shall return 0.] */
-			result = 0;
-		}
+        if (current_item == NULL)
+        {
+            /* Codes_SRS_LIST_01_025: [If the item item_handle is not found in the list, then singlylinkedlist_remove shall fail and return a non-zero value.] */
+            result = __FAILURE__;
+        }
+        else
+        {
+            /* Codes_SRS_LIST_01_023: [singlylinkedlist_remove shall remove a list item from the list and on success it shall return 0.] */
+            result = 0;
+        }
     }
 
     return result;
@@ -159,8 +163,8 @@
     if (list == NULL)
     {
         /* Codes_SRS_LIST_01_009: [If the list argument is NULL, singlylinkedlist_get_head_item shall return NULL.] */
-		LogError("Invalid argument (list=NULL)");
-		result = NULL;
+        LogError("Invalid argument (list=NULL)");
+        result = NULL;
     }
     else
     {
@@ -180,7 +184,7 @@
 
     if (item_handle == NULL)
     {
-		LogError("Invalid argument (list is NULL)");
+        LogError("Invalid argument (list is NULL)");
         /* Codes_SRS_LIST_01_019: [If item_handle is NULL then singlylinkedlist_get_next_item shall return NULL.] */
         result = NULL;
     }
@@ -199,7 +203,7 @@
 
     if (item_handle == NULL)
     {
-		LogError("Invalid argument (item_handle is NULL)");
+        LogError("Invalid argument (item_handle is NULL)");
         /* Codes_SRS_LIST_01_021: [If item_handle is NULL, singlylinkedlist_item_get_value shall return NULL.] */
         result = NULL;
     }
@@ -219,7 +223,7 @@
     if ((list == NULL) ||
         (match_function == NULL))
     {
-		LogError("Invalid argument (list=%p, match_function=%p)", list, match_function);
+        LogError("Invalid argument (list=%p, match_function=%p)", list, match_function);
         /* Codes_SRS_LIST_01_012: [If the list or the match_function argument is NULL, singlylinkedlist_find shall return NULL.] */
         result = NULL;
     }
@@ -259,100 +263,105 @@
 
 int singlylinkedlist_remove_if(SINGLYLINKEDLIST_HANDLE list, LIST_CONDITION_FUNCTION condition_function, const void* match_context)
 {
-	int result;
-	/* Codes_SRS_LIST_09_001: [ If the list or the condition_function argument is NULL, singlylinkedlist_remove_if shall return non-zero value. ] */
-	if ((list == NULL) ||
-		(condition_function == NULL))
-	{
-		LogError("Invalid argument (list=%p, condition_function=%p)", list, condition_function);
-		result = __FAILURE__;
-	}
-	else
-	{
-		LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
-		LIST_ITEM_INSTANCE* current_item = list_instance->head;
-		LIST_ITEM_INSTANCE* next_item = NULL;
-		LIST_ITEM_INSTANCE* previous_item = NULL;
+    int result;
+    /* Codes_SRS_LIST_09_001: [ If the list or the condition_function argument is NULL, singlylinkedlist_remove_if shall return non-zero value. ] */
+    if ((list == NULL) ||
+        (condition_function == NULL))
+    {
+        LogError("Invalid argument (list=%p, condition_function=%p)", list, condition_function);
+        result = __FAILURE__;
+    }
+    else
+    {
+        LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
+        LIST_ITEM_INSTANCE* current_item = list_instance->head;
+        LIST_ITEM_INSTANCE* next_item = NULL;
+        LIST_ITEM_INSTANCE* previous_item = NULL;
 
-		/* Codes_SRS_LIST_09_002: [ singlylinkedlist_remove_if shall iterate through all items in a list and remove all that satisfies a certain condition function. ] */
-		while (current_item != NULL)
-		{
-			bool continue_processing = false;
+        /* Codes_SRS_LIST_09_002: [ singlylinkedlist_remove_if shall iterate through all items in a list and remove all that satisfies a certain condition function. ] */
+        while (current_item != NULL)
+        {
+            bool continue_processing = false;
 
-			next_item = (LIST_ITEM_INSTANCE*)current_item->next;
+            next_item = (LIST_ITEM_INSTANCE*)current_item->next;
 
-			/* Codes_SRS_LIST_09_003: [ singlylinkedlist_remove_if shall determine whether an item satisfies the condition criteria by invoking the condition function for that item. ] */
-			/* Codes_SRS_LIST_09_004: [ If the condition function returns true, singlylinkedlist_find shall consider that item as to be removed. ] */
-			if (condition_function(current_item->item, match_context, &continue_processing) == true)
-			{
-				if (previous_item != NULL)
-				{
-					previous_item->next = next_item;
-				}
-				else
-				{
-					list_instance->head = next_item;
-				}
+            /* Codes_SRS_LIST_09_003: [ singlylinkedlist_remove_if shall determine whether an item satisfies the condition criteria by invoking the condition function for that item. ] */
+            /* Codes_SRS_LIST_09_004: [ If the condition function returns true, singlylinkedlist_find shall consider that item as to be removed. ] */
+            if (condition_function(current_item->item, match_context, &continue_processing) == true)
+            {
+                if (previous_item != NULL)
+                {
+                    previous_item->next = next_item;
+                }
+                else
+                {
+                    list_instance->head = next_item;
+                }
+
+                if (current_item == list_instance->tail)
+                {
+                    list_instance->tail = previous_item;
+                }
 
-				free(current_item);
-			}
-			/* Codes_SRS_LIST_09_005: [ If the condition function returns false, singlylinkedlist_find shall consider that item as not to be removed. ] */
-			else
-			{
-				previous_item = current_item;
-			}
+                free(current_item);
+            }
+            /* Codes_SRS_LIST_09_005: [ If the condition function returns false, singlylinkedlist_find shall consider that item as not to be removed. ] */
+            else
+            {
+                previous_item = current_item;
+            }
 
-			/* Codes_SRS_LIST_09_006: [ If the condition function returns continue_processing as false, singlylinkedlist_remove_if shall stop iterating through the list and return. ] */
-			if (continue_processing == false)
-			{
-				break;
-			}
+            /* Codes_SRS_LIST_09_006: [ If the condition function returns continue_processing as false, singlylinkedlist_remove_if shall stop iterating through the list and return. ] */
+            if (continue_processing == false)
+            {
+                break;
+            }
 
-			current_item = next_item;
-		}
+            current_item = next_item;
+        }
 
-		/* Codes_SRS_LIST_09_007: [ If no errors occur, singlylinkedlist_remove_if shall return zero. ] */
-		result = 0;
-	}
+        /* Codes_SRS_LIST_09_007: [ If no errors occur, singlylinkedlist_remove_if shall return zero. ] */
+        result = 0;
+    }
 
-	return result;
+    return result;
 }
 
 int singlylinkedlist_foreach(SINGLYLINKEDLIST_HANDLE list, LIST_ACTION_FUNCTION action_function, const void* action_context)
 {
-	int result;
+    int result;
 
-	/* Codes_SRS_LIST_09_008: [ If the list or the action_function argument is NULL, singlylinkedlist_foreach shall return non-zero value. ] */
-	if ((list == NULL) ||
-		(action_function == NULL))
-	{
-		LogError("Invalid argument (list=%p, action_function=%p)", list, action_function);
-		result = __FAILURE__;
-	}
-	else
-	{
-		LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
-		LIST_ITEM_INSTANCE* list_item = list_instance->head;
+    /* Codes_SRS_LIST_09_008: [ If the list or the action_function argument is NULL, singlylinkedlist_foreach shall return non-zero value. ] */
+    if ((list == NULL) ||
+        (action_function == NULL))
+    {
+        LogError("Invalid argument (list=%p, action_function=%p)", list, action_function);
+        result = __FAILURE__;
+    }
+    else
+    {
+        LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
+        LIST_ITEM_INSTANCE* list_item = list_instance->head;
 
-		while (list_item != NULL)
-		{
-			bool continue_processing = false;
+        while (list_item != NULL)
+        {
+            bool continue_processing = false;
 
-			/* Codes_SRS_LIST_09_009: [ singlylinkedlist_foreach shall iterate through all items in a list and invoke action_function for each one of them. ] */
-			action_function(list_item->item, action_context, &continue_processing);
+            /* Codes_SRS_LIST_09_009: [ singlylinkedlist_foreach shall iterate through all items in a list and invoke action_function for each one of them. ] */
+            action_function(list_item->item, action_context, &continue_processing);
 
-			/* Codes_SRS_LIST_09_010: [ If the condition function returns continue_processing as false, singlylinkedlist_foreach shall stop iterating through the list and return. ] */
-			if (continue_processing == false)
-			{
-				break;
-			}
+            /* Codes_SRS_LIST_09_010: [ If the condition function returns continue_processing as false, singlylinkedlist_foreach shall stop iterating through the list and return. ] */
+            if (continue_processing == false)
+            {
+                break;
+            }
 
-			list_item = (LIST_ITEM_INSTANCE*)list_item->next;
-		}
+            list_item = (LIST_ITEM_INSTANCE*)list_item->next;
+        }
 
-		/* Codes_SRS_LIST_09_011: [ If no errors occur, singlylinkedlist_foreach shall return zero. ] */
-		result = 0;
-	}
+        /* Codes_SRS_LIST_09_011: [ If no errors occur, singlylinkedlist_foreach shall return zero. ] */
+        result = 0;
+    }
 
-	return result;
+    return result;
 }
\ No newline at end of file