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