LCOV - code coverage report
Current view: top level - src/plugins/yamlcpp - yamlcpp.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 27 35 77.1 %
Date: 2019-09-12 12:28:41 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Source for yamlcpp plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : // -- Imports ------------------------------------------------------------------------------------------------------------------------------
      11             : 
      12             : #include "yamlcpp.hpp"
      13             : #include "log.hpp"
      14             : #include "read.hpp"
      15             : #include "write.hpp"
      16             : using namespace yamlcpp;
      17             : 
      18             : #include <kdb.hpp>
      19             : using namespace ckdb;
      20             : #include <kdberrors.h>
      21             : #include <kdblogger.h>
      22             : 
      23             : #include "yaml-cpp/yaml.h"
      24             : 
      25             : // -- Functions ----------------------------------------------------------------------------------------------------------------------------
      26             : 
      27             : namespace
      28             : {
      29             : 
      30             : /**
      31             :  * @brief This function returns a key set containing the contract of this plugin.
      32             :  *
      33             :  * @return A contract describing the functionality of this plugin.
      34             :  */
      35         474 : kdb::KeySet contractYamlCpp (void)
      36             : {
      37             :         return kdb::KeySet{ 30,
      38             :                             keyNew ("system/elektra/modules/yamlcpp", KEY_VALUE, "yamlcpp plugin waits for your orders", KEY_END),
      39             :                             keyNew ("system/elektra/modules/yamlcpp/exports", KEY_END),
      40             :                             keyNew ("system/elektra/modules/yamlcpp/exports/get", KEY_FUNC, elektraYamlcppGet, KEY_END),
      41             :                             keyNew ("system/elektra/modules/yamlcpp/exports/set", KEY_FUNC, elektraYamlcppSet, KEY_END),
      42             : #include ELEKTRA_README
      43             :                             keyNew ("system/elektra/modules/yamlcpp/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END),
      44             :                             keyNew ("system/elektra/modules/yamlcpp/config/needs/binary/meta", KEY_VALUE, "true", KEY_END),
      45             :                             keyNew ("system/elektra/modules/yamlcpp/config/needs/boolean/restore", KEY_VALUE, "#1", KEY_END),
      46         474 :                             KS_END };
      47             : }
      48             : }
      49             : 
      50             : // ====================
      51             : // = Plugin Interface =
      52             : // ====================
      53             : 
      54             : /** @see elektraDocGet */
      55         619 : int elektraYamlcppGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
      56             : {
      57        1238 :         kdb::Key parent = kdb::Key (parentKey);
      58        1238 :         kdb::KeySet keys = kdb::KeySet (returned);
      59             : 
      60        1857 :         if (parent.getName () == "system/elektra/modules/yamlcpp")
      61             :         {
      62        1422 :                 keys.append (contractYamlCpp ());
      63         474 :                 parent.release ();
      64         474 :                 keys.release ();
      65             : 
      66         474 :                 return ELEKTRA_PLUGIN_STATUS_SUCCESS;
      67             :         }
      68             : 
      69         145 :         int status = ELEKTRA_PLUGIN_STATUS_ERROR;
      70             : 
      71             :         try
      72             :         {
      73         145 :                 yamlRead (keys, parent);
      74             :                 status = ELEKTRA_PLUGIN_STATUS_SUCCESS;
      75             :         }
      76           4 :         catch (YAML::ParserException const & exception)
      77             :         {
      78           8 :                 ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parent.getKey (), "Unable to parse file '%s'. Reason: %s",
      79             :                                                          parent.getString ().c_str (), exception.what ());
      80             :         }
      81           0 :         catch (std::overflow_error const & exception)
      82             :         {
      83           0 :                 ELEKTRA_SET_RESOURCE_ERRORF (parent.getKey (), "Unable to read data from file '%s'. Reason: %s",
      84             :                                              parent.getString ().c_str (), exception.what ());
      85             :         }
      86           0 :         catch (YAML::RepresentationException const & exception)
      87             :         {
      88           0 :                 ELEKTRA_SET_RESOURCE_ERRORF (parent.getKey (), "Unable to read data from file '%s'. Reason: %s",
      89             :                                              parent.getString ().c_str (), exception.what ());
      90             :         }
      91             : 
      92         145 :         parent.release ();
      93         145 :         keys.release ();
      94             : 
      95         145 :         return status;
      96             : }
      97             : 
      98             : /** @see elektraDocSet */
      99          68 : int elektraYamlcppSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
     100             : {
     101         136 :         kdb::Key parent = kdb::Key (parentKey);
     102         136 :         kdb::KeySet keys = kdb::KeySet (returned);
     103             : 
     104             : #ifdef HAVE_LOGGER
     105             :         ELEKTRA_LOG_DEBUG ("Write keys:");
     106             :         logKeySet (keys);
     107             : #endif
     108             : 
     109          68 :         int status = ELEKTRA_PLUGIN_STATUS_ERROR;
     110             : 
     111             :         try
     112             :         {
     113          68 :                 yamlWrite (keys, parent);
     114             :                 status = ELEKTRA_PLUGIN_STATUS_SUCCESS;
     115             :         }
     116           0 :         catch (YAML::BadFile const & exception)
     117             :         {
     118           0 :                 ELEKTRA_SET_RESOURCE_ERRORF (parent.getKey (), "Unable to write to file '%s'. Reason: %s.", parent.getString ().c_str (),
     119             :                                              exception.what ());
     120             :         }
     121           0 :         catch (YAML::EmitterException const & exception)
     122             :         {
     123           0 :                 ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (parent.getKey (),
     124             :                                                        "Something went wrong while emitting YAML data to file '%s'. Reason: %s.",
     125             :                                                        parent.getString ().c_str (), exception.what ());
     126             :         }
     127             : 
     128          68 :         parent.release ();
     129          68 :         keys.release ();
     130             : 
     131         136 :         return status;
     132             : }
     133             : 
     134        1094 : Plugin * ELEKTRA_PLUGIN_EXPORT
     135             : {
     136             :         return elektraPluginExport ("yamlcpp", ELEKTRA_PLUGIN_GET, &elektraYamlcppGet, ELEKTRA_PLUGIN_SET, &elektraYamlcppSet,
     137        1094 :                                     ELEKTRA_PLUGIN_END);
     138             : }

Generated by: LCOV version 1.13