source: azure_iot_hub/trunk/azure_iohub/c-utility/src/constmap.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

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