LCOV - code coverage report
Current view: top level - src/tools/kdb - find.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 23 25 92.0 %
Date: 2019-09-12 12:28:41 Functions: 4 5 80.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 <find.hpp>
      10             : 
      11             : #include <iostream>
      12             : #include <regex>
      13             : 
      14             : #include <cmdline.hpp>
      15             : #include <kdb.hpp>
      16             : #include <keysetio.hpp>
      17             : 
      18             : using namespace kdb;
      19             : using namespace std;
      20             : 
      21          80 : FindCommand::FindCommand ()
      22             : {
      23          80 : }
      24             : 
      25           2 : int FindCommand::execute (Cmdline const & cl)
      26             : {
      27           4 :         Key root ("/", KEY_END);
      28           4 :         KDB kdb (root);
      29           4 :         KeySet ks;
      30             : 
      31           2 :         printWarnings (cerr, root, cl.verbose, cl.debug);
      32             : 
      33           2 :         kdb.get (ks, root);
      34             : 
      35           2 :         if (cl.verbose) cout << "size of all keys: " << ks.size () << endl;
      36             : 
      37           4 :         KeySet part;
      38           8 :         std::smatch match;
      39             : 
      40             :         try
      41             :         {
      42           6 :                 std::regex reg (cl.arguments[0]);
      43             : 
      44         810 :                 for (const auto & it : ks)
      45             :                 {
      46         536 :                         std::string name = it.getName ();
      47         268 :                         if (std::regex_search (name, match, reg))
      48             :                         {
      49             :                                 part.append (it);
      50             :                         }
      51             :                 }
      52             :         }
      53           0 :         catch (const regex_error & error)
      54             :         {
      55           0 :                 cerr << "Regex error in “" << cl.arguments[0] << "”: " << error.what () << endl;
      56             :         }
      57             : 
      58           2 :         if (cl.verbose) cout << "size of found keys: " << part.size () << endl;
      59           2 :         cout.setf (std::ios_base::unitbuf);
      60           2 :         if (cl.null)
      61             :         {
      62             :                 cout.unsetf (std::ios_base::skipws);
      63             :         }
      64             : 
      65           2 :         std::cout << part;
      66             : 
      67           2 :         printWarnings (cerr, root, cl.verbose, cl.debug);
      68             : 
      69           4 :         return 0;
      70             : }
      71             : 
      72          80 : FindCommand::~FindCommand ()
      73             : {
      74        7244 : }

Generated by: LCOV version 1.13