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