LCOV - code coverage report
Current view: top level - src/plugins/mathcheck - floathelper.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 19 25 76.0 %
Date: 2019-09-12 12:28:41 Functions: 2 2 100.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 <kdbhelper.h>
      10             : #include <locale.h>
      11             : #include <stdio.h>
      12             : #include <stdlib.h>
      13             : #include <string.h>
      14             : 
      15             : #include "floathelper.h"
      16             : 
      17          49 : char * elektraFtoA (char * buffer, ssize_t bufSize, double val)
      18             : {
      19          49 :         if (buffer == NULL)
      20             :         {
      21           0 :                 bufSize = MAX_CHARS_DOUBLE;
      22           0 :                 buffer = elektraMalloc (bufSize);
      23             :         }
      24          49 :         snprintf (buffer, bufSize, "%g", val);
      25             : 
      26             :         struct lconv * locale;
      27          49 :         locale = localeconv ();
      28          49 :         char sysSep = (locale->decimal_point)[0];
      29             : 
      30          49 :         if (sysSep == ELEKTRA_DEFAULT_DECIMAL_POINT)
      31             :         {
      32             :                 return buffer;
      33             :         }
      34             :         else
      35             :         {
      36           0 :                 char * sepPtr = strchr (buffer, sysSep);
      37           0 :                 if (sepPtr == NULL)
      38             :                 {
      39             :                         return buffer;
      40             :                 }
      41             :                 else
      42             :                 {
      43           0 :                         *sepPtr = ELEKTRA_DEFAULT_DECIMAL_POINT;
      44           0 :                         return buffer;
      45             :                 }
      46             :         }
      47             : }
      48             : 
      49         126 : double elektraEFtoF (const char * string)
      50             : {
      51         126 :         char * buffer = elektraMalloc (elektraStrLen (string));
      52         126 :         strcpy (buffer, string);
      53         126 :         char * sepPtr = strchr (buffer, ELEKTRA_DEFAULT_DECIMAL_POINT);
      54         126 :         if (sepPtr == NULL)
      55             :         {
      56          83 :                 elektraFree (buffer);
      57          83 :                 return atof (string);
      58             :         }
      59             :         else
      60             :         {
      61             :                 struct lconv * locale;
      62          43 :                 locale = localeconv ();
      63          43 :                 char sysSep = (locale->decimal_point)[0];
      64          43 :                 *sepPtr = sysSep;
      65          43 :                 double retval = atof (buffer);
      66          43 :                 elektraFree (buffer);
      67          43 :                 return retval;
      68             :         }
      69             : }

Generated by: LCOV version 1.13