LCOV - code coverage report
Current view: top level - src/plugins/doc - doc.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 20 67 29.9 %
Date: 2019-09-12 12:28:41 Functions: 4 13 30.8 %

          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 "doc.h"
      10             : 
      11             : //! [plugin include]
      12             : #include <kdbplugin.h>
      13             : //! [plugin include]
      14             : //
      15             : //! [plugin errors include]
      16             : // using namespace ckdb; // for C++
      17             : #include <kdberrors.h>
      18             : //! [plugin errors include]
      19             : 
      20             : #include <stdio.h>
      21             : #include <stdlib.h>
      22             : 
      23             : #ifndef HAVE_KDBCONFIG
      24             : #include "kdbconfig.h"
      25             : #endif
      26             : 
      27             : 
      28             : //! [global data]
      29             : typedef struct
      30             : {
      31             :         int global;
      32             : } GlobalData;
      33             : //! [global data]
      34             : 
      35             : 
      36             : #define DOC_PLUGIN_NAME "doc"
      37             : #define DOC_PLUGIN_VERSION "1.0.0"
      38             : 
      39             : 
      40             : //! [doc open]
      41          20 : int elektraDocOpen (Plugin * handle, Key * warningsKey ELEKTRA_UNUSED)
      42             : {
      43             :         GlobalData * data;
      44          20 :         KeySet * config = elektraPluginGetConfig (handle);
      45          20 :         Key * kg = ksLookupByName (config, "/global", 0);
      46             : 
      47          20 :         data = elektraMalloc (sizeof (GlobalData));
      48          20 :         data->global = 0;
      49          20 :         if (kg) data->global = atoi (keyString (kg));
      50          20 :         elektraPluginSetData (handle, data);
      51             :         //! [doc open]
      52             : 
      53             :         //! [doc module]
      54          20 :         if (ksLookupByName (config, "/module", 0))
      55             :         {
      56             :                 return 0;
      57             :         }
      58             :         // do some setup that will fail without configuration
      59             :         //! [doc module]
      60             : 
      61             :         return 0; /* success */
      62             : }
      63             : 
      64             : 
      65             : //! [doc close]
      66          20 : int elektraDocClose (Plugin * handle, Key * warningsKey ELEKTRA_UNUSED)
      67             : {
      68          20 :         elektraFree (elektraPluginGetData (handle));
      69             : 
      70          20 :         return 0; /* success */
      71             : }
      72             : //! [doc close]
      73             : 
      74             : static int parseKey (FILE * fp ELEKTRA_UNUSED, char ** key ELEKTRA_UNUSED, char ** value ELEKTRA_UNUSED)
      75             : {
      76             :         return 0;
      77             : }
      78             : 
      79             : static void doAction (Key * k ELEKTRA_UNUSED)
      80             : {
      81             : }
      82             : 
      83             : /*
      84             : //! [plugin errors spec]
      85             : number:60
      86             : description:Invalid Line encountered
      87             : severity:error
      88             : macro:NOEOF
      89             : module:simpleini
      90             : //! [plugin errors spec]
      91             : 
      92             : //! [plugin errors usage]
      93             : ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR ( parentKey, "Not at the end of file");
      94             : //! [plugin errors usage]
      95             : */
      96             : 
      97             : //![get contract]
      98          20 : int elektraDocGet (Plugin * plugin ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
      99             : {
     100          20 :         if (!strcmp (keyName (parentKey), "system/elektra/modules/doc"))
     101             :         {
     102          20 :                 KeySet * contract =
     103          20 :                         ksNew (30, keyNew ("system/elektra/modules/doc", KEY_VALUE, "doc plugin waits for your orders", KEY_END),
     104             :                                keyNew ("system/elektra/modules/doc/exports", KEY_END),
     105             :                                keyNew ("system/elektra/modules/doc/exports/open", KEY_FUNC, elektraDocOpen, KEY_END),
     106             :                                keyNew ("system/elektra/modules/doc/exports/close", KEY_FUNC, elektraDocClose, KEY_END),
     107             :                                keyNew ("system/elektra/modules/doc/exports/get", KEY_FUNC, elektraDocGet, KEY_END),
     108             :                                keyNew ("system/elektra/modules/doc/exports/set", KEY_FUNC, elektraDocSet, KEY_END),
     109             :                                keyNew ("system/elektra/modules/doc/exports/commit", KEY_FUNC, elektraDocCommit, KEY_END),
     110             :                                keyNew ("system/elektra/modules/doc/exports/error", KEY_FUNC, elektraDocError, KEY_END),
     111             :                                keyNew ("system/elektra/modules/doc/exports/checkconf", KEY_FUNC, elektraDocCheckConf, KEY_END),
     112             : #include ELEKTRA_README
     113             :                                keyNew ("system/elektra/modules/doc/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
     114          20 :                 ksAppend (returned, contract);
     115          20 :                 ksDel (contract);
     116             : 
     117          20 :                 return 1; /* success */
     118             :         }
     119             :         //![get contract]
     120             : 
     121             :         //![get storage]
     122           0 :         FILE * fp = fopen (keyString (parentKey), "r");
     123           0 :         char * key = 0;
     124           0 :         char * value = 0;
     125             : 
     126           0 :         while (parseKey (fp, &key, &value) >= 1)
     127             :         {
     128             :                 Key * read = keyNew (keyName (parentKey), KEY_END);
     129             :                 if (keyAddName (read, key) == -1)
     130             :                 {
     131             :                         ELEKTRA_ADD_VALIDATION_SYNTACTIC_WARNINGF (parentKey, "Key name %s is not valid, discarding key", key);
     132             :                         keyDel (read);
     133             :                         continue;
     134             :                 }
     135             :                 keySetString (read, value);
     136             : 
     137             :                 ksAppendKey (returned, read);
     138             :         }
     139             : 
     140           0 :         if (feof (fp) == 0)
     141             :         {
     142           0 :                 fclose (fp);
     143           0 :                 ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, "Invalid line encountered: not at the end of file");
     144           0 :                 return -1;
     145             :         }
     146             : 
     147           0 :         fclose (fp);
     148             :         //![get storage]
     149             : 
     150             :         //![get global keyset]
     151           0 :         KeySet * globalKS = elektraPluginGetGlobalKeySet (plugin);
     152             :         // now we can read something from the global keyset
     153             :         // or add something for us or others to read
     154           0 :         Key * important = keyNew ("user/global/myDocKey", KEY_VALUE, "global plugins can see me", KEY_END);
     155           0 :         ksAppendKey (globalKS, important);
     156             :         //![get global keyset]
     157             : 
     158             :         //![get global keyset cleanup]
     159             :         // clean up parts of the global keyset which we do not need
     160           0 :         Key * cutKey = keyNew ("user/global/myDocKey", KEY_END);
     161           0 :         KeySet * notNeeded = ksCut (globalKS, cutKey);
     162           0 :         ksDel (notNeeded);
     163             :         //![get global keyset cleanup]
     164             : 
     165             :         //![get filter]
     166             :         Key * k;
     167           0 :         ksRewind (returned);
     168           0 :         while ((k = ksNext (returned)) != 0)
     169             :         {
     170             :                 doAction (k);
     171             :         }
     172             : 
     173             :         return 1; // success
     174             : }
     175             : //![get filter]
     176             : 
     177           0 : int elektraDocSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
     178             : {
     179           0 :         ssize_t nr_keys = 0;
     180             :         /* set all keys below parentKey and count them with nr_keys */
     181             : 
     182             : 
     183             :         //![opening files]
     184           0 :         FILE * fp = fopen (keyString (parentKey), "w");
     185           0 :         if (!fp)
     186             :         {
     187           0 :                 ELEKTRA_SET_ERROR_SET (parentKey);
     188             :                 return -1;
     189             :         }
     190             :         //![opening files]
     191           0 :         fclose (fp);
     192             : 
     193           0 :         return nr_keys;
     194             : }
     195             : 
     196           0 : int elektraDocError (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
     197             : {
     198           0 :         return 0;
     199             : }
     200             : 
     201           0 : int elektraDocCommit (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
     202             : {
     203           0 :         return 0;
     204             : }
     205             : 
     206             : static Plugin * findPlugin (KDB * handle ELEKTRA_UNUSED)
     207             : {
     208             :         return 0;
     209             : }
     210             : 
     211             : static void saveToDisc (Key * k ELEKTRA_UNUSED)
     212             : {
     213             : }
     214             : 
     215             : //![validate configuration]
     216           0 : int elektraDocCheckConf (Key * errorKey ELEKTRA_UNUSED, KeySet * conf ELEKTRA_UNUSED)
     217             : {
     218             :         /* validate plugin configuration */
     219             : 
     220             :         // the return codes have the following meaning:
     221             :         // 0: The configuration was OK and has not been changed
     222             :         // 1: The configuration has been changed and now it is OK
     223             :         // -1: The configuration was not OK and could not be fixed. An error has to be set to errorKey.
     224           0 :         return 0;
     225             : }
     226             : //![validate configuration]
     227             : 
     228             : //![set full]
     229           0 : static void usercode (KDB * handle, KeySet * keyset, Key * key)
     230             : {
     231             :         // some more user code
     232           0 :         keySetString (key, "mycomment"); // the user changes the key
     233           0 :         ksAppendKey (keyset, key);       // append the key to the keyset
     234           0 :         kdbSet (handle, keyset, 0);      // and syncs it to disc
     235           0 : }
     236             : 
     237             : // so now kdbSet is called
     238           0 : int elektraKdbSet (KDB * handle, KeySet * keyset, Key * parentKey)
     239             : {
     240           0 :         int ret = 0;
     241             :         // find appropriate plugin and then call it:
     242           0 :         Plugin * plugin = findPlugin (handle);
     243           0 :         ret = elektraDocSet (plugin, keyset, parentKey);
     244             :         // the keyset with the key (and others for this plugin)
     245             :         // will be passed to this function
     246           0 :         return ret;
     247             : }
     248             : 
     249             : // so now elektraPluginSet(), which is the function described here,
     250             : // is called:
     251           0 : int elektraPluginSet (Plugin * plugin ELEKTRA_UNUSED, KeySet * returned, Key * parentKey ELEKTRA_UNUSED)
     252             : {
     253             :         // the task of elektraPluginSet is now to store the keys
     254             :         Key * k;
     255           0 :         ksRewind (returned);
     256           0 :         while ((k = ksNext (returned)) != 0)
     257             :         {
     258             :                 saveToDisc (k);
     259             :         }
     260             : 
     261           0 :         return 1; /* success */
     262             : }
     263             : //![set full]
     264             : 
     265             : 
     266           0 : void elektraUsercodeUselessSymbol (void)
     267             : {
     268           0 :         usercode (0, 0, 0);
     269           0 : }
     270             : 
     271             : //![export]
     272          20 : Plugin * ELEKTRA_PLUGIN_EXPORT
     273             : {
     274             :         // clang-format off
     275          20 :         return elektraPluginExport(DOC_PLUGIN_NAME,
     276             :                 ELEKTRA_PLUGIN_OPEN,    &elektraDocOpen,
     277             :                 ELEKTRA_PLUGIN_CLOSE,   &elektraDocClose,
     278             :                 ELEKTRA_PLUGIN_GET,     &elektraDocGet,
     279             :                 ELEKTRA_PLUGIN_SET,     &elektraDocSet,
     280             :                 ELEKTRA_PLUGIN_ERROR,   &elektraDocError,
     281             :                 ELEKTRA_PLUGIN_COMMIT,  &elektraDocCommit,
     282             :                 ELEKTRA_PLUGIN_END);
     283             : }
     284             : //![export]
     285             : 
     286             : /**
     287             :  * @}
     288             :  */

Generated by: LCOV version 1.13