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:
27:8656a313842b
1.2.5

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/map.h"
AzureIoTClient 21:b92006c5b9ff 7 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 6:c55b013dfc2a 8 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 9 #include "azure_c_shared_utility/strings.h"
Azure.IoT Build 0:fa2de1b79154 10
Azure.IoT Build 0:fa2de1b79154 11 DEFINE_ENUM_STRINGS(MAP_RESULT, MAP_RESULT_VALUES);
Azure.IoT Build 0:fa2de1b79154 12
Azure.IoT Build 0:fa2de1b79154 13 typedef struct MAP_HANDLE_DATA_TAG
Azure.IoT Build 0:fa2de1b79154 14 {
Azure.IoT Build 0:fa2de1b79154 15 char** keys;
Azure.IoT Build 0:fa2de1b79154 16 char** values;
Azure.IoT Build 0:fa2de1b79154 17 size_t count;
Azure.IoT Build 0:fa2de1b79154 18 MAP_FILTER_CALLBACK mapFilterCallback;
Azure.IoT Build 0:fa2de1b79154 19 }MAP_HANDLE_DATA;
Azure.IoT Build 0:fa2de1b79154 20
AzureIoTClient 1:9190c0f4d23a 21 #define LOG_MAP_ERROR LogError("result = %s", ENUM_TO_STRING(MAP_RESULT, result));
Azure.IoT Build 0:fa2de1b79154 22
Azure.IoT Build 0:fa2de1b79154 23 MAP_HANDLE Map_Create(MAP_FILTER_CALLBACK mapFilterFunc)
Azure.IoT Build 0:fa2de1b79154 24 {
Azure.IoT Build 0:fa2de1b79154 25 /*Codes_SRS_MAP_02_001: [Map_Create shall create a new, empty map.]*/
Azure.IoT Build 0:fa2de1b79154 26 MAP_HANDLE_DATA* result = (MAP_HANDLE_DATA*)malloc(sizeof(MAP_HANDLE_DATA));
Azure.IoT Build 0:fa2de1b79154 27 /*Codes_SRS_MAP_02_002: [If during creation there are any error, then Map_Create shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 28 if (result != NULL)
Azure.IoT Build 0:fa2de1b79154 29 {
Azure.IoT Build 0:fa2de1b79154 30 /*Codes_SRS_MAP_02_003: [Otherwise, it shall return a non-NULL handle that can be used in subsequent calls.] */
Azure.IoT Build 0:fa2de1b79154 31 result->keys = NULL;
Azure.IoT Build 0:fa2de1b79154 32 result->values = NULL;
Azure.IoT Build 0:fa2de1b79154 33 result->count = 0;
Azure.IoT Build 0:fa2de1b79154 34 result->mapFilterCallback = mapFilterFunc;
Azure.IoT Build 0:fa2de1b79154 35 }
Azure.IoT Build 0:fa2de1b79154 36 return (MAP_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 37 }
Azure.IoT Build 0:fa2de1b79154 38
Azure.IoT Build 0:fa2de1b79154 39 void Map_Destroy(MAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 40 {
Azure.IoT Build 0:fa2de1b79154 41 /*Codes_SRS_MAP_02_005: [If parameter handle is NULL then Map_Destroy shall take no action.] */
Azure.IoT Build 0:fa2de1b79154 42 if (handle != NULL)
Azure.IoT Build 0:fa2de1b79154 43 {
Azure.IoT Build 0:fa2de1b79154 44 /*Codes_SRS_MAP_02_004: [Map_Destroy shall release all resources associated with the map.] */
Azure.IoT Build 0:fa2de1b79154 45 MAP_HANDLE_DATA* handleData = (MAP_HANDLE_DATA*)handle;
Azure.IoT Build 0:fa2de1b79154 46 size_t i;
AzureIoTClient 45:1119d0f2c4d8 47
Azure.IoT Build 0:fa2de1b79154 48 for (i = 0; i < handleData->count; i++)
Azure.IoT Build 0:fa2de1b79154 49 {
Azure.IoT Build 0:fa2de1b79154 50 free(handleData->keys[i]);
Azure.IoT Build 0:fa2de1b79154 51 free(handleData->values[i]);
Azure.IoT Build 0:fa2de1b79154 52 }
Azure.IoT Build 0:fa2de1b79154 53 free(handleData->keys);
Azure.IoT Build 0:fa2de1b79154 54 free(handleData->values);
Azure.IoT Build 0:fa2de1b79154 55 free(handleData);
Azure.IoT Build 0:fa2de1b79154 56 }
Azure.IoT Build 0:fa2de1b79154 57 }
Azure.IoT Build 0:fa2de1b79154 58
Azure.IoT Build 0:fa2de1b79154 59 /*makes a copy of a vector of const char*, having size "size". source cannot be NULL*/
Azure.IoT Build 0:fa2de1b79154 60 /*returns NULL if it fails*/
Azure.IoT Build 0:fa2de1b79154 61 static char** Map_CloneVector(const char*const * source, size_t count)
Azure.IoT Build 0:fa2de1b79154 62 {
Azure.IoT Build 0:fa2de1b79154 63 char** result;
Azure.IoT Build 0:fa2de1b79154 64 result = (char**)malloc(count *sizeof(char*));
Azure.IoT Build 0:fa2de1b79154 65 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 66 {
Azure.IoT Build 0:fa2de1b79154 67 /*do nothing, just return it (NULL)*/
Azure.IoT Build 0:fa2de1b79154 68 }
Azure.IoT Build 0:fa2de1b79154 69 else
Azure.IoT Build 0:fa2de1b79154 70 {
Azure.IoT Build 0:fa2de1b79154 71 size_t i;
Azure.IoT Build 0:fa2de1b79154 72 for (i = 0; i < count; i++)
Azure.IoT Build 0:fa2de1b79154 73 {
Azure.IoT Build 0:fa2de1b79154 74 if (mallocAndStrcpy_s(result + i, source[i]) != 0)
Azure.IoT Build 0:fa2de1b79154 75 {
Azure.IoT Build 0:fa2de1b79154 76 break;
Azure.IoT Build 0:fa2de1b79154 77 }
Azure.IoT Build 0:fa2de1b79154 78 }
Azure.IoT Build 0:fa2de1b79154 79
Azure.IoT Build 0:fa2de1b79154 80 if (i == count)
Azure.IoT Build 0:fa2de1b79154 81 {
Azure.IoT Build 0:fa2de1b79154 82 /*it is all good, proceed to return result*/
Azure.IoT Build 0:fa2de1b79154 83 }
Azure.IoT Build 0:fa2de1b79154 84 else
Azure.IoT Build 0:fa2de1b79154 85 {
Azure.IoT Build 0:fa2de1b79154 86 size_t j;
Azure.IoT Build 0:fa2de1b79154 87 for (j = 0; j < i; j++)
Azure.IoT Build 0:fa2de1b79154 88 {
Azure.IoT Build 0:fa2de1b79154 89 free(result[j]);
Azure.IoT Build 0:fa2de1b79154 90 }
Azure.IoT Build 0:fa2de1b79154 91 free(result);
Azure.IoT Build 0:fa2de1b79154 92 result = NULL;
Azure.IoT Build 0:fa2de1b79154 93 }
Azure.IoT Build 0:fa2de1b79154 94 }
Azure.IoT Build 0:fa2de1b79154 95 return result;
Azure.IoT Build 0:fa2de1b79154 96 }
Azure.IoT Build 0:fa2de1b79154 97
Azure.IoT Build 0:fa2de1b79154 98 /*Codes_SRS_MAP_02_039: [Map_Clone shall make a copy of the map indicated by parameter handle and return a non-NULL handle to it.]*/
Azure.IoT Build 0:fa2de1b79154 99 MAP_HANDLE Map_Clone(MAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 100 {
Azure.IoT Build 0:fa2de1b79154 101 MAP_HANDLE_DATA* result;
Azure.IoT Build 0:fa2de1b79154 102 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 103 {
Azure.IoT Build 0:fa2de1b79154 104 /*Codes_SRS_MAP_02_038: [Map_Clone returns NULL if parameter handle is NULL.]*/
Azure.IoT Build 0:fa2de1b79154 105 result = NULL;
AzureIoTClient 1:9190c0f4d23a 106 LogError("invalid arg to Map_Clone (NULL)");
Azure.IoT Build 0:fa2de1b79154 107 }
Azure.IoT Build 0:fa2de1b79154 108 else
Azure.IoT Build 0:fa2de1b79154 109 {
Azure.IoT Build 0:fa2de1b79154 110 MAP_HANDLE_DATA * handleData = (MAP_HANDLE_DATA *)handle;
Azure.IoT Build 0:fa2de1b79154 111 result = (MAP_HANDLE_DATA*)malloc(sizeof(MAP_HANDLE_DATA));
Azure.IoT Build 0:fa2de1b79154 112 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 113 {
Azure.IoT Build 0:fa2de1b79154 114 /*Codes_SRS_MAP_02_047: [If during cloning, any operation fails, then Map_Clone shall return NULL.] */
Azure.IoT Build 0:fa2de1b79154 115 /*do nothing, proceed to return it, this is an error case*/
AzureIoTClient 1:9190c0f4d23a 116 LogError("unable to malloc");
Azure.IoT Build 0:fa2de1b79154 117 }
Azure.IoT Build 0:fa2de1b79154 118 else
Azure.IoT Build 0:fa2de1b79154 119 {
AzureIoTClient 45:1119d0f2c4d8 120 if (handleData->count == 0)
Azure.IoT Build 0:fa2de1b79154 121 {
Azure.IoT Build 0:fa2de1b79154 122 result->count = 0;
Azure.IoT Build 0:fa2de1b79154 123 result->keys = NULL;
Azure.IoT Build 0:fa2de1b79154 124 result->values = NULL;
Azure.IoT Build 0:fa2de1b79154 125 result->mapFilterCallback = NULL;
Azure.IoT Build 0:fa2de1b79154 126 }
Azure.IoT Build 0:fa2de1b79154 127 else
Azure.IoT Build 0:fa2de1b79154 128 {
Azure.IoT Build 0:fa2de1b79154 129 result->mapFilterCallback = handleData->mapFilterCallback;
Azure.IoT Build 0:fa2de1b79154 130 result->count = handleData->count;
Azure.IoT Build 0:fa2de1b79154 131 if( (result->keys = Map_CloneVector((const char* const*)handleData->keys, handleData->count))==NULL)
Azure.IoT Build 0:fa2de1b79154 132 {
Azure.IoT Build 0:fa2de1b79154 133 /*Codes_SRS_MAP_02_047: [If during cloning, any operation fails, then Map_Clone shall return NULL.] */
AzureIoTClient 1:9190c0f4d23a 134 LogError("unable to clone keys");
Azure.IoT Build 0:fa2de1b79154 135 free(result);
Azure.IoT Build 0:fa2de1b79154 136 result = NULL;
Azure.IoT Build 0:fa2de1b79154 137 }
Azure.IoT Build 0:fa2de1b79154 138 else if ((result->values = Map_CloneVector((const char* const*)handleData->values, handleData->count)) == NULL)
Azure.IoT Build 0:fa2de1b79154 139 {
AzureIoTClient 27:8656a313842b 140 size_t i;
Azure.IoT Build 0:fa2de1b79154 141 /*Codes_SRS_MAP_02_047: [If during cloning, any operation fails, then Map_Clone shall return NULL.] */
AzureIoTClient 1:9190c0f4d23a 142 LogError("unable to clone values");
Azure.IoT Build 0:fa2de1b79154 143 for (i = 0; i < result->count; i++)
Azure.IoT Build 0:fa2de1b79154 144 {
AzureIoTClient 45:1119d0f2c4d8 145 free(result->keys[i]);
Azure.IoT Build 0:fa2de1b79154 146 }
Azure.IoT Build 0:fa2de1b79154 147 free(result->keys);
Azure.IoT Build 0:fa2de1b79154 148 free(result);
Azure.IoT Build 0:fa2de1b79154 149 result = NULL;
Azure.IoT Build 0:fa2de1b79154 150 }
Azure.IoT Build 0:fa2de1b79154 151 else
Azure.IoT Build 0:fa2de1b79154 152 {
Azure.IoT Build 0:fa2de1b79154 153 /*all fine, return it*/
Azure.IoT Build 0:fa2de1b79154 154 }
Azure.IoT Build 0:fa2de1b79154 155 }
Azure.IoT Build 0:fa2de1b79154 156 }
Azure.IoT Build 0:fa2de1b79154 157 }
Azure.IoT Build 0:fa2de1b79154 158 return (MAP_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 159 }
Azure.IoT Build 0:fa2de1b79154 160
Azure.IoT Build 0:fa2de1b79154 161 static int Map_IncreaseStorageKeysValues(MAP_HANDLE_DATA* handleData)
Azure.IoT Build 0:fa2de1b79154 162 {
Azure.IoT Build 0:fa2de1b79154 163 int result;
Azure.IoT Build 0:fa2de1b79154 164 char** newKeys = (char**)realloc(handleData->keys, (handleData->count + 1) * sizeof(char*));
Azure.IoT Build 0:fa2de1b79154 165 if (newKeys == NULL)
Azure.IoT Build 0:fa2de1b79154 166 {
AzureIoTClient 1:9190c0f4d23a 167 LogError("realloc error");
AzureIoTClient 21:b92006c5b9ff 168 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 169 }
Azure.IoT Build 0:fa2de1b79154 170 else
Azure.IoT Build 0:fa2de1b79154 171 {
Azure.IoT Build 0:fa2de1b79154 172 char** newValues;
Azure.IoT Build 0:fa2de1b79154 173 handleData->keys = newKeys;
Azure.IoT Build 0:fa2de1b79154 174 handleData->keys[handleData->count] = NULL;
Azure.IoT Build 0:fa2de1b79154 175 newValues = (char**)realloc(handleData->values, (handleData->count + 1) * sizeof(char*));
Azure.IoT Build 0:fa2de1b79154 176 if (newValues == NULL)
Azure.IoT Build 0:fa2de1b79154 177 {
AzureIoTClient 1:9190c0f4d23a 178 LogError("realloc error");
Azure.IoT Build 0:fa2de1b79154 179 if (handleData->count == 0) /*avoiding an implementation defined behavior */
Azure.IoT Build 0:fa2de1b79154 180 {
Azure.IoT Build 0:fa2de1b79154 181 free(handleData->keys);
Azure.IoT Build 0:fa2de1b79154 182 handleData->keys = NULL;
Azure.IoT Build 0:fa2de1b79154 183 }
Azure.IoT Build 0:fa2de1b79154 184 else
Azure.IoT Build 0:fa2de1b79154 185 {
Azure.IoT Build 0:fa2de1b79154 186 char** undoneKeys = (char**)realloc(handleData->keys, (handleData->count) * sizeof(char*));
Azure.IoT Build 0:fa2de1b79154 187 if (undoneKeys == NULL)
Azure.IoT Build 0:fa2de1b79154 188 {
AzureIoTClient 1:9190c0f4d23a 189 LogError("CATASTROPHIC error, unable to undo through realloc to a smaller size");
Azure.IoT Build 0:fa2de1b79154 190 }
Azure.IoT Build 0:fa2de1b79154 191 else
Azure.IoT Build 0:fa2de1b79154 192 {
Azure.IoT Build 0:fa2de1b79154 193 handleData->keys = undoneKeys;
Azure.IoT Build 0:fa2de1b79154 194 }
Azure.IoT Build 0:fa2de1b79154 195 }
AzureIoTClient 21:b92006c5b9ff 196 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 197 }
Azure.IoT Build 0:fa2de1b79154 198 else
Azure.IoT Build 0:fa2de1b79154 199 {
Azure.IoT Build 0:fa2de1b79154 200 handleData->values = newValues;
Azure.IoT Build 0:fa2de1b79154 201 handleData->values[handleData->count] = NULL;
Azure.IoT Build 0:fa2de1b79154 202 handleData->count++;
Azure.IoT Build 0:fa2de1b79154 203 result = 0;
Azure.IoT Build 0:fa2de1b79154 204 }
Azure.IoT Build 0:fa2de1b79154 205 }
Azure.IoT Build 0:fa2de1b79154 206 return result;
Azure.IoT Build 0:fa2de1b79154 207 }
Azure.IoT Build 0:fa2de1b79154 208
Azure.IoT Build 0:fa2de1b79154 209 static void Map_DecreaseStorageKeysValues(MAP_HANDLE_DATA* handleData)
Azure.IoT Build 0:fa2de1b79154 210 {
Azure.IoT Build 0:fa2de1b79154 211 if (handleData->count == 1)
Azure.IoT Build 0:fa2de1b79154 212 {
Azure.IoT Build 0:fa2de1b79154 213 free(handleData->keys);
Azure.IoT Build 0:fa2de1b79154 214 handleData->keys = NULL;
Azure.IoT Build 0:fa2de1b79154 215 free(handleData->values);
Azure.IoT Build 0:fa2de1b79154 216 handleData->values = NULL;
Azure.IoT Build 0:fa2de1b79154 217 handleData->count = 0;
Azure.IoT Build 0:fa2de1b79154 218 handleData->mapFilterCallback = NULL;
Azure.IoT Build 0:fa2de1b79154 219 }
Azure.IoT Build 0:fa2de1b79154 220 else
Azure.IoT Build 0:fa2de1b79154 221 {
Azure.IoT Build 0:fa2de1b79154 222 /*certainly > 1...*/
Azure.IoT Build 0:fa2de1b79154 223 char** undoneValues;
AzureIoTClient 45:1119d0f2c4d8 224 char** undoneKeys = (char**)realloc(handleData->keys, sizeof(char*)* (handleData->count - 1));
Azure.IoT Build 0:fa2de1b79154 225 if (undoneKeys == NULL)
Azure.IoT Build 0:fa2de1b79154 226 {
AzureIoTClient 1:9190c0f4d23a 227 LogError("CATASTROPHIC error, unable to undo through realloc to a smaller size");
Azure.IoT Build 0:fa2de1b79154 228 }
Azure.IoT Build 0:fa2de1b79154 229 else
Azure.IoT Build 0:fa2de1b79154 230 {
Azure.IoT Build 0:fa2de1b79154 231 handleData->keys = undoneKeys;
Azure.IoT Build 0:fa2de1b79154 232 }
Azure.IoT Build 0:fa2de1b79154 233
Azure.IoT Build 0:fa2de1b79154 234 undoneValues = (char**)realloc(handleData->values, sizeof(char*)* (handleData->count - 1));
Azure.IoT Build 0:fa2de1b79154 235 if (undoneValues == NULL)
Azure.IoT Build 0:fa2de1b79154 236 {
AzureIoTClient 1:9190c0f4d23a 237 LogError("CATASTROPHIC error, unable to undo through realloc to a smaller size");
Azure.IoT Build 0:fa2de1b79154 238 }
Azure.IoT Build 0:fa2de1b79154 239 else
Azure.IoT Build 0:fa2de1b79154 240 {
Azure.IoT Build 0:fa2de1b79154 241 handleData->values = undoneValues;
Azure.IoT Build 0:fa2de1b79154 242 }
Azure.IoT Build 0:fa2de1b79154 243
Azure.IoT Build 0:fa2de1b79154 244 handleData->count--;
Azure.IoT Build 0:fa2de1b79154 245 }
Azure.IoT Build 0:fa2de1b79154 246 }
Azure.IoT Build 0:fa2de1b79154 247
Azure.IoT Build 0:fa2de1b79154 248 static char** findKey(MAP_HANDLE_DATA* handleData, const char* key)
Azure.IoT Build 0:fa2de1b79154 249 {
Azure.IoT Build 0:fa2de1b79154 250 char** result;
Azure.IoT Build 0:fa2de1b79154 251 if (handleData->keys == NULL)
Azure.IoT Build 0:fa2de1b79154 252 {
Azure.IoT Build 0:fa2de1b79154 253 result = NULL;
Azure.IoT Build 0:fa2de1b79154 254 }
Azure.IoT Build 0:fa2de1b79154 255 else
Azure.IoT Build 0:fa2de1b79154 256 {
Azure.IoT Build 0:fa2de1b79154 257 size_t i;
Azure.IoT Build 0:fa2de1b79154 258 result = NULL;
Azure.IoT Build 0:fa2de1b79154 259 for (i = 0; i < handleData->count; i++)
Azure.IoT Build 0:fa2de1b79154 260 {
Azure.IoT Build 0:fa2de1b79154 261 if (strcmp(handleData->keys[i], key) == 0)
Azure.IoT Build 0:fa2de1b79154 262 {
Azure.IoT Build 0:fa2de1b79154 263 result = handleData->keys + i;
Azure.IoT Build 0:fa2de1b79154 264 break;
Azure.IoT Build 0:fa2de1b79154 265 }
Azure.IoT Build 0:fa2de1b79154 266 }
Azure.IoT Build 0:fa2de1b79154 267 }
Azure.IoT Build 0:fa2de1b79154 268 return result;
Azure.IoT Build 0:fa2de1b79154 269 }
Azure.IoT Build 0:fa2de1b79154 270
Azure.IoT Build 0:fa2de1b79154 271 static char** findValue(MAP_HANDLE_DATA* handleData, const char* value)
Azure.IoT Build 0:fa2de1b79154 272 {
Azure.IoT Build 0:fa2de1b79154 273 char** result;
Azure.IoT Build 0:fa2de1b79154 274 if (handleData->values == NULL)
Azure.IoT Build 0:fa2de1b79154 275 {
Azure.IoT Build 0:fa2de1b79154 276 result = NULL;
Azure.IoT Build 0:fa2de1b79154 277 }
Azure.IoT Build 0:fa2de1b79154 278 else
Azure.IoT Build 0:fa2de1b79154 279 {
Azure.IoT Build 0:fa2de1b79154 280 size_t i;
Azure.IoT Build 0:fa2de1b79154 281 result = NULL;
Azure.IoT Build 0:fa2de1b79154 282 for (i = 0; i < handleData->count; i++)
Azure.IoT Build 0:fa2de1b79154 283 {
Azure.IoT Build 0:fa2de1b79154 284 if (strcmp(handleData->values[i], value) == 0)
Azure.IoT Build 0:fa2de1b79154 285 {
Azure.IoT Build 0:fa2de1b79154 286 result = handleData->values + i;
Azure.IoT Build 0:fa2de1b79154 287 break;
Azure.IoT Build 0:fa2de1b79154 288 }
Azure.IoT Build 0:fa2de1b79154 289 }
Azure.IoT Build 0:fa2de1b79154 290 }
Azure.IoT Build 0:fa2de1b79154 291 return result;
Azure.IoT Build 0:fa2de1b79154 292 }
Azure.IoT Build 0:fa2de1b79154 293
Azure.IoT Build 0:fa2de1b79154 294 static int insertNewKeyValue(MAP_HANDLE_DATA* handleData, const char* key, const char* value)
Azure.IoT Build 0:fa2de1b79154 295 {
Azure.IoT Build 0:fa2de1b79154 296 int result;
Azure.IoT Build 0:fa2de1b79154 297 if (Map_IncreaseStorageKeysValues(handleData) != 0) /*this increases handleData->count*/
Azure.IoT Build 0:fa2de1b79154 298 {
AzureIoTClient 21:b92006c5b9ff 299 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 300 }
Azure.IoT Build 0:fa2de1b79154 301 else
Azure.IoT Build 0:fa2de1b79154 302 {
Azure.IoT Build 0:fa2de1b79154 303 if (mallocAndStrcpy_s(&(handleData->keys[handleData->count - 1]), key) != 0)
Azure.IoT Build 0:fa2de1b79154 304 {
Azure.IoT Build 0:fa2de1b79154 305 Map_DecreaseStorageKeysValues(handleData);
AzureIoTClient 1:9190c0f4d23a 306 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 21:b92006c5b9ff 307 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 308 }
Azure.IoT Build 0:fa2de1b79154 309 else
Azure.IoT Build 0:fa2de1b79154 310 {
Azure.IoT Build 0:fa2de1b79154 311 if (mallocAndStrcpy_s(&(handleData->values[handleData->count - 1]), value) != 0)
Azure.IoT Build 0:fa2de1b79154 312 {
Azure.IoT Build 0:fa2de1b79154 313 free(handleData->keys[handleData->count - 1]);
Azure.IoT Build 0:fa2de1b79154 314 Map_DecreaseStorageKeysValues(handleData);
AzureIoTClient 1:9190c0f4d23a 315 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 21:b92006c5b9ff 316 result = __FAILURE__;
Azure.IoT Build 0:fa2de1b79154 317 }
Azure.IoT Build 0:fa2de1b79154 318 else
Azure.IoT Build 0:fa2de1b79154 319 {
Azure.IoT Build 0:fa2de1b79154 320 result = 0;
Azure.IoT Build 0:fa2de1b79154 321 }
Azure.IoT Build 0:fa2de1b79154 322 }
Azure.IoT Build 0:fa2de1b79154 323 }
AzureIoTClient 45:1119d0f2c4d8 324 return result;
Azure.IoT Build 0:fa2de1b79154 325 }
Azure.IoT Build 0:fa2de1b79154 326
Azure.IoT Build 0:fa2de1b79154 327 MAP_RESULT Map_Add(MAP_HANDLE handle, const char* key, const char* value)
Azure.IoT Build 0:fa2de1b79154 328 {
Azure.IoT Build 0:fa2de1b79154 329 MAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 330 /*Codes_SRS_MAP_02_006: [If parameter handle is NULL then Map_Add shall return MAP_INVALID_ARG.] */
Azure.IoT Build 0:fa2de1b79154 331 /*Codes_SRS_MAP_02_007: [If parameter key is NULL then Map_Add shall return MAP_INVALID_ARG.]*/
Azure.IoT Build 0:fa2de1b79154 332 /*Codes_SRS_MAP_02_008: [If parameter value is NULL then Map_Add shall return MAP_INVALID_ARG.] */
Azure.IoT Build 0:fa2de1b79154 333 if (
Azure.IoT Build 0:fa2de1b79154 334 (handle == NULL) ||
Azure.IoT Build 0:fa2de1b79154 335 (key == NULL) ||
Azure.IoT Build 0:fa2de1b79154 336 (value == NULL)
Azure.IoT Build 0:fa2de1b79154 337 )
Azure.IoT Build 0:fa2de1b79154 338 {
Azure.IoT Build 0:fa2de1b79154 339 result = MAP_INVALIDARG;
AzureIoTClient 45:1119d0f2c4d8 340 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 341 }
Azure.IoT Build 0:fa2de1b79154 342 else
Azure.IoT Build 0:fa2de1b79154 343 {
Azure.IoT Build 0:fa2de1b79154 344 MAP_HANDLE_DATA* handleData = (MAP_HANDLE_DATA*)handle;
Azure.IoT Build 0:fa2de1b79154 345 /*Codes_SRS_MAP_02_009: [If the key already exists, then Map_Add shall return MAP_KEYEXISTS.] */
Azure.IoT Build 0:fa2de1b79154 346 if (findKey(handleData, key) != NULL)
Azure.IoT Build 0:fa2de1b79154 347 {
Azure.IoT Build 0:fa2de1b79154 348 result = MAP_KEYEXISTS;
Azure.IoT Build 0:fa2de1b79154 349 }
Azure.IoT Build 0:fa2de1b79154 350 else
Azure.IoT Build 0:fa2de1b79154 351 {
Azure.IoT Build 0:fa2de1b79154 352 /* Codes_SRS_MAP_07_009: [If the mapFilterCallback function is not NULL, then the return value will be check and if it is not zero then Map_Add shall return MAP_FILTER_REJECT.] */
Azure.IoT Build 0:fa2de1b79154 353 if ( (handleData->mapFilterCallback != NULL) && (handleData->mapFilterCallback(key, value) != 0) )
Azure.IoT Build 0:fa2de1b79154 354 {
Azure.IoT Build 0:fa2de1b79154 355 result = MAP_FILTER_REJECT;
Azure.IoT Build 0:fa2de1b79154 356 }
Azure.IoT Build 0:fa2de1b79154 357 else
Azure.IoT Build 0:fa2de1b79154 358 {
Azure.IoT Build 0:fa2de1b79154 359 /*Codes_SRS_MAP_02_010: [Otherwise, Map_Add shall add the pair <key,value> to the map.] */
Azure.IoT Build 0:fa2de1b79154 360 if (insertNewKeyValue(handleData, key, value) != 0)
Azure.IoT Build 0:fa2de1b79154 361 {
Azure.IoT Build 0:fa2de1b79154 362 /*Codes_SRS_MAP_02_011: [If adding the pair <key,value> fails then Map_Add shall return MAP_ERROR.] */
Azure.IoT Build 0:fa2de1b79154 363 result = MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 364 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 365 }
Azure.IoT Build 0:fa2de1b79154 366 else
Azure.IoT Build 0:fa2de1b79154 367 {
Azure.IoT Build 0:fa2de1b79154 368 /*Codes_SRS_MAP_02_012: [Otherwise, Map_Add shall return MAP_OK.] */
Azure.IoT Build 0:fa2de1b79154 369 result = MAP_OK;
Azure.IoT Build 0:fa2de1b79154 370 }
Azure.IoT Build 0:fa2de1b79154 371 }
Azure.IoT Build 0:fa2de1b79154 372 }
Azure.IoT Build 0:fa2de1b79154 373 }
Azure.IoT Build 0:fa2de1b79154 374 return result;
Azure.IoT Build 0:fa2de1b79154 375 }
Azure.IoT Build 0:fa2de1b79154 376
Azure.IoT Build 0:fa2de1b79154 377 MAP_RESULT Map_AddOrUpdate(MAP_HANDLE handle, const char* key, const char* value)
Azure.IoT Build 0:fa2de1b79154 378 {
Azure.IoT Build 0:fa2de1b79154 379 MAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 380 /*Codes_SRS_MAP_02_013: [If parameter handle is NULL then Map_AddOrUpdate shall return MAP_INVALID_ARG.]*/
Azure.IoT Build 0:fa2de1b79154 381 /*Codes_SRS_MAP_02_014: [If parameter key is NULL then Map_AddOrUpdate shall return MAP_INVALID_ARG.]*/
Azure.IoT Build 0:fa2de1b79154 382 /*Codes_SRS_MAP_02_015: [If parameter value is NULL then Map_AddOrUpdate shall return MAP_INVALID_ARG.] */
Azure.IoT Build 0:fa2de1b79154 383 if (
Azure.IoT Build 0:fa2de1b79154 384 (handle == NULL) ||
Azure.IoT Build 0:fa2de1b79154 385 (key == NULL) ||
Azure.IoT Build 0:fa2de1b79154 386 (value == NULL)
Azure.IoT Build 0:fa2de1b79154 387 )
Azure.IoT Build 0:fa2de1b79154 388 {
Azure.IoT Build 0:fa2de1b79154 389 result = MAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 390 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 391 }
Azure.IoT Build 0:fa2de1b79154 392 else
Azure.IoT Build 0:fa2de1b79154 393 {
Azure.IoT Build 0:fa2de1b79154 394 MAP_HANDLE_DATA* handleData = (MAP_HANDLE_DATA*)handle;
Azure.IoT Build 0:fa2de1b79154 395
Azure.IoT Build 0:fa2de1b79154 396 /* Codes_SRS_MAP_07_008: [If the mapFilterCallback function is not NULL, then the return value will be check and if it is not zero then Map_AddOrUpdate shall return MAP_FILTER_REJECT.] */
Azure.IoT Build 0:fa2de1b79154 397 if (handleData->mapFilterCallback != NULL && handleData->mapFilterCallback(key, value) != 0)
Azure.IoT Build 0:fa2de1b79154 398 {
Azure.IoT Build 0:fa2de1b79154 399 result = MAP_FILTER_REJECT;
Azure.IoT Build 0:fa2de1b79154 400 }
Azure.IoT Build 0:fa2de1b79154 401 else
Azure.IoT Build 0:fa2de1b79154 402 {
Azure.IoT Build 0:fa2de1b79154 403 char** whereIsIt = findKey(handleData, key);
Azure.IoT Build 0:fa2de1b79154 404 if (whereIsIt == NULL)
Azure.IoT Build 0:fa2de1b79154 405 {
Azure.IoT Build 0:fa2de1b79154 406 /*Codes_SRS_MAP_02_017: [Otherwise, Map_AddOrUpdate shall add the pair <key,value> to the map.]*/
Azure.IoT Build 0:fa2de1b79154 407 if (insertNewKeyValue(handleData, key, value) != 0)
Azure.IoT Build 0:fa2de1b79154 408 {
Azure.IoT Build 0:fa2de1b79154 409 result = MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 410 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 411 }
Azure.IoT Build 0:fa2de1b79154 412 else
Azure.IoT Build 0:fa2de1b79154 413 {
Azure.IoT Build 0:fa2de1b79154 414 result = MAP_OK;
Azure.IoT Build 0:fa2de1b79154 415 }
Azure.IoT Build 0:fa2de1b79154 416 }
Azure.IoT Build 0:fa2de1b79154 417 else
Azure.IoT Build 0:fa2de1b79154 418 {
Azure.IoT Build 0:fa2de1b79154 419 /*Codes_SRS_MAP_02_016: [If the key already exists, then Map_AddOrUpdate shall overwrite the value of the existing key with parameter value.]*/
Azure.IoT Build 0:fa2de1b79154 420 size_t index = whereIsIt - handleData->keys;
Azure.IoT Build 0:fa2de1b79154 421 size_t valueLength = strlen(value);
Azure.IoT Build 0:fa2de1b79154 422 /*try to realloc value of this key*/
Azure.IoT Build 0:fa2de1b79154 423 char* newValue = (char*)realloc(handleData->values[index],valueLength + 1);
Azure.IoT Build 0:fa2de1b79154 424 if (newValue == NULL)
Azure.IoT Build 0:fa2de1b79154 425 {
Azure.IoT Build 0:fa2de1b79154 426 result = MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 427 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 428 }
Azure.IoT Build 0:fa2de1b79154 429 else
Azure.IoT Build 0:fa2de1b79154 430 {
AzureIoTClient 19:2e0811512ceb 431 (void)memcpy(newValue, value, valueLength + 1);
Azure.IoT Build 0:fa2de1b79154 432 handleData->values[index] = newValue;
Azure.IoT Build 0:fa2de1b79154 433 /*Codes_SRS_MAP_02_019: [Otherwise, Map_AddOrUpdate shall return MAP_OK.] */
Azure.IoT Build 0:fa2de1b79154 434 result = MAP_OK;
Azure.IoT Build 0:fa2de1b79154 435 }
Azure.IoT Build 0:fa2de1b79154 436 }
Azure.IoT Build 0:fa2de1b79154 437 }
Azure.IoT Build 0:fa2de1b79154 438 }
Azure.IoT Build 0:fa2de1b79154 439 return result;
Azure.IoT Build 0:fa2de1b79154 440 }
Azure.IoT Build 0:fa2de1b79154 441
Azure.IoT Build 0:fa2de1b79154 442 MAP_RESULT Map_Delete(MAP_HANDLE handle, const char* key)
Azure.IoT Build 0:fa2de1b79154 443 {
Azure.IoT Build 0:fa2de1b79154 444 MAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 445 /*Codes_SRS_MAP_02_020: [If parameter handle is NULL then Map_Delete shall return MAP_INVALIDARG.]*/
Azure.IoT Build 0:fa2de1b79154 446 /*Codes_SRS_MAP_02_021: [If parameter key is NULL then Map_Delete shall return MAP_INVALIDARG.]*/
Azure.IoT Build 0:fa2de1b79154 447 if (
Azure.IoT Build 0:fa2de1b79154 448 (handle == NULL) ||
Azure.IoT Build 0:fa2de1b79154 449 (key == NULL)
Azure.IoT Build 0:fa2de1b79154 450 )
Azure.IoT Build 0:fa2de1b79154 451 {
Azure.IoT Build 0:fa2de1b79154 452 result = MAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 453 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 454 }
Azure.IoT Build 0:fa2de1b79154 455 else
Azure.IoT Build 0:fa2de1b79154 456 {
Azure.IoT Build 0:fa2de1b79154 457 MAP_HANDLE_DATA* handleData = (MAP_HANDLE_DATA*)handle;
Azure.IoT Build 0:fa2de1b79154 458 char** whereIsIt = findKey(handleData,key);
Azure.IoT Build 0:fa2de1b79154 459 if (whereIsIt == NULL)
Azure.IoT Build 0:fa2de1b79154 460 {
Azure.IoT Build 0:fa2de1b79154 461 /*Codes_SRS_MAP_02_022: [If key does not exist then Map_Delete shall return MAP_KEYNOTFOUND.]*/
Azure.IoT Build 0:fa2de1b79154 462 result = MAP_KEYNOTFOUND;
Azure.IoT Build 0:fa2de1b79154 463 }
Azure.IoT Build 0:fa2de1b79154 464 else
Azure.IoT Build 0:fa2de1b79154 465 {
Azure.IoT Build 0:fa2de1b79154 466 /*Codes_SRS_MAP_02_023: [Otherwise, Map_Delete shall remove the key and its associated value from the map and return MAP_OK.]*/
Azure.IoT Build 0:fa2de1b79154 467 size_t index = whereIsIt - handleData->keys;
Azure.IoT Build 0:fa2de1b79154 468 free(handleData->keys[index]);
Azure.IoT Build 0:fa2de1b79154 469 free(handleData->values[index]);
Azure.IoT Build 0:fa2de1b79154 470 memmove(handleData->keys + index, handleData->keys + index + 1, (handleData->count - index - 1)*sizeof(char*)); /*if order doesn't matter... then this can be optimized*/
Azure.IoT Build 0:fa2de1b79154 471 memmove(handleData->values + index, handleData->values + index + 1, (handleData->count - index - 1)*sizeof(char*));
Azure.IoT Build 0:fa2de1b79154 472 Map_DecreaseStorageKeysValues(handleData);
Azure.IoT Build 0:fa2de1b79154 473 result = MAP_OK;
Azure.IoT Build 0:fa2de1b79154 474 }
Azure.IoT Build 0:fa2de1b79154 475
Azure.IoT Build 0:fa2de1b79154 476 }
Azure.IoT Build 0:fa2de1b79154 477 return result;
Azure.IoT Build 0:fa2de1b79154 478 }
Azure.IoT Build 0:fa2de1b79154 479
Azure.IoT Build 0:fa2de1b79154 480 MAP_RESULT Map_ContainsKey(MAP_HANDLE handle, const char* key, bool* keyExists)
Azure.IoT Build 0:fa2de1b79154 481 {
Azure.IoT Build 0:fa2de1b79154 482 MAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 483 /*Codes_SRS_MAP_02_024: [If parameter handle, key or keyExists are NULL then Map_ContainsKey shall return MAP_INVALIDARG.]*/
Azure.IoT Build 0:fa2de1b79154 484 if (
Azure.IoT Build 0:fa2de1b79154 485 (handle ==NULL) ||
Azure.IoT Build 0:fa2de1b79154 486 (key == NULL) ||
Azure.IoT Build 0:fa2de1b79154 487 (keyExists == NULL)
Azure.IoT Build 0:fa2de1b79154 488 )
Azure.IoT Build 0:fa2de1b79154 489 {
Azure.IoT Build 0:fa2de1b79154 490 result = MAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 491 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 492 }
Azure.IoT Build 0:fa2de1b79154 493 else
Azure.IoT Build 0:fa2de1b79154 494 {
Azure.IoT Build 0:fa2de1b79154 495 MAP_HANDLE_DATA* handleData = (MAP_HANDLE_DATA*)handle;
Azure.IoT Build 0:fa2de1b79154 496 /*Codes_SRS_MAP_02_025: [Otherwise if a key exists then Map_ContainsKey shall return MAP_OK and shall write in keyExists "true".]*/
Azure.IoT Build 0:fa2de1b79154 497 /*Codes_SRS_MAP_02_026: [If a key doesn't exist, then Map_ContainsKey shall return MAP_OK and write in keyExists "false".] */
Azure.IoT Build 0:fa2de1b79154 498 *keyExists = (findKey(handleData, key) != NULL) ? true: false;
Azure.IoT Build 0:fa2de1b79154 499 result = MAP_OK;
Azure.IoT Build 0:fa2de1b79154 500 }
Azure.IoT Build 0:fa2de1b79154 501 return result;
Azure.IoT Build 0:fa2de1b79154 502 }
Azure.IoT Build 0:fa2de1b79154 503
Azure.IoT Build 0:fa2de1b79154 504 MAP_RESULT Map_ContainsValue(MAP_HANDLE handle, const char* value, bool* valueExists)
Azure.IoT Build 0:fa2de1b79154 505 {
Azure.IoT Build 0:fa2de1b79154 506 MAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 507 /*Codes_SRS_MAP_02_027: [If parameter handle, value or valueExists is NULL then Map_ContainsValue shall return MAP_INVALIDARG.] */
Azure.IoT Build 0:fa2de1b79154 508 if (
Azure.IoT Build 0:fa2de1b79154 509 (handle == NULL) ||
Azure.IoT Build 0:fa2de1b79154 510 (value == NULL) ||
Azure.IoT Build 0:fa2de1b79154 511 (valueExists == NULL)
Azure.IoT Build 0:fa2de1b79154 512 )
Azure.IoT Build 0:fa2de1b79154 513 {
Azure.IoT Build 0:fa2de1b79154 514 result = MAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 515 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 516 }
Azure.IoT Build 0:fa2de1b79154 517 else
Azure.IoT Build 0:fa2de1b79154 518 {
Azure.IoT Build 0:fa2de1b79154 519 MAP_HANDLE_DATA* handleData = (MAP_HANDLE_DATA*)handle;
Azure.IoT Build 0:fa2de1b79154 520 /*Codes_SRS_MAP_02_028: [Otherwise, if a pair <key, value> has its value equal to the parameter value, the Map_ContainsValue shall return MAP_OK and shall write in valueExists "true".]*/
Azure.IoT Build 0:fa2de1b79154 521 /*Codes_SRS_MAP_02_029: [Otherwise, if such a <key, value> does not exist, then Map_ContainsValue shall return MAP_OK and shall write in valueExists "false".] */
Azure.IoT Build 0:fa2de1b79154 522 *valueExists = (findValue(handleData, value) != NULL) ? true : false;
Azure.IoT Build 0:fa2de1b79154 523 result = MAP_OK;
Azure.IoT Build 0:fa2de1b79154 524 }
Azure.IoT Build 0:fa2de1b79154 525 return result;
Azure.IoT Build 0:fa2de1b79154 526 }
Azure.IoT Build 0:fa2de1b79154 527
Azure.IoT Build 0:fa2de1b79154 528 const char* Map_GetValueFromKey(MAP_HANDLE handle, const char* key)
Azure.IoT Build 0:fa2de1b79154 529 {
Azure.IoT Build 0:fa2de1b79154 530 const char* result;
Azure.IoT Build 0:fa2de1b79154 531 /*Codes_SRS_MAP_02_040: [If parameter handle or key is NULL then Map_GetValueFromKey returns NULL.]*/
Azure.IoT Build 0:fa2de1b79154 532 if (
Azure.IoT Build 0:fa2de1b79154 533 (handle == NULL) ||
Azure.IoT Build 0:fa2de1b79154 534 (key == NULL)
Azure.IoT Build 0:fa2de1b79154 535 )
Azure.IoT Build 0:fa2de1b79154 536 {
Azure.IoT Build 0:fa2de1b79154 537 result = NULL;
AzureIoTClient 1:9190c0f4d23a 538 LogError("invalid parameter to Map_GetValueFromKey");
Azure.IoT Build 0:fa2de1b79154 539 }
Azure.IoT Build 0:fa2de1b79154 540 else
Azure.IoT Build 0:fa2de1b79154 541 {
Azure.IoT Build 0:fa2de1b79154 542 MAP_HANDLE_DATA * handleData = (MAP_HANDLE_DATA *)handle;
Azure.IoT Build 0:fa2de1b79154 543 char** whereIsIt = findKey(handleData, key);
Azure.IoT Build 0:fa2de1b79154 544 if(whereIsIt == NULL)
Azure.IoT Build 0:fa2de1b79154 545 {
Azure.IoT Build 0:fa2de1b79154 546 /*Codes_SRS_MAP_02_041: [If the key is not found, then Map_GetValueFromKey returns NULL.]*/
Azure.IoT Build 0:fa2de1b79154 547 result = NULL;
Azure.IoT Build 0:fa2de1b79154 548 }
Azure.IoT Build 0:fa2de1b79154 549 else
Azure.IoT Build 0:fa2de1b79154 550 {
Azure.IoT Build 0:fa2de1b79154 551 /*Codes_SRS_MAP_02_042: [Otherwise, Map_GetValueFromKey returns the key's value.] */
Azure.IoT Build 0:fa2de1b79154 552 size_t index = whereIsIt - handleData->keys;
Azure.IoT Build 0:fa2de1b79154 553 result = handleData->values[index];
Azure.IoT Build 0:fa2de1b79154 554 }
Azure.IoT Build 0:fa2de1b79154 555 }
Azure.IoT Build 0:fa2de1b79154 556 return result;
Azure.IoT Build 0:fa2de1b79154 557 }
Azure.IoT Build 0:fa2de1b79154 558
Azure.IoT Build 0:fa2de1b79154 559 MAP_RESULT Map_GetInternals(MAP_HANDLE handle, const char*const** keys, const char*const** values, size_t* count)
Azure.IoT Build 0:fa2de1b79154 560 {
Azure.IoT Build 0:fa2de1b79154 561 MAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 562 /*Codes_SRS_MAP_02_046: [If parameter handle, keys, values or count is NULL then Map_GetInternals shall return MAP_INVALIDARG.] */
Azure.IoT Build 0:fa2de1b79154 563 if (
Azure.IoT Build 0:fa2de1b79154 564 (handle == NULL) ||
Azure.IoT Build 0:fa2de1b79154 565 (keys == NULL) ||
Azure.IoT Build 0:fa2de1b79154 566 (values == NULL) ||
Azure.IoT Build 0:fa2de1b79154 567 (count == NULL)
Azure.IoT Build 0:fa2de1b79154 568 )
Azure.IoT Build 0:fa2de1b79154 569 {
Azure.IoT Build 0:fa2de1b79154 570 result = MAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 571 LOG_MAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 572 }
Azure.IoT Build 0:fa2de1b79154 573 else
Azure.IoT Build 0:fa2de1b79154 574 {
Azure.IoT Build 0:fa2de1b79154 575 /*Codes_SRS_MAP_02_043: [Map_GetInternals shall produce in *keys an pointer to an array of const char* having all the keys stored so far by the map.]*/
Azure.IoT Build 0:fa2de1b79154 576 /*Codes_SRS_MAP_02_044: [Map_GetInternals shall produce in *values a pointer to an array of const char* having all the values stored so far by the map.]*/
Azure.IoT Build 0:fa2de1b79154 577 /*Codes_SRS_MAP_02_045: [ Map_GetInternals shall produce in *count the number of stored keys and values.]*/
Azure.IoT Build 0:fa2de1b79154 578 MAP_HANDLE_DATA * handleData = (MAP_HANDLE_DATA *)handle;
Azure.IoT Build 0:fa2de1b79154 579 *keys =(const char* const*)(handleData->keys);
Azure.IoT Build 0:fa2de1b79154 580 *values = (const char* const*)(handleData->values);
Azure.IoT Build 0:fa2de1b79154 581 *count = handleData->count;
Azure.IoT Build 0:fa2de1b79154 582 result = MAP_OK;
Azure.IoT Build 0:fa2de1b79154 583 }
Azure.IoT Build 0:fa2de1b79154 584 return result;
Azure.IoT Build 0:fa2de1b79154 585 }
Azure.IoT Build 0:fa2de1b79154 586
Azure.IoT Build 0:fa2de1b79154 587 STRING_HANDLE Map_ToJSON(MAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 588 {
Azure.IoT Build 0:fa2de1b79154 589 STRING_HANDLE result;
Azure.IoT Build 0:fa2de1b79154 590 /*Codes_SRS_MAP_02_052: [If parameter handle is NULL then Map_ToJSON shall return NULL.] */
Azure.IoT Build 0:fa2de1b79154 591 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 592 {
Azure.IoT Build 0:fa2de1b79154 593 result = NULL;
Azure.IoT Build 0:fa2de1b79154 594 LogError("invalid arg (NULL)");
Azure.IoT Build 0:fa2de1b79154 595 }
Azure.IoT Build 0:fa2de1b79154 596 else
Azure.IoT Build 0:fa2de1b79154 597 {
Azure.IoT Build 0:fa2de1b79154 598 /*Codes_SRS_MAP_02_048: [Map_ToJSON shall produce a STRING_HANDLE representing the content of the MAP.] */
Azure.IoT Build 0:fa2de1b79154 599 result = STRING_construct("{");
Azure.IoT Build 0:fa2de1b79154 600 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 601 {
Azure.IoT Build 0:fa2de1b79154 602 LogError("STRING_construct failed");
Azure.IoT Build 0:fa2de1b79154 603 }
Azure.IoT Build 0:fa2de1b79154 604 else
Azure.IoT Build 0:fa2de1b79154 605 {
Azure.IoT Build 0:fa2de1b79154 606 size_t i;
Azure.IoT Build 0:fa2de1b79154 607 MAP_HANDLE_DATA* handleData = (MAP_HANDLE_DATA *)handle;
Azure.IoT Build 0:fa2de1b79154 608 /*Codes_SRS_MAP_02_049: [If the MAP is empty, then Map_ToJSON shall produce the string "{}".*/
Azure.IoT Build 0:fa2de1b79154 609 bool breakFor = false; /*used to break out of for*/
Azure.IoT Build 0:fa2de1b79154 610 for (i = 0; (i < handleData->count) && (!breakFor); i++)
Azure.IoT Build 0:fa2de1b79154 611 {
Azure.IoT Build 0:fa2de1b79154 612 /*add one entry to the JSON*/
Azure.IoT Build 0:fa2de1b79154 613 /*Codes_SRS_MAP_02_050: [If the map has properties then Map_ToJSON shall produce the following string:{"name1":"value1", "name2":"value2" ...}]*/
Azure.IoT Build 0:fa2de1b79154 614 STRING_HANDLE key = STRING_new_JSON(handleData->keys[i]);
Azure.IoT Build 0:fa2de1b79154 615 if (key == NULL)
Azure.IoT Build 0:fa2de1b79154 616 {
Azure.IoT Build 0:fa2de1b79154 617 LogError("STRING_new_JSON failed");
Azure.IoT Build 0:fa2de1b79154 618 STRING_delete(result);
Azure.IoT Build 0:fa2de1b79154 619 result = NULL;
Azure.IoT Build 0:fa2de1b79154 620 breakFor = true;
Azure.IoT Build 0:fa2de1b79154 621 }
Azure.IoT Build 0:fa2de1b79154 622 else
Azure.IoT Build 0:fa2de1b79154 623 {
Azure.IoT Build 0:fa2de1b79154 624 STRING_HANDLE value = STRING_new_JSON(handleData->values[i]);
Azure.IoT Build 0:fa2de1b79154 625 if (value == NULL)
Azure.IoT Build 0:fa2de1b79154 626 {
Azure.IoT Build 0:fa2de1b79154 627 LogError("STRING_new_JSON failed");
Azure.IoT Build 0:fa2de1b79154 628 STRING_delete(result);
Azure.IoT Build 0:fa2de1b79154 629 result = NULL;
Azure.IoT Build 0:fa2de1b79154 630 breakFor = true;
Azure.IoT Build 0:fa2de1b79154 631 }
Azure.IoT Build 0:fa2de1b79154 632 else
Azure.IoT Build 0:fa2de1b79154 633 {
Azure.IoT Build 0:fa2de1b79154 634 if (!(
Azure.IoT Build 0:fa2de1b79154 635 ((i>0) ? (STRING_concat(result, ",") == 0) : 1) &&
Azure.IoT Build 0:fa2de1b79154 636 (STRING_concat_with_STRING(result, key) == 0) &&
Azure.IoT Build 0:fa2de1b79154 637 (STRING_concat(result, ":") == 0) &&
Azure.IoT Build 0:fa2de1b79154 638 (STRING_concat_with_STRING(result, value) == 0)
Azure.IoT Build 0:fa2de1b79154 639 ))
Azure.IoT Build 0:fa2de1b79154 640 {
Azure.IoT Build 0:fa2de1b79154 641 LogError("failed to build the JSON");
Azure.IoT Build 0:fa2de1b79154 642 STRING_delete(result);
Azure.IoT Build 0:fa2de1b79154 643 result = NULL;
Azure.IoT Build 0:fa2de1b79154 644 breakFor = true;
Azure.IoT Build 0:fa2de1b79154 645 }
Azure.IoT Build 0:fa2de1b79154 646 else
Azure.IoT Build 0:fa2de1b79154 647 {
Azure.IoT Build 0:fa2de1b79154 648 /*all nice, go to the next element in the map*/
Azure.IoT Build 0:fa2de1b79154 649 }
Azure.IoT Build 0:fa2de1b79154 650 STRING_delete(value);
Azure.IoT Build 0:fa2de1b79154 651 }
Azure.IoT Build 0:fa2de1b79154 652 STRING_delete(key);
Azure.IoT Build 0:fa2de1b79154 653 }
Azure.IoT Build 0:fa2de1b79154 654 }
AzureIoTClient 45:1119d0f2c4d8 655
Azure.IoT Build 0:fa2de1b79154 656 if (breakFor)
Azure.IoT Build 0:fa2de1b79154 657 {
Azure.IoT Build 0:fa2de1b79154 658 LogError("error happened during JSON string builder");
Azure.IoT Build 0:fa2de1b79154 659 }
Azure.IoT Build 0:fa2de1b79154 660 else
Azure.IoT Build 0:fa2de1b79154 661 {
Azure.IoT Build 0:fa2de1b79154 662 if (STRING_concat(result, "}") != 0)
Azure.IoT Build 0:fa2de1b79154 663 {
Azure.IoT Build 0:fa2de1b79154 664 LogError("failed to build the JSON");
Azure.IoT Build 0:fa2de1b79154 665 STRING_delete(result);
Azure.IoT Build 0:fa2de1b79154 666 result = NULL;
Azure.IoT Build 0:fa2de1b79154 667 }
Azure.IoT Build 0:fa2de1b79154 668 else
Azure.IoT Build 0:fa2de1b79154 669 {
Azure.IoT Build 0:fa2de1b79154 670 /*return as is, JSON has been build*/
Azure.IoT Build 0:fa2de1b79154 671 }
Azure.IoT Build 0:fa2de1b79154 672 }
Azure.IoT Build 0:fa2de1b79154 673 }
Azure.IoT Build 0:fa2de1b79154 674 }
Azure.IoT Build 0:fa2de1b79154 675 return result;
Azure.IoT Build 0:fa2de1b79154 676
AzureIoTClient 45:1119d0f2c4d8 677 }