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