Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
AzureIoTClient
Date:
Sun Apr 24 16:41:14 2016 -0700
Revision:
1:9190c0f4d23a
Parent:
0:fa2de1b79154
Child:
6:c55b013dfc2a
1.0.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 #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 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 9
Azure.IoT Build 0:fa2de1b79154 10 #include "azure_c_shared_utility/map.h"
Azure.IoT Build 0:fa2de1b79154 11 #include "azure_c_shared_utility/constmap.h"
Azure.IoT Build 0:fa2de1b79154 12 #include "azure_c_shared_utility/iot_logging.h"
Azure.IoT Build 0:fa2de1b79154 13 #include "azure_c_shared_utility/refcount.h"
Azure.IoT Build 0:fa2de1b79154 14
Azure.IoT Build 0:fa2de1b79154 15 DEFINE_ENUM_STRINGS(CONSTMAP_RESULT, CONSTMAP_RESULT_VALUES);
Azure.IoT Build 0:fa2de1b79154 16
Azure.IoT Build 0:fa2de1b79154 17 typedef struct CONSTMAP_HANDLE_DATA_TAG
Azure.IoT Build 0:fa2de1b79154 18 {
Azure.IoT Build 0:fa2de1b79154 19 MAP_HANDLE map;
Azure.IoT Build 0:fa2de1b79154 20 } CONSTMAP_HANDLE_DATA;
Azure.IoT Build 0:fa2de1b79154 21
Azure.IoT Build 0:fa2de1b79154 22 DEFINE_REFCOUNT_TYPE(CONSTMAP_HANDLE_DATA);
Azure.IoT Build 0:fa2de1b79154 23
AzureIoTClient 1:9190c0f4d23a 24 #define LOG_CONSTMAP_ERROR(result) LogError("result = %s", ENUM_TO_STRING(CONSTMAP_RESULT, (result)));
Azure.IoT Build 0:fa2de1b79154 25
Azure.IoT Build 0:fa2de1b79154 26 CONSTMAP_HANDLE ConstMap_Create(MAP_HANDLE sourceMap)
Azure.IoT Build 0:fa2de1b79154 27 {
Azure.IoT Build 0:fa2de1b79154 28 CONSTMAP_HANDLE_DATA* result = REFCOUNT_TYPE_CREATE(CONSTMAP_HANDLE_DATA);
Azure.IoT Build 0:fa2de1b79154 29
Azure.IoT Build 0:fa2de1b79154 30 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 31 {
Azure.IoT Build 0:fa2de1b79154 32 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 33 }
Azure.IoT Build 0:fa2de1b79154 34 else
Azure.IoT Build 0:fa2de1b79154 35 {
Azure.IoT Build 0:fa2de1b79154 36 /*Codes_SRS_CONSTMAP_17_048: [ConstMap_Create shall accept any non-NULL MAP_HANDLE as input.]*/
Azure.IoT Build 0:fa2de1b79154 37 /*Codes_SRS_CONSTMAP_17_001: [ConstMap_Create shall create an immutable map, populated by the key, value pairs in the source map.]*/
Azure.IoT Build 0:fa2de1b79154 38 result->map = Map_Clone(sourceMap);
Azure.IoT Build 0:fa2de1b79154 39 if (result->map == NULL)
Azure.IoT Build 0:fa2de1b79154 40 {
Azure.IoT Build 0:fa2de1b79154 41 free(result);
Azure.IoT Build 0:fa2de1b79154 42 /*Codes_SRS_CONSTMAP_17_002: [If during creation there are any errors, then ConstMap_Create shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 43 result = NULL;
Azure.IoT Build 0:fa2de1b79154 44 LOG_CONSTMAP_ERROR(CONSTMAP_ERROR);
Azure.IoT Build 0:fa2de1b79154 45 }
Azure.IoT Build 0:fa2de1b79154 46
Azure.IoT Build 0:fa2de1b79154 47 }
Azure.IoT Build 0:fa2de1b79154 48 /*Codes_SRS_CONSTMAP_17_003: [Otherwise, it shall return a non-NULL handle that can be used in subsequent calls.]*/
Azure.IoT Build 0:fa2de1b79154 49 return (CONSTMAP_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 50 }
Azure.IoT Build 0:fa2de1b79154 51
Azure.IoT Build 0:fa2de1b79154 52 void ConstMap_Destroy(CONSTMAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 53 {
Azure.IoT Build 0:fa2de1b79154 54 /*Codes_SRS_CONSTMAP_17_005: [If parameter handle is NULL then ConstMap_Destroy shall take no action.]*/
Azure.IoT Build 0:fa2de1b79154 55 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 56 {
Azure.IoT Build 0:fa2de1b79154 57 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 58 }
Azure.IoT Build 0:fa2de1b79154 59 else
Azure.IoT Build 0:fa2de1b79154 60 {
Azure.IoT Build 0:fa2de1b79154 61 /*Codes_SRS_CONSTMAP_17_049: [ConstMap_Destroy shall decrement the internal reference count of the immutable map.]*/
Azure.IoT Build 0:fa2de1b79154 62 if (DEC_REF(CONSTMAP_HANDLE_DATA, handle) == DEC_RETURN_ZERO)
Azure.IoT Build 0:fa2de1b79154 63 {
Azure.IoT Build 0:fa2de1b79154 64 /*Codes_SRS_CONSTMAP_17_004: [If the reference count is zero, ConstMap_Destroy shall release all resources associated with the immutable map.]*/
Azure.IoT Build 0:fa2de1b79154 65 Map_Destroy(((CONSTMAP_HANDLE_DATA *)handle)->map);
Azure.IoT Build 0:fa2de1b79154 66 free(handle);
Azure.IoT Build 0:fa2de1b79154 67 }
Azure.IoT Build 0:fa2de1b79154 68
Azure.IoT Build 0:fa2de1b79154 69 }
Azure.IoT Build 0:fa2de1b79154 70 }
Azure.IoT Build 0:fa2de1b79154 71
Azure.IoT Build 0:fa2de1b79154 72 CONSTMAP_HANDLE ConstMap_Clone(CONSTMAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 73 {
Azure.IoT Build 0:fa2de1b79154 74 /*Codes_SRS_CONSTMAP_17_038: [ConstMap_Clone returns NULL if parameter handle is NULL.] */
Azure.IoT Build 0:fa2de1b79154 75 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 76 {
Azure.IoT Build 0:fa2de1b79154 77 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 78 }
Azure.IoT Build 0:fa2de1b79154 79 else
Azure.IoT Build 0:fa2de1b79154 80 {
Azure.IoT Build 0:fa2de1b79154 81 /*Codes_SRS_CONSTMAP_17_039: [ConstMap_Clone shall increase the internal reference count of the immutable map indicated by parameter handle]*/
Azure.IoT Build 0:fa2de1b79154 82 /*Codes_SRS_CONSTMAP_17_050: [ConstMap_Clone shall return the non-NULL handle. ]*/
Azure.IoT Build 0:fa2de1b79154 83 INC_REF(CONSTMAP_HANDLE_DATA, handle);
Azure.IoT Build 0:fa2de1b79154 84 }
Azure.IoT Build 0:fa2de1b79154 85 return (handle);
Azure.IoT Build 0:fa2de1b79154 86 }
Azure.IoT Build 0:fa2de1b79154 87
Azure.IoT Build 0:fa2de1b79154 88 static CONSTMAP_RESULT ConstMap_ErrorConvert(MAP_RESULT mapResult)
Azure.IoT Build 0:fa2de1b79154 89 {
Azure.IoT Build 0:fa2de1b79154 90 CONSTMAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 91 switch (mapResult)
Azure.IoT Build 0:fa2de1b79154 92 {
Azure.IoT Build 0:fa2de1b79154 93 case MAP_OK:
Azure.IoT Build 0:fa2de1b79154 94 result = CONSTMAP_OK;
Azure.IoT Build 0:fa2de1b79154 95 break;
Azure.IoT Build 0:fa2de1b79154 96 case MAP_INVALIDARG:
Azure.IoT Build 0:fa2de1b79154 97 result = CONSTMAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 98 break;
Azure.IoT Build 0:fa2de1b79154 99 case MAP_KEYNOTFOUND:
Azure.IoT Build 0:fa2de1b79154 100 result = CONSTMAP_KEYNOTFOUND;
Azure.IoT Build 0:fa2de1b79154 101 break;
Azure.IoT Build 0:fa2de1b79154 102 default:
Azure.IoT Build 0:fa2de1b79154 103 result = CONSTMAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 104 break;
Azure.IoT Build 0:fa2de1b79154 105 }
Azure.IoT Build 0:fa2de1b79154 106 return result;
Azure.IoT Build 0:fa2de1b79154 107 }
Azure.IoT Build 0:fa2de1b79154 108
Azure.IoT Build 0:fa2de1b79154 109 MAP_HANDLE ConstMap_CloneWriteable(CONSTMAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 110 {
Azure.IoT Build 0:fa2de1b79154 111 MAP_HANDLE result = NULL;
Azure.IoT Build 0:fa2de1b79154 112 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 113 {
Azure.IoT Build 0:fa2de1b79154 114 /*Codes_SRS_CONSTMAP_17_051: [ConstMap_CloneWriteable returns NULL if parameter handle is NULL. ]*/
Azure.IoT Build 0:fa2de1b79154 115 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 116 }
Azure.IoT Build 0:fa2de1b79154 117 else
Azure.IoT Build 0:fa2de1b79154 118 {
Azure.IoT Build 0:fa2de1b79154 119 /*Codes_SRS_CONSTMAP_17_052: [ConstMap_CloneWriteable shall create a new, writeable map, populated by the key, value pairs in the parameter defined by handle.]*/
Azure.IoT Build 0:fa2de1b79154 120 /*Codes_SRS_CONSTMAP_17_053: [If during cloning, any operation fails, then ConstMap_CloneWriteableap_Clone shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 121 /*Codes_SRS_CONSTMAP_17_054: [Otherwise, ConstMap_CloneWriteable shall return a non-NULL handle that can be used in subsequent calls.]*/
Azure.IoT Build 0:fa2de1b79154 122 result = Map_Clone(((CONSTMAP_HANDLE_DATA *)handle)->map);
Azure.IoT Build 0:fa2de1b79154 123 }
Azure.IoT Build 0:fa2de1b79154 124 return result;
Azure.IoT Build 0:fa2de1b79154 125 }
Azure.IoT Build 0:fa2de1b79154 126
Azure.IoT Build 0:fa2de1b79154 127 bool ConstMap_ContainsKey(CONSTMAP_HANDLE handle, const char* key )
Azure.IoT Build 0:fa2de1b79154 128 {
Azure.IoT Build 0:fa2de1b79154 129 bool keyExists = false;
Azure.IoT Build 0:fa2de1b79154 130 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 131 {
Azure.IoT Build 0:fa2de1b79154 132 /*Codes_SRS_CONSTMAP_17_024: [If parameter handle or key are NULL then ConstMap_ContainsKey shall return false.]*/
Azure.IoT Build 0:fa2de1b79154 133 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 134 }
Azure.IoT Build 0:fa2de1b79154 135 else
Azure.IoT Build 0:fa2de1b79154 136 {
Azure.IoT Build 0:fa2de1b79154 137 if (key == NULL)
Azure.IoT Build 0:fa2de1b79154 138 {
Azure.IoT Build 0:fa2de1b79154 139 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 140 }
Azure.IoT Build 0:fa2de1b79154 141 else
Azure.IoT Build 0:fa2de1b79154 142 {
Azure.IoT Build 0:fa2de1b79154 143 /*Codes_SRS_CONSTMAP_17_025: [Otherwise if a key exists then ConstMap_ContainsKey shall return true.]*/
Azure.IoT Build 0:fa2de1b79154 144 MAP_RESULT mapResult = Map_ContainsKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key, &keyExists);
Azure.IoT Build 0:fa2de1b79154 145 if (mapResult != MAP_OK)
Azure.IoT Build 0:fa2de1b79154 146 {
Azure.IoT Build 0:fa2de1b79154 147 /*Codes_SRS_CONSTMAP_17_026: [If a key doesn't exist, then ConstMap_ContainsKey shall return false.]*/
Azure.IoT Build 0:fa2de1b79154 148 keyExists = false;
Azure.IoT Build 0:fa2de1b79154 149 LOG_CONSTMAP_ERROR(ConstMap_ErrorConvert(mapResult));
Azure.IoT Build 0:fa2de1b79154 150 }
Azure.IoT Build 0:fa2de1b79154 151 }
Azure.IoT Build 0:fa2de1b79154 152 }
Azure.IoT Build 0:fa2de1b79154 153 return keyExists;
Azure.IoT Build 0:fa2de1b79154 154 }
Azure.IoT Build 0:fa2de1b79154 155
Azure.IoT Build 0:fa2de1b79154 156 bool ConstMap_ContainsValue(CONSTMAP_HANDLE handle, const char* value)
Azure.IoT Build 0:fa2de1b79154 157 {
Azure.IoT Build 0:fa2de1b79154 158 bool valueExists = false;
Azure.IoT Build 0:fa2de1b79154 159 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 160 {
Azure.IoT Build 0:fa2de1b79154 161 /*Codes_SRS_CONSTMAP_17_027: [If parameter handle or value is NULL then ConstMap_ContainsValue shall return false.]*/
Azure.IoT Build 0:fa2de1b79154 162 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 163 }
Azure.IoT Build 0:fa2de1b79154 164 else
Azure.IoT Build 0:fa2de1b79154 165 {
Azure.IoT Build 0:fa2de1b79154 166 if (value == NULL)
Azure.IoT Build 0:fa2de1b79154 167 {
Azure.IoT Build 0:fa2de1b79154 168 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
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 /*Codes_SRS_CONSTMAP_17_028: [Otherwise, if a pair has its value equal to the parameter value, the ConstMap_ContainsValue shall return true.]*/
Azure.IoT Build 0:fa2de1b79154 173 MAP_RESULT mapResult = Map_ContainsValue(((CONSTMAP_HANDLE_DATA *)handle)->map, value, &valueExists);
Azure.IoT Build 0:fa2de1b79154 174 if (mapResult != MAP_OK)
Azure.IoT Build 0:fa2de1b79154 175 {
Azure.IoT Build 0:fa2de1b79154 176 /*Codes_SRS_CONSTMAP_17_029: [Otherwise, if such a does not exist, then ConstMap_ContainsValue shall return false.]*/
Azure.IoT Build 0:fa2de1b79154 177 LOG_CONSTMAP_ERROR(ConstMap_ErrorConvert(mapResult));
Azure.IoT Build 0:fa2de1b79154 178 }
Azure.IoT Build 0:fa2de1b79154 179 }
Azure.IoT Build 0:fa2de1b79154 180 }
Azure.IoT Build 0:fa2de1b79154 181 return valueExists;
Azure.IoT Build 0:fa2de1b79154 182 }
Azure.IoT Build 0:fa2de1b79154 183
Azure.IoT Build 0:fa2de1b79154 184 const char* ConstMap_GetValue(CONSTMAP_HANDLE handle, const char* key)
Azure.IoT Build 0:fa2de1b79154 185 {
Azure.IoT Build 0:fa2de1b79154 186 const char* value = NULL;
Azure.IoT Build 0:fa2de1b79154 187
Azure.IoT Build 0:fa2de1b79154 188 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 189 {
Azure.IoT Build 0:fa2de1b79154 190 /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/
Azure.IoT Build 0:fa2de1b79154 191 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 192 }
Azure.IoT Build 0:fa2de1b79154 193 else
Azure.IoT Build 0:fa2de1b79154 194 {
Azure.IoT Build 0:fa2de1b79154 195 if (key == NULL)
Azure.IoT Build 0:fa2de1b79154 196 {
Azure.IoT Build 0:fa2de1b79154 197 /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/
Azure.IoT Build 0:fa2de1b79154 198 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 199 }
Azure.IoT Build 0:fa2de1b79154 200 else
Azure.IoT Build 0:fa2de1b79154 201 {
Azure.IoT Build 0:fa2de1b79154 202 /*Codes_SRS_CONSTMAP_17_041: [If the key is not found, then ConstMap_GetValue returns NULL.]*/
Azure.IoT Build 0:fa2de1b79154 203 /*Codes_SRS_CONSTMAP_17_042: [Otherwise, ConstMap_GetValue returns the key's value.]*/
Azure.IoT Build 0:fa2de1b79154 204 value = Map_GetValueFromKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key);
Azure.IoT Build 0:fa2de1b79154 205 }
Azure.IoT Build 0:fa2de1b79154 206 }
Azure.IoT Build 0:fa2de1b79154 207 return value;
Azure.IoT Build 0:fa2de1b79154 208 }
Azure.IoT Build 0:fa2de1b79154 209
Azure.IoT Build 0:fa2de1b79154 210 CONSTMAP_RESULT ConstMap_GetInternals(CONSTMAP_HANDLE handle, const char*const** keys, const char*const** values, size_t* count)
Azure.IoT Build 0:fa2de1b79154 211 {
Azure.IoT Build 0:fa2de1b79154 212 CONSTMAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 213 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 214 {
Azure.IoT Build 0:fa2de1b79154 215 /*Codes_SRS_CONSTMAP_17_046: [If parameter handle, keys, values or count is NULL then ConstMap_GetInternals shall return CONSTMAP_INVALIDARG.]*/
Azure.IoT Build 0:fa2de1b79154 216 result = CONSTMAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 217 LOG_CONSTMAP_ERROR(result);
Azure.IoT Build 0:fa2de1b79154 218 }
Azure.IoT Build 0:fa2de1b79154 219 else
Azure.IoT Build 0:fa2de1b79154 220 {
Azure.IoT Build 0:fa2de1b79154 221 /*Codes_SRS_CONSTMAP_17_043: [ConstMap_GetInternals shall produce in *keys a pointer to an array of const char* having all the keys stored so far by the map.]
Azure.IoT Build 0:fa2de1b79154 222 *Codes_SRS_CONSTMAP_17_044: [ConstMap_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 223 *Codes_SRS_CONSTMAP_17_045: [ ConstMap_GetInternals shall produce in *count the number of stored keys and values.]
Azure.IoT Build 0:fa2de1b79154 224 */
Azure.IoT Build 0:fa2de1b79154 225 MAP_RESULT mapResult = Map_GetInternals(((CONSTMAP_HANDLE_DATA *)handle)->map, keys, values, count);
Azure.IoT Build 0:fa2de1b79154 226 result = ConstMap_ErrorConvert(mapResult);
Azure.IoT Build 0:fa2de1b79154 227 }
Azure.IoT Build 0:fa2de1b79154 228 return result;
Azure.IoT Build 0:fa2de1b79154 229 }