A modelling and serializer library for Microsoft Azure IoTHub client applications
Dependents: sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more
This library implements a serializer library to be used in projects involving Microsoft Azure IoT Hub connectivity. The code is replicated from https://github.com/Azure/azure-iot-sdks
multitree.c@26:7c0e6f86d034, 2017-07-14 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Jul 14 16:38:27 2017 -0700
- Revision:
- 26:7c0e6f86d034
- Parent:
- 21:6d3dea1abd9c
- Child:
- 30:5fabc80edeb1
1.1.19
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 0:1f9b2707ec7d | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 0:1f9b2707ec7d | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 0:1f9b2707ec7d | 3 | |
AzureIoTClient | 0:1f9b2707ec7d | 4 | #include <stdlib.h> |
Azure.IoT Build | 10:c2aee3965a83 | 5 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 0:1f9b2707ec7d | 6 | |
AzureIoTClient | 0:1f9b2707ec7d | 7 | #include "multitree.h" |
AzureIoTClient | 0:1f9b2707ec7d | 8 | #include <string.h> |
Azure.IoT Build | 10:c2aee3965a83 | 9 | #include "azure_c_shared_utility/crt_abstractions.h" |
Azure.IoT Build | 13:16e88f0cfa5f | 10 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 13:16e88f0cfa5f | 11 | #include "azure_c_shared_utility/macro_utils.h" |
AzureIoTClient | 0:1f9b2707ec7d | 12 | |
AzureIoTClient | 0:1f9b2707ec7d | 13 | /*assume a name cannot be longer than 100 characters*/ |
AzureIoTClient | 0:1f9b2707ec7d | 14 | #define INNER_NODE_NAME_SIZE 128 |
AzureIoTClient | 0:1f9b2707ec7d | 15 | |
AzureIoTClient | 0:1f9b2707ec7d | 16 | DEFINE_ENUM_STRINGS(MULTITREE_RESULT, MULTITREE_RESULT_VALUES); |
AzureIoTClient | 0:1f9b2707ec7d | 17 | |
AzureIoTClient | 17:fa1bba4c6053 | 18 | typedef struct MULTITREE_HANDLE_DATA_TAG |
AzureIoTClient | 0:1f9b2707ec7d | 19 | { |
AzureIoTClient | 0:1f9b2707ec7d | 20 | char* name; |
AzureIoTClient | 0:1f9b2707ec7d | 21 | void* value; |
AzureIoTClient | 0:1f9b2707ec7d | 22 | MULTITREE_CLONE_FUNCTION cloneFunction; |
AzureIoTClient | 0:1f9b2707ec7d | 23 | MULTITREE_FREE_FUNCTION freeFunction; |
AzureIoTClient | 0:1f9b2707ec7d | 24 | size_t nChildren; |
AzureIoTClient | 17:fa1bba4c6053 | 25 | struct MULTITREE_HANDLE_DATA_TAG** children; /*an array of nChildren count of MULTITREE_HANDLE_DATA* */ |
AzureIoTClient | 17:fa1bba4c6053 | 26 | }MULTITREE_HANDLE_DATA; |
AzureIoTClient | 0:1f9b2707ec7d | 27 | |
AzureIoTClient | 0:1f9b2707ec7d | 28 | |
AzureIoTClient | 0:1f9b2707ec7d | 29 | MULTITREE_HANDLE MultiTree_Create(MULTITREE_CLONE_FUNCTION cloneFunction, MULTITREE_FREE_FUNCTION freeFunction) |
AzureIoTClient | 0:1f9b2707ec7d | 30 | { |
AzureIoTClient | 17:fa1bba4c6053 | 31 | MULTITREE_HANDLE_DATA* result; |
AzureIoTClient | 0:1f9b2707ec7d | 32 | |
AzureIoTClient | 0:1f9b2707ec7d | 33 | /* Codes_SRS_MULTITREE_99_052:[If any of the arguments passed to MultiTree_Create is NULL, the call shall return NULL.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 34 | if ((cloneFunction == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 35 | (freeFunction == NULL)) |
AzureIoTClient | 0:1f9b2707ec7d | 36 | { |
AzureIoTClient | 11:b1327861f5e0 | 37 | LogError("CloneFunction or FreeFunction is Null."); |
AzureIoTClient | 0:1f9b2707ec7d | 38 | result = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 39 | } |
AzureIoTClient | 0:1f9b2707ec7d | 40 | else |
AzureIoTClient | 0:1f9b2707ec7d | 41 | { |
AzureIoTClient | 0:1f9b2707ec7d | 42 | /*Codes_SRS_MULTITREE_99_005:[ MultiTree_Create creates a new tree.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 43 | /*Codes_SRS_MULTITREE_99_006:[MultiTree_Create returns a non - NULL pointer if the tree has been successfully created.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 44 | /*Codes_SRS_MULTITREE_99_007:[MultiTree_Create returns NULL if the tree has not been successfully created.]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 45 | result = (MULTITREE_HANDLE_DATA*)malloc(sizeof(MULTITREE_HANDLE_DATA)); |
AzureIoTClient | 0:1f9b2707ec7d | 46 | if (result != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 47 | { |
AzureIoTClient | 0:1f9b2707ec7d | 48 | result->name = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 49 | result->value = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 50 | result->cloneFunction = cloneFunction; |
AzureIoTClient | 0:1f9b2707ec7d | 51 | result->freeFunction = freeFunction; |
AzureIoTClient | 0:1f9b2707ec7d | 52 | result->nChildren = 0; |
AzureIoTClient | 0:1f9b2707ec7d | 53 | result->children = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 54 | } |
AzureIoTClient | 0:1f9b2707ec7d | 55 | else |
AzureIoTClient | 0:1f9b2707ec7d | 56 | { |
AzureIoTClient | 11:b1327861f5e0 | 57 | LogError("MultiTree_Create failed because malloc failed"); |
AzureIoTClient | 0:1f9b2707ec7d | 58 | } |
AzureIoTClient | 0:1f9b2707ec7d | 59 | } |
AzureIoTClient | 0:1f9b2707ec7d | 60 | |
AzureIoTClient | 0:1f9b2707ec7d | 61 | return (MULTITREE_HANDLE)result; |
AzureIoTClient | 0:1f9b2707ec7d | 62 | } |
AzureIoTClient | 0:1f9b2707ec7d | 63 | |
AzureIoTClient | 0:1f9b2707ec7d | 64 | |
AzureIoTClient | 0:1f9b2707ec7d | 65 | /*return NULL if a child with the name "name" doesn't exists*/ |
AzureIoTClient | 0:1f9b2707ec7d | 66 | /*returns a pointer to the existing child (if any)*/ |
AzureIoTClient | 17:fa1bba4c6053 | 67 | static MULTITREE_HANDLE_DATA* getChildByName(MULTITREE_HANDLE_DATA* node, const char* name) |
AzureIoTClient | 0:1f9b2707ec7d | 68 | { |
AzureIoTClient | 17:fa1bba4c6053 | 69 | MULTITREE_HANDLE_DATA* result = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 70 | size_t i; |
AzureIoTClient | 0:1f9b2707ec7d | 71 | for (i = 0; i < node->nChildren; i++) |
AzureIoTClient | 0:1f9b2707ec7d | 72 | { |
AzureIoTClient | 0:1f9b2707ec7d | 73 | if (strcmp(node->children[i]->name, name) == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 74 | { |
AzureIoTClient | 0:1f9b2707ec7d | 75 | result = node->children[i]; |
AzureIoTClient | 0:1f9b2707ec7d | 76 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 77 | } |
AzureIoTClient | 0:1f9b2707ec7d | 78 | } |
AzureIoTClient | 0:1f9b2707ec7d | 79 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 80 | } |
AzureIoTClient | 0:1f9b2707ec7d | 81 | |
AzureIoTClient | 0:1f9b2707ec7d | 82 | /*helper function to create a child immediately under this node*/ |
AzureIoTClient | 0:1f9b2707ec7d | 83 | /*return 0 if it created it, any other number is error*/ |
AzureIoTClient | 0:1f9b2707ec7d | 84 | |
AzureIoTClient | 0:1f9b2707ec7d | 85 | typedef enum CREATELEAF_RESULT_TAG |
AzureIoTClient | 0:1f9b2707ec7d | 86 | { |
AzureIoTClient | 0:1f9b2707ec7d | 87 | CREATELEAF_OK, |
AzureIoTClient | 0:1f9b2707ec7d | 88 | CREATELEAF_ALREADY_EXISTS, |
AzureIoTClient | 0:1f9b2707ec7d | 89 | CREATELEAF_EMPTY_NAME, |
AzureIoTClient | 0:1f9b2707ec7d | 90 | CREATELEAF_ERROR, |
AzureIoTClient | 0:1f9b2707ec7d | 91 | CREATELEAF_RESULT_COUNT // Used to track the number of elements in the enum |
AzureIoTClient | 0:1f9b2707ec7d | 92 | // Do not remove, or add new enum values below this one |
AzureIoTClient | 0:1f9b2707ec7d | 93 | }CREATELEAF_RESULT; |
AzureIoTClient | 0:1f9b2707ec7d | 94 | |
AzureIoTClient | 0:1f9b2707ec7d | 95 | static const char* CreateLeaf_ResultAsString[CREATELEAF_RESULT_COUNT] = |
AzureIoTClient | 0:1f9b2707ec7d | 96 | { |
Azure.IoT Build | 13:16e88f0cfa5f | 97 | TOSTRING(CREATELEAF_OK), |
Azure.IoT Build | 13:16e88f0cfa5f | 98 | TOSTRING(CREATELEAF_ALREADY_EXISTS), |
Azure.IoT Build | 13:16e88f0cfa5f | 99 | TOSTRING(CREATELEAF_EMPTY_NAME), |
Azure.IoT Build | 13:16e88f0cfa5f | 100 | TOSTRING(CREATELEAF_ERROR) |
AzureIoTClient | 0:1f9b2707ec7d | 101 | }; |
AzureIoTClient | 0:1f9b2707ec7d | 102 | |
AzureIoTClient | 0:1f9b2707ec7d | 103 | /*name cannot be empty, value can be empty or NULL*/ |
AzureIoTClient | 0:1f9b2707ec7d | 104 | #ifdef _MSC_VER |
AzureIoTClient | 0:1f9b2707ec7d | 105 | #pragma warning(disable: 4701) /* potentially uninitialized local variable 'result' used */ /* the scanner cannot track linked "newNode" and "result" therefore the warning*/ |
AzureIoTClient | 0:1f9b2707ec7d | 106 | #endif |
AzureIoTClient | 17:fa1bba4c6053 | 107 | static CREATELEAF_RESULT createLeaf(MULTITREE_HANDLE_DATA* node, const char*name, const char*value, MULTITREE_HANDLE_DATA** childNode) |
AzureIoTClient | 0:1f9b2707ec7d | 108 | { |
AzureIoTClient | 0:1f9b2707ec7d | 109 | CREATELEAF_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 110 | /*can only create it if it doesn't exist*/ |
AzureIoTClient | 0:1f9b2707ec7d | 111 | if (strlen(name) == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 112 | { |
AzureIoTClient | 0:1f9b2707ec7d | 113 | /*Codes_SRS_MULTITREE_99_024:[ if a child name is empty (such as in "/child1//child12"), MULTITREE_EMPTY_CHILD_NAME shall be returned.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 114 | result = CREATELEAF_EMPTY_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 115 | LogError("(result = %s)", CreateLeaf_ResultAsString[result]); |
AzureIoTClient | 0:1f9b2707ec7d | 116 | } |
AzureIoTClient | 0:1f9b2707ec7d | 117 | else if (getChildByName(node, name) != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 118 | { |
AzureIoTClient | 0:1f9b2707ec7d | 119 | result = CREATELEAF_ALREADY_EXISTS; |
AzureIoTClient | 11:b1327861f5e0 | 120 | LogError("(result = %s)", CreateLeaf_ResultAsString[result]); |
AzureIoTClient | 0:1f9b2707ec7d | 121 | } |
AzureIoTClient | 0:1f9b2707ec7d | 122 | else |
AzureIoTClient | 0:1f9b2707ec7d | 123 | { |
AzureIoTClient | 17:fa1bba4c6053 | 124 | MULTITREE_HANDLE_DATA* newNode = (MULTITREE_HANDLE_DATA*)malloc(sizeof(MULTITREE_HANDLE_DATA)); |
AzureIoTClient | 0:1f9b2707ec7d | 125 | if (newNode == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 126 | { |
AzureIoTClient | 0:1f9b2707ec7d | 127 | result = CREATELEAF_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 128 | LogError("(result = %s)", CreateLeaf_ResultAsString[result]); |
AzureIoTClient | 0:1f9b2707ec7d | 129 | } |
AzureIoTClient | 0:1f9b2707ec7d | 130 | else |
AzureIoTClient | 0:1f9b2707ec7d | 131 | { |
AzureIoTClient | 0:1f9b2707ec7d | 132 | newNode->nChildren = 0; |
AzureIoTClient | 0:1f9b2707ec7d | 133 | newNode->children = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 134 | if (mallocAndStrcpy_s(&(newNode->name), name) != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 135 | { |
AzureIoTClient | 0:1f9b2707ec7d | 136 | /*not nice*/ |
AzureIoTClient | 0:1f9b2707ec7d | 137 | free(newNode); |
AzureIoTClient | 0:1f9b2707ec7d | 138 | newNode = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 139 | result = CREATELEAF_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 140 | LogError("(result = %s)", CreateLeaf_ResultAsString[result]); |
AzureIoTClient | 0:1f9b2707ec7d | 141 | } |
AzureIoTClient | 0:1f9b2707ec7d | 142 | else |
AzureIoTClient | 0:1f9b2707ec7d | 143 | { |
AzureIoTClient | 0:1f9b2707ec7d | 144 | newNode->cloneFunction = node->cloneFunction; |
AzureIoTClient | 0:1f9b2707ec7d | 145 | newNode->freeFunction = node->freeFunction; |
AzureIoTClient | 0:1f9b2707ec7d | 146 | |
AzureIoTClient | 0:1f9b2707ec7d | 147 | if (value == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 148 | { |
AzureIoTClient | 0:1f9b2707ec7d | 149 | newNode->value = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 150 | } |
AzureIoTClient | 0:1f9b2707ec7d | 151 | else if (node->cloneFunction(&(newNode->value), value) != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 152 | { |
AzureIoTClient | 0:1f9b2707ec7d | 153 | free(newNode->name); |
AzureIoTClient | 0:1f9b2707ec7d | 154 | newNode->name = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 155 | free(newNode); |
AzureIoTClient | 0:1f9b2707ec7d | 156 | newNode = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 157 | result = CREATELEAF_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 158 | LogError("(result = %s)", CreateLeaf_ResultAsString[result]); |
AzureIoTClient | 0:1f9b2707ec7d | 159 | } |
AzureIoTClient | 0:1f9b2707ec7d | 160 | else |
AzureIoTClient | 0:1f9b2707ec7d | 161 | { |
AzureIoTClient | 0:1f9b2707ec7d | 162 | /*all is fine until now*/ |
AzureIoTClient | 0:1f9b2707ec7d | 163 | } |
AzureIoTClient | 0:1f9b2707ec7d | 164 | } |
AzureIoTClient | 0:1f9b2707ec7d | 165 | |
AzureIoTClient | 0:1f9b2707ec7d | 166 | |
AzureIoTClient | 0:1f9b2707ec7d | 167 | if (newNode!=NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 168 | { |
AzureIoTClient | 0:1f9b2707ec7d | 169 | /*allocate space in the father node*/ |
AzureIoTClient | 17:fa1bba4c6053 | 170 | MULTITREE_HANDLE_DATA** newChildren = (MULTITREE_HANDLE_DATA**)realloc(node->children, (node->nChildren + 1)*sizeof(MULTITREE_HANDLE_DATA*)); |
AzureIoTClient | 0:1f9b2707ec7d | 171 | if (newChildren == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 172 | { |
AzureIoTClient | 0:1f9b2707ec7d | 173 | /*no space for the new node*/ |
AzureIoTClient | 0:1f9b2707ec7d | 174 | newNode->value = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 175 | free(newNode->name); |
AzureIoTClient | 0:1f9b2707ec7d | 176 | newNode->name = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 177 | free(newNode); |
AzureIoTClient | 0:1f9b2707ec7d | 178 | newNode = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 179 | result = CREATELEAF_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 180 | LogError("(result = %s)", CreateLeaf_ResultAsString[result]); |
AzureIoTClient | 0:1f9b2707ec7d | 181 | } |
AzureIoTClient | 0:1f9b2707ec7d | 182 | else |
AzureIoTClient | 0:1f9b2707ec7d | 183 | { |
AzureIoTClient | 0:1f9b2707ec7d | 184 | node->children = newChildren; |
AzureIoTClient | 0:1f9b2707ec7d | 185 | node->children[node->nChildren] = newNode; |
AzureIoTClient | 0:1f9b2707ec7d | 186 | node->nChildren++; |
AzureIoTClient | 0:1f9b2707ec7d | 187 | if (childNode != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 188 | { |
AzureIoTClient | 0:1f9b2707ec7d | 189 | *childNode = newNode; |
AzureIoTClient | 0:1f9b2707ec7d | 190 | } |
AzureIoTClient | 0:1f9b2707ec7d | 191 | result = CREATELEAF_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 192 | } |
AzureIoTClient | 0:1f9b2707ec7d | 193 | } |
AzureIoTClient | 0:1f9b2707ec7d | 194 | } |
AzureIoTClient | 0:1f9b2707ec7d | 195 | } |
AzureIoTClient | 0:1f9b2707ec7d | 196 | |
AzureIoTClient | 0:1f9b2707ec7d | 197 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 198 | #ifdef _MSC_VER |
AzureIoTClient | 0:1f9b2707ec7d | 199 | #pragma warning(default: 4701) /* potentially uninitialized local variable 'result' used */ /* the scanner cannot track linked "newNode" and "result" therefore the warning*/ |
AzureIoTClient | 0:1f9b2707ec7d | 200 | #endif |
AzureIoTClient | 0:1f9b2707ec7d | 201 | } |
AzureIoTClient | 0:1f9b2707ec7d | 202 | |
AzureIoTClient | 0:1f9b2707ec7d | 203 | MULTITREE_RESULT MultiTree_AddLeaf(MULTITREE_HANDLE treeHandle, const char* destinationPath, const void* value) |
AzureIoTClient | 0:1f9b2707ec7d | 204 | { |
AzureIoTClient | 0:1f9b2707ec7d | 205 | /*codes_SRS_MULTITREE_99_018:[ If the treeHandle parameter is NULL, MULTITREE_INVALID_ARG shall be returned.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 206 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 207 | if (treeHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 208 | { |
AzureIoTClient | 0:1f9b2707ec7d | 209 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 210 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 211 | } |
AzureIoTClient | 0:1f9b2707ec7d | 212 | /*Codes_SRS_MULTITREE_99_019:[ If parameter destinationPath is NULL, MULTITREE_INVALID_ARG shall be returned.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 213 | else if (destinationPath == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 214 | { |
AzureIoTClient | 0:1f9b2707ec7d | 215 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 216 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 217 | } |
AzureIoTClient | 0:1f9b2707ec7d | 218 | /*Codes_SRS_MULTITREE_99_020:[ If parameter value is NULL, MULTITREE_INVALID_ARG shall be returned.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 219 | else if (value == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 220 | { |
AzureIoTClient | 0:1f9b2707ec7d | 221 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 222 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 223 | } |
AzureIoTClient | 0:1f9b2707ec7d | 224 | /*Codes_SRS_MULTITREE_99_050:[ If destinationPath a string with zero characters, MULTITREE_INVALID_ARG shall be returned.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 225 | else if (strlen(destinationPath) == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 226 | { |
AzureIoTClient | 0:1f9b2707ec7d | 227 | result = MULTITREE_EMPTY_CHILD_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 228 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 229 | } |
AzureIoTClient | 0:1f9b2707ec7d | 230 | else |
AzureIoTClient | 0:1f9b2707ec7d | 231 | { |
AzureIoTClient | 0:1f9b2707ec7d | 232 | /*break the path into components*/ |
AzureIoTClient | 0:1f9b2707ec7d | 233 | /*find the first child name*/ |
AzureIoTClient | 17:fa1bba4c6053 | 234 | MULTITREE_HANDLE_DATA * node = (MULTITREE_HANDLE_DATA *)treeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 235 | char * whereIsDelimiter; |
AzureIoTClient | 0:1f9b2707ec7d | 236 | /*if first character is / then skip it*/ |
AzureIoTClient | 0:1f9b2707ec7d | 237 | /*Codes_SRS_MULTITREE_99_014:[DestinationPath is a string in the following format: /child1/child12 or child1/child12] */ |
AzureIoTClient | 0:1f9b2707ec7d | 238 | if (destinationPath[0] == '/') |
AzureIoTClient | 0:1f9b2707ec7d | 239 | { |
AzureIoTClient | 0:1f9b2707ec7d | 240 | destinationPath++; |
AzureIoTClient | 0:1f9b2707ec7d | 241 | } |
AzureIoTClient | 0:1f9b2707ec7d | 242 | /*if there's just a string, it needs to be created here*/ |
AzureIoTClient | 0:1f9b2707ec7d | 243 | whereIsDelimiter = (char*)strchr(destinationPath, '/'); |
AzureIoTClient | 0:1f9b2707ec7d | 244 | if (whereIsDelimiter == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 245 | { |
AzureIoTClient | 0:1f9b2707ec7d | 246 | /*Codes_SRS_MULTITREE_99_017:[ Subsequent names designate hierarchical children in the tree. The last child designates the child that will receive the value.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 247 | CREATELEAF_RESULT res = createLeaf(node, destinationPath, (const char*)value, NULL); |
AzureIoTClient | 0:1f9b2707ec7d | 248 | switch (res) |
AzureIoTClient | 0:1f9b2707ec7d | 249 | { |
AzureIoTClient | 0:1f9b2707ec7d | 250 | default: |
AzureIoTClient | 0:1f9b2707ec7d | 251 | { |
AzureIoTClient | 0:1f9b2707ec7d | 252 | /*Codes_SRS_MULTITREE_99_025:[The function shall return MULTITREE_ERROR to indicate any other error not specified here.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 253 | result = MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 254 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 255 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 256 | } |
AzureIoTClient | 0:1f9b2707ec7d | 257 | case CREATELEAF_ALREADY_EXISTS: |
AzureIoTClient | 0:1f9b2707ec7d | 258 | { |
AzureIoTClient | 0:1f9b2707ec7d | 259 | /*Codes_SRS_MULTITREE_99_021:[ If the node already has a value assigned to it, MULTITREE_ALREADY_HAS_A_VALUE shall be returned and the existing value shall not be changed.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 260 | result = MULTITREE_ALREADY_HAS_A_VALUE; |
AzureIoTClient | 11:b1327861f5e0 | 261 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 262 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 263 | } |
AzureIoTClient | 0:1f9b2707ec7d | 264 | case CREATELEAF_OK: |
AzureIoTClient | 0:1f9b2707ec7d | 265 | { |
AzureIoTClient | 0:1f9b2707ec7d | 266 | /*Codes_SRS_MULTITREE_99_034:[ The function returns MULTITREE_OK when data has been stored in the tree.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 267 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 268 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 269 | } |
AzureIoTClient | 0:1f9b2707ec7d | 270 | case CREATELEAF_EMPTY_NAME: |
AzureIoTClient | 0:1f9b2707ec7d | 271 | { |
AzureIoTClient | 0:1f9b2707ec7d | 272 | /*Codes_SRS_MULTITREE_99_024:[ if a child name is empty (such as in "/child1//child12"), MULTITREE_EMPTY_CHILD_NAME shall be returned.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 273 | result = MULTITREE_EMPTY_CHILD_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 274 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 275 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 276 | } |
AzureIoTClient | 0:1f9b2707ec7d | 277 | } |
AzureIoTClient | 0:1f9b2707ec7d | 278 | } |
AzureIoTClient | 0:1f9b2707ec7d | 279 | else |
AzureIoTClient | 0:1f9b2707ec7d | 280 | { |
AzureIoTClient | 0:1f9b2707ec7d | 281 | /*if there's more or 1 delimiter in the path... */ |
AzureIoTClient | 0:1f9b2707ec7d | 282 | /*Codes_SRS_MULTITREE_99_017:[ Subsequent names designate hierarchical children in the tree. The last child designates the child that will receive the value.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 283 | char firstInnerNodeName[INNER_NODE_NAME_SIZE]; |
AzureIoTClient | 0:1f9b2707ec7d | 284 | if (strncpy_s(firstInnerNodeName, INNER_NODE_NAME_SIZE, destinationPath, whereIsDelimiter - destinationPath) != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 285 | { |
AzureIoTClient | 0:1f9b2707ec7d | 286 | /*Codes_SRS_MULTITREE_99_025:[ The function shall return MULTITREE_ERROR to indicate any other error not specified here.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 287 | result = MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 288 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 289 | } |
AzureIoTClient | 0:1f9b2707ec7d | 290 | else |
AzureIoTClient | 0:1f9b2707ec7d | 291 | { |
AzureIoTClient | 17:fa1bba4c6053 | 292 | MULTITREE_HANDLE_DATA *child = getChildByName(node, firstInnerNodeName); |
AzureIoTClient | 0:1f9b2707ec7d | 293 | if (child == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 294 | { |
AzureIoTClient | 0:1f9b2707ec7d | 295 | /*Codes_SRS_MULTITREE_99_022:[ If a child along the path does not exist, it shall be created.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 296 | /*Codes_SRS_MULTITREE_99_023:[ The newly created children along the path shall have a NULL value by default.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 297 | CREATELEAF_RESULT res = createLeaf(node, firstInnerNodeName, NULL, NULL); |
AzureIoTClient | 0:1f9b2707ec7d | 298 | switch (res) |
AzureIoTClient | 0:1f9b2707ec7d | 299 | { |
AzureIoTClient | 0:1f9b2707ec7d | 300 | default: |
AzureIoTClient | 0:1f9b2707ec7d | 301 | { |
AzureIoTClient | 0:1f9b2707ec7d | 302 | result = MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 303 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 304 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 305 | } |
AzureIoTClient | 0:1f9b2707ec7d | 306 | case(CREATELEAF_EMPTY_NAME): |
AzureIoTClient | 0:1f9b2707ec7d | 307 | { |
AzureIoTClient | 0:1f9b2707ec7d | 308 | /*Codes_SRS_MULTITREE_99_024:[ if a child name is empty (such as in "/child1//child12"), MULTITREE_EMPTY_CHILD_NAME shall be returned.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 309 | result = MULTITREE_EMPTY_CHILD_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 310 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 311 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 312 | } |
AzureIoTClient | 0:1f9b2707ec7d | 313 | case(CREATELEAF_OK): |
AzureIoTClient | 0:1f9b2707ec7d | 314 | { |
AzureIoTClient | 17:fa1bba4c6053 | 315 | MULTITREE_HANDLE_DATA *createdChild = getChildByName(node, firstInnerNodeName); |
AzureIoTClient | 0:1f9b2707ec7d | 316 | result = MultiTree_AddLeaf(createdChild, whereIsDelimiter, value); |
AzureIoTClient | 0:1f9b2707ec7d | 317 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 318 | } |
AzureIoTClient | 0:1f9b2707ec7d | 319 | }; |
AzureIoTClient | 0:1f9b2707ec7d | 320 | } |
AzureIoTClient | 0:1f9b2707ec7d | 321 | else |
AzureIoTClient | 0:1f9b2707ec7d | 322 | { |
AzureIoTClient | 0:1f9b2707ec7d | 323 | result = MultiTree_AddLeaf(child, whereIsDelimiter, value); |
AzureIoTClient | 0:1f9b2707ec7d | 324 | } |
AzureIoTClient | 0:1f9b2707ec7d | 325 | } |
AzureIoTClient | 0:1f9b2707ec7d | 326 | } |
AzureIoTClient | 0:1f9b2707ec7d | 327 | } |
AzureIoTClient | 0:1f9b2707ec7d | 328 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 329 | } |
AzureIoTClient | 0:1f9b2707ec7d | 330 | |
AzureIoTClient | 0:1f9b2707ec7d | 331 | /* Codes_SRS_MULTITREE_99_053:[ MultiTree_AddChild shall add a new node with the name childName to the multi tree node identified by treeHandle] */ |
AzureIoTClient | 0:1f9b2707ec7d | 332 | MULTITREE_RESULT MultiTree_AddChild(MULTITREE_HANDLE treeHandle, const char* childName, MULTITREE_HANDLE* childHandle) |
AzureIoTClient | 0:1f9b2707ec7d | 333 | { |
AzureIoTClient | 0:1f9b2707ec7d | 334 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 335 | /* Codes_SRS_MULTITREE_99_055:[ If any argument is NULL, MultiTree_AddChild shall return MULTITREE_INVALID_ARG.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 336 | if ((treeHandle == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 337 | (childName == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 338 | (childHandle == NULL)) |
AzureIoTClient | 0:1f9b2707ec7d | 339 | { |
AzureIoTClient | 0:1f9b2707ec7d | 340 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 341 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 342 | } |
AzureIoTClient | 0:1f9b2707ec7d | 343 | else |
AzureIoTClient | 0:1f9b2707ec7d | 344 | { |
AzureIoTClient | 17:fa1bba4c6053 | 345 | MULTITREE_HANDLE_DATA* childNode; |
AzureIoTClient | 0:1f9b2707ec7d | 346 | |
AzureIoTClient | 0:1f9b2707ec7d | 347 | /* Codes_SRS_MULTITREE_99_060:[ The value associated with the new node shall be NULL.] */ |
AzureIoTClient | 17:fa1bba4c6053 | 348 | CREATELEAF_RESULT res = createLeaf((MULTITREE_HANDLE_DATA*)treeHandle, childName, NULL, &childNode); |
AzureIoTClient | 0:1f9b2707ec7d | 349 | switch (res) |
AzureIoTClient | 0:1f9b2707ec7d | 350 | { |
AzureIoTClient | 0:1f9b2707ec7d | 351 | default: |
AzureIoTClient | 0:1f9b2707ec7d | 352 | { |
AzureIoTClient | 0:1f9b2707ec7d | 353 | result = MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 354 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 355 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 356 | } |
AzureIoTClient | 0:1f9b2707ec7d | 357 | case CREATELEAF_ALREADY_EXISTS: |
AzureIoTClient | 0:1f9b2707ec7d | 358 | { |
AzureIoTClient | 0:1f9b2707ec7d | 359 | /* Codes_SRS_MULTITREE_99_061:[ If a child node with the same name already exists, MultiTree_AddChild shall return MULTITREE_ALREADY_HAS_A_VALUE.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 360 | result = MULTITREE_ALREADY_HAS_A_VALUE; |
AzureIoTClient | 11:b1327861f5e0 | 361 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 362 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 363 | } |
AzureIoTClient | 0:1f9b2707ec7d | 364 | case CREATELEAF_OK: |
AzureIoTClient | 0:1f9b2707ec7d | 365 | { |
AzureIoTClient | 0:1f9b2707ec7d | 366 | /* Codes_SRS_MULTITREE_99_062:[ The new node handle shall be returned in the childHandle argument.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 367 | *childHandle = childNode; |
AzureIoTClient | 0:1f9b2707ec7d | 368 | |
AzureIoTClient | 0:1f9b2707ec7d | 369 | /* Codes_SRS_MULTITREE_99_054:[ On success, MultiTree_AddChild shall return MULTITREE_OK.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 370 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 371 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 372 | } |
AzureIoTClient | 0:1f9b2707ec7d | 373 | case CREATELEAF_EMPTY_NAME: |
AzureIoTClient | 0:1f9b2707ec7d | 374 | { |
AzureIoTClient | 0:1f9b2707ec7d | 375 | /* Tests_SRS_MULTITREE_99_066:[ If the childName argument is an empty string, MultiTree_AddChild shall return MULTITREE_EMPTY_CHILD_NAME.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 376 | result = MULTITREE_EMPTY_CHILD_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 377 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 378 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 379 | } |
AzureIoTClient | 0:1f9b2707ec7d | 380 | } |
AzureIoTClient | 0:1f9b2707ec7d | 381 | } |
AzureIoTClient | 0:1f9b2707ec7d | 382 | |
AzureIoTClient | 0:1f9b2707ec7d | 383 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 384 | } |
AzureIoTClient | 0:1f9b2707ec7d | 385 | |
AzureIoTClient | 0:1f9b2707ec7d | 386 | MULTITREE_RESULT MultiTree_GetChildCount(MULTITREE_HANDLE treeHandle, size_t* count) |
AzureIoTClient | 0:1f9b2707ec7d | 387 | { |
AzureIoTClient | 0:1f9b2707ec7d | 388 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 389 | /*Codes_SRS_MULTITREE_99_027:[If treeHandle is NULL, the function returns MULTITREE_INVALID_ARG.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 390 | if (treeHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 391 | { |
AzureIoTClient | 0:1f9b2707ec7d | 392 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 393 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 394 | } |
AzureIoTClient | 0:1f9b2707ec7d | 395 | /*Codes_SRS_MULTITREE_99_028:[ If parameter count is NULL, the function returns MULTITREE_INVALID_ARG.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 396 | else if (count == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 397 | { |
AzureIoTClient | 0:1f9b2707ec7d | 398 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 399 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 400 | } |
AzureIoTClient | 0:1f9b2707ec7d | 401 | else |
AzureIoTClient | 0:1f9b2707ec7d | 402 | { |
AzureIoTClient | 0:1f9b2707ec7d | 403 | /*Codes_SRS_MULTITREE_99_029:[ This function writes in *count the number of direct children for a tree node specified by the parameter treeHandle]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 404 | *count = ((MULTITREE_HANDLE_DATA*)treeHandle)->nChildren; |
AzureIoTClient | 0:1f9b2707ec7d | 405 | /*Codes_SRS_MULTITREE_99_035:[ The function shall return MULTITREE_OK when *count contains the number of children of the node pointed to be parameter treeHandle.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 406 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 407 | } |
AzureIoTClient | 0:1f9b2707ec7d | 408 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 409 | } |
AzureIoTClient | 0:1f9b2707ec7d | 410 | |
AzureIoTClient | 0:1f9b2707ec7d | 411 | MULTITREE_RESULT MultiTree_GetChild(MULTITREE_HANDLE treeHandle, size_t index, MULTITREE_HANDLE *childHandle) |
AzureIoTClient | 0:1f9b2707ec7d | 412 | { |
AzureIoTClient | 0:1f9b2707ec7d | 413 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 414 | /*Codes_SRS_MULTITREE_99_031:[ If parameter treeHandle is NULL, the function returns MULTITREE_INVALID_ARG.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 415 | if (treeHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 416 | { |
AzureIoTClient | 0:1f9b2707ec7d | 417 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 418 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 419 | } |
AzureIoTClient | 0:1f9b2707ec7d | 420 | /*Codes_SRS_MULTITREE_99_033:[ If parameter childHandle is NULL, the function shall return MULTITREE_INVALID_ARG.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 421 | else if (childHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 422 | { |
AzureIoTClient | 0:1f9b2707ec7d | 423 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 424 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 425 | } |
AzureIoTClient | 0:1f9b2707ec7d | 426 | else |
AzureIoTClient | 0:1f9b2707ec7d | 427 | { |
AzureIoTClient | 17:fa1bba4c6053 | 428 | MULTITREE_HANDLE_DATA * node = (MULTITREE_HANDLE_DATA *)treeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 429 | /*Codes_SRS_MULTITREE_99_032:[If parameter index is out of range, the function shall return MULTITREE_OUT_OF_RANGE_INDEX]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 430 | if (node->nChildren <= index) |
AzureIoTClient | 0:1f9b2707ec7d | 431 | { |
AzureIoTClient | 0:1f9b2707ec7d | 432 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 433 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 434 | } |
AzureIoTClient | 0:1f9b2707ec7d | 435 | else |
AzureIoTClient | 0:1f9b2707ec7d | 436 | { |
AzureIoTClient | 0:1f9b2707ec7d | 437 | /*Codes_SRS_MULTITREE_99_030:[ This function writes in *childHandle parameter the "index"th child of the node pointed to by parameter treeHandle]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 438 | /*Codes_SRS_MULTITREE_99_035:[ The function returns MULTITREE_OK when *childHandle contains a handle to the "index"th child of the tree designated by parameter treeHandle.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 439 | *childHandle = node->children[index]; |
AzureIoTClient | 0:1f9b2707ec7d | 440 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 441 | } |
AzureIoTClient | 0:1f9b2707ec7d | 442 | } |
AzureIoTClient | 0:1f9b2707ec7d | 443 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 444 | } |
AzureIoTClient | 0:1f9b2707ec7d | 445 | |
AzureIoTClient | 0:1f9b2707ec7d | 446 | MULTITREE_RESULT MultiTree_GetName(MULTITREE_HANDLE treeHandle, STRING_HANDLE destination) |
AzureIoTClient | 0:1f9b2707ec7d | 447 | { |
AzureIoTClient | 0:1f9b2707ec7d | 448 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 449 | /*Codes_SRS_MULTITREE_99_037:[ If treeHandle is NULL, the function shall return MULTITREE_INVALID_ARG.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 450 | if (treeHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 451 | { |
AzureIoTClient | 0:1f9b2707ec7d | 452 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 453 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 454 | } |
AzureIoTClient | 0:1f9b2707ec7d | 455 | /*Codes_SRS_MULTITREE_99_038:[If destination is NULL, the function shall return MULTITREE_INVALID_ARG.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 456 | else if (destination == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 457 | { |
AzureIoTClient | 0:1f9b2707ec7d | 458 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 459 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 460 | } |
AzureIoTClient | 0:1f9b2707ec7d | 461 | else |
AzureIoTClient | 0:1f9b2707ec7d | 462 | { |
AzureIoTClient | 17:fa1bba4c6053 | 463 | MULTITREE_HANDLE_DATA *node = (MULTITREE_HANDLE_DATA*)treeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 464 | /*Codes_SRS_MULTITREE_99_051:[ The function returns MULTITREE_EMPTY_CHILD_NAME when used with the root of the tree.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 465 | if (node->name == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 466 | { |
AzureIoTClient | 0:1f9b2707ec7d | 467 | result = MULTITREE_EMPTY_CHILD_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 468 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 469 | } |
AzureIoTClient | 0:1f9b2707ec7d | 470 | /*Codes_SRS_MULTITREE_99_036:[ This function fills the buffer pointed to by parameter destination with the name of the root node of the tree designated by parameter treeHandle.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 471 | else if (STRING_concat(destination, node->name)!=0) |
AzureIoTClient | 0:1f9b2707ec7d | 472 | { |
AzureIoTClient | 0:1f9b2707ec7d | 473 | /*Codes_SRS_MULTITREE_99_040:[ The function returns MULTITREE_ERROR to indicate any other error.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 474 | result = MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 475 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 476 | } |
AzureIoTClient | 0:1f9b2707ec7d | 477 | else |
AzureIoTClient | 0:1f9b2707ec7d | 478 | { |
AzureIoTClient | 0:1f9b2707ec7d | 479 | /*Codes_SRS_MULTITREE_99_039:[ The function returns MULTITREE_OK when destination contains the name of the root node of the tree designated by treeHandle parameter.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 480 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 481 | } |
AzureIoTClient | 0:1f9b2707ec7d | 482 | } |
AzureIoTClient | 0:1f9b2707ec7d | 483 | |
AzureIoTClient | 0:1f9b2707ec7d | 484 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 485 | } |
AzureIoTClient | 0:1f9b2707ec7d | 486 | |
AzureIoTClient | 0:1f9b2707ec7d | 487 | /* Codes_SRS_MULTITREE_99_063:[ MultiTree_GetChildByName shall retrieve the handle of the child node childName from the treeNode node.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 488 | MULTITREE_RESULT MultiTree_GetChildByName(MULTITREE_HANDLE treeHandle, const char* childName, MULTITREE_HANDLE *childHandle) |
AzureIoTClient | 0:1f9b2707ec7d | 489 | { |
AzureIoTClient | 0:1f9b2707ec7d | 490 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 491 | |
AzureIoTClient | 0:1f9b2707ec7d | 492 | /* Codes_SRS_MULTITREE_99_065:[ If any argument is NULL, MultiTree_GetChildByName shall return MULTITREE_INVALID_ARG.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 493 | if ((treeHandle == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 494 | (childHandle == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 495 | (childName == NULL)) |
AzureIoTClient | 0:1f9b2707ec7d | 496 | { |
AzureIoTClient | 0:1f9b2707ec7d | 497 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 498 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 499 | } |
AzureIoTClient | 0:1f9b2707ec7d | 500 | else |
AzureIoTClient | 0:1f9b2707ec7d | 501 | { |
AzureIoTClient | 17:fa1bba4c6053 | 502 | MULTITREE_HANDLE_DATA * node = (MULTITREE_HANDLE_DATA *)treeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 503 | size_t i; |
AzureIoTClient | 0:1f9b2707ec7d | 504 | |
AzureIoTClient | 0:1f9b2707ec7d | 505 | for (i = 0; i < node->nChildren; i++) |
AzureIoTClient | 0:1f9b2707ec7d | 506 | { |
AzureIoTClient | 0:1f9b2707ec7d | 507 | if (strcmp(node->children[i]->name, childName) == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 508 | { |
AzureIoTClient | 0:1f9b2707ec7d | 509 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 510 | } |
AzureIoTClient | 0:1f9b2707ec7d | 511 | } |
AzureIoTClient | 0:1f9b2707ec7d | 512 | |
AzureIoTClient | 0:1f9b2707ec7d | 513 | if (i == node->nChildren) |
AzureIoTClient | 0:1f9b2707ec7d | 514 | { |
AzureIoTClient | 0:1f9b2707ec7d | 515 | /* Codes_SRS_MULTITREE_99_068:[ If the specified child is not found, MultiTree_GetChildByName shall return MULTITREE_CHILD_NOT_FOUND.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 516 | result = MULTITREE_CHILD_NOT_FOUND; |
AzureIoTClient | 11:b1327861f5e0 | 517 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 518 | } |
AzureIoTClient | 0:1f9b2707ec7d | 519 | else |
AzureIoTClient | 0:1f9b2707ec7d | 520 | { |
AzureIoTClient | 0:1f9b2707ec7d | 521 | /* Codes_SRS_MULTITREE_99_067:[ The child node handle shall be returned in the childHandle argument.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 522 | *childHandle = node->children[i]; |
AzureIoTClient | 0:1f9b2707ec7d | 523 | |
AzureIoTClient | 0:1f9b2707ec7d | 524 | /* Codes_SRS_MULTITREE_99_064:[ On success, MultiTree_GetChildByName shall return MULTITREE_OK.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 525 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 526 | } |
AzureIoTClient | 0:1f9b2707ec7d | 527 | } |
AzureIoTClient | 0:1f9b2707ec7d | 528 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 529 | } |
AzureIoTClient | 0:1f9b2707ec7d | 530 | |
AzureIoTClient | 0:1f9b2707ec7d | 531 | MULTITREE_RESULT MultiTree_GetValue(MULTITREE_HANDLE treeHandle, const void** destination) |
AzureIoTClient | 0:1f9b2707ec7d | 532 | { |
AzureIoTClient | 0:1f9b2707ec7d | 533 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 534 | /*Codes_SRS_MULTITREE_99_042:[If treeHandle is NULL, the function shall return MULTITREE_INVALID_ARG.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 535 | if (treeHandle == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 536 | { |
AzureIoTClient | 0:1f9b2707ec7d | 537 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 538 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 539 | } |
AzureIoTClient | 0:1f9b2707ec7d | 540 | /*Codes_SRS_MULTITREE_99_043:[ If destination is NULL, the function shall return MULTITREE_INVALID_ARG.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 541 | else if (destination == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 542 | { |
AzureIoTClient | 0:1f9b2707ec7d | 543 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 544 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 545 | } |
AzureIoTClient | 0:1f9b2707ec7d | 546 | else |
AzureIoTClient | 0:1f9b2707ec7d | 547 | { |
AzureIoTClient | 17:fa1bba4c6053 | 548 | MULTITREE_HANDLE_DATA * node = (MULTITREE_HANDLE_DATA*)treeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 549 | /*Codes_SRS_MULTITREE_99_044:[ If there is no value in the node then MULTITREE_EMPTY_VALUE shall be returned.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 550 | if (node->value == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 551 | { |
AzureIoTClient | 0:1f9b2707ec7d | 552 | result = MULTITREE_EMPTY_VALUE; |
AzureIoTClient | 11:b1327861f5e0 | 553 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 554 | } |
AzureIoTClient | 0:1f9b2707ec7d | 555 | else |
AzureIoTClient | 0:1f9b2707ec7d | 556 | { |
AzureIoTClient | 0:1f9b2707ec7d | 557 | /*Codes_SRS_MULTITREE_99_041:[This function updates the *destination parameter to the internally stored value.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 558 | *destination = node->value; |
AzureIoTClient | 0:1f9b2707ec7d | 559 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 560 | } |
AzureIoTClient | 0:1f9b2707ec7d | 561 | } |
AzureIoTClient | 0:1f9b2707ec7d | 562 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 563 | } |
AzureIoTClient | 0:1f9b2707ec7d | 564 | |
AzureIoTClient | 0:1f9b2707ec7d | 565 | MULTITREE_RESULT MultiTree_SetValue(MULTITREE_HANDLE treeHandle, void* value) |
AzureIoTClient | 0:1f9b2707ec7d | 566 | { |
AzureIoTClient | 0:1f9b2707ec7d | 567 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 568 | |
AzureIoTClient | 0:1f9b2707ec7d | 569 | /* Codes_SRS_MULTITREE_99_074:[ If any argument is NULL, MultiTree_SetValue shall return MULTITREE_INVALID_ARG.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 570 | if ((treeHandle == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 571 | (value == NULL)) |
AzureIoTClient | 0:1f9b2707ec7d | 572 | { |
AzureIoTClient | 0:1f9b2707ec7d | 573 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 574 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 575 | } |
AzureIoTClient | 0:1f9b2707ec7d | 576 | else |
AzureIoTClient | 0:1f9b2707ec7d | 577 | { |
AzureIoTClient | 17:fa1bba4c6053 | 578 | MULTITREE_HANDLE_DATA * node = (MULTITREE_HANDLE_DATA*)treeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 579 | if (node->value != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 580 | { |
AzureIoTClient | 0:1f9b2707ec7d | 581 | /* Codes_SRS_MULTITREE_99_076:[ If the node already has a value then MultiTree_SetValue shall return MULTITREE_ALREADY_HAS_A_VALUE.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 582 | result = MULTITREE_ALREADY_HAS_A_VALUE; |
AzureIoTClient | 11:b1327861f5e0 | 583 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 584 | } |
AzureIoTClient | 0:1f9b2707ec7d | 585 | else |
AzureIoTClient | 0:1f9b2707ec7d | 586 | { |
AzureIoTClient | 0:1f9b2707ec7d | 587 | /* Codes_SRS_MULTITREE_99_072:[ MultiTree_SetValue shall set the value of the node indicated by the treeHandle argument to the value of the argument value.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 588 | if (node->cloneFunction(&node->value, value) != 0) |
AzureIoTClient | 0:1f9b2707ec7d | 589 | { |
AzureIoTClient | 0:1f9b2707ec7d | 590 | /* Codes_SRS_MULTITREE_99_075:[ MultiTree_SetValue shall return MULTITREE_ERROR to indicate any other error.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 591 | result = MULTITREE_ERROR; |
AzureIoTClient | 11:b1327861f5e0 | 592 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 593 | } |
AzureIoTClient | 0:1f9b2707ec7d | 594 | else |
AzureIoTClient | 0:1f9b2707ec7d | 595 | { |
AzureIoTClient | 0:1f9b2707ec7d | 596 | /* Codes_SRS_MULTITREE_99_073:[ On success, MultiTree_SetValue shall return MULTITREE_OK.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 597 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 598 | } |
AzureIoTClient | 0:1f9b2707ec7d | 599 | } |
AzureIoTClient | 0:1f9b2707ec7d | 600 | } |
AzureIoTClient | 0:1f9b2707ec7d | 601 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 602 | } |
AzureIoTClient | 0:1f9b2707ec7d | 603 | |
AzureIoTClient | 0:1f9b2707ec7d | 604 | void MultiTree_Destroy(MULTITREE_HANDLE treeHandle) |
AzureIoTClient | 0:1f9b2707ec7d | 605 | { |
AzureIoTClient | 0:1f9b2707ec7d | 606 | if (treeHandle != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 607 | { |
AzureIoTClient | 17:fa1bba4c6053 | 608 | MULTITREE_HANDLE_DATA* node = (MULTITREE_HANDLE_DATA*)treeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 609 | size_t i; |
AzureIoTClient | 0:1f9b2707ec7d | 610 | for (i = 0; i < node->nChildren;i++) |
AzureIoTClient | 0:1f9b2707ec7d | 611 | { |
AzureIoTClient | 0:1f9b2707ec7d | 612 | /*Codes_SRS_MULTITREE_99_047:[ This function frees any system resource used by the tree designated by parameter treeHandle]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 613 | MultiTree_Destroy(node->children[i]); |
AzureIoTClient | 0:1f9b2707ec7d | 614 | } |
AzureIoTClient | 0:1f9b2707ec7d | 615 | /*Codes_SRS_MULTITREE_99_047:[ This function frees any system resource used by the tree designated by parameter treeHandle]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 616 | if (node->children != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 617 | { |
AzureIoTClient | 0:1f9b2707ec7d | 618 | free(node->children); |
AzureIoTClient | 0:1f9b2707ec7d | 619 | node->children = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 620 | } |
AzureIoTClient | 0:1f9b2707ec7d | 621 | |
AzureIoTClient | 0:1f9b2707ec7d | 622 | /*Codes_SRS_MULTITREE_99_047:[ This function frees any system resource used by the tree designated by parameter treeHandle]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 623 | if (node->name != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 624 | { |
AzureIoTClient | 0:1f9b2707ec7d | 625 | free(node->name); |
AzureIoTClient | 0:1f9b2707ec7d | 626 | node->name = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 627 | } |
AzureIoTClient | 0:1f9b2707ec7d | 628 | |
AzureIoTClient | 0:1f9b2707ec7d | 629 | /*Codes_SRS_MULTITREE_99_047:[ This function frees any system resource used by the tree designated by parameter treeHandle]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 630 | if (node->value != NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 631 | { |
AzureIoTClient | 0:1f9b2707ec7d | 632 | node->freeFunction(node->value); |
AzureIoTClient | 0:1f9b2707ec7d | 633 | node->value = NULL; |
AzureIoTClient | 0:1f9b2707ec7d | 634 | } |
AzureIoTClient | 0:1f9b2707ec7d | 635 | |
AzureIoTClient | 0:1f9b2707ec7d | 636 | /*Codes_SRS_MULTITREE_99_047:[ This function frees any system resource used by the tree designated by parameter treeHandle]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 637 | free(node); |
AzureIoTClient | 0:1f9b2707ec7d | 638 | } |
AzureIoTClient | 0:1f9b2707ec7d | 639 | } |
AzureIoTClient | 0:1f9b2707ec7d | 640 | |
AzureIoTClient | 0:1f9b2707ec7d | 641 | MULTITREE_RESULT MultiTree_GetLeafValue(MULTITREE_HANDLE treeHandle, const char* leafPath, const void** destination) |
AzureIoTClient | 0:1f9b2707ec7d | 642 | { |
AzureIoTClient | 0:1f9b2707ec7d | 643 | MULTITREE_RESULT result; |
AzureIoTClient | 0:1f9b2707ec7d | 644 | |
AzureIoTClient | 0:1f9b2707ec7d | 645 | /* Codes_SRS_MULTITREE_99_055:[ If any argument is NULL, MultiTree_GetLeafValue shall return MULTITREE_INVALID_ARG.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 646 | if ((treeHandle == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 647 | (leafPath == NULL) || |
AzureIoTClient | 0:1f9b2707ec7d | 648 | (destination == NULL)) |
AzureIoTClient | 0:1f9b2707ec7d | 649 | { |
AzureIoTClient | 0:1f9b2707ec7d | 650 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 11:b1327861f5e0 | 651 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 652 | } |
AzureIoTClient | 0:1f9b2707ec7d | 653 | /* Codes_SRS_MULTITREE_99_058:[ The last child designates the child that will receive the value. If a child name is empty (such as in "/child1//child12"), MULTITREE_EMPTY_CHILD_NAME shall be returned.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 654 | else if (strlen(leafPath) == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 655 | { |
AzureIoTClient | 0:1f9b2707ec7d | 656 | result = MULTITREE_EMPTY_CHILD_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 657 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 658 | } |
AzureIoTClient | 0:1f9b2707ec7d | 659 | else |
AzureIoTClient | 0:1f9b2707ec7d | 660 | { |
AzureIoTClient | 0:1f9b2707ec7d | 661 | /*break the path into components*/ |
AzureIoTClient | 0:1f9b2707ec7d | 662 | /*find the first child name*/ |
AzureIoTClient | 17:fa1bba4c6053 | 663 | MULTITREE_HANDLE_DATA* node = (MULTITREE_HANDLE_DATA *)treeHandle; |
AzureIoTClient | 0:1f9b2707ec7d | 664 | const char* pos = leafPath; |
AzureIoTClient | 0:1f9b2707ec7d | 665 | const char * whereIsDelimiter; |
AzureIoTClient | 0:1f9b2707ec7d | 666 | |
AzureIoTClient | 0:1f9b2707ec7d | 667 | /*if first character is / then skip it*/ |
AzureIoTClient | 0:1f9b2707ec7d | 668 | if (*pos == '/') |
AzureIoTClient | 0:1f9b2707ec7d | 669 | { |
AzureIoTClient | 0:1f9b2707ec7d | 670 | pos++; |
AzureIoTClient | 0:1f9b2707ec7d | 671 | } |
AzureIoTClient | 0:1f9b2707ec7d | 672 | |
AzureIoTClient | 0:1f9b2707ec7d | 673 | if (*pos == '\0') |
AzureIoTClient | 0:1f9b2707ec7d | 674 | { |
AzureIoTClient | 0:1f9b2707ec7d | 675 | /* Codes_SRS_MULTITREE_99_069:[ If a child name is empty (such as in "/child1//child12"), MULTITREE_EMPTY_CHILD_NAME shall be returned.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 676 | result = MULTITREE_EMPTY_CHILD_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 677 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 678 | } |
AzureIoTClient | 0:1f9b2707ec7d | 679 | else |
AzureIoTClient | 0:1f9b2707ec7d | 680 | { |
AzureIoTClient | 0:1f9b2707ec7d | 681 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 682 | |
AzureIoTClient | 0:1f9b2707ec7d | 683 | /* Codes_SRS_MULTITREE_99_056:[ The leafPath argument is a string in the following format: /child1/child12 or child1/child12.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 684 | /* Codes_SRS_MULTITREE_99_058:[ The last child designates the child that will receive the value.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 685 | while (*pos != '\0') |
AzureIoTClient | 0:1f9b2707ec7d | 686 | { |
AzureIoTClient | 0:1f9b2707ec7d | 687 | size_t i; |
AzureIoTClient | 0:1f9b2707ec7d | 688 | size_t childCount = node->nChildren; |
AzureIoTClient | 0:1f9b2707ec7d | 689 | |
AzureIoTClient | 0:1f9b2707ec7d | 690 | whereIsDelimiter = pos; |
AzureIoTClient | 0:1f9b2707ec7d | 691 | |
AzureIoTClient | 0:1f9b2707ec7d | 692 | while ((*whereIsDelimiter != '/') && (*whereIsDelimiter != '\0')) |
AzureIoTClient | 0:1f9b2707ec7d | 693 | { |
AzureIoTClient | 0:1f9b2707ec7d | 694 | whereIsDelimiter++; |
AzureIoTClient | 0:1f9b2707ec7d | 695 | } |
AzureIoTClient | 0:1f9b2707ec7d | 696 | |
AzureIoTClient | 0:1f9b2707ec7d | 697 | if (whereIsDelimiter == pos) |
AzureIoTClient | 0:1f9b2707ec7d | 698 | { |
AzureIoTClient | 0:1f9b2707ec7d | 699 | /* Codes_SRS_MULTITREE_99_069:[ If a child name is empty (such as in "/child1//child12"), MULTITREE_EMPTY_CHILD_NAME shall be returned.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 700 | result = MULTITREE_EMPTY_CHILD_NAME; |
AzureIoTClient | 11:b1327861f5e0 | 701 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 702 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 703 | } |
AzureIoTClient | 0:1f9b2707ec7d | 704 | else if (childCount == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 705 | { |
AzureIoTClient | 0:1f9b2707ec7d | 706 | /* Codes_SRS_MULTITREE_99_071:[ When the child node is not found, MultiTree_GetLeafValue shall return MULTITREE_CHILD_NOT_FOUND.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 707 | result = MULTITREE_CHILD_NOT_FOUND; |
AzureIoTClient | 11:b1327861f5e0 | 708 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 709 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 710 | } |
AzureIoTClient | 0:1f9b2707ec7d | 711 | else |
AzureIoTClient | 0:1f9b2707ec7d | 712 | { |
AzureIoTClient | 0:1f9b2707ec7d | 713 | for (i = 0; i < childCount; i++) |
AzureIoTClient | 0:1f9b2707ec7d | 714 | { |
AzureIoTClient | 0:1f9b2707ec7d | 715 | if (strncmp(node->children[i]->name, pos, whereIsDelimiter - pos) == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 716 | { |
AzureIoTClient | 0:1f9b2707ec7d | 717 | /* Codes_SRS_MULTITREE_99_057:[ Subsequent names designate hierarchical children in the tree.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 718 | node = node->children[i]; |
AzureIoTClient | 0:1f9b2707ec7d | 719 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 720 | } |
AzureIoTClient | 0:1f9b2707ec7d | 721 | } |
AzureIoTClient | 0:1f9b2707ec7d | 722 | |
AzureIoTClient | 0:1f9b2707ec7d | 723 | if (i == childCount) |
AzureIoTClient | 0:1f9b2707ec7d | 724 | { |
AzureIoTClient | 0:1f9b2707ec7d | 725 | /* Codes_SRS_MULTITREE_99_071:[ When the child node is not found, MultiTree_GetLeafValue shall return MULTITREE_CHILD_NOT_FOUND.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 726 | result = MULTITREE_CHILD_NOT_FOUND; |
AzureIoTClient | 11:b1327861f5e0 | 727 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 728 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 729 | } |
AzureIoTClient | 0:1f9b2707ec7d | 730 | else |
AzureIoTClient | 0:1f9b2707ec7d | 731 | { |
AzureIoTClient | 0:1f9b2707ec7d | 732 | if (*whereIsDelimiter == '/') |
AzureIoTClient | 0:1f9b2707ec7d | 733 | { |
AzureIoTClient | 0:1f9b2707ec7d | 734 | pos = whereIsDelimiter + 1; |
AzureIoTClient | 0:1f9b2707ec7d | 735 | } |
AzureIoTClient | 0:1f9b2707ec7d | 736 | else |
AzureIoTClient | 0:1f9b2707ec7d | 737 | { |
AzureIoTClient | 0:1f9b2707ec7d | 738 | /* end of path */ |
AzureIoTClient | 0:1f9b2707ec7d | 739 | pos = whereIsDelimiter; |
AzureIoTClient | 0:1f9b2707ec7d | 740 | break; |
AzureIoTClient | 0:1f9b2707ec7d | 741 | } |
AzureIoTClient | 0:1f9b2707ec7d | 742 | } |
AzureIoTClient | 0:1f9b2707ec7d | 743 | } |
AzureIoTClient | 0:1f9b2707ec7d | 744 | } |
AzureIoTClient | 0:1f9b2707ec7d | 745 | |
AzureIoTClient | 0:1f9b2707ec7d | 746 | if (*pos == 0) |
AzureIoTClient | 0:1f9b2707ec7d | 747 | { |
AzureIoTClient | 0:1f9b2707ec7d | 748 | if (node->value == NULL) |
AzureIoTClient | 0:1f9b2707ec7d | 749 | { |
AzureIoTClient | 0:1f9b2707ec7d | 750 | /* Codes_SRS_MULTITREE_99_070:[ If an attempt is made to get the value for a node that does not have a value set, then MultiTree_GetLeafValue shall return MULTITREE_EMPTY_VALUE.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 751 | result = MULTITREE_EMPTY_VALUE; |
AzureIoTClient | 11:b1327861f5e0 | 752 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 0:1f9b2707ec7d | 753 | } |
AzureIoTClient | 0:1f9b2707ec7d | 754 | /*Codes_SRS_MULTITREE_99_053:[ MultiTree_GetLeafValue shall copy into the *destination argument the value of the node identified by the leafPath argument.]*/ |
AzureIoTClient | 0:1f9b2707ec7d | 755 | else |
AzureIoTClient | 0:1f9b2707ec7d | 756 | { |
AzureIoTClient | 0:1f9b2707ec7d | 757 | *destination = node->value; |
AzureIoTClient | 0:1f9b2707ec7d | 758 | /* Codes_SRS_MULTITREE_99_054:[ On success, MultiTree_GetLeafValue shall return MULTITREE_OK.] */ |
AzureIoTClient | 0:1f9b2707ec7d | 759 | result = MULTITREE_OK; |
AzureIoTClient | 0:1f9b2707ec7d | 760 | } |
AzureIoTClient | 0:1f9b2707ec7d | 761 | } |
AzureIoTClient | 0:1f9b2707ec7d | 762 | } |
AzureIoTClient | 0:1f9b2707ec7d | 763 | } |
AzureIoTClient | 0:1f9b2707ec7d | 764 | return result; |
AzureIoTClient | 0:1f9b2707ec7d | 765 | } |
AzureIoTClient | 26:7c0e6f86d034 | 766 | |
AzureIoTClient | 26:7c0e6f86d034 | 767 | /* Codes_SRS_MULTITREE_99_077:[ MultiTree_DeleteChild shall remove the direct children node (no recursive search) set by childName.] */ |
AzureIoTClient | 26:7c0e6f86d034 | 768 | MULTITREE_RESULT MultiTree_DeleteChild(MULTITREE_HANDLE treeHandle, const char* childName) |
AzureIoTClient | 26:7c0e6f86d034 | 769 | { |
AzureIoTClient | 26:7c0e6f86d034 | 770 | MULTITREE_RESULT result; |
AzureIoTClient | 26:7c0e6f86d034 | 771 | /* Codes_SRS_MULTITREE_99_077:[ If any argument is NULL, MultiTree_DeleteChild shall return MULTITREE_INVALID_ARG.] */ |
AzureIoTClient | 26:7c0e6f86d034 | 772 | if ((treeHandle == NULL) || |
AzureIoTClient | 26:7c0e6f86d034 | 773 | (childName == NULL)) |
AzureIoTClient | 26:7c0e6f86d034 | 774 | { |
AzureIoTClient | 26:7c0e6f86d034 | 775 | result = MULTITREE_INVALID_ARG; |
AzureIoTClient | 26:7c0e6f86d034 | 776 | LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); |
AzureIoTClient | 26:7c0e6f86d034 | 777 | } |
AzureIoTClient | 26:7c0e6f86d034 | 778 | else |
AzureIoTClient | 26:7c0e6f86d034 | 779 | { |
AzureIoTClient | 26:7c0e6f86d034 | 780 | size_t i; |
AzureIoTClient | 26:7c0e6f86d034 | 781 | size_t childToRemove = treeHandle->nChildren; |
AzureIoTClient | 26:7c0e6f86d034 | 782 | MULTITREE_HANDLE treeToRemove = NULL; |
AzureIoTClient | 26:7c0e6f86d034 | 783 | |
AzureIoTClient | 26:7c0e6f86d034 | 784 | for (i = 0; i < treeHandle->nChildren; i++) |
AzureIoTClient | 26:7c0e6f86d034 | 785 | { |
AzureIoTClient | 26:7c0e6f86d034 | 786 | if (0 == strcmp(treeHandle->children[i]->name, childName)) |
AzureIoTClient | 26:7c0e6f86d034 | 787 | { |
AzureIoTClient | 26:7c0e6f86d034 | 788 | childToRemove = i; |
AzureIoTClient | 26:7c0e6f86d034 | 789 | treeToRemove = treeHandle->children[childToRemove]; |
AzureIoTClient | 26:7c0e6f86d034 | 790 | break; |
AzureIoTClient | 26:7c0e6f86d034 | 791 | } |
AzureIoTClient | 26:7c0e6f86d034 | 792 | } |
AzureIoTClient | 26:7c0e6f86d034 | 793 | |
AzureIoTClient | 26:7c0e6f86d034 | 794 | if (i == treeHandle->nChildren) |
AzureIoTClient | 26:7c0e6f86d034 | 795 | { |
AzureIoTClient | 26:7c0e6f86d034 | 796 | /* Codes_SRS_MULTITREE_99_079:[If childName is not found, MultiTree_DeleteChild shall return MULTITREE_CHILD_NOT_FOUND.] */ |
AzureIoTClient | 26:7c0e6f86d034 | 797 | result = MULTITREE_CHILD_NOT_FOUND; |
AzureIoTClient | 26:7c0e6f86d034 | 798 | // Don't log error; this function is best effort only. Caller will determine actual error state. |
AzureIoTClient | 26:7c0e6f86d034 | 799 | } |
AzureIoTClient | 26:7c0e6f86d034 | 800 | else |
AzureIoTClient | 26:7c0e6f86d034 | 801 | { |
AzureIoTClient | 26:7c0e6f86d034 | 802 | for (i = childToRemove; i < treeHandle->nChildren - 1; i++) |
AzureIoTClient | 26:7c0e6f86d034 | 803 | { |
AzureIoTClient | 26:7c0e6f86d034 | 804 | treeHandle->children[i] = treeHandle->children[i+1]; |
AzureIoTClient | 26:7c0e6f86d034 | 805 | } |
AzureIoTClient | 26:7c0e6f86d034 | 806 | |
AzureIoTClient | 26:7c0e6f86d034 | 807 | /* Codes_SRS_MULTITREE_99_077:[ MultiTree_DeleteChild shall remove the direct children node (no recursive search) set by childName */ |
AzureIoTClient | 26:7c0e6f86d034 | 808 | MultiTree_Destroy(treeToRemove); |
AzureIoTClient | 26:7c0e6f86d034 | 809 | |
AzureIoTClient | 26:7c0e6f86d034 | 810 | // Even though this isn't reachable anymore after decrementing count, NULL out for cleanliness |
AzureIoTClient | 26:7c0e6f86d034 | 811 | treeHandle->children[treeHandle->nChildren - 1] = NULL; |
AzureIoTClient | 26:7c0e6f86d034 | 812 | treeHandle->nChildren = treeHandle->nChildren - 1; |
AzureIoTClient | 26:7c0e6f86d034 | 813 | |
AzureIoTClient | 26:7c0e6f86d034 | 814 | result = MULTITREE_OK; |
AzureIoTClient | 26:7c0e6f86d034 | 815 | } |
AzureIoTClient | 26:7c0e6f86d034 | 816 | } |
AzureIoTClient | 26:7c0e6f86d034 | 817 | |
AzureIoTClient | 26:7c0e6f86d034 | 818 | return result; |
AzureIoTClient | 26:7c0e6f86d034 | 819 | } |
AzureIoTClient | 26:7c0e6f86d034 | 820 |