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

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <kdb.h>
      10             : #include <kdbmacros.h> // Declares `ELEKTRA_STRINGIFY`
      11             : #include <stdint.h>
      12             : #include <stdio.h>
      13             : #include <stdlib.h>
      14             : #include <string.h>
      15             : 
      16             : void printError (Key * key);
      17             : void printWarnings (Key * key);
      18             : void removeMetaData (Key * key, const char * searchfor);
      19             : 
      20           0 : int main (void)
      21             : {
      22           0 :         KeySet * myConfig = ksNew (0, KS_END);
      23           0 :         Key * key = keyNew ("/sw/MyApp", KEY_CASCADING_NAME, KEY_END);
      24           0 :         KDB * handle = kdbOpen (key);
      25             : 
      26           0 :         if (!handle) printError (key);
      27             : 
      28             : 
      29           0 :         printWarnings (key);
      30             : 
      31           0 :         if (kdbGet (handle, myConfig, key) < 0) printError (key);
      32             : 
      33             : 
      34           0 :         printWarnings (key);
      35             : 
      36           0 :         keyDel (key);
      37             : 
      38             :         // lookup
      39           0 :         Key * result = ksLookupByName (myConfig, "/sw/MyApp/Tests/TestKey1", 0);
      40           0 :         if (!result)
      41           0 :                 printf ("Key not found in KeySet\n");
      42             :         else
      43             :         {
      44             :                 // do something with the key
      45           0 :                 const char * key_name = keyName (result);
      46           0 :                 const char * key_value = keyString (result);
      47           0 :                 const char * key_comment = keyString (keyGetMeta (result, "comment"));
      48           0 :                 printf ("key: %s value: %s comment: %s\n", key_name, key_value, key_comment);
      49             :         }
      50             : 
      51           0 :         ksDel (myConfig); // delete the in-memory configuration
      52             : 
      53             : 
      54             :         // maybe you want kdbSet() myConfig here
      55             : 
      56           0 :         kdbClose (handle, 0); // no more affairs with the key database.
      57             : }
      58             : 
      59             : 
      60             : /* Print and remove Error.
      61             :  * Note: not all available information will be printed!
      62             :  * Fields for more information are listed in the value from
      63             :  * the Key returned by keyGetMeta(key,"error").
      64             :  * Or print all MetaData, by using the loop from removeMetaData ().
      65             :  */
      66           0 : void printError (Key * key)
      67             : {
      68           0 :         printf ("Error occurred: %s\n", keyString (keyGetMeta (key, "error/description")));
      69             : 
      70             :         /*remove error*/
      71           0 :         removeMetaData (key, "error");
      72           0 : }
      73             : 
      74             : 
      75             : /* Check for warnings, print and remove.
      76             :  * Note: not all available information will be printed!
      77             :  * Fields for more information are listed in the value from
      78             :  * the Key returned by keyGetMeta(key,"warnings/#XX") where XX
      79             :  * is the Warning number, starting at 00.
      80             :  * Or print all MetaData, by using the loop from removeMetaData ().
      81             :  */
      82           0 : void printWarnings (Key * key)
      83             : {
      84           0 :         if (!keyGetMeta (key, "warnings")) return;
      85             :         char * end;
      86           0 :         size_t warn_count = strtol (keyString (keyGetMeta (key, "warnings")), &end, 10);
      87           0 :         if (*end)
      88             :         {
      89           0 :                 printf ("strtol error\n");
      90           0 :                 return;
      91             :         }
      92             :         size_t warn_iter = 0;
      93             : 
      94             :         char buffer[sizeof ("warnings/#00/description") + sizeof (ELEKTRA_STRINGIFY (SIZE_MAX))];
      95             : 
      96             :         do
      97             :         {
      98           0 :                 snprintf (&buffer[0], sizeof (buffer), "warnings/#%02zu/description", warn_iter);
      99             : 
     100           0 :                 const Key * warnkey = keyGetMeta (key, buffer);
     101           0 :                 printf ("Warning occurred: %s\n", keyString (warnkey));
     102           0 :                 ++warn_iter;
     103           0 :         } while (warn_iter <= warn_count);
     104             : 
     105             :         /*remove all warnings*/
     106           0 :         removeMetaData (key, "warnings");
     107             : }
     108             : 
     109             : 
     110             : /* Helper which iterates over MetaKeys from key
     111             :  * and removes all MetaKeys starting with
     112             :  * searchfor.
     113             :  */
     114           0 : void removeMetaData (Key * key, const char * searchfor)
     115             : {
     116             :         const Key * iter_key;
     117           0 :         keyRewindMeta (key);
     118           0 :         while ((iter_key = keyNextMeta (key)) != 0)
     119             :         {
     120             :                 /*startsWith*/
     121           0 :                 if (strncmp (searchfor, keyName (iter_key), strlen (searchfor)) == 0)
     122             :                 {
     123           0 :                         if (keySetMeta (key, keyName (iter_key), 0) != 0) printf ("Error while deleting %s\n", searchfor);
     124             :                 }
     125             :         }
     126           0 : }

Generated by: LCOV version 1.13