/** * @internal * Remove plugin at all placements from list plugin configuration and apply it. * * @param list List plugin * @param plugin Plugin to remove * @retval 0 on error * @retval 1 on success */ static int listRemovePlugin (Plugin * list, Plugin * plugin) { ELEKTRA_NOT_NULL (list); ELEKTRA_NOT_NULL (plugin); KeySet * newConfig = ksDup (list->config); Key * configBase = keyNew ("user/plugins", KEY_END); KeySet * array = elektraArrayGet (configBase, newConfig); // Find the plugin with our handle Key * current; ksRewind (array); while ((current = ksNext (array)) != NULL) { Key * handleLookup = keyDup (current); keyAddBaseName (handleLookup, "handle"); Key * handle = ksLookup (newConfig, handleLookup, 0); keyDel (handleLookup); if (handle) { Plugin * handleValue = (*(Plugin **) keyValue (handle)); if (handleValue == plugin) { // Remove plugin configuration KeySet * cut = ksCut (newConfig, current); ksDel (cut); } } } ksDel (array); // Renumber array items KeySet * sourceArray = elektraArrayGet (configBase, newConfig); Key * renumberBase = keyNew ("user/plugins/#", KEY_END); ksRewind (sourceArray); while ((current = ksNext (sourceArray)) != NULL) { // Create new array item base name e.g. "user/plugins/#0" elektraArrayIncName (renumberBase); moveKeysRecursive (keyName (current), keyName (renumberBase), newConfig); } keyDel (configBase); keyDel (renumberBase); ksDel (sourceArray); ksDel (list->config); // Apply new configuration list->config = newConfig; list->kdbOpen (list, NULL); return 1; }
static void writeLineComments(Key *key, FILE *fp) { // TODO: this is really inefficient KeySet *metaKeys = elektraKeyGetMetaKeySet(key); Key *commentParent = keyNew("comment", KEY_META_NAME, KEY_END); KeySet *comments = elektraArrayGet(commentParent, metaKeys); keyDel(commentParent); ksRewind(comments); Key *current; while ((current = ksNext (comments))) { if (strcmp (keyName (current), "comment/#0")) { Key *spaceKey = keyDup (current); keyAddBaseName (spaceKey, "space"); Key *startKey = keyDup (current); keyAddBaseName (startKey, "start"); const char *spaces = getMetaValue (key, keyName (spaceKey)); const char *start = getMetaValue (key, keyName (startKey)); const char *comment = getMetaValue (key, keyName (current)); keyDel (spaceKey); keyDel (startKey); writeComment (spaces, start, comment, fp); fprintf (fp, "\n"); } } ksDel(metaKeys); ksDel(comments); }
/** * Gets the size of an array. * * @param elektra The Elektra instance to use. * @param name The (relative) name of the array. * @return the size of the array */ size_t elektraArraySize (Elektra * elektra, const char * name) { elektraSetLookupKey (elektra, name); KeySet * arrayKeys = elektraArrayGet (elektra->lookupKey, elektra->config); size_t size = (size_t) ksGetSize (arrayKeys); ksDel (arrayKeys); return size; }
/** * @internal * Add plugin at placement to list plugin configuration and apply it. * * @param list List plugin * @param plugin Plugin to add * @param placement Placement name * @retval 0 on error * @retval 0 on success */ static int listAddPlugin (Plugin * list, Plugin * plugin, char * placement) { ELEKTRA_NOT_NULL (list); ELEKTRA_NOT_NULL (plugin); ELEKTRA_NOT_NULL (placement); KeySet * newConfig = ksDup (list->config); // Find name for next item in plugins array Key * configBase = keyNew ("user/plugins", KEY_END); KeySet * array = elektraArrayGet (configBase, newConfig); Key * pluginItem = elektraArrayGetNextKey (array); ELEKTRA_NOT_NULL (pluginItem); keySetString (pluginItem, plugin->name); keyDel (configBase); // Create key with plugin handle Key * pluginHandle = keyDup (pluginItem); keyAddName (pluginHandle, "handle"); keySetBinary (pluginHandle, &plugin, sizeof (plugin)); // Create key with plugin placement char * placementType = placementToListPositionType (placement); if (placementType == NULL) { keyDel (configBase); keyDel (pluginItem); keyDel (pluginHandle); return 0; } Key * pluginPlacements = keyDup (pluginItem); keyAddName (pluginPlacements, "placements/"); keyAddName (pluginPlacements, placementType); keySetString (pluginPlacements, placement); // Append keys to list plugin configuration ksAppendKey (newConfig, pluginItem); ksAppendKey (newConfig, pluginHandle); ksAppendKey (newConfig, pluginPlacements); ksDel (array); ksDel (list->config); // Apply new configuration list->config = newConfig; list->kdbOpen (list, NULL); return 1; }
static void test_getArray (void) { printf ("Test get array"); KeySet * keys = ksNew (10, keyNew ("user/test/key1", KEY_END), keyNew ("user/test/key2", KEY_END), keyNew ("user/test/array", KEY_END), keyNew ("user/test/array/#0", KEY_END), keyNew ("user/test/array/#0/below", KEY_END), keyNew ("user/test/array/#1", KEY_END), keyNew ("user/test/yetanotherkey", KEY_END), KS_END); Key * arrayParent = keyNew ("user/test/array", KEY_END); KeySet * array = elektraArrayGet (arrayParent, keys); succeed_if (array, "The getarray function did not return a proper keyset"); succeed_if (ksGetSize (array) == 2, "the array contains a wrong number of elements"); succeed_if (ksLookupByName (array, "user/test/array/#0", KDB_O_NONE), "the array does not contain #0"); succeed_if (ksLookupByName (array, "user/test/array/#1", KDB_O_NONE), "the array does not contain #1"); keyDel (arrayParent); ksDel (array); ksDel (keys); }