static void ExistAndEmptyTest() { static char pathBuffer[STR_SIZE] = { 0 }; snprintf(pathBuffer, STR_SIZE, "/%s/existAndEmptyTest/", TestRootDir); { le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(pathBuffer); le_cfg_SetEmpty(iterRef, ""); LE_TEST(le_cfg_IsEmpty(iterRef, "") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueA") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueB") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueC") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueD") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueA") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueB") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueC") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueD") == true); le_cfg_SetString(iterRef, "valueA", "aNewValue"); le_cfg_SetInt(iterRef, "valueB", 10); le_cfg_SetBool(iterRef, "valueC", true); le_cfg_SetFloat(iterRef, "valueD", 10.24); LE_TEST(le_cfg_NodeExists(iterRef, "valueA") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueB") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueC") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueD") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueA") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueB") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueC") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueD") == false); le_cfg_CommitTxn(iterRef); } }
// ------------------------------------------------------------------------------------------------- static int HandleSet ( void ) // ------------------------------------------------------------------------------------------------- { // Looks like we're trying to write a value to a node. Get the node's current type and then // write the requested value to that node. le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(NodePath); le_cfg_nodeType_t originalType = le_cfg_GetNodeType(iterRef, ""); le_cfg_nodeType_t newType = DataType; if ( (newType != originalType) && (originalType != LE_CFG_TYPE_DOESNT_EXIST)) { printf("Converting node '%s' type from %s to %s.\n", NodePath, NodeTypeStr(originalType), NodeTypeStr(newType)); } int result = EXIT_SUCCESS; switch (newType) { case LE_CFG_TYPE_STRING: le_cfg_SetString(iterRef, "", NodeValue); break; case LE_CFG_TYPE_BOOL: if (strcmp(NodeValue, "false") == 0) { le_cfg_SetBool(iterRef, "", false); } else if (strcmp(NodeValue, "true") == 0) { le_cfg_SetBool(iterRef, "", true); } else { fprintf(stderr, "Bad boolean value '%s'.\n", NodeValue); } break; case LE_CFG_TYPE_INT: { char *endIntp; errno = 0; int32_t value = strtol(NodeValue, &endIntp, 10); if (errno != 0) { fprintf(stderr, "Integer '%s' out of range\n", NodeValue); result = EXIT_FAILURE; } else if (*endIntp != '\0') { fprintf(stderr, "Invalid character in integer '%s'\n", NodeValue); result = EXIT_FAILURE; } else { le_cfg_SetInt(iterRef, "", value); } break; } case LE_CFG_TYPE_FLOAT: { char *endFloatp; errno = 0; double floatVal = strtod(NodeValue, &endFloatp); if (errno != 0) { fprintf(stderr, "Float value '%s' out of range\n", NodeValue); result = EXIT_FAILURE; } else if (*endFloatp != '\0') { fprintf(stderr, "Invalid character in float value '%s'\n", NodeValue); result = EXIT_FAILURE; } else { le_cfg_SetFloat(iterRef, "", floatVal); } break; } case LE_CFG_TYPE_DOESNT_EXIST: result = EXIT_FAILURE; break; default: fprintf(stderr, "Unexpected node type specified, %s.\n", NodeTypeStr(newType)); result = EXIT_FAILURE; break; } // Finally, commit the value update, if the set was successful. if (result != EXIT_FAILURE) { le_cfg_CommitTxn(iterRef); } else { le_cfg_CancelTxn(iterRef); } return result; }
// ------------------------------------------------------------------------------------------------- static le_result_t HandleImportJSONIteration ( le_cfg_IteratorRef_t iterRef, ///< Dump the JSON data into this iterator. json_t* nodePtr ///< From this JSON object. ) // ------------------------------------------------------------------------------------------------- { // Get value json_t* value = json_object_get(nodePtr, JSON_FIELD_VALUE); // Check type const char* typeStr = json_string_value(json_object_get(nodePtr, JSON_FIELD_TYPE)); le_cfg_nodeType_t type = GetNodeTypeFromString(typeStr); switch (type) { case LE_CFG_TYPE_BOOL: le_cfg_SetBool(iterRef, "", json_is_true(value)); break; case LE_CFG_TYPE_STRING: le_cfg_SetString(iterRef, "", json_string_value(value)); break; case LE_CFG_TYPE_INT: le_cfg_SetInt(iterRef, "", json_integer_value(value)); break; case LE_CFG_TYPE_FLOAT: le_cfg_SetFloat(iterRef, "", json_real_value(value)); break; case LE_CFG_TYPE_STEM: { // Iterate on children json_t* childrenPtr = json_object_get(nodePtr, JSON_FIELD_CHILDREN); json_t* childPtr; int i; json_array_foreach(childrenPtr, i, childPtr) { // Get name const char* name = json_string_value(json_object_get(childPtr, JSON_FIELD_NAME)); // Is node exist with this name? le_cfg_nodeType_t existingType = le_cfg_GetNodeType(iterRef, name); switch (existingType) { case LE_CFG_TYPE_DOESNT_EXIST: case LE_CFG_TYPE_STEM: case LE_CFG_TYPE_EMPTY: // Not existing, already a stem or empty node, nothing to do break; default: // Issue with node creation fprintf(stderr, "Node conflict when importing, at node %s", name); return LE_NOT_POSSIBLE; break; } // Iterate to this child le_cfg_GoToNode(iterRef, name); // Iterate le_result_t subResult = HandleImportJSONIteration(iterRef, childPtr); if (subResult != LE_OK) { // Something went wrong return subResult; } // Go back to parent le_cfg_GoToParent(iterRef); } } break; default: return LE_FAULT; } return LE_OK; }