LCOV - code coverage report
Current view: top level - examples - iterate.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 28 0.0 %
Date: 2019-09-12 12:28:41 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief some possibilites how to iterate ver a KeySet in an elegant way.
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <kdb.h>
      10             : #include <kdbextension.h>
      11             : #include <stdio.h>
      12             : 
      13             : /*
      14             :  * This function demonstrates how easy it is to extend
      15             :  * elektra. We use ksNext() and keyIsBelow() to implement a function
      16             :  * which skips to the next directory. */
      17           0 : Key * ksNextDir (KeySet * ks)
      18             : {
      19             :         Key * cur;
      20           0 :         Key * startKey = ksCurrent (ks);
      21             : 
      22           0 :         if (!startKey) return (ksNext (ks));
      23             : 
      24           0 :         while ((cur = ksNext (ks)) != 0)
      25             :         {
      26           0 :                 if (!keyIsBelow (startKey, cur)) return cur;
      27             :         }
      28             : 
      29             :         return 0;
      30             : }
      31             : 
      32           0 : int main (void)
      33             : {
      34           0 :         Key * cur = 0;
      35           0 :         Key * found = 0;
      36           0 :         KeySet * ks = ksNew (
      37             :                 30, keyNew ("user/dir1", KEY_DIR, KEY_END), keyNew ("user/dir1/key1", KEY_VALUE, "value1", KEY_END),
      38             :                 keyNew ("user/dir1/key2", KEY_VALUE, "value2", KEY_END), keyNew ("user/dir1/key3", KEY_VALUE, "value3", KEY_END),
      39             :                 keyNew ("user/dir1/key4", KEY_VALUE, "value4", KEY_END),
      40             :                 keyNew ("user/dir1/.inactive1", KEY_COMMENT, "key is inactive", KEY_END),
      41             :                 keyNew ("user/dir1/.inactive2", KEY_COMMENT, "additional information", KEY_END), keyNew ("user/dir2", KEY_DIR, KEY_END),
      42             :                 keyNew ("user/dir2/key1", KEY_VALUE, "value1", KEY_END), keyNew ("user/dir2/key2", KEY_VALUE, "value2", KEY_END),
      43             :                 keyNew ("user/dir2/key3", KEY_VALUE, "value3", KEY_END), keyNew ("user/dir2/key4", KEY_VALUE, "value4", KEY_END),
      44             :                 keyNew ("user/dir3", KEY_DIR, KEY_END), keyNew ("user/dir3/key1", KEY_VALUE, "value1", KEY_END),
      45             :                 keyNew ("user/dir3/.inactive1", KEY_COMMENT, "key is inactive", KEY_END),
      46             :                 keyNew ("user/dir3/.inactive2", KEY_COMMENT, "a users comment", KEY_END), keyNew ("user/dir4", KEY_DIR, KEY_END),
      47             :                 keyNew ("user/dir5", KEY_DIR, KEY_END), KS_END);
      48             : 
      49           0 :         printf ("Iterate over all keys:\n");
      50           0 :         ksRewind (ks);
      51           0 :         while ((cur = ksNext (ks)) != 0)
      52             :         { /* Iterates over all keys and prints their name */
      53           0 :                 printf ("%s\n", keyName (cur));
      54             :         }
      55             : 
      56           0 :         printf ("\nIterate over all directories:\n");
      57           0 :         ksRewind (ks);
      58           0 :         while ((cur = ksNextDir (ks)) != 0)
      59             :         { /* Iterates over all keys and prints their name */
      60           0 :                 printf ("%s\n", keyName (cur));
      61             :         }
      62             : 
      63           0 :         printf ("\nLookup and then iterate:\n");
      64           0 :         found = ksLookupByName (ks, "user/dir2", 0);
      65           0 :         printf ("Found key %s\n", keyName (found));
      66           0 :         while ((cur = ksNext (ks)) != 0)
      67             :         { /* Iterates over all keys direct below and prints their name */
      68           0 :                 if (keyIsDirectBelow (found, cur) == 0) break;
      69           0 :                 printf ("%s\n", keyName (cur));
      70             :         }
      71             : 
      72           0 :         printf ("\nIterate over inactive keys:\n");
      73           0 :         ksRewind (ks);
      74           0 :         while ((cur = ksNext (ks)) != 0)
      75             :         { /* Iterates over inactive keys and prints their name */
      76           0 :                 if (keyIsInactive (cur) == 0) continue;
      77           0 :                 printf ("%s %s\n", keyName (cur), keyString (keyGetMeta (cur, "comment")));
      78             :         }
      79             : 
      80             :         return 0;
      81             : }

Generated by: LCOV version 1.13