Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed
Fork of iothub_client_sample_amqp by
constmap.c
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 #include <stdlib.h> 00005 #ifdef _CRTDBG_MAP_ALLOC 00006 #include <crtdbg.h> 00007 #endif 00008 #include "azure_c_shared_utility/gballoc.h" 00009 00010 #include "azure_c_shared_utility/map.h" 00011 #include "azure_c_shared_utility/constmap.h" 00012 #include "azure_c_shared_utility/xlogging.h" 00013 #include "azure_c_shared_utility/refcount.h" 00014 00015 DEFINE_ENUM_STRINGS(CONSTMAP_RESULT, CONSTMAP_RESULT_VALUES); 00016 00017 typedef struct CONSTMAP_HANDLE_DATA_TAG 00018 { 00019 MAP_HANDLE map; 00020 } CONSTMAP_HANDLE_DATA; 00021 00022 DEFINE_REFCOUNT_TYPE(CONSTMAP_HANDLE_DATA); 00023 00024 #define LOG_CONSTMAP_ERROR(result) LogError("result = %s", ENUM_TO_STRING(CONSTMAP_RESULT, (result))); 00025 00026 CONSTMAP_HANDLE ConstMap_Create(MAP_HANDLE sourceMap) 00027 { 00028 CONSTMAP_HANDLE_DATA* result = REFCOUNT_TYPE_CREATE(CONSTMAP_HANDLE_DATA); 00029 00030 if (result == NULL) 00031 { 00032 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00033 } 00034 else 00035 { 00036 /*Codes_SRS_CONSTMAP_17_048: [ConstMap_Create shall accept any non-NULL MAP_HANDLE as input.]*/ 00037 /*Codes_SRS_CONSTMAP_17_001: [ConstMap_Create shall create an immutable map, populated by the key, value pairs in the source map.]*/ 00038 result->map = Map_Clone(sourceMap); 00039 if (result->map == NULL) 00040 { 00041 free(result); 00042 /*Codes_SRS_CONSTMAP_17_002: [If during creation there are any errors, then ConstMap_Create shall return NULL.]*/ 00043 result = NULL; 00044 LOG_CONSTMAP_ERROR(CONSTMAP_ERROR); 00045 } 00046 00047 } 00048 /*Codes_SRS_CONSTMAP_17_003: [Otherwise, it shall return a non-NULL handle that can be used in subsequent calls.]*/ 00049 return (CONSTMAP_HANDLE)result; 00050 } 00051 00052 void ConstMap_Destroy(CONSTMAP_HANDLE handle) 00053 { 00054 /*Codes_SRS_CONSTMAP_17_005: [If parameter handle is NULL then ConstMap_Destroy shall take no action.]*/ 00055 if (handle == NULL) 00056 { 00057 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00058 } 00059 else 00060 { 00061 /*Codes_SRS_CONSTMAP_17_049: [ConstMap_Destroy shall decrement the internal reference count of the immutable map.]*/ 00062 if (DEC_REF(CONSTMAP_HANDLE_DATA, handle) == DEC_RETURN_ZERO) 00063 { 00064 /*Codes_SRS_CONSTMAP_17_004: [If the reference count is zero, ConstMap_Destroy shall release all resources associated with the immutable map.]*/ 00065 Map_Destroy(((CONSTMAP_HANDLE_DATA *)handle)->map); 00066 free(handle); 00067 } 00068 00069 } 00070 } 00071 00072 CONSTMAP_HANDLE ConstMap_Clone(CONSTMAP_HANDLE handle) 00073 { 00074 /*Codes_SRS_CONSTMAP_17_038: [ConstMap_Clone returns NULL if parameter handle is NULL.] */ 00075 if (handle == NULL) 00076 { 00077 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00078 } 00079 else 00080 { 00081 /*Codes_SRS_CONSTMAP_17_039: [ConstMap_Clone shall increase the internal reference count of the immutable map indicated by parameter handle]*/ 00082 /*Codes_SRS_CONSTMAP_17_050: [ConstMap_Clone shall return the non-NULL handle. ]*/ 00083 INC_REF(CONSTMAP_HANDLE_DATA, handle); 00084 } 00085 return (handle); 00086 } 00087 00088 static CONSTMAP_RESULT ConstMap_ErrorConvert(MAP_RESULT mapResult) 00089 { 00090 CONSTMAP_RESULT result; 00091 switch (mapResult) 00092 { 00093 case MAP_OK: 00094 result = CONSTMAP_OK; 00095 break; 00096 case MAP_INVALIDARG: 00097 result = CONSTMAP_INVALIDARG; 00098 break; 00099 case MAP_KEYNOTFOUND: 00100 result = CONSTMAP_KEYNOTFOUND; 00101 break; 00102 default: 00103 result = CONSTMAP_ERROR; 00104 break; 00105 } 00106 return result; 00107 } 00108 00109 MAP_HANDLE ConstMap_CloneWriteable(CONSTMAP_HANDLE handle) 00110 { 00111 MAP_HANDLE result = NULL; 00112 if (handle == NULL) 00113 { 00114 /*Codes_SRS_CONSTMAP_17_051: [ConstMap_CloneWriteable returns NULL if parameter handle is NULL. ]*/ 00115 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00116 } 00117 else 00118 { 00119 /*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.]*/ 00120 /*Codes_SRS_CONSTMAP_17_053: [If during cloning, any operation fails, then ConstMap_CloneWriteableap_Clone shall return NULL.]*/ 00121 /*Codes_SRS_CONSTMAP_17_054: [Otherwise, ConstMap_CloneWriteable shall return a non-NULL handle that can be used in subsequent calls.]*/ 00122 result = Map_Clone(((CONSTMAP_HANDLE_DATA *)handle)->map); 00123 } 00124 return result; 00125 } 00126 00127 bool ConstMap_ContainsKey(CONSTMAP_HANDLE handle, const char* key ) 00128 { 00129 bool keyExists = false; 00130 if (handle == NULL) 00131 { 00132 /*Codes_SRS_CONSTMAP_17_024: [If parameter handle or key are NULL then ConstMap_ContainsKey shall return false.]*/ 00133 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00134 } 00135 else 00136 { 00137 if (key == NULL) 00138 { 00139 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00140 } 00141 else 00142 { 00143 /*Codes_SRS_CONSTMAP_17_025: [Otherwise if a key exists then ConstMap_ContainsKey shall return true.]*/ 00144 MAP_RESULT mapResult = Map_ContainsKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key, &keyExists); 00145 if (mapResult != MAP_OK) 00146 { 00147 /*Codes_SRS_CONSTMAP_17_026: [If a key doesn't exist, then ConstMap_ContainsKey shall return false.]*/ 00148 keyExists = false; 00149 LOG_CONSTMAP_ERROR(ConstMap_ErrorConvert(mapResult)); 00150 } 00151 } 00152 } 00153 return keyExists; 00154 } 00155 00156 bool ConstMap_ContainsValue(CONSTMAP_HANDLE handle, const char* value) 00157 { 00158 bool valueExists = false; 00159 if (handle == NULL) 00160 { 00161 /*Codes_SRS_CONSTMAP_17_027: [If parameter handle or value is NULL then ConstMap_ContainsValue shall return false.]*/ 00162 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00163 } 00164 else 00165 { 00166 if (value == NULL) 00167 { 00168 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00169 } 00170 else 00171 { 00172 /*Codes_SRS_CONSTMAP_17_028: [Otherwise, if a pair has its value equal to the parameter value, the ConstMap_ContainsValue shall return true.]*/ 00173 MAP_RESULT mapResult = Map_ContainsValue(((CONSTMAP_HANDLE_DATA *)handle)->map, value, &valueExists); 00174 if (mapResult != MAP_OK) 00175 { 00176 /*Codes_SRS_CONSTMAP_17_029: [Otherwise, if such a does not exist, then ConstMap_ContainsValue shall return false.]*/ 00177 LOG_CONSTMAP_ERROR(ConstMap_ErrorConvert(mapResult)); 00178 } 00179 } 00180 } 00181 return valueExists; 00182 } 00183 00184 const char* ConstMap_GetValue(CONSTMAP_HANDLE handle, const char* key) 00185 { 00186 const char* value = NULL; 00187 00188 if (handle == NULL) 00189 { 00190 /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/ 00191 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00192 } 00193 else 00194 { 00195 if (key == NULL) 00196 { 00197 /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/ 00198 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); 00199 } 00200 else 00201 { 00202 /*Codes_SRS_CONSTMAP_17_041: [If the key is not found, then ConstMap_GetValue returns NULL.]*/ 00203 /*Codes_SRS_CONSTMAP_17_042: [Otherwise, ConstMap_GetValue returns the key's value.]*/ 00204 value = Map_GetValueFromKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key); 00205 } 00206 } 00207 return value; 00208 } 00209 00210 CONSTMAP_RESULT ConstMap_GetInternals(CONSTMAP_HANDLE handle, const char*const** keys, const char*const** values, size_t* count) 00211 { 00212 CONSTMAP_RESULT result; 00213 if (handle == NULL) 00214 { 00215 /*Codes_SRS_CONSTMAP_17_046: [If parameter handle, keys, values or count is NULL then ConstMap_GetInternals shall return CONSTMAP_INVALIDARG.]*/ 00216 result = CONSTMAP_INVALIDARG; 00217 LOG_CONSTMAP_ERROR(result); 00218 } 00219 else 00220 { 00221 /*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.] 00222 *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.] 00223 *Codes_SRS_CONSTMAP_17_045: [ ConstMap_GetInternals shall produce in *count the number of stored keys and values.] 00224 */ 00225 MAP_RESULT mapResult = Map_GetInternals(((CONSTMAP_HANDLE_DATA *)handle)->map, keys, values, count); 00226 result = ConstMap_ErrorConvert(mapResult); 00227 } 00228 return result; 00229 }
Generated on Tue Jul 12 2022 12:43:18 by
