LCOV - code coverage report
Current view: top level - util - util.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 13 26 50.0 %
Date: 2017-09-17 17:24:28 Functions: 2 4 50.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014 GNUnet e.V.
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : 
      17             : /**
      18             :  * @file util.c
      19             :  * @brief Common utility functions; we might choose to move those to GNUnet at some point
      20             :  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
      21             :  * @author Florian Dold
      22             :  * @author Benedikt Mueller
      23             :  */
      24             : 
      25             : #include "platform.h"
      26             : #include "taler_util.h"
      27             : #include <gcrypt.h>
      28             : 
      29             : 
      30             : /**
      31             :  * Convert a buffer to an 8-character string
      32             :  * representative of the contents. This is used
      33             :  * for logging binary data when debugging.
      34             :  *
      35             :  * @param buf buffer to log
      36             :  * @param buf_size number of bytes in @a buf
      37             :  * @return text representation of buf, valid until next
      38             :  *         call to this function
      39             :  */
      40             : const char *
      41          10 : TALER_b2s (const void *buf,
      42             :            size_t buf_size)
      43             : {
      44             :   static char ret[9];
      45             :   struct GNUNET_HashCode hc;
      46             :   char *tmp;
      47             : 
      48          10 :   GNUNET_CRYPTO_hash (buf,
      49             :                       buf_size,
      50             :                       &hc);
      51          10 :   tmp = GNUNET_STRINGS_data_to_string_alloc (&hc,
      52             :                                              sizeof (hc));
      53          10 :   memcpy (ret,
      54             :           tmp,
      55             :           8);
      56          10 :   GNUNET_free (tmp);
      57          10 :   ret[8] = '\0';
      58          10 :   return ret;
      59             : }
      60             : 
      61             : 
      62             : /**
      63             :  * Obtain denomination amount from configuration file.
      64             :  *
      65             :  * @param cfg configuration to use
      66             :  * @param section section of the configuration to access
      67             :  * @param option option of the configuration to access
      68             :  * @param[out] denom set to the amount found in configuration
      69             :  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
      70             :  */
      71             : int
      72         110 : TALER_config_get_denom (const struct GNUNET_CONFIGURATION_Handle *cfg,
      73             :                         const char *section,
      74             :                         const char *option,
      75             :                         struct TALER_Amount *denom)
      76             : {
      77             :   char *str;
      78             : 
      79         110 :   if (GNUNET_OK !=
      80         110 :       GNUNET_CONFIGURATION_get_value_string (cfg,
      81             :                                              section,
      82             :                                              option,
      83             :                                              &str))
      84           0 :     return GNUNET_NO;
      85         110 :   if (GNUNET_OK != TALER_string_to_amount (str,
      86             :                                            denom))
      87             :   {
      88           0 :     GNUNET_free (str);
      89           0 :     return GNUNET_SYSERR;
      90             :   }
      91         110 :   GNUNET_free (str);
      92         110 :   return GNUNET_OK;
      93             : }
      94             : 
      95             : 
      96             : 
      97             : 
      98             : /**
      99             :  * Set an option with an amount from the command line.  A pointer to
     100             :  * this function should be passed as part of the 'struct
     101             :  * GNUNET_GETOPT_CommandLineOption' array to initialize options of
     102             :  * this type.
     103             :  *
     104             :  * @param ctx command line processing context
     105             :  * @param scls additional closure (will point to the `struct TALER_Amount`)
     106             :  * @param option name of the option
     107             :  * @param value actual value of the option as a string.
     108             :  * @return #GNUNET_OK if parsing the value worked
     109             :  */
     110             : static int
     111           0 : set_amount (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
     112             :             void *scls,
     113             :             const char *option,
     114             :             const char *value)
     115             : {
     116           0 :   struct TALER_Amount *amount = scls;
     117             : 
     118           0 :   if (GNUNET_OK !=
     119           0 :       TALER_string_to_amount (value,
     120             :                               amount))
     121             :   {
     122           0 :     FPRINTF (stderr,
     123             :              _("Failed to parse amount in option `%s'\n"),
     124             :              option);
     125           0 :     return GNUNET_SYSERR;
     126             :   }
     127             : 
     128           0 :   return GNUNET_OK;
     129             : }
     130             : 
     131             : 
     132             : /**
     133             :  * Allow user to specify an amount on the command line.
     134             :  *
     135             :  * @param shortName short name of the option
     136             :  * @param name long name of the option
     137             :  * @param argumentHelp help text for the option argument
     138             :  * @param description long help text for the option
     139             :  * @param[out] amount set to the amount specified at the command line
     140             :  */
     141             : struct GNUNET_GETOPT_CommandLineOption
     142           0 : TALER_getopt_get_amount (char shortName,
     143             :                          const char *name,
     144             :                          const char *argumentHelp,
     145             :                          const char *description,
     146             :                          struct TALER_Amount *amount)
     147             : {
     148           0 :   struct GNUNET_GETOPT_CommandLineOption clo = {
     149             :     .shortName =  shortName,
     150             :     .name = name,
     151             :     .argumentHelp = argumentHelp,
     152             :     .description = description,
     153             :     .require_argument = 1,
     154             :     .processor = &set_amount,
     155             :     .scls = (void *) amount
     156             :   };
     157             : 
     158           0 :   return clo;
     159             : }
     160             : 
     161             : 
     162             : /* end of util.c */

Generated by: LCOV version 1.13