Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Mon Jun 11 15:41:10 2018 -0700
Revision:
45:1119d0f2c4d8
Parent:
34:0a87c89bdcd0
Child:
48:81866008bba4
1.2.5

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