Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
AzureIoTClient
Date:
Sat Jan 28 09:35:22 2017 -0800
Revision:
19:2e0811512ceb
Parent:
11:77df6d7e65ae
Child:
21:b92006c5b9ff
1.1.6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 #include <stdlib.h>
Azure.IoT Build 0:fa2de1b79154 5 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 6 #include "azure_c_shared_utility/vector.h"
AzureIoTClient 19:2e0811512ceb 7 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 8
AzureIoTClient 19:2e0811512ceb 9 #include "azure_c_shared_utility/vector_types_internal.h"
Azure.IoT Build 0:fa2de1b79154 10
Azure.IoT Build 0:fa2de1b79154 11 VECTOR_HANDLE VECTOR_create(size_t elementSize)
Azure.IoT Build 0:fa2de1b79154 12 {
Azure.IoT Build 0:fa2de1b79154 13 VECTOR_HANDLE result;
Azure.IoT Build 0:fa2de1b79154 14
AzureIoTClient 19:2e0811512ceb 15 /* Codes_SRS_VECTOR_10_002: [VECTOR_create shall fail and return NULL if elementsize is 0.] */
AzureIoTClient 19:2e0811512ceb 16 if (elementSize == 0)
Azure.IoT Build 0:fa2de1b79154 17 {
AzureIoTClient 19:2e0811512ceb 18 LogError("invalid elementSize(%zd).", elementSize);
Azure.IoT Build 0:fa2de1b79154 19 result = NULL;
Azure.IoT Build 0:fa2de1b79154 20 }
Azure.IoT Build 0:fa2de1b79154 21 else
Azure.IoT Build 0:fa2de1b79154 22 {
AzureIoTClient 19:2e0811512ceb 23 result = (VECTOR*)malloc(sizeof(VECTOR));
AzureIoTClient 19:2e0811512ceb 24 /* Codes_SRS_VECTOR_10_002 : [VECTOR_create shall fail and return NULL if malloc fails.] */
AzureIoTClient 19:2e0811512ceb 25 if (result == NULL)
AzureIoTClient 19:2e0811512ceb 26 {
AzureIoTClient 19:2e0811512ceb 27 LogError("malloc failed.");
AzureIoTClient 19:2e0811512ceb 28 }
AzureIoTClient 19:2e0811512ceb 29 else
AzureIoTClient 19:2e0811512ceb 30 {
AzureIoTClient 19:2e0811512ceb 31 /* Codes_SRS_VECTOR_10_001: [VECTOR_create shall allocate a VECTOR_HANDLE that will contain an empty vector.The size of each element is given with the parameter elementSize.] */
AzureIoTClient 19:2e0811512ceb 32 result->storage = NULL;
AzureIoTClient 19:2e0811512ceb 33 result->count = 0;
AzureIoTClient 19:2e0811512ceb 34 result->elementSize = elementSize;
AzureIoTClient 19:2e0811512ceb 35 }
Azure.IoT Build 0:fa2de1b79154 36 }
Azure.IoT Build 0:fa2de1b79154 37 return result;
Azure.IoT Build 0:fa2de1b79154 38 }
Azure.IoT Build 0:fa2de1b79154 39
Azure.IoT Build 0:fa2de1b79154 40 void VECTOR_destroy(VECTOR_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 41 {
AzureIoTClient 19:2e0811512ceb 42 /* Codes_SRS_VECTOR_10_009: [VECTOR_destroy shall return if the given handle is NULL.] */
AzureIoTClient 19:2e0811512ceb 43 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 44 {
AzureIoTClient 19:2e0811512ceb 45 LogError("invalid argument handle(NULL).");
AzureIoTClient 19:2e0811512ceb 46 }
AzureIoTClient 19:2e0811512ceb 47 else
AzureIoTClient 19:2e0811512ceb 48 {
AzureIoTClient 19:2e0811512ceb 49 /* Codes_SRS_VECTOR_10_008: [VECTOR_destroy shall free the given handle and its internal storage.] */
AzureIoTClient 19:2e0811512ceb 50 free(handle->storage);
AzureIoTClient 19:2e0811512ceb 51 free(handle);
Azure.IoT Build 0:fa2de1b79154 52 }
Azure.IoT Build 0:fa2de1b79154 53 }
Azure.IoT Build 0:fa2de1b79154 54
AzureIoTClient 19:2e0811512ceb 55 VECTOR_HANDLE VECTOR_move(VECTOR_HANDLE handle)
AzureIoTClient 19:2e0811512ceb 56 {
AzureIoTClient 19:2e0811512ceb 57 VECTOR_HANDLE result;
AzureIoTClient 19:2e0811512ceb 58 if (handle == NULL)
AzureIoTClient 19:2e0811512ceb 59 {
AzureIoTClient 19:2e0811512ceb 60 /* Codes_SRS_VECTOR_10_005: [VECTOR_move shall fail and return NULL if the given handle is NULL.] */
AzureIoTClient 19:2e0811512ceb 61 LogError("invalid argument - handle(NULL).");
AzureIoTClient 19:2e0811512ceb 62 result = NULL;
AzureIoTClient 19:2e0811512ceb 63 }
AzureIoTClient 19:2e0811512ceb 64 else
AzureIoTClient 19:2e0811512ceb 65 {
AzureIoTClient 19:2e0811512ceb 66 result = (VECTOR*)malloc(sizeof(VECTOR));
AzureIoTClient 19:2e0811512ceb 67 if (result == NULL)
AzureIoTClient 19:2e0811512ceb 68 {
AzureIoTClient 19:2e0811512ceb 69 /* Codes_SRS_VECTOR_10_006: [VECTOR_move shall fail and return NULL if malloc fails.] */
AzureIoTClient 19:2e0811512ceb 70 LogError("malloc failed.");
AzureIoTClient 19:2e0811512ceb 71 }
AzureIoTClient 19:2e0811512ceb 72 else
AzureIoTClient 19:2e0811512ceb 73 {
AzureIoTClient 19:2e0811512ceb 74 /* Codes_SRS_VECTOR_10_004: [VECTOR_move shall allocate a VECTOR_HANDLE and move the data to it from the given handle.] */
AzureIoTClient 19:2e0811512ceb 75 result->count = handle->count;
AzureIoTClient 19:2e0811512ceb 76 result->elementSize = handle->elementSize;
AzureIoTClient 19:2e0811512ceb 77 result->storage = handle->storage;
AzureIoTClient 19:2e0811512ceb 78
AzureIoTClient 19:2e0811512ceb 79 handle->storage = NULL;
AzureIoTClient 19:2e0811512ceb 80 handle->count = 0;
AzureIoTClient 19:2e0811512ceb 81 }
AzureIoTClient 19:2e0811512ceb 82 }
AzureIoTClient 19:2e0811512ceb 83 return result;
AzureIoTClient 19:2e0811512ceb 84 }
AzureIoTClient 19:2e0811512ceb 85
Azure.IoT Build 0:fa2de1b79154 86 /* insertion */
AzureIoTClient 19:2e0811512ceb 87
Azure.IoT Build 0:fa2de1b79154 88 int VECTOR_push_back(VECTOR_HANDLE handle, const void* elements, size_t numElements)
Azure.IoT Build 0:fa2de1b79154 89 {
Azure.IoT Build 0:fa2de1b79154 90 int result;
Azure.IoT Build 0:fa2de1b79154 91 if (handle == NULL || elements == NULL || numElements == 0)
Azure.IoT Build 0:fa2de1b79154 92 {
AzureIoTClient 19:2e0811512ceb 93 /* Codes_SRS_VECTOR_10_011: [VECTOR_push_back shall fail and return non-zero if `handle` is NULL.] */
AzureIoTClient 19:2e0811512ceb 94 /* Codes_SRS_VECTOR_10_034: [VECTOR_push_back shall fail and return non-zero if `elements` is NULL.] */
AzureIoTClient 19:2e0811512ceb 95 /* Codes_SRS_VECTOR_10_035: [VECTOR_push_back shall fail and return non-zero if `numElements` is 0.] */
AzureIoTClient 19:2e0811512ceb 96 LogError("invalid argument - handle(%p), elements(%p), numElements(%zd).", handle, elements, numElements);
Azure.IoT Build 0:fa2de1b79154 97 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 98 }
Azure.IoT Build 0:fa2de1b79154 99 else
Azure.IoT Build 0:fa2de1b79154 100 {
AzureIoTClient 19:2e0811512ceb 101 size_t curSize = handle->elementSize * handle->count;
AzureIoTClient 19:2e0811512ceb 102 size_t appendSize = handle->elementSize * numElements;
Azure.IoT Build 0:fa2de1b79154 103
AzureIoTClient 19:2e0811512ceb 104 void* temp = realloc(handle->storage, curSize + appendSize);
Azure.IoT Build 0:fa2de1b79154 105 if (temp == NULL)
Azure.IoT Build 0:fa2de1b79154 106 {
AzureIoTClient 19:2e0811512ceb 107 /* Codes_SRS_VECTOR_10_012: [VECTOR_push_back shall fail and return non-zero if memory allocation fails.] */
AzureIoTClient 19:2e0811512ceb 108 LogError("realloc failed.");
Azure.IoT Build 0:fa2de1b79154 109 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 110 }
Azure.IoT Build 0:fa2de1b79154 111 else
Azure.IoT Build 0:fa2de1b79154 112 {
AzureIoTClient 19:2e0811512ceb 113 /* Codes_SRS_VECTOR_10_013: [VECTOR_push_back shall append the given elements and return 0 indicating success.] */
AzureIoTClient 19:2e0811512ceb 114 (void)memcpy((unsigned char*)temp + curSize, elements, appendSize);
AzureIoTClient 19:2e0811512ceb 115 handle->storage = temp;
AzureIoTClient 19:2e0811512ceb 116 handle->count += numElements;
Azure.IoT Build 0:fa2de1b79154 117 result = 0;
Azure.IoT Build 0:fa2de1b79154 118 }
Azure.IoT Build 0:fa2de1b79154 119 }
Azure.IoT Build 0:fa2de1b79154 120 return result;
Azure.IoT Build 0:fa2de1b79154 121 }
Azure.IoT Build 0:fa2de1b79154 122
Azure.IoT Build 0:fa2de1b79154 123 /* removal */
AzureIoTClient 19:2e0811512ceb 124
Azure.IoT Build 0:fa2de1b79154 125 void VECTOR_erase(VECTOR_HANDLE handle, void* elements, size_t numElements)
Azure.IoT Build 0:fa2de1b79154 126 {
AzureIoTClient 19:2e0811512ceb 127 if (handle == NULL || elements == NULL || numElements == 0)
Azure.IoT Build 0:fa2de1b79154 128 {
AzureIoTClient 19:2e0811512ceb 129 /* Codes_SRS_VECTOR_10_015: [VECTOR_erase shall return if `handle` is NULL.] */
AzureIoTClient 19:2e0811512ceb 130 /* Codes_SRS_VECTOR_10_038: [VECTOR_erase shall return if `elements` is NULL.] */
AzureIoTClient 19:2e0811512ceb 131 /* Codes_SRS_VECTOR_10_039: [VECTOR_erase shall return if `numElements` is 0.] */
AzureIoTClient 19:2e0811512ceb 132 LogError("invalid argument - handle(%p), elements(%p), numElements(%zd).", handle, elements, numElements);
AzureIoTClient 19:2e0811512ceb 133 }
AzureIoTClient 19:2e0811512ceb 134 else
AzureIoTClient 19:2e0811512ceb 135 {
AzureIoTClient 19:2e0811512ceb 136 if (elements < handle->storage)
Azure.IoT Build 0:fa2de1b79154 137 {
AzureIoTClient 19:2e0811512ceb 138 /* Codes_SRS_VECTOR_10_040: [VECTOR_erase shall return if `elements` is out of bound.] */
AzureIoTClient 19:2e0811512ceb 139 LogError("invalid argument elements(%p) is not a member of this object.", elements);
Azure.IoT Build 0:fa2de1b79154 140 }
Azure.IoT Build 0:fa2de1b79154 141 else
Azure.IoT Build 0:fa2de1b79154 142 {
AzureIoTClient 19:2e0811512ceb 143 ptrdiff_t diff = ((unsigned char*)elements) - ((unsigned char*)handle->storage);
AzureIoTClient 19:2e0811512ceb 144 if ((diff % handle->elementSize) != 0)
AzureIoTClient 19:2e0811512ceb 145 {
AzureIoTClient 19:2e0811512ceb 146 /* Codes_SRS_VECTOR_10_041: [VECTOR_erase shall return if elements is misaligned.] */
AzureIoTClient 19:2e0811512ceb 147 LogError("invalid argument - elements(%p) is misaligned", elements);
AzureIoTClient 19:2e0811512ceb 148 }
AzureIoTClient 19:2e0811512ceb 149 else
AzureIoTClient 19:2e0811512ceb 150 {
AzureIoTClient 19:2e0811512ceb 151 /* Compute the arguments needed for memmove. */
AzureIoTClient 19:2e0811512ceb 152 unsigned char* src = (unsigned char*)elements + (handle->elementSize * numElements);
AzureIoTClient 19:2e0811512ceb 153 unsigned char* srcEnd = (unsigned char*)handle->storage + (handle->elementSize * handle->count);
AzureIoTClient 19:2e0811512ceb 154 if (src > srcEnd)
AzureIoTClient 19:2e0811512ceb 155 {
AzureIoTClient 19:2e0811512ceb 156 /* Codes_SRS_VECTOR_10_040: [VECTOR_erase shall return if `elements` is out of bound.] */
AzureIoTClient 19:2e0811512ceb 157 LogError("invalid argument - numElements(%zd) is out of bound.", numElements);
AzureIoTClient 19:2e0811512ceb 158 }
AzureIoTClient 19:2e0811512ceb 159 else
AzureIoTClient 19:2e0811512ceb 160 {
AzureIoTClient 19:2e0811512ceb 161 /* Codes_SRS_VECTOR_10_014: [VECTOR_erase shall remove the 'numElements' starting at 'elements' and reduce its internal storage.] */
AzureIoTClient 19:2e0811512ceb 162 handle->count -= numElements;
AzureIoTClient 19:2e0811512ceb 163 if (handle->count == 0)
AzureIoTClient 19:2e0811512ceb 164 {
AzureIoTClient 19:2e0811512ceb 165 free(handle->storage);
AzureIoTClient 19:2e0811512ceb 166 handle->storage = NULL;
AzureIoTClient 19:2e0811512ceb 167 }
AzureIoTClient 19:2e0811512ceb 168 else
AzureIoTClient 19:2e0811512ceb 169 {
AzureIoTClient 19:2e0811512ceb 170 void* tmp;
AzureIoTClient 19:2e0811512ceb 171 (void)memmove(elements, src, srcEnd - src);
AzureIoTClient 19:2e0811512ceb 172 tmp = realloc(handle->storage, (handle->elementSize * handle->count));
AzureIoTClient 19:2e0811512ceb 173 if (tmp == NULL)
AzureIoTClient 19:2e0811512ceb 174 {
AzureIoTClient 19:2e0811512ceb 175 LogInfo("realloc failed. Keeping original internal storage pointer.");
AzureIoTClient 19:2e0811512ceb 176 }
AzureIoTClient 19:2e0811512ceb 177 else
AzureIoTClient 19:2e0811512ceb 178 {
AzureIoTClient 19:2e0811512ceb 179 handle->storage = tmp;
AzureIoTClient 19:2e0811512ceb 180 }
AzureIoTClient 19:2e0811512ceb 181 }
AzureIoTClient 19:2e0811512ceb 182 }
AzureIoTClient 19:2e0811512ceb 183 }
Azure.IoT Build 0:fa2de1b79154 184 }
Azure.IoT Build 0:fa2de1b79154 185 }
Azure.IoT Build 0:fa2de1b79154 186 }
Azure.IoT Build 0:fa2de1b79154 187
Azure.IoT Build 0:fa2de1b79154 188 void VECTOR_clear(VECTOR_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 189 {
AzureIoTClient 19:2e0811512ceb 190 /* Codes_SRS_VECTOR_10_017: [VECTOR_clear shall if the object is NULL or empty.] */
AzureIoTClient 19:2e0811512ceb 191 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 192 {
AzureIoTClient 19:2e0811512ceb 193 LogError("invalid argument handle(NULL).");
AzureIoTClient 19:2e0811512ceb 194 }
AzureIoTClient 19:2e0811512ceb 195 else
AzureIoTClient 19:2e0811512ceb 196 {
AzureIoTClient 19:2e0811512ceb 197 /* Codes_SRS_VECTOR_10_016: [VECTOR_clear shall remove all elements from the object and release internal storage.] */
AzureIoTClient 19:2e0811512ceb 198 free(handle->storage);
AzureIoTClient 19:2e0811512ceb 199 handle->storage = NULL;
AzureIoTClient 19:2e0811512ceb 200 handle->count = 0;
Azure.IoT Build 0:fa2de1b79154 201 }
Azure.IoT Build 0:fa2de1b79154 202 }
Azure.IoT Build 0:fa2de1b79154 203
Azure.IoT Build 0:fa2de1b79154 204 /* access */
Azure.IoT Build 0:fa2de1b79154 205
AzureIoTClient 19:2e0811512ceb 206 void* VECTOR_element(VECTOR_HANDLE handle, size_t index)
Azure.IoT Build 0:fa2de1b79154 207 {
AzureIoTClient 19:2e0811512ceb 208 void* result;
AzureIoTClient 19:2e0811512ceb 209 if (handle == NULL)
AzureIoTClient 19:2e0811512ceb 210 {
AzureIoTClient 19:2e0811512ceb 211 /* Codes_SRS_VECTOR_10_019: [VECTOR_element shall fail and return NULL if handle is NULL.] */
AzureIoTClient 19:2e0811512ceb 212 LogError("invalid argument handle(NULL).");
AzureIoTClient 19:2e0811512ceb 213 result = NULL;
AzureIoTClient 19:2e0811512ceb 214 }
AzureIoTClient 19:2e0811512ceb 215 else
Azure.IoT Build 0:fa2de1b79154 216 {
AzureIoTClient 19:2e0811512ceb 217 if (index >= handle->count)
Azure.IoT Build 0:fa2de1b79154 218 {
AzureIoTClient 19:2e0811512ceb 219 /* Codes_SRS_VECTOR_10_020: [VECTOR_element shall fail and return NULL if the given index is out of range.] */
AzureIoTClient 19:2e0811512ceb 220 LogError("invalid argument - index(%zd); should be >= 0 and < %zd.", index, handle->count);
AzureIoTClient 19:2e0811512ceb 221 result = NULL;
AzureIoTClient 19:2e0811512ceb 222 }
AzureIoTClient 19:2e0811512ceb 223 else
AzureIoTClient 19:2e0811512ceb 224 {
AzureIoTClient 19:2e0811512ceb 225 /* Codes_SRS_VECTOR_10_018: [VECTOR_element shall return the element at the given index.] */
AzureIoTClient 19:2e0811512ceb 226 result = (unsigned char*)handle->storage + (handle->elementSize * index);
Azure.IoT Build 0:fa2de1b79154 227 }
Azure.IoT Build 0:fa2de1b79154 228 }
Azure.IoT Build 0:fa2de1b79154 229 return result;
Azure.IoT Build 0:fa2de1b79154 230 }
Azure.IoT Build 0:fa2de1b79154 231
AzureIoTClient 19:2e0811512ceb 232 void* VECTOR_front(VECTOR_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 233 {
AzureIoTClient 19:2e0811512ceb 234 void* result;
AzureIoTClient 19:2e0811512ceb 235 if (handle == NULL)
AzureIoTClient 19:2e0811512ceb 236 {
AzureIoTClient 19:2e0811512ceb 237 /* Codes_SRS_VECTOR_10_022: [VECTOR_front shall fail and return NULL if handle is NULL.] */
AzureIoTClient 19:2e0811512ceb 238 LogError("invalid argument handle (NULL).");
AzureIoTClient 19:2e0811512ceb 239 result = NULL;
AzureIoTClient 19:2e0811512ceb 240 }
AzureIoTClient 19:2e0811512ceb 241 else
Azure.IoT Build 0:fa2de1b79154 242 {
AzureIoTClient 19:2e0811512ceb 243 if (handle->count == 0)
AzureIoTClient 19:2e0811512ceb 244 {
AzureIoTClient 19:2e0811512ceb 245 /* Codes_SRS_VECTOR_10_028: [VECTOR_front shall return NULL if the vector is empty.] */
AzureIoTClient 19:2e0811512ceb 246 LogError("vector is empty.");
AzureIoTClient 19:2e0811512ceb 247 result = NULL;
AzureIoTClient 19:2e0811512ceb 248 }
AzureIoTClient 19:2e0811512ceb 249 else
AzureIoTClient 19:2e0811512ceb 250 {
AzureIoTClient 19:2e0811512ceb 251 /* Codes_SRS_VECTOR_10_021: [VECTOR_front shall return a pointer to the element at index 0.] */
AzureIoTClient 19:2e0811512ceb 252 result = handle->storage;
AzureIoTClient 19:2e0811512ceb 253 }
Azure.IoT Build 0:fa2de1b79154 254 }
Azure.IoT Build 0:fa2de1b79154 255 return result;
Azure.IoT Build 0:fa2de1b79154 256 }
Azure.IoT Build 0:fa2de1b79154 257
AzureIoTClient 19:2e0811512ceb 258 void* VECTOR_back(VECTOR_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 259 {
AzureIoTClient 19:2e0811512ceb 260 void* result;
AzureIoTClient 19:2e0811512ceb 261 if (handle == NULL)
AzureIoTClient 19:2e0811512ceb 262 {
AzureIoTClient 19:2e0811512ceb 263 /* Codes_SRS_VECTOR_10_024: [VECTOR_back shall fail and return NULL if handle is NULL.] */
AzureIoTClient 19:2e0811512ceb 264 LogError("invalid argument handle (NULL).");
AzureIoTClient 19:2e0811512ceb 265 result = NULL;
AzureIoTClient 19:2e0811512ceb 266 }
AzureIoTClient 19:2e0811512ceb 267 else
Azure.IoT Build 0:fa2de1b79154 268 {
AzureIoTClient 19:2e0811512ceb 269 if (handle->count == 0)
AzureIoTClient 19:2e0811512ceb 270 {
AzureIoTClient 19:2e0811512ceb 271 /* Codes_SRS_VECTOR_10_029: [VECTOR_back shall return NULL if the vector is empty.] */
AzureIoTClient 19:2e0811512ceb 272 LogError("vector is empty.");
AzureIoTClient 19:2e0811512ceb 273 result = NULL;
AzureIoTClient 19:2e0811512ceb 274 }
AzureIoTClient 19:2e0811512ceb 275 else
AzureIoTClient 19:2e0811512ceb 276 {
AzureIoTClient 19:2e0811512ceb 277 /* Codes_SRS_VECTOR_10_023: [VECTOR_front shall return the last element of the vector.] */
AzureIoTClient 19:2e0811512ceb 278 result = (unsigned char*)handle->storage + (handle->elementSize * (handle->count - 1));
AzureIoTClient 19:2e0811512ceb 279 }
Azure.IoT Build 0:fa2de1b79154 280 }
Azure.IoT Build 0:fa2de1b79154 281 return result;
Azure.IoT Build 0:fa2de1b79154 282 }
Azure.IoT Build 0:fa2de1b79154 283
AzureIoTClient 19:2e0811512ceb 284 void* VECTOR_find_if(VECTOR_HANDLE handle, PREDICATE_FUNCTION pred, const void* value)
Azure.IoT Build 0:fa2de1b79154 285 {
AzureIoTClient 19:2e0811512ceb 286 void* result;
AzureIoTClient 19:2e0811512ceb 287 if (handle == NULL || pred == NULL)
Azure.IoT Build 0:fa2de1b79154 288 {
AzureIoTClient 19:2e0811512ceb 289 /* Codes_SRS_VECTOR_10_030: [VECTOR_find_if shall fail and return NULL if `handle` is NULL.] */
AzureIoTClient 19:2e0811512ceb 290 /* Codes_SRS_VECTOR_10_036: [VECTOR_find_if shall fail and return NULL if `pred` is NULL.] */
AzureIoTClient 19:2e0811512ceb 291 LogError("invalid argument - handle(%p), pred(%p)", handle, pred);
AzureIoTClient 19:2e0811512ceb 292 result = NULL;
AzureIoTClient 19:2e0811512ceb 293 }
AzureIoTClient 19:2e0811512ceb 294 else
AzureIoTClient 19:2e0811512ceb 295 {
AzureIoTClient 19:2e0811512ceb 296 size_t i;
AzureIoTClient 19:2e0811512ceb 297 for (i = 0; i < handle->count; ++i)
Azure.IoT Build 0:fa2de1b79154 298 {
AzureIoTClient 19:2e0811512ceb 299 if (true == pred((unsigned char*)handle->storage + (handle->elementSize * i), value))
Azure.IoT Build 0:fa2de1b79154 300 {
AzureIoTClient 19:2e0811512ceb 301 /* Codes_SRS_VECTOR_10_031: [VECTOR_find_if shall return the first element in the vector that matches `pred`.] */
Azure.IoT Build 0:fa2de1b79154 302 break;
Azure.IoT Build 0:fa2de1b79154 303 }
Azure.IoT Build 0:fa2de1b79154 304 }
AzureIoTClient 19:2e0811512ceb 305
AzureIoTClient 19:2e0811512ceb 306 if (i == handle->count)
AzureIoTClient 19:2e0811512ceb 307 {
AzureIoTClient 19:2e0811512ceb 308 /* Codes_SRS_VECTOR_10_032: [VECTOR_find_if shall return NULL if no matching element is found.] */
AzureIoTClient 19:2e0811512ceb 309 result = NULL;
AzureIoTClient 19:2e0811512ceb 310 }
AzureIoTClient 19:2e0811512ceb 311 else
AzureIoTClient 19:2e0811512ceb 312 {
AzureIoTClient 19:2e0811512ceb 313 /* Codes_SRS_VECTOR_10_031: [VECTOR_find_if shall return the first element in the vector that matches `pred`.]*/
AzureIoTClient 19:2e0811512ceb 314 result = (unsigned char*)handle->storage + (handle->elementSize * i);
AzureIoTClient 19:2e0811512ceb 315 }
Azure.IoT Build 0:fa2de1b79154 316 }
Azure.IoT Build 0:fa2de1b79154 317 return result;
Azure.IoT Build 0:fa2de1b79154 318 }
Azure.IoT Build 0:fa2de1b79154 319
Azure.IoT Build 0:fa2de1b79154 320 /* capacity */
Azure.IoT Build 0:fa2de1b79154 321
AzureIoTClient 19:2e0811512ceb 322 size_t VECTOR_size(VECTOR_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 323 {
AzureIoTClient 19:2e0811512ceb 324 size_t result;
AzureIoTClient 19:2e0811512ceb 325 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 326 {
AzureIoTClient 19:2e0811512ceb 327 /* Codes_SRS_VECTOR_10_026: [**VECTOR_size shall return 0 if the given handle is NULL.] */
AzureIoTClient 19:2e0811512ceb 328 LogError("invalid argument handle(NULL).");
AzureIoTClient 19:2e0811512ceb 329 result = 0;
AzureIoTClient 19:2e0811512ceb 330 }
AzureIoTClient 19:2e0811512ceb 331 else
AzureIoTClient 19:2e0811512ceb 332 {
AzureIoTClient 19:2e0811512ceb 333 /* Codes_SRS_VECTOR_10_025: [VECTOR_size shall return the number of elements stored with the given handle.] */
AzureIoTClient 19:2e0811512ceb 334 result = handle->count;
Azure.IoT Build 0:fa2de1b79154 335 }
Azure.IoT Build 0:fa2de1b79154 336 return result;
Azure.IoT Build 0:fa2de1b79154 337 }