Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
wiggly
Date:
Thu Aug 24 14:14:15 2017 +0100
Revision:
34:651c23af382c
Parent:
30:ce3813c5a692
Pass in network stack to platform initialisation
Remove NTP setup from azure platform code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 13:920e00014ee3 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 13:920e00014ee3 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 13:920e00014ee3 3
AzureIoTClient 13:920e00014ee3 4 #include <stdlib.h>
AzureIoTClient 13:920e00014ee3 5 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 13:920e00014ee3 6 #include "azure_c_shared_utility/singlylinkedlist.h"
AzureIoTClient 21:b92006c5b9ff 7 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 30:ce3813c5a692 8 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 13:920e00014ee3 9
AzureIoTClient 13:920e00014ee3 10 typedef struct LIST_ITEM_INSTANCE_TAG
AzureIoTClient 13:920e00014ee3 11 {
AzureIoTClient 13:920e00014ee3 12 const void* item;
AzureIoTClient 13:920e00014ee3 13 void* next;
AzureIoTClient 13:920e00014ee3 14 } LIST_ITEM_INSTANCE;
AzureIoTClient 13:920e00014ee3 15
AzureIoTClient 13:920e00014ee3 16 typedef struct SINGLYLINKEDLIST_INSTANCE_TAG
AzureIoTClient 13:920e00014ee3 17 {
AzureIoTClient 13:920e00014ee3 18 LIST_ITEM_INSTANCE* head;
AzureIoTClient 13:920e00014ee3 19 } LIST_INSTANCE;
AzureIoTClient 13:920e00014ee3 20
AzureIoTClient 13:920e00014ee3 21 SINGLYLINKEDLIST_HANDLE singlylinkedlist_create(void)
AzureIoTClient 13:920e00014ee3 22 {
AzureIoTClient 13:920e00014ee3 23 LIST_INSTANCE* result;
AzureIoTClient 13:920e00014ee3 24
AzureIoTClient 13:920e00014ee3 25 /* Codes_SRS_LIST_01_001: [singlylinkedlist_create shall create a new list and return a non-NULL handle on success.] */
AzureIoTClient 13:920e00014ee3 26 result = (LIST_INSTANCE*)malloc(sizeof(LIST_INSTANCE));
AzureIoTClient 13:920e00014ee3 27 if (result != NULL)
AzureIoTClient 13:920e00014ee3 28 {
AzureIoTClient 13:920e00014ee3 29 /* Codes_SRS_LIST_01_002: [If any error occurs during the list creation, singlylinkedlist_create shall return NULL.] */
AzureIoTClient 13:920e00014ee3 30 result->head = NULL;
AzureIoTClient 13:920e00014ee3 31 }
AzureIoTClient 13:920e00014ee3 32
AzureIoTClient 13:920e00014ee3 33 return result;
AzureIoTClient 13:920e00014ee3 34 }
AzureIoTClient 13:920e00014ee3 35
AzureIoTClient 13:920e00014ee3 36 void singlylinkedlist_destroy(SINGLYLINKEDLIST_HANDLE list)
AzureIoTClient 13:920e00014ee3 37 {
AzureIoTClient 13:920e00014ee3 38 /* Codes_SRS_LIST_01_004: [If the list argument is NULL, no freeing of resources shall occur.] */
AzureIoTClient 13:920e00014ee3 39 if (list != NULL)
AzureIoTClient 13:920e00014ee3 40 {
AzureIoTClient 13:920e00014ee3 41 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 42
AzureIoTClient 13:920e00014ee3 43 while (list_instance->head != NULL)
AzureIoTClient 13:920e00014ee3 44 {
AzureIoTClient 13:920e00014ee3 45 LIST_ITEM_INSTANCE* current_item = list_instance->head;
AzureIoTClient 13:920e00014ee3 46 list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next;
AzureIoTClient 13:920e00014ee3 47 free(current_item);
AzureIoTClient 13:920e00014ee3 48 }
AzureIoTClient 13:920e00014ee3 49
AzureIoTClient 13:920e00014ee3 50 /* Codes_SRS_LIST_01_003: [singlylinkedlist_destroy shall free all resources associated with the list identified by the handle argument.] */
AzureIoTClient 13:920e00014ee3 51 free(list_instance);
AzureIoTClient 13:920e00014ee3 52 }
AzureIoTClient 13:920e00014ee3 53 }
AzureIoTClient 13:920e00014ee3 54
AzureIoTClient 13:920e00014ee3 55 LIST_ITEM_HANDLE singlylinkedlist_add(SINGLYLINKEDLIST_HANDLE list, const void* item)
AzureIoTClient 13:920e00014ee3 56 {
AzureIoTClient 13:920e00014ee3 57 LIST_ITEM_INSTANCE* result;
AzureIoTClient 13:920e00014ee3 58
AzureIoTClient 13:920e00014ee3 59 /* Codes_SRS_LIST_01_006: [If any of the arguments is NULL, singlylinkedlist_add shall not add the item to the list and return NULL.] */
AzureIoTClient 13:920e00014ee3 60 if ((list == NULL) ||
AzureIoTClient 13:920e00014ee3 61 (item == NULL))
AzureIoTClient 13:920e00014ee3 62 {
AzureIoTClient 30:ce3813c5a692 63 LogError("Invalid argument (list=%p, item=%p)", list, item);
AzureIoTClient 13:920e00014ee3 64 result = NULL;
AzureIoTClient 13:920e00014ee3 65 }
AzureIoTClient 13:920e00014ee3 66 else
AzureIoTClient 13:920e00014ee3 67 {
AzureIoTClient 13:920e00014ee3 68 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 69 result = (LIST_ITEM_INSTANCE*)malloc(sizeof(LIST_ITEM_INSTANCE));
AzureIoTClient 13:920e00014ee3 70
AzureIoTClient 13:920e00014ee3 71 if (result == NULL)
AzureIoTClient 13:920e00014ee3 72 {
AzureIoTClient 13:920e00014ee3 73 /* Codes_SRS_LIST_01_007: [If allocating the new list node fails, singlylinkedlist_add shall return NULL.] */
AzureIoTClient 13:920e00014ee3 74 result = NULL;
AzureIoTClient 13:920e00014ee3 75 }
AzureIoTClient 13:920e00014ee3 76 else
AzureIoTClient 13:920e00014ee3 77 {
AzureIoTClient 13:920e00014ee3 78 /* Codes_SRS_LIST_01_005: [singlylinkedlist_add shall add one item to the tail of the list and on success it shall return a handle to the added item.] */
AzureIoTClient 13:920e00014ee3 79 result->next = NULL;
AzureIoTClient 13:920e00014ee3 80 result->item = item;
AzureIoTClient 13:920e00014ee3 81
AzureIoTClient 13:920e00014ee3 82 if (list_instance->head == NULL)
AzureIoTClient 13:920e00014ee3 83 {
AzureIoTClient 13:920e00014ee3 84 list_instance->head = result;
AzureIoTClient 13:920e00014ee3 85 }
AzureIoTClient 13:920e00014ee3 86 else
AzureIoTClient 13:920e00014ee3 87 {
AzureIoTClient 13:920e00014ee3 88 LIST_ITEM_INSTANCE* current = list_instance->head;
AzureIoTClient 13:920e00014ee3 89 while (current->next != NULL)
AzureIoTClient 13:920e00014ee3 90 {
AzureIoTClient 13:920e00014ee3 91 current = (LIST_ITEM_INSTANCE*)current->next;
AzureIoTClient 13:920e00014ee3 92 }
AzureIoTClient 13:920e00014ee3 93
AzureIoTClient 13:920e00014ee3 94 current->next = result;
AzureIoTClient 13:920e00014ee3 95 }
AzureIoTClient 13:920e00014ee3 96 }
AzureIoTClient 13:920e00014ee3 97 }
AzureIoTClient 13:920e00014ee3 98
AzureIoTClient 13:920e00014ee3 99 return result;
AzureIoTClient 13:920e00014ee3 100 }
AzureIoTClient 13:920e00014ee3 101
AzureIoTClient 13:920e00014ee3 102 int singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE item)
AzureIoTClient 13:920e00014ee3 103 {
AzureIoTClient 13:920e00014ee3 104 int result;
AzureIoTClient 13:920e00014ee3 105
AzureIoTClient 13:920e00014ee3 106 /* 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.] */
AzureIoTClient 13:920e00014ee3 107 if ((list == NULL) ||
AzureIoTClient 13:920e00014ee3 108 (item == NULL))
AzureIoTClient 13:920e00014ee3 109 {
AzureIoTClient 30:ce3813c5a692 110 LogError("Invalid argument (list=%p, item=%p)", list, item);
AzureIoTClient 21:b92006c5b9ff 111 result = __FAILURE__;
AzureIoTClient 13:920e00014ee3 112 }
AzureIoTClient 13:920e00014ee3 113 else
AzureIoTClient 13:920e00014ee3 114 {
AzureIoTClient 13:920e00014ee3 115 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 116 LIST_ITEM_INSTANCE* current_item = list_instance->head;
AzureIoTClient 13:920e00014ee3 117 LIST_ITEM_INSTANCE* previous_item = NULL;
AzureIoTClient 13:920e00014ee3 118
AzureIoTClient 13:920e00014ee3 119 while (current_item != NULL)
AzureIoTClient 13:920e00014ee3 120 {
AzureIoTClient 13:920e00014ee3 121 if (current_item == item)
AzureIoTClient 13:920e00014ee3 122 {
AzureIoTClient 13:920e00014ee3 123 if (previous_item != NULL)
AzureIoTClient 13:920e00014ee3 124 {
AzureIoTClient 13:920e00014ee3 125 previous_item->next = current_item->next;
AzureIoTClient 13:920e00014ee3 126 }
AzureIoTClient 13:920e00014ee3 127 else
AzureIoTClient 13:920e00014ee3 128 {
AzureIoTClient 13:920e00014ee3 129 list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next;
AzureIoTClient 13:920e00014ee3 130 }
AzureIoTClient 13:920e00014ee3 131
AzureIoTClient 13:920e00014ee3 132 free(current_item);
AzureIoTClient 13:920e00014ee3 133
AzureIoTClient 13:920e00014ee3 134 break;
AzureIoTClient 13:920e00014ee3 135 }
AzureIoTClient 13:920e00014ee3 136 previous_item = current_item;
AzureIoTClient 13:920e00014ee3 137 current_item = (LIST_ITEM_INSTANCE*)current_item->next;
AzureIoTClient 13:920e00014ee3 138 }
AzureIoTClient 13:920e00014ee3 139
AzureIoTClient 13:920e00014ee3 140 if (current_item == NULL)
AzureIoTClient 13:920e00014ee3 141 {
AzureIoTClient 13:920e00014ee3 142 /* 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.] */
AzureIoTClient 21:b92006c5b9ff 143 result = __FAILURE__;
AzureIoTClient 13:920e00014ee3 144 }
AzureIoTClient 13:920e00014ee3 145 else
AzureIoTClient 13:920e00014ee3 146 {
AzureIoTClient 13:920e00014ee3 147 /* Codes_SRS_LIST_01_023: [singlylinkedlist_remove shall remove a list item from the list and on success it shall return 0.] */
AzureIoTClient 13:920e00014ee3 148 result = 0;
AzureIoTClient 13:920e00014ee3 149 }
AzureIoTClient 13:920e00014ee3 150 }
AzureIoTClient 13:920e00014ee3 151
AzureIoTClient 13:920e00014ee3 152 return result;
AzureIoTClient 13:920e00014ee3 153 }
AzureIoTClient 13:920e00014ee3 154
AzureIoTClient 13:920e00014ee3 155 LIST_ITEM_HANDLE singlylinkedlist_get_head_item(SINGLYLINKEDLIST_HANDLE list)
AzureIoTClient 13:920e00014ee3 156 {
AzureIoTClient 13:920e00014ee3 157 LIST_ITEM_HANDLE result;
AzureIoTClient 13:920e00014ee3 158
AzureIoTClient 13:920e00014ee3 159 if (list == NULL)
AzureIoTClient 13:920e00014ee3 160 {
AzureIoTClient 13:920e00014ee3 161 /* Codes_SRS_LIST_01_009: [If the list argument is NULL, singlylinkedlist_get_head_item shall return NULL.] */
AzureIoTClient 30:ce3813c5a692 162 LogError("Invalid argument (list=NULL)");
AzureIoTClient 30:ce3813c5a692 163 result = NULL;
AzureIoTClient 13:920e00014ee3 164 }
AzureIoTClient 13:920e00014ee3 165 else
AzureIoTClient 13:920e00014ee3 166 {
AzureIoTClient 13:920e00014ee3 167 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 168
AzureIoTClient 13:920e00014ee3 169 /* Codes_SRS_LIST_01_008: [singlylinkedlist_get_head_item shall return the head of the list.] */
AzureIoTClient 13:920e00014ee3 170 /* Codes_SRS_LIST_01_010: [If the list is empty, singlylinkedlist_get_head_item_shall_return NULL.] */
AzureIoTClient 13:920e00014ee3 171 result = list_instance->head;
AzureIoTClient 13:920e00014ee3 172 }
AzureIoTClient 13:920e00014ee3 173
AzureIoTClient 13:920e00014ee3 174 return result;
AzureIoTClient 13:920e00014ee3 175 }
AzureIoTClient 13:920e00014ee3 176
AzureIoTClient 13:920e00014ee3 177 LIST_ITEM_HANDLE singlylinkedlist_get_next_item(LIST_ITEM_HANDLE item_handle)
AzureIoTClient 13:920e00014ee3 178 {
AzureIoTClient 13:920e00014ee3 179 LIST_ITEM_HANDLE result;
AzureIoTClient 13:920e00014ee3 180
AzureIoTClient 13:920e00014ee3 181 if (item_handle == NULL)
AzureIoTClient 13:920e00014ee3 182 {
AzureIoTClient 30:ce3813c5a692 183 LogError("Invalid argument (list is NULL)");
AzureIoTClient 13:920e00014ee3 184 /* Codes_SRS_LIST_01_019: [If item_handle is NULL then singlylinkedlist_get_next_item shall return NULL.] */
AzureIoTClient 13:920e00014ee3 185 result = NULL;
AzureIoTClient 13:920e00014ee3 186 }
AzureIoTClient 13:920e00014ee3 187 else
AzureIoTClient 13:920e00014ee3 188 {
AzureIoTClient 13:920e00014ee3 189 /* Codes_SRS_LIST_01_018: [singlylinkedlist_get_next_item shall return the next item in the list following the item item_handle.] */
AzureIoTClient 13:920e00014ee3 190 result = (LIST_ITEM_HANDLE)((LIST_ITEM_INSTANCE*)item_handle)->next;
AzureIoTClient 13:920e00014ee3 191 }
AzureIoTClient 13:920e00014ee3 192
AzureIoTClient 13:920e00014ee3 193 return result;
AzureIoTClient 13:920e00014ee3 194 }
AzureIoTClient 13:920e00014ee3 195
AzureIoTClient 13:920e00014ee3 196 const void* singlylinkedlist_item_get_value(LIST_ITEM_HANDLE item_handle)
AzureIoTClient 13:920e00014ee3 197 {
AzureIoTClient 13:920e00014ee3 198 const void* result;
AzureIoTClient 13:920e00014ee3 199
AzureIoTClient 13:920e00014ee3 200 if (item_handle == NULL)
AzureIoTClient 13:920e00014ee3 201 {
AzureIoTClient 30:ce3813c5a692 202 LogError("Invalid argument (item_handle is NULL)");
AzureIoTClient 13:920e00014ee3 203 /* Codes_SRS_LIST_01_021: [If item_handle is NULL, singlylinkedlist_item_get_value shall return NULL.] */
AzureIoTClient 13:920e00014ee3 204 result = NULL;
AzureIoTClient 13:920e00014ee3 205 }
AzureIoTClient 13:920e00014ee3 206 else
AzureIoTClient 13:920e00014ee3 207 {
AzureIoTClient 13:920e00014ee3 208 /* Codes_SRS_LIST_01_020: [singlylinkedlist_item_get_value shall return the value associated with the list item identified by the item_handle argument.] */
AzureIoTClient 13:920e00014ee3 209 result = ((LIST_ITEM_INSTANCE*)item_handle)->item;
AzureIoTClient 13:920e00014ee3 210 }
AzureIoTClient 13:920e00014ee3 211
AzureIoTClient 13:920e00014ee3 212 return result;
AzureIoTClient 13:920e00014ee3 213 }
AzureIoTClient 13:920e00014ee3 214
AzureIoTClient 13:920e00014ee3 215 LIST_ITEM_HANDLE singlylinkedlist_find(SINGLYLINKEDLIST_HANDLE list, LIST_MATCH_FUNCTION match_function, const void* match_context)
AzureIoTClient 13:920e00014ee3 216 {
AzureIoTClient 13:920e00014ee3 217 LIST_ITEM_HANDLE result;
AzureIoTClient 13:920e00014ee3 218
AzureIoTClient 13:920e00014ee3 219 if ((list == NULL) ||
AzureIoTClient 13:920e00014ee3 220 (match_function == NULL))
AzureIoTClient 13:920e00014ee3 221 {
AzureIoTClient 30:ce3813c5a692 222 LogError("Invalid argument (list=%p, match_function=%p)", list, match_function);
AzureIoTClient 13:920e00014ee3 223 /* Codes_SRS_LIST_01_012: [If the list or the match_function argument is NULL, singlylinkedlist_find shall return NULL.] */
AzureIoTClient 13:920e00014ee3 224 result = NULL;
AzureIoTClient 13:920e00014ee3 225 }
AzureIoTClient 13:920e00014ee3 226 else
AzureIoTClient 13:920e00014ee3 227 {
AzureIoTClient 13:920e00014ee3 228 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 229 LIST_ITEM_INSTANCE* current = list_instance->head;
AzureIoTClient 13:920e00014ee3 230
AzureIoTClient 13:920e00014ee3 231 /* Codes_SRS_LIST_01_011: [singlylinkedlist_find shall iterate through all items in a list and return the first one that satisfies a certain match function.] */
AzureIoTClient 13:920e00014ee3 232 while (current != NULL)
AzureIoTClient 13:920e00014ee3 233 {
AzureIoTClient 13:920e00014ee3 234 /* Codes_SRS_LIST_01_014: [list find shall determine whether an item satisfies the match criteria by invoking the match function for each item in the list until a matching item is found.] */
AzureIoTClient 13:920e00014ee3 235 /* Codes_SRS_LIST_01_013: [The match_function shall get as arguments the list item being attempted to be matched and the match_context as is.] */
AzureIoTClient 13:920e00014ee3 236 if (match_function((LIST_ITEM_HANDLE)current, match_context) == true)
AzureIoTClient 13:920e00014ee3 237 {
AzureIoTClient 13:920e00014ee3 238 /* Codes_SRS_LIST_01_017: [If the match function returns true, singlylinkedlist_find shall consider that item as matching.] */
AzureIoTClient 13:920e00014ee3 239 break;
AzureIoTClient 13:920e00014ee3 240 }
AzureIoTClient 13:920e00014ee3 241
AzureIoTClient 13:920e00014ee3 242 /* Codes_SRS_LIST_01_016: [If the match function returns false, singlylinkedlist_find shall consider that item as not matching.] */
AzureIoTClient 13:920e00014ee3 243 current = (LIST_ITEM_INSTANCE*)current->next;
AzureIoTClient 13:920e00014ee3 244 }
AzureIoTClient 13:920e00014ee3 245
AzureIoTClient 13:920e00014ee3 246 if (current == NULL)
AzureIoTClient 13:920e00014ee3 247 {
AzureIoTClient 13:920e00014ee3 248 /* Codes_SRS_LIST_01_015: [If the list is empty, singlylinkedlist_find shall return NULL.] */
AzureIoTClient 13:920e00014ee3 249 result = NULL;
AzureIoTClient 13:920e00014ee3 250 }
AzureIoTClient 13:920e00014ee3 251 else
AzureIoTClient 13:920e00014ee3 252 {
AzureIoTClient 13:920e00014ee3 253 result = current;
AzureIoTClient 13:920e00014ee3 254 }
AzureIoTClient 13:920e00014ee3 255 }
AzureIoTClient 13:920e00014ee3 256
AzureIoTClient 13:920e00014ee3 257 return result;
AzureIoTClient 13:920e00014ee3 258 }
AzureIoTClient 30:ce3813c5a692 259
AzureIoTClient 30:ce3813c5a692 260 int singlylinkedlist_remove_if(SINGLYLINKEDLIST_HANDLE list, LIST_CONDITION_FUNCTION condition_function, const void* match_context)
AzureIoTClient 30:ce3813c5a692 261 {
AzureIoTClient 30:ce3813c5a692 262 int result;
AzureIoTClient 30:ce3813c5a692 263 /* Codes_SRS_LIST_09_001: [ If the list or the condition_function argument is NULL, singlylinkedlist_remove_if shall return non-zero value. ] */
AzureIoTClient 30:ce3813c5a692 264 if ((list == NULL) ||
AzureIoTClient 30:ce3813c5a692 265 (condition_function == NULL))
AzureIoTClient 30:ce3813c5a692 266 {
AzureIoTClient 30:ce3813c5a692 267 LogError("Invalid argument (list=%p, condition_function=%p)", list, condition_function);
AzureIoTClient 30:ce3813c5a692 268 result = __FAILURE__;
AzureIoTClient 30:ce3813c5a692 269 }
AzureIoTClient 30:ce3813c5a692 270 else
AzureIoTClient 30:ce3813c5a692 271 {
AzureIoTClient 30:ce3813c5a692 272 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 30:ce3813c5a692 273 LIST_ITEM_INSTANCE* current_item = list_instance->head;
AzureIoTClient 30:ce3813c5a692 274 LIST_ITEM_INSTANCE* next_item = NULL;
AzureIoTClient 30:ce3813c5a692 275 LIST_ITEM_INSTANCE* previous_item = NULL;
AzureIoTClient 30:ce3813c5a692 276
AzureIoTClient 30:ce3813c5a692 277 /* 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. ] */
AzureIoTClient 30:ce3813c5a692 278 while (current_item != NULL)
AzureIoTClient 30:ce3813c5a692 279 {
AzureIoTClient 30:ce3813c5a692 280 bool continue_processing = false;
AzureIoTClient 30:ce3813c5a692 281
AzureIoTClient 30:ce3813c5a692 282 next_item = (LIST_ITEM_INSTANCE*)current_item->next;
AzureIoTClient 30:ce3813c5a692 283
AzureIoTClient 30:ce3813c5a692 284 /* 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. ] */
AzureIoTClient 30:ce3813c5a692 285 /* Codes_SRS_LIST_09_004: [ If the condition function returns true, singlylinkedlist_find shall consider that item as to be removed. ] */
AzureIoTClient 30:ce3813c5a692 286 if (condition_function(current_item->item, match_context, &continue_processing) == true)
AzureIoTClient 30:ce3813c5a692 287 {
AzureIoTClient 30:ce3813c5a692 288 if (previous_item != NULL)
AzureIoTClient 30:ce3813c5a692 289 {
AzureIoTClient 30:ce3813c5a692 290 previous_item->next = next_item;
AzureIoTClient 30:ce3813c5a692 291 }
AzureIoTClient 30:ce3813c5a692 292 else
AzureIoTClient 30:ce3813c5a692 293 {
AzureIoTClient 30:ce3813c5a692 294 list_instance->head = next_item;
AzureIoTClient 30:ce3813c5a692 295 }
AzureIoTClient 30:ce3813c5a692 296
AzureIoTClient 30:ce3813c5a692 297 free(current_item);
AzureIoTClient 30:ce3813c5a692 298 }
AzureIoTClient 30:ce3813c5a692 299 /* Codes_SRS_LIST_09_005: [ If the condition function returns false, singlylinkedlist_find shall consider that item as not to be removed. ] */
AzureIoTClient 30:ce3813c5a692 300 else
AzureIoTClient 30:ce3813c5a692 301 {
AzureIoTClient 30:ce3813c5a692 302 previous_item = current_item;
AzureIoTClient 30:ce3813c5a692 303 }
AzureIoTClient 30:ce3813c5a692 304
AzureIoTClient 30:ce3813c5a692 305 /* 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. ] */
AzureIoTClient 30:ce3813c5a692 306 if (continue_processing == false)
AzureIoTClient 30:ce3813c5a692 307 {
AzureIoTClient 30:ce3813c5a692 308 break;
AzureIoTClient 30:ce3813c5a692 309 }
AzureIoTClient 30:ce3813c5a692 310
AzureIoTClient 30:ce3813c5a692 311 current_item = next_item;
AzureIoTClient 30:ce3813c5a692 312 }
AzureIoTClient 30:ce3813c5a692 313
AzureIoTClient 30:ce3813c5a692 314 /* Codes_SRS_LIST_09_007: [ If no errors occur, singlylinkedlist_remove_if shall return zero. ] */
AzureIoTClient 30:ce3813c5a692 315 result = 0;
AzureIoTClient 30:ce3813c5a692 316 }
AzureIoTClient 30:ce3813c5a692 317
AzureIoTClient 30:ce3813c5a692 318 return result;
AzureIoTClient 30:ce3813c5a692 319 }
AzureIoTClient 30:ce3813c5a692 320
AzureIoTClient 30:ce3813c5a692 321 int singlylinkedlist_foreach(SINGLYLINKEDLIST_HANDLE list, LIST_ACTION_FUNCTION action_function, const void* action_context)
AzureIoTClient 30:ce3813c5a692 322 {
AzureIoTClient 30:ce3813c5a692 323 int result;
AzureIoTClient 30:ce3813c5a692 324
AzureIoTClient 30:ce3813c5a692 325 /* Codes_SRS_LIST_09_008: [ If the list or the action_function argument is NULL, singlylinkedlist_foreach shall return non-zero value. ] */
AzureIoTClient 30:ce3813c5a692 326 if ((list == NULL) ||
AzureIoTClient 30:ce3813c5a692 327 (action_function == NULL))
AzureIoTClient 30:ce3813c5a692 328 {
AzureIoTClient 30:ce3813c5a692 329 LogError("Invalid argument (list=%p, action_function=%p)", list, action_function);
AzureIoTClient 30:ce3813c5a692 330 result = __FAILURE__;
AzureIoTClient 30:ce3813c5a692 331 }
AzureIoTClient 30:ce3813c5a692 332 else
AzureIoTClient 30:ce3813c5a692 333 {
AzureIoTClient 30:ce3813c5a692 334 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 30:ce3813c5a692 335 LIST_ITEM_INSTANCE* list_item = list_instance->head;
AzureIoTClient 30:ce3813c5a692 336
AzureIoTClient 30:ce3813c5a692 337 while (list_item != NULL)
AzureIoTClient 30:ce3813c5a692 338 {
AzureIoTClient 30:ce3813c5a692 339 bool continue_processing = false;
AzureIoTClient 30:ce3813c5a692 340
AzureIoTClient 30:ce3813c5a692 341 /* Codes_SRS_LIST_09_009: [ singlylinkedlist_foreach shall iterate through all items in a list and invoke action_function for each one of them. ] */
AzureIoTClient 30:ce3813c5a692 342 action_function(list_item->item, action_context, &continue_processing);
AzureIoTClient 30:ce3813c5a692 343
AzureIoTClient 30:ce3813c5a692 344 /* Codes_SRS_LIST_09_010: [ If the condition function returns continue_processing as false, singlylinkedlist_foreach shall stop iterating through the list and return. ] */
AzureIoTClient 30:ce3813c5a692 345 if (continue_processing == false)
AzureIoTClient 30:ce3813c5a692 346 {
AzureIoTClient 30:ce3813c5a692 347 break;
AzureIoTClient 30:ce3813c5a692 348 }
AzureIoTClient 30:ce3813c5a692 349
AzureIoTClient 30:ce3813c5a692 350 list_item = (LIST_ITEM_INSTANCE*)list_item->next;
AzureIoTClient 30:ce3813c5a692 351 }
AzureIoTClient 30:ce3813c5a692 352
AzureIoTClient 30:ce3813c5a692 353 /* Codes_SRS_LIST_09_011: [ If no errors occur, singlylinkedlist_foreach shall return zero. ] */
AzureIoTClient 30:ce3813c5a692 354 result = 0;
AzureIoTClient 30:ce3813c5a692 355 }
AzureIoTClient 30:ce3813c5a692 356
AzureIoTClient 30:ce3813c5a692 357 return result;
AzureIoTClient 30:ce3813c5a692 358 }